UNPKG

meshcentral

Version:

Web based remote computer management server

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