meshcentral
Version:
Web based remote computer management server
1 lines • 6.87 kB
JavaScript
try{Object.defineProperty(Array.prototype,"peek",{value:function(){return 0<this.length?this[this.length-1]:void 0}})}catch(e){}try{Object.defineProperty(String.prototype,"replaceAll",{value:function(e,t){return this.split(e).join(t)}})}catch(e){}var RSMB=1381190978,memoryLocation={1:"Other",2:"Unknown",3:"System Board",4:"ISA",5:"EISA",6:"PCI",7:"MCA",8:"PCMCIA",9:"Proprietary",10:"NuBus",160:"PC-98/C20",161:"PC-98/C24",162:"PC-98/E",163:"PC-98/LB"},wakeReason=["Reserved","Other","Unknown","APM Timer","Modem Ring","LAN","Power Switch","PCI","AC Power"];function zeroLeftPad(e,t){if(null==t&&"number"!=typeof t)return null;null==e&&(e="");for(var r="",i=0;i<t-e.length;i++)r+="0";return r+e}function SMBiosTables(){this._ObjectID="SMBiosTable","win32"==process.platform&&(this._marshal=require("_GenericMarshal"),this._native=this._marshal.CreateNativeProxy("Kernel32.dll"),this._native.CreateMethod("EnumSystemFirmwareTables"),this._native.CreateMethod("GetSystemFirmwareTable")),"linux"==process.platform&&(this._canonicalizeData=function(e){for(var t=e.toString().split("Header and Data:\n"),r=new(require("MemoryStream")),i=1;i<t.length;++i){var n=t[i].split("Strings:\n"),o=n[0].split("\n\n")[0].replaceAll("\n","").trim().replaceAll(" ","").replaceAll("\t","");if(r.write(Buffer.from(o,"hex")),1<n.length){var a,s=n[1].split("\n\n")[0].split("\n"),l=[];for(a in s){var c=s[a].trim().replaceAll(" ","").replaceAll("\t","");'"'!=c[0]&&l.push(c)}r.write(Buffer.from(l.join(""),"hex")),r.write(Buffer.from("00","hex"))}else r.write(Buffer.from("0000","hex"))}e=r.buffer;return e.ms=r,e}),this._parse=function(e){for(var t={},r=0;e&&r<e.length;){var i=e[r],n=e[r+1];for(t[i]||(t[i]=[]),t[i].push(e.slice(r+4,r+n)),"win32"==process.platform&&(t[i].peek()._ext=void 0),r+=n,t[i].peek()._strings=[];0!=e[r]&&r<=e.length;){for(var o=r;0!=e[r++]&&r<=e.length;);try{t[i].peek()._strings.push(e.slice(o,r).toString().trim())}catch(e){}}r+=0==t[i].peek()._strings.length?2:1,0}return t},this.get=function(e){var t,r;return"win32"==process.platform?(t=this._native.GetSystemFirmwareTable(RSMB,0,0,0).Val,this._marshal.CreatePointer()._size,r=this._marshal.CreateVariable(t),this._native.GetSystemFirmwareTable(RSMB,0,r,t).Val,r.toBuffer(),t=r.Deref(4,4).toBuffer().readUInt32LE(0),pbyte=r.Deref(8,t),SMData=pbyte.toBuffer(),e?void e.apply(this,[this._parse(SMData)]):this._parse(SMData)):"linux"!=process.platform?e?void e.apply(this,[null]):null:(r=require("MemoryStream"),this.child=require("child_process").execFile("/usr/sbin/dmidecode",["dmidecode","-u"]),(this.child.SMBiosTable=this).child.ms=new r,this.child.ms.callback=e,this.child.ms.child=this.child,this.child.stdout.on("data",function(e){this.parent.ms.write(e)}),this.child.on("exit",function(){this.ms.end()}),void this.child.ms.on("end",function(){var e;this.buffer.length<300?this.callback&&this.callback.apply(this.child.SMBiosTable,[]):(e=this.child.SMBiosTable._canonicalizeData(this.buffer),e=this.child.SMBiosTable._parse(e),this.callback&&this.callback.apply(this.child.SMBiosTable,[e]))}))},this.parse=function(e){var t={};try{t.processorInfo=this.processorInfo(e)}catch(e){}try{t.memoryInfo=this.memoryInfo(e)}catch(e){}try{t.systemInfo=this.systemInfo(e)}catch(e){}try{t.systemSlots=this.systemInfo(e)}catch(e){}try{t.amtInfo=this.amtInfo(e)}catch(e){}try{65535<JSON.stringify(t).length&&(t={})}catch(e){}return t},this.processorInfo=function(e){if(!e)throw"no data";for(var t=[],r=["ERROR","Other","Unknown","CPU","ALU","DSP","GPU"],i=["Unknown","Enabled","Disabled by user","Disabled by BIOS","Idle","Reserved","Reserved","Other"];e[4]&&0<e[4].length;){var n=e[4].pop(),o=64&n[20],a=7&n[20];o&&((o={_ObjectID:"SMBiosTables.processorInfo"}).Processor=r[n[1]],o.MaxSpeed=n.readUInt16LE(16)+" Mhz",n[31]&&(o.Cores=n[31]),n[33]&&(o.Threads=n[33]),o.Populated=1,o.Status=i[a],o.Socket=n._strings[n[0]-1],o.Manufacturer=n._strings[n[3]-1],o.Version=n._strings[n[12]-1],t.push(o))}return t},this.memoryInfo=function(e){var t;if(e)return t={_ObjectID:"SMBiosTables.memoryInfo"},e[16]&&(e=e[16].peek(),t.location=memoryLocation[e[0]],2147483648==(t.maxCapacityKb=e.readUInt32LE(3)))&&(t.maxCapacityKb="A really big number"),t;throw"no data"},this.systemInfo=function(e){var t,r;if(e)return t={_ObjectID:"SMBiosTables.systemInfo"},e[1]&&(r=(e=e[1].peek()).slice(4,20),t.uuid=[zeroLeftPad(r.readUInt32LE(0).toString(16),8),zeroLeftPad(r.readUInt16LE(4).toString(16),4),zeroLeftPad(r.readUInt16LE(6).toString(16),4),zeroLeftPad(r.readUInt16BE(8).toString(16),4),zeroLeftPad(r.slice(10).toString("hex").toLowerCase(),12)].join("-"),t.wakeReason=wakeReason[e[20]]),t;throw"no data"},this.systemSlots=function(e){if(!e)throw"no data";var t=[];if(e[9])for(;0<e[9].length;){var r=e[9].pop();t.push({name:r._strings[r[0]-1]})}return t},this.amtInfo=function(e){var t,r,i;if(e)return t={AMT:!1},e[130]&&"$AMT"==e[130].peek().slice(0,4).toString()&&(r=e[130].peek(),t.AMT=!!r[4],t.AMT)&&(t.enabled=!!r[5],t.storageRedirection=!!r[6],t.serialOverLan=!!r[7],t.kvm=!!r[14],e[131].peek())&&"vPro"==e[131].peek().slice(52,56).toString()&&(4&(i=e[131].peek())[0]&&(t.TXT=!!(8&i[0])),16&i[0]&&(t.VMX=!!(32&i[0])),t.MEBX=i.readUInt16LE(4).toString()+"."+i.readUInt16LE(6).toString()+"."+i.readUInt16LE(8).toString()+"."+i.readUInt16LE(10).toString(),r=i.slice(20,32),t.ManagementEngine=r.readUInt16LE(6).toString()+"."+r.readUInt16LE(4).toString()+"."+r.readUInt16LE(10).toString()+"."+r.readUInt16LE(8).toString()),t.AMT||e[131].peek()&&"vPro"==e[131].peek().slice(52,56).toString()&&8==(8&(i=e[131].peek())[20])&&(t.AMT=!0),t;throw"no data"},this.smTableTypes={0:"BIOS information",1:"System information",2:"Baseboard (or Module) information",4:"Processor information",5:"memory controller information",6:"Memory module information",7:"Cache information",8:"Port connector information",9:"System slots",10:"On board devices information",11:"OEM strings",12:"System configuration options",13:"BIOS language information",14:"Group associations",15:"System event log",16:"Physical memory array",17:"Memory device",18:"32bit memory error information",19:"Memory array mapped address",20:"Memory device mapped address",21:"Built-in pointing device",22:"Portable battery",23:"System reset",24:"Hardware security",25:"System power controls",26:"Voltage probe",27:"Cooling device",28:"Temperature probe",29:"Electrical current probe",30:"Out-of-band remote access",31:"Boot integrity services (BIS) entry point",32:"System boot information",33:"64bit memory error information",34:"Management device",35:"Management device component",36:"Management device threshold data",37:"Memory channel",38:"IPMI device information",39:"System power supply",40:"Additional information",41:"Onboard devices extended information",42:"Management controller host interface",126:"Inactive",127:"End-of-table"}}module.exports=new SMBiosTables