MeshCentral/agents/modules_meshcore_min/amt-lme.min.js

1 line
34 KiB
JavaScript
Raw Normal View History

2019-01-28 18:49:08 -05:00
var MemoryStream=require("MemoryStream");var lme_id=0;var lme_port_offset=0;var xmlParser=require("amt-xml");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");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._binded={};this._LME.LMS=this;this._LME.on("error",function(d){this.LMS.emit("error",d)});this._LME.on("connect",function(){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){this[z][H]=require("net").createServer();this[z][H].HECI=this;try{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)})}}catch(p){console.log("Binding error, LMS port "+(H+lme_port_offset)+": "+p)}this[z][H].on("connection",function(e){this.HECI.LMS.bindDuplexStream(e,e.remoteFamily,e.localPort-lme_port_offset)});this._binded[H]=true;this.LMS.emit("bind",this._binded)}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;