meshcentral
Version:
Web based remote computer management server
1 lines • 9.81 kB
JavaScript
var MemoryStream=require("MemoryStream");var lme_id=0;var lme_port_offset=0;var xmlParser=null;try{xmlParser=require("amt-xml")}catch(ex){}var APF_DISCONNECT=1;var APF_SERVICE_REQUEST=5;var APF_SERVICE_ACCEPT=6;var APF_USERAUTH_REQUEST=50;var APF_USERAUTH_FAILURE=51;var APF_USERAUTH_SUCCESS=52;var APF_GLOBAL_REQUEST=80;var APF_REQUEST_SUCCESS=81;var APF_REQUEST_FAILURE=82;var APF_CHANNEL_OPEN=90;var APF_CHANNEL_OPEN_CONFIRMATION=91;var APF_CHANNEL_OPEN_FAILURE=92;var APF_CHANNEL_WINDOW_ADJUST=93;var APF_CHANNEL_DATA=94;var APF_CHANNEL_CLOSE=97;var APF_PROTOCOLVERSION=192;function lme_object(){this.ourId=++lme_id;this.amtId=-1;this.LME_CHANNEL_STATUS="LME_CS_FREE";this.txWindow=0;this.rxWindow=0;this.localPort=0;this.errorCount=0}function stream_bufferedWrite(){var a=require("events").inherits(this);this.buffer=[];this._readCheckImmediate=undefined;this._ObjectID="bufferedWriteStream";a.createEvent("close");a.createEvent("drain");a.createEvent("error");a.createEvent("finish");a.createEvent("pipe");a.createEvent("unpipe");a.createEvent("readable");this.isEmpty=function(){return(this.buffer.length==0)};this.isWaiting=function(){return(this._readCheckImmediate==undefined)};this.write=function(c){for(var b in arguments){if(typeof(arguments[b])=="function"){this.once("drain",arguments[b]);break}}var d=Buffer.alloc(c.length);c.copy(d);this.buffer.push({offset:0,data:d});this.emit("readable");return(this.buffer.length==0?true:false)};this.read=function(){var f=arguments.length==0?undefined:arguments[0];var b=0;var d=[];while((f==undefined||b<f)&&this.buffer.length>0){var c=this.buffer[0].data.length-this.buffer[0].offset;var e=this.buffer[0].offset;if(c>(f-b)){d.push(this.buffer[0].data.slice(e,e+f-b));this.buffer[0].offset+=(f-b);b+=(f-b)}else{d.push(this.buffer[0].data.slice(e));b+=c;this.buffer.shift()}}this._readCheckImmediate=setImmediate(function(g){g._readCheckImmediate=undefined;if(g.buffer.length==0){g.emit("drain")}else{g.emit("readable")}},this);return(Buffer.concat(d))}}function lme_heci(c){var a=require("events").inherits(this);a.createEvent("error");a.createEvent("connect");a.createEvent("notify");a.createEvent("bind");this.on("newListener",function(e,d){if(e=="connect"&&this._LME._connected==true){d.call(this)}if(e=="error"&&this._LME._error!=null){d.call(this,this._LME._error)}});if((c!=null)&&(c.debug==true)){lme_port_offset=-100}var b=require("heci");this.INITIAL_RXWINDOW_SIZE=4096;this._ObjectID="lme";this._LME=b.create();this._LME._connected=false;this._LME._error=null;this._LME.descriptorMetadata="amt-lme";this._LME._binded={};this._LME.LMS=this;this._LME.on("error",function(d){this._error=d;this.LMS.emit("error",d)});this._LME.on("connect",function(){this._connected=true;this._emitConnected=false;this.on("data",function(k){var l=k.readUInt8(0);switch(l){default:console.log("Unhandled LME Command "+l+", "+k.length+" byte(s).");break;case APF_SERVICE_REQUEST:var A=k.readUInt32BE(1);var z=k.slice(5,A+5);if(z=="pfwd@amt.intel.com"||z=="auth@amt.intel.com"){var G=Buffer.alloc(5+A);G.writeUInt8(6,0);G.writeUInt32BE(A,1);G.write(z.toString(),5);this.write(G)}else{}break;case APF_GLOBAL_REQUEST:var A=k.readUInt32BE(1);var z=k.slice(5,A+5).toString();switch(z){case"tcpip-forward":var w=k.readUInt32BE(A+6);var H=k.readUInt32BE(A+10+w);if(this[z]==undefined){this[z]={}}if(this[z][H]!=null){for(var t in this.sockets){var h=this.sockets[t];if(h.localPort==H){this.sockets[t].end();delete this.sockets[t]}}}if(this[z][H]==null){try{this[z][H]=require("net").createServer();this[z][H].descriptorMetadata="amt-lme (port: "+H+")";this[z][H].HECI=this;if(lme_port_offset==0){this[z][H].listen({port:H,host:"127.0.0.1"})}else{this[z][H].listen({port:(H+lme_port_offset)})}this[z][H].on("connection",function(e){this.HECI.LMS.bindDuplexStream(e,e.remoteFamily,e.localPort-lme_port_offset)});this._binded[H]=true;if(!this._emitConnected){this._emitConnected=true;this.LMS.emit("error","APF/BIND error")}this.LMS.emit("bind",this._binded)}catch(p){console.info1(p,"Port "+H);if(!this._emitConnected){this._emitConnected=true;this.LMS.emit("error","APF/BIND error")}}}var G=Buffer.alloc(5);G.writeUInt8(81,0);G.writeUInt32BE(H,1);this.write(G);break;case"cancel-tcpip-forward":var G=Buffer.alloc(1);G.writeUInt8(APF_REQUEST_SUCCESS,0);this.write(G);break;case"udp-send-to@amt.intel.com":var G=Buffer.alloc(1);G.writeUInt8(APF_REQUEST_FAILURE,0);this.write(G);break;default:break}break;case APF_CHANNEL_OPEN_CONFIRMATION:var J=k.readUInt32BE(1);var N=k.readUInt32BE(5);var O=k.readUInt32BE(9);if(this.sockets[J]!=undefined){this.sockets[J].lme.amtId=N;this.sockets[J].lme.rxWindow=O;this.sockets[J].lme.txWindow=O;this.sockets[J].lme.LME_CHANNEL_STATUS="LME_CS_CONNECTED";this.sockets[J].bufferedStream=new stream_bufferedWrite();this.sockets[J].bufferedStream.socket=this.sockets[J];this.sockets[J].bufferedStream.on("readable",function(){if(this.socket.lme.txWindow>0){var e=this.read(this.socket.lme.txWindow);var i=Buffer.alloc(9+e.length);i.writeUInt8(APF_CHANNEL_DATA,0);i.writeUInt32BE(this.socket.lme.amtId,1);i.writeUInt32BE(e.length,5);e.copy(i,9);this.socket.lme.txWindow-=e.length;this.socket.HECI.write(i)}});this.sockets[J].bufferedStream.on("drain",function(){this.socket.resume()});this.sockets[J].on("data",function(e){if(!this.bufferedStream.write(e)){this.pause()}});this.sockets[J].on("end",function(){var e=Buffer.alloc(5);e.writeUInt8(APF_CHANNEL_CLOSE,0);e.writeUInt32BE(this.lme.amtId,1);this.HECI.write(e)});this.sockets[J].resume()}break;case APF_PROTOCOLVERSION:var x=k.readUInt32BE(1);var y=k.readUInt32BE(5);var L=k.readUInt32BE(9);var G=Buffer.alloc(93);G.writeUInt8(192,0);G.writeUInt32BE(1,1);G.writeUInt32BE(0,5);G.writeUInt32BE(L,9);this.write(G);break;case APF_CHANNEL_WINDOW_ADJUST:var K=k.readUInt32BE(1);var g=k.readUInt32BE(5);if(this.sockets[K]!=undefined){this.sockets[K].lme.txWindow+=g;if(!this.sockets[K].bufferedStream.isEmpty()&&this.sockets[K].bufferedStream.isWaiting()){this.sockets[K].bufferedStream.emit("readable")}}else{console.log("Unknown Recipient ID/"+K+" for APF_CHANNEL_WINDOW_ADJUST")}break;case APF_CHANNEL_DATA:var K=k.readUInt32BE(1);var n=k.readUInt32BE(5);var m=k.slice(9,9+n);if((this.sockets!=null)&&(this.sockets[K]!=undefined)){this.sockets[K].pendingBytes.push(m.length);this.sockets[K].write(m,function(){var i=this.pendingBytes.shift();var e=Buffer.alloc(9);e.writeUInt8(APF_CHANNEL_WINDOW_ADJUST,0);e.writeUInt32BE(this.lme.amtId,1);e.writeUInt32BE(i,5);this.HECI.write(e)})}else{if((this.insockets!=null)&&(this.insockets[K]!=undefined)){var h=this.insockets[K];if(h.data==null){h.data=m.toString()}else{h.data+=m.toString()}h.rxWindow+=n;var s=parseHttp(h.data);if((s!=null)||(h.data.length>=8000)){var B=null;if(xmlParser!=null){try{B=xmlParser.ParseWsman(s)}catch(o){}}if(B!=null){this.LMS.emit("notify",B,h.options,_lmsNotifyToCode(B))}var f=Buffer.alloc(5);f.writeUInt8(APF_CHANNEL_CLOSE,0);f.writeUInt32BE(d,1);this.write(f)}else{if(h.rxWindow>6000){var f=Buffer.alloc(9);f.writeUInt8(APF_CHANNEL_WINDOW_ADJUST,0);f.writeUInt32BE(h.amtId,1);f.writeUInt32BE(h.rxWindow,5);this.write(f);h.rxWindow=0}}}else{console.log("Unknown Recipient ID/"+K+" for APF_CHANNEL_DATA")}}break;case APF_CHANNEL_OPEN_FAILURE:var K=k.readUInt32BE(1);var M=k.readUInt32BE(5);if((this.sockets!=null)&&(this.sockets[K]!=undefined)){this.sockets[K].end();delete this.sockets[K]}else{if((this.insockets!=null)&&(this.insockets[K]!=undefined)){delete this.insockets[K]}else{console.log("Unknown Recipient ID/"+K+" for APF_CHANNEL_OPEN_FAILURE")}}break;case APF_CHANNEL_CLOSE:var K=k.readUInt32BE(1);if((this.sockets!=null)&&(this.sockets[K]!=undefined)){this.sockets[K].end();var d=this.sockets[K].lme.amtId;var f=Buffer.alloc(5);delete this.sockets[K];f.writeUInt8(APF_CHANNEL_CLOSE,0);f.writeUInt32BE(d,1);this.write(f)}else{if((this.insockets!=null)&&(this.insockets[K]!=undefined)){delete this.insockets[K]}else{console.log("Unknown Recipient ID/"+K+" for APF_CHANNEL_CLOSE")}}break;case APF_CHANNEL_OPEN:var A=k.readUInt32BE(1);var z=k.slice(5,A+5).toString();var j=k.readUInt32BE(A+5);var u=k.readUInt32BE(A+9);var r=k.readUInt32BE(A+17);var q=k.slice(A+21,A+21+r).toString();var I=k.readUInt32BE(A+21+r);var D=k.readUInt32BE(A+25+r);var C=k.slice(A+29+r,A+29+r+D).toString();var E=k.readUInt32BE(A+29+r+D);if(this.insockets==null){this.insockets={}}var F=++lme_id;var v=new lme_object();v.ourId=F;v.amtId=j;v.txWindow=u;v.rxWindow=0;v.options={target:q,targetPort:I,source:C,sourcePort:E};this.insockets[F]=v;var f=Buffer.alloc(17);f.writeUInt8(APF_CHANNEL_OPEN_CONFIRMATION,0);f.writeUInt32BE(j,1);f.writeUInt32BE(F,5);f.writeUInt32BE(4000,9);f.writeUInt32BE(4294967295,13);this.write(f);break}});this.resume()});this.bindDuplexStream=function(e,h,g){var j=e;j.pendingBytes=[];j.HECI=this._LME;j.LMS=this;j.lme=new lme_object();j.lme.Socket=j;j.localPort=g;var d=new MemoryStream();d.writeUInt8(90);d.writeUInt32BE(15);d.write("forwarded-tcpip");d.writeUInt32BE(j.lme.ourId);d.writeUInt32BE(this.INITIAL_RXWINDOW_SIZE);d.writeUInt32BE(4294967295);for(var f=0;f<2;++f){if(h=="IPv6"){d.writeUInt32BE(3);d.write("::1")}else{d.writeUInt32BE(9);d.write("127.0.0.1")}d.writeUInt32BE(g)}this._LME.write(d.buffer);if(this._LME.sockets==undefined){this._LME.sockets={}}this._LME.sockets[j.lme.ourId]=j;j.pause()};this._LME.connect(b.GUIDS.LME,{noPipeline:0})}function parseHttp(c){var d=c.indexOf("\r\n\r\n");if((d==-1)||(c.length<(d+2))){return null}var b=require("http-headers")(c.substring(0,d),true);var a=parseInt(b["content-length"]);if(c.length>=a+d+4){return c.substring(d+4,d+4+a)}return null}function _lmsNotifyToCode(c){if((c==null)||(c.Body==null)||(c.Body.MessageID==null)){return null}var b=c.Body.MessageID;try{b+="-"+c.Body.MessageArguments[0]}catch(a){}return b}module.exports=lme_heci;