UNPKG

meshcentral

Version:

Web based remote computer management server

1 lines • 11.2 kB
var Q=require("queue");var g_internal=null;function retry_pthi_later(){if(++g_internal.errorCount<20){g_internal.timeout=setTimeout(function(a){a.connect(require("heci").GUIDS.AMT,{noPipeline:1})},250,this)}else{this.Parent.emit("error","PTHI Connection could not be established")}}function amt_heci(){var d=require("events").inherits(this);d.createEvent("error");var u=require("heci");var x=function(G){try{require("MeshAgent").SendCommand({action:"msg",type:"console",value:G})}catch(F){}};this._ObjectID="pthi";var B=this;if(g_internal==null){g_internal={_rq:new Q(),_amt:null,errorCount:0};g_internal._setupPTHI=function a(){console.info1("setupPTHI()");this._amt=u.create();this._amt.descriptorMetadata="amt-pthi";this._amt.BiosVersionLen=65;this._amt.UnicodeStringLen=20;this._amt.Parent=B;this._amt.on("error",function G(H){console.info1("PTHIError: "+H);if(g_internal._rq.isEmpty()){console.info1(" Queue is empty");this.Parent.emit("error",H)}else{console.info1(" Queue is NOT empty");retry_pthi_later.call(this)}});this._amt.on("connect",function F(){g_internal.errorCount=0;this.on("data",function H(J){var K=this.Parent.getCommand(J);console.info1("CMD = "+K.Command+" (Status: "+K.Status+") Response = "+K.IsResponse);var M=g_internal._rq.deQueue();var L=M.optional;var I=M.func;L.unshift(K);I.apply(this.Parent,L);if(g_internal._rq.isEmpty()){console.info1("No more requests, disconnecting");g_internal._amt.disconnect();g_internal._amt=null}else{console.info1("Sending Next Request");this.write(g_internal._rq.peekQueue().send)}});this.write(g_internal._rq.peekQueue().send)})}}function C(F){var G=F.indexOf("\0");if(G>=0){return F.substring(0,G)}else{return F}}this.getCommand=function g(F){var G=F.length==0?(g_internal._rq.peekQueue().cmd|8388608):F.readUInt32LE(4);var H={IsResponse:(G&8388608)==8388608?true:false,Command:(G&8388607),Status:F.length!=0?F.readUInt32LE(12):-1,Data:F.length!=0?F.slice(16):null};return(H)};this.sendCommand=function w(){if(arguments.length<3||typeof(arguments[0])!="number"||typeof(arguments[1])!="object"||typeof(arguments[2])!="function"){throw ("invalid parameters")}var F=[];for(var H=3;H<arguments.length;++H){F.push(arguments[H])}console.info1("sendCommand("+arguments[0]+")",this._hashCode());var G=Buffer.from("010100000000000000000000","hex");G.writeUInt32LE(arguments[0]|67108864,4);G.writeUInt32LE(arguments[1]==null?0:arguments[1].length,8);g_internal._rq.enQueue({cmd:arguments[0],func:arguments[2],optional:F,send:(arguments[1]==null?G:Buffer.concat([G,arguments[1]]))});if(!g_internal._amt){g_internal._setupPTHI();g_internal._amt.connect(u.GUIDS.AMT,{noPipeline:1})}};this.getVersion=function t(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(26,null,function(K,J,M){if(K.Status==0){var L,I=K.Data,O={BiosVersion:I.slice(0,g_internal._amt.BiosVersionLen).toString(),Versions:[]},N=I.slice(g_internal._amt.BiosVersionLen+4);for(L=0;L<I.readUInt32LE(g_internal._amt.BiosVersionLen);++L){O.Versions[L]={Description:N.slice(2,N.readUInt16LE(0)+2).toString(),Version:N.slice(4+g_internal._amt.UnicodeStringLen,4+g_internal._amt.UnicodeStringLen+N.readUInt16LE(2+g_internal._amt.UnicodeStringLen)).toString()};N=N.slice(4+(2*g_internal._amt.UnicodeStringLen))}if(O.BiosVersion.indexOf("\0")>0){O.BiosVersion=O.BiosVersion.substring(0,O.BiosVersion.indexOf("\0"))}M.unshift(O)}else{M.unshift(null)}J.apply(this,M)},F,H)};function E(H,G){if((G==null)&&(typeof(G)!="number")){return null}if(H==null){H=""}var I="";for(var F=0;F<G-H.length;F++){I+="0"}return I+H}this.getUuid=function s(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(92,null,function(J,I,K){if(J.Status==0){var L={};L.uuid=[E(J.Data.readUInt32LE(0).toString(16),8),E(J.Data.readUInt16LE(4).toString(16),4),E(J.Data.readUInt16LE(6).toString(16),4),E(J.Data.readUInt16BE(8).toString(16),4),E(J.Data.slice(10).toString("hex").toLowerCase(),12)].join("-");K.unshift(L)}else{K.unshift(null)}I.apply(this,K)},F,H)};this.getProvisioningState=function q(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(17,null,function(J,I,K){if(J.Status==0){var L={};L.state=J.Data.readUInt32LE(0);if(L.state<3){L.stateStr=["PRE","IN","POST"][L.state]}K.unshift(L)}else{K.unshift(null)}I.apply(this,K)},F,H)};this.getProvisioningMode=function p(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(8,null,function(J,I,K){if(J.Status==0){var L={};L.mode=J.Data.readUInt32LE(0);if(L.mode<4){L.modeStr=["NONE","ENTERPRISE","SMALL_BUSINESS","REMOTE_ASSISTANCE"][L.mode]}L.legacy=J.Data.readUInt32LE(4)==0?false:true;K.unshift(L)}else{K.unshift(null)}I.apply(this,K)},F,H)};this.getEHBCState=function j(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(132,null,function(J,I,K){if(J.Status==0){K.unshift({EHBC:J.Data.readUInt32LE(0)!=0})}else{K.unshift(null)}I.apply(this,K)},F,H)};this.getControlMode=function h(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(107,null,function(J,I,K){if(J.Status==0){var L={};L.controlMode=J.Data.readUInt32LE(0);if(L.controlMode<3){L.controlModeStr=["NONE_RPAT","CLIENT","ADMIN","REMOTE_ASSISTANCE"][L.controlMode]}K.unshift(L)}else{K.unshift(null)}I.apply(this,K)},F,H)};this.getMACAddresses=function n(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(37,null,function(J,I,K){if(J.Status==0){K.unshift({DedicatedMAC:J.Data.slice(0,6).toString("hex:"),HostMAC:J.Data.slice(6,12).toString("hex:")})}else{K.unshift({DedicatedMAC:null,HostMAC:null})}I.apply(this,K)},F,H)};this.getDnsSuffix=function i(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(54,null,function(J,I,K){if(J.Status==0){var L=J.Data.readUInt16LE(0);if(L>0){K.unshift(J.Data.slice(2,2+L).toString())}else{K.unshift(null)}}else{K.unshift(null)}I.apply(this,K)},F,H)};this.getHashHandles=function k(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(44,null,function(J,I,L){var M=[];if(J.Status==0){var N=J.Data.readUInt32LE(0);for(var K=0;K<N;++K){M.push(J.Data.readUInt32LE(4+(4*K)))}}L.unshift(M);I.apply(this,L)},F,H)};this.getCertHashEntry=function f(H,F){var J=[];for(var I=2;I<arguments.length;++I){J.push(arguments[I])}var G=Buffer.alloc(4);G.writeUInt32LE(H,0);this.sendCommand(45,G,function(L,K,M){if(L.Status==0){var N={};N.isDefault=L.Data.readUInt32LE(0);N.isActive=L.Data.readUInt32LE(4);N.hashAlgorithm=L.Data.readUInt8(72);if(N.hashAlgorithm<4){N.hashAlgorithmStr=["MD5","SHA1","SHA256","SHA512"][N.hashAlgorithm];N.hashAlgorithmSize=[16,20,32,64][N.hashAlgorithm];N.certificateHash=L.Data.slice(8,8+N.hashAlgorithmSize).toString("hex")}N.name=L.Data.slice(73+2,73+2+L.Data.readUInt16LE(73)).toString();M.unshift(N)}else{M.unshift(null)}K.apply(this,M)},F,J)};this.getCertHashEntries=function e(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.getHashHandles(function(K,J,L){var I=[];this.getCertHashEntry(K.shift(),this._getHashEntrySink,J,L,I,K)},F,H)};this._getHashEntrySink=function b(J,G,I,F,H){F.push(J);if(H.length>0){this.getCertHashEntry(H.shift(),this._getHashEntrySink,G,I,F,H)}else{I.unshift(F);G.apply(this,I)}};this.getLocalSystemAccount=function m(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(103,Buffer.alloc(40),function(J,I,K){if(J.Status==0&&J.Data.length==68){K.unshift({user:C(J.Data.slice(0,33).toString()),pass:C(J.Data.slice(33,67).toString()),raw:J.Data})}else{K.unshift(null)}I.apply(this,K)},F,H)};this.getLanInterfaceSettings=function l(I,F){var K=[];for(var G=2;G<arguments.length;++G){K.push(arguments[G])}var H=Buffer.alloc(4);H.writeUInt32LE(I);this.sendCommand(72,H,function J(N,M,P){if(N.Status==0){var O={};O.enabled=N.Data.readUInt32LE(0);O.dhcpEnabled=N.Data.readUInt32LE(8);switch(N.Data[12]){case 1:O.dhcpMode="ACTIVE";break;case 2:O.dhcpMode="PASSIVE";break;default:O.dhcpMode="UNKNOWN";break}O.mac=N.Data.slice(14).toString("hex:");var L=N.Data.readUInt32LE(4);O.address=((L>>24)&255)+"."+((L>>16)&255)+"."+((L>>8)&255)+"."+(L&255);P.unshift(O);M.apply(this,P)}else{P.unshift(null);M.apply(this,P)}},F,K)};this.unprovision=function D(I,F){var J=[];for(var H=2;H<arguments.length;++H){J.push(arguments[H])}var G=Buffer.alloc(4);G.writeUInt32LE(I,0);this.sendCommand(16,G,function(L,K,M){M.unshift(L.Status);K.apply(this,M)},F,J)};this.startConfiguration=function y(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(41,null,function(J,I,K){K.unshift(J.Status);I.apply(this,K)},F,H)};this.stopConfiguration=function A(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(94,null,function(J,I,K){K.unshift(J.Status);I.apply(this,K)},F,H)};this.openUserInitiatedConnection=function v(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(68,null,function(J,I,K){K.unshift(J.Status);I.apply(this,K)},F,H)};this.closeUserInitiatedConnection=function c(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(69,null,function(J,I,K){K.unshift(J.Status);I.apply(this,K)},F,H)};this.getRemoteAccessConnectionStatus=function r(F){var H=[];for(var G=1;G<arguments.length;++G){H.push(arguments[G])}this.sendCommand(70,null,function(J,I,L){if(J.Status==0){var K=J.Data.slice(14,J.Data.readUInt16LE(12)+14).toString();L.unshift({status:J.Status,networkStatus:J.Data.readUInt32LE(0),remoteAccessStatus:J.Data.readUInt32LE(4),remoteAccessTrigger:J.Data.readUInt32LE(8),mpsHostname:K,raw:J.Data})}else{L.unshift({status:J.Status})}I.apply(this,L)},F,H)};this.getProtocolVersion=function o(F){var H=[];for(var G=1;G<arguments.length;++G){opt.push(arguments[G])}if(!this._tmpSession){this._tmpSession=u.create();this._tmpSession.parent=this}this._tmpSession.doIoctl(u.IOCTL.HECI_VERSION,Buffer.alloc(5),Buffer.alloc(5),function(N,I,M,J,K){if(N==0){var L=I.readUInt8(0).toString()+"."+I.readUInt8(1).toString()+"."+I.readUInt8(2).toString()+"."+I.readUInt16BE(3).toString();K.unshift(L);J.apply(M,K)}else{K.unshift(null);J.apply(M,K)}},this,F,H)};this.startConfigurationHBased=function z(F,I,G,H){if((F==null)||((F.length!=32)&&(F.length!=48))){H({status:-101})}this.stopConfiguration(function(K){if(K==0){var J=function L(){delete L.parent.xtimeout;L.parent.startConfigurationHBasedEx(F,I,G,H)};J.parent=this;this.xtimeout=setTimeout(J,20000)}else{this.startConfigurationHBasedEx(F,I,G,H)}})};this.startConfigurationHBasedEx=function z(F,J,H,I){var L=[];for(var K=4;K<arguments.length;++K){L.push(arguments[K])}var G=Buffer.alloc(1+64+4+4+((H!=null)?320:0));G[0]=(F.length==48)?3:2;F.copy(G,1);G.writeUInt32LE(J?1:0,65);if(H!=null){G.writeUInt32LE(H.length,69);var M=73;for(var K=0;K<H.length;K++){M+=G.write(H[K],M)+1}}this.sendCommand(139,G,function(P,O,R){if(P.Status==0){var N=null;if(P.Data[0]==2){N=P.Data.slice(1,33)}if(P.Data[0]==3){N=P.Data.slice(1,49)}R.unshift({status:P.Status,hash:N.toString("hex")})}else{R.unshift({status:P.Status})}O.apply(this,R)},I,L)}}module.exports=amt_heci;