UNPKG

meshcentral

Version:

Web based remote computer management server

1 lines • 8.49 kB
function CreateAPFClient(t,e){if(null==e.clientuuid||36!=e.clientuuid.length)return null;var E={};function u(t,e){return 16777216*t.charCodeAt(e)+(t.charCodeAt(e+1)<<16)+(t.charCodeAt(e+2)<<8)+t.charCodeAt(e+3)}function C(t){return String.fromCharCode(t>>24&255,t>>16&255,t>>8&255,255&t)}function i(t){for(var e,n="",r=(""+t).match(/../g);e=r.shift();)n+=String.fromCharCode("0x"+e);return n}function d(t){E.parent.debug&&console.log(t)}function l(t){for(var e="",n=0;n<t;n++)e+=String.fromCharCode(0);return e}E.parent=t,E.args=e,E.http=require("http"),E.net=require("net"),E.forwardClient=null,E.downlinks={},E.pfwd_idx=0,E.timer=null;var S={INITIAL:0,PROTOCOL_VERSION_SENT:1,AUTH_SERVICE_REQUEST_SENT:2,AUTH_REQUEST_SENT:3,PFWD_SERVICE_REQUEST_SENT:4,GLOBAL_REQUEST_SENT:5,FAILED:-1},f=(E.cirastate=S.INITIAL,E.RedirectStartSol=String.fromCharCode(16,0,0,0,83,79,76,32),E.RedirectStartKvm=String.fromCharCode(16,1,0,0,75,86,77,82),E.RedirectStartIder=String.fromCharCode(16,0,0,0,73,68,69,82),[16992,16993]),A={UNKNOWN:0,DISCONNECT:1,SERVICE_REQUEST:5,SERVICE_ACCEPT:6,USERAUTH_REQUEST:50,USERAUTH_FAILURE:51,USERAUTH_SUCCESS:52,GLOBAL_REQUEST:80,REQUEST_SUCCESS:81,REQUEST_FAILURE:82,CHANNEL_OPEN:90,CHANNEL_OPEN_CONFIRMATION:91,CHANNEL_OPEN_FAILURE:92,CHANNEL_WINDOW_ADJUST:93,CHANNEL_DATA:94,CHANNEL_CLOSE:97,PROTOCOLVERSION:192,KEEPALIVE_REQUEST:208,KEEPALIVE_REPLY:209,KEEPALIVE_OPTIONS_REQUEST:210,KEEPALIVE_OPTIONS_REPLY:211,JSON_CONTROL:250};function o(t,e){e=JSON.stringify(e);t.write(String.fromCharCode(A.JSON_CONTROL)+C(e.length)+e),d("APF: Send JSON control: "+e)}function _(t,e){var n=String.fromCharCode(A.SERVICE_REQUEST)+C(e.length)+e;t.write(n),d("APF: Send service request "+e),"auth@amt.intel.com"==e?E.cirastate=S.AUTH_SERVICE_REQUEST_SENT:"pfwd@amt.intel.com"==e&&(E.cirastate=S.PFWD_SERVICE_REQUEST_SENT)}function N(t,e,n){var r="tcpip-forward",r=String.fromCharCode(A.GLOBAL_REQUEST)+C(r.length)+r+l(1);r+=C(e.length)+e+C(n),t.write(r),d("APF: Send tcpip-forward "+e+":"+n),E.cirastate=S.GLOBAL_REQUEST_SENT}function w(t,e){t.write(String.fromCharCode(A.CHANNEL_OPEN_FAILURE)+C(e.sender_chan)+C(2)+C(0)+C(0)),d("APF: Send ChannelOpenFailure")}return E.onSecureConnect=function(t,e,n){var r,a;d("APF Secure WebSocket connected."),E.forwardClient.tag={accumulator:[]},E.forwardClient.ws=e,E.forwardClient.ws.on("end",function(){d("APF: Connection is closing."),null!=E.timer&&(clearInterval(E.timer),E.timer=null),E.onChannelClosed&&E.onChannelClosed(E)}),E.forwardClient.ws.on("data",function(t){E.forwardClient.tag.accumulator+=i(function(t){for(var e="",n=0;n<t.length;n++)e+=(t[n]/256+1/512).toString(16).substring(2,4);return e}(t));try{for(var e;0<(e=function(r){var t=r.tag.accumulator.charCodeAt(0),e=r.tag.accumulator.length,n=r.tag.accumulator;if(0==e)return 0;switch(t){case A.SERVICE_ACCEPT:var a=u(n,1),i=n.substring(5,6+a);return d("APF: Service request to "+i+" accepted."),"auth@amt.intel.com"==i?E.cirastate>=S.AUTH_SERVICE_REQUEST_SENT&&function(t,e,n){var r="pfwd@amt.intel.com",e=String.fromCharCode(A.USERAUTH_REQUEST)+C(e.length)+e+C(r.length)+r;e=(e+=C(8)+"password")+(l(1)+C(n.length)+n),t.write(e),d("APF: Send username password authentication to MPS"),E.cirastate=S.AUTH_REQUEST_SENT}(r.ws,E.args.mpsuser,E.args.mpspass):"pfwd@amt.intel.com"==i&&E.cirastate>=S.PFWD_SERVICE_REQUEST_SENT&&N(r.ws,E.args.clientname,f[E.pfwd_idx++]),5+a;case A.REQUEST_SUCCESS:return 5<=e?(d("APF: Request to port forward "+u(n,1)+" successful."),E.pfwd_idx<f.length?N(r.ws,E.args.clientname,f[E.pfwd_idx++]):(d("APF: Start keep alive for every "+E.args.mpskeepalive+" ms."),E.timer=setInterval(function(){E.forwardClient.ws.write(String.fromCharCode(A.KEEPALIVE_REQUEST)+C(255)),d("APF: Send keepalive request")},E.args.mpskeepalive)),5):(d("APF: Request successful."),1);case A.USERAUTH_SUCCESS:return d("APF: User Authentication successful"),_(r.ws,"pfwd@amt.intel.com"),1;case A.USERAUTH_FAILURE:return d("APF: User Authentication failed"),E.cirastate=S.FAILED,14;case A.KEEPALIVE_REQUEST:return d("APF: Keep Alive Request with cookie: "+u(n,1)),function(t,e){t.write(String.fromCharCode(A.KEEPALIVE_REPLY)+C(e)),d("APF: Send keepalive reply")}(r.ws,u(n,1)),5;case A.KEEPALIVE_REPLY:return d("APF: Keep Alive Reply with cookie: "+u(n,1)),5;case A.CHANNEL_OPEN:var o=function(t){var e={cmd:A.CHANNEL_OPEN},n=u(t,1),r=(e.chan_type=t.substring(5,5+n),e.sender_chan=u(t,5+n),e.window_size=u(t,9+n),u(t,17+n)),a=(e.target_address=t.substring(21+n,21+n+r),e.target_port=u(t,21+n+r),u(t,25+n+r));return e.origin_address=t.substring(29+n+r,29+n+r+a),e.origin_port=u(t,29+n+r+a),e.len=33+n+r+a,e}(n);return d("APF: CHANNEL_OPEN request: "+JSON.stringify(o)),0<=f.indexOf(o.target_port)?((c=E.net.createConnection({host:E.args.clientaddress,port:o.target_port},function(){var t,e;t=r.ws,e=o,t.write(String.fromCharCode(A.CHANNEL_OPEN_CONFIRMATION)+C(e.sender_chan)+C(e.sender_chan)+C(e.window_size)+C(4294967295)),d("APF: Send ChannelOpenConfirmation")})).maxInWindow=o.window_size,c.curInWindow=0,c.on("data",function(t){var e,n;e=r.ws,n=o.sender_chan,t=t,e.write(Buffer.concat([Buffer.from(String.fromCharCode(A.CHANNEL_DATA)+C(n)+C(t.length),"binary"),t])),d("APF: Send ChannelData: "+t.toString("hex"))}),c.on("error",function(t){w(r.ws,o)}),c.on("end",function(){var t,e;if(null!=E.downlinks[o.sender_chan]){d("Socket ends.");try{t=r.ws,e=o.sender_chan,t.write(String.fromCharCode(A.CHANNEL_CLOSE)+C(e)),d("APF: Send ChannelClose ")}catch(t){}delete E.downlinks[o.sender_chan]}}),E.downlinks[o.sender_chan]=c):w(r.ws,o),o.len;case A.CHANNEL_OPEN_CONFIRMATION:return d("APF: CHANNEL_OPEN_CONFIRMATION"),17;case A.CHANNEL_CLOSE:d("APF: CHANNEL_CLOSE: "+(s=u(n,1)));try{E.downlinks[s].end()}catch(t){}return 5;case A.CHANNEL_DATA:d("APF: CHANNEL_DATA: "+JSON.stringify(function(t){for(var e="",n=0;n<t.length;n++)e+=(t.charCodeAt(n)+256).toString(16).substr(-2).toUpperCase();return e}(n)));var s=u(n,1),i=u(n,5),a=n.substring(9,9+i);if(null!=(c=E.downlinks[s])){c.curInWindow+=i;try{c.write(Buffer.from(a,"binary"),function(){var t,e,n;d("Write completed."),this.curInWindow>this.maxInWindow/2&&(t=r.ws,e=s,n=this.curInWindow,t.write(String.fromCharCode(A.CHANNEL_WINDOW_ADJUST)+C(e)+C(n)),d("APF: Send ChannelWindowAdjust, channel: "+e+", size: "+n),this.curInWindow=0)})}catch(t){d("Cannot forward data to downlink socket.")}}return 9+i;case A.CHANNEL_WINDOW_ADJUST:return d("APF: CHANNEL_WINDOW_ADJUST"),9;case A.JSON_CONTROL:d("APF: JSON_CONTROL");e=u(n,1);if(E.onJsonControl){var c=null;try{c=JSON.parse(n.substring(5,5+e))}catch(t){}null!=c&&E.onJsonControl(c)}return 5+e;default:return d("CMD: "+t+" is not implemented."),E.cirastate=S.FAILED,0}}(E.forwardClient))&&(E.forwardClient.tag.accumulator=E.forwardClient.tag.accumulator.slice(e)),E.cirastate==S.FAILED&&(d("APF: in a failed state, destroying socket."),E.forwardClient.ws.end()),0<e;);}catch(t){d(t)}}),E.forwardClient.ws.on("error",function(t){d("APF: Connection error, ending connecting."),null!=E.timer&&(clearInterval(E.timer),E.timer=null)}),E.state=S.INITIAL,"number"==typeof E.args.conntype&&0!=E.args.conntype&&(o(E.forwardClient.ws,{action:"connType",value:E.args.conntype}),null!=E.args.meiState)&&o(E.forwardClient.ws,{action:"meiState",value:E.args.meiState}),e=E.forwardClient.ws,r=E.args.clientuuid,a=String.fromCharCode(A.PROTOCOLVERSION)+C(1)+C(0)+C(0)+i(function(t){return(t=t.replace(/-/g,"")).substring(6,8)+t.substring(4,6)+t.substring(2,4)+t.substring(0,2)+t.substring(10,12)+t.substring(8,10)+t.substring(14,16)+t.substring(12,14)+t.substring(16,20)+t.substring(20)}(r))+l(64),e.write(a),d("APF: Send protocol version 1 0 "+r),E.cirastate=S.PROTOCOL_VERSION_SENT,_(E.forwardClient.ws,"auth@amt.intel.com")},E.updateMeiState=function(t){o(E.forwardClient.ws,{action:"meiState",value:t})},E.sendMeiDeactivationState=function(t){o(E.forwardClient.ws,{action:"deactivate",value:t})},E.sendStartTlsHostConfigResponse=function(t){o(E.forwardClient.ws,{action:"startTlsHostConfig",value:t})},E.sendStopConfigurationResponse=function(t){o(E.forwardClient.ws,{action:"stopConfiguration",value:t})},E.connect=function(){if(null!=E.forwardClient)try{E.forwardClient.ws.end()}catch(t){d(t)}E.cirastate=S.INITIAL,E.pfwd_idx=0;var t=E.http.parseUri(E.args.mpsurl);t.rejectUnauthorized=0,E.forwardClient=E.http.request(t),E.forwardClient.upgrade=E.onSecureConnect,E.forwardClient.end()},E.disconnect=function(){try{E.forwardClient.ws.end()}catch(t){d(t)}},E}module.exports=CreateAPFClient