UNPKG

meshcentral

Version:

Web based remote computer management server

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