UNPKG

meshcentral

Version:

Web based remote computer management server

1 lines • 12.7 kB
function ShortToStr(e){return new Buffer([e>>8&255,255&e])}function ShortToStrX(e){return new Buffer([255&e,e>>8&255])}function IntToStr(e){return new Buffer([e>>24&255,e>>16&255,e>>8&255,255&e])}function IntToStrX(e){return new Buffer([255&e,e>>8&255,e>>16&255,e>>24&255])}function ReadShort(e,n){return(e[n]<<8)+e[n+1]}function ReadShortX(e,n){return(e[n+1]<<8)+e[n]}function ReadInt(e,n){return 16777216*e[n]+(e[n+1]<<16)+(e[n+2]<<8)+e[n+3]}function ReadSInt(e,n){return(e[n]<<24)+(e[n+1]<<16)+(e[n+2]<<8)+e[n+3]}function ReadIntX(e,n){return 16777216*e[n+3]+(e[n+2]<<16)+(e[n+1]<<8)+e[n]}module.exports=function(){var S={protocol:3,bytesToAmt:0,bytesFromAmt:0,rx_timeout:3e4,tx_timeout:0,heartbeat:2e4,version:1,acc:null,inSequence:0,outSequence:0,iderinfo:null,enabled:!1,iderStart:0,floppy:null,cdrom:null,floppyReady:!1,cdromReady:!1,sectorStats:null,debug:!1},i=new Buffer([0,38,49,128,0,0,0,0,5,30,16,169,8,32,2,0,3,195,0,0,0,0,0,0,0,0,0,0,40,0,0,0,0,0,0,0,2,208,0,0]),m=new Buffer([0,92,36,128,0,0,0,0,1,10,0,1,0,0,0,0,2,0,0,0,3,22,0,160,0,0,0,0,0,18,2,0,0,0,0,0,0,0,160,0,0,0,5,30,16,169,8,32,2,0,3,195,0,0,0,0,0,0,0,0,0,0,40,0,0,0,0,0,0,0,2,208,0,0,8,10,0,0,0,0,0,0,0,0,0,0,11,6,0,0,0,17,36,49]),g=new Buffer([0,38,36,128,0,0,0,0,5,30,4,176,2,18,2,0,0,80,0,0,0,0,0,0,0,0,0,0,40,0,0,0,0,0,0,0,2,208,0,0]),b=new Buffer([0,92,36,128,0,0,0,0,1,10,0,1,0,0,0,0,2,0,0,0,3,22,0,160,0,0,0,0,0,18,2,0,0,0,0,0,0,0,160,0,0,0,5,30,4,176,2,18,2,0,0,80,0,0,0,0,0,0,0,0,0,0,40,0,0,0,0,0,0,0,2,208,0,0,8,10,0,0,0,0,0,0,0,0,0,0,11,6,0,0,0,17,36,49]),p=new Buffer([0,18,1,128,0,0,0,0,26,10,0,0,0,0,0,0,0,0,0,0]),R=new Buffer([0,18,1,128,0,0,0,0,29,10,0,0,0,0,0,0,0,0,0,0]),I=new Buffer([0,32,1,128,0,0,0,0,42,24,0,0,0,0,32,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0]),C=new Buffer([0,40,1,128,0,0,0,0,1,6,0,255,0,0,0,0,42,24,0,0,0,0,2,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0]),E=(new Buffer([0,0,0,40,0,0,0,8]),new Buffer([0,0,3,4,0,8,1,0])),T=new Buffer([0,1,3,4,0,0,0,2]),w=new Buffer([0,2,3,4,0,0,0,0]),B=new Buffer([0,3,3,4,41,0,0,2]),D=new Buffer([0,16,1,8,0,0,8,0,0,1,0,0]),h=new Buffer([0,30,3,0]),y=new Buffer([1,0,3,0]),k=new Buffer([1,5,3,0]),_=new Buffer([0,18,36,128,0,0,0,0,1,10,0,1,0,0,0,0,2,0,0,0]),A=new Buffer([0,18,49,128,0,0,0,0,1,10,0,1,0,0,0,0,2,0,0,0]),X=new Buffer([0,14,1,128,0,0,0,0,1,6,0,255,0,0,0,0]),v=new Buffer([0,32,14,1,1,1,1,32,255,0,0,0,0,0,0,0,255,255,255,255,255,255,255,255,0,0,0,0,0,0,0,0,0,0,0,0]),O=new Buffer([0,0,0,4,2,0,0,0]);function F(e,n,o,r){var t=null,a=0;160==e&&(t=S.floppy,null!=S.floppy)&&(a=S.floppy.size>>9),176==e&&(t=S.cdrom,null!=S.cdrom)&&(a=S.cdrom.size>>11),o<0||a<n+o?S.SendCommandEndResponse(1,5,e,33,0):0==o?S.SendCommandEndResponse(1,0,e,0,0):null!=t&&(S.sectorStats&&S.sectorStats(1,160==e?0:1,a,n,o),160==e?(n<<=9,o<<=9):(n<<=11,o<<=11),null!==f?u.push({media:t,dev:e,lba:n,len:o,fr:r}):(f=t,c=e,d=n,l=o,function e(n){var o=l,r=d;l>S.iderinfo.readbfr&&(o=S.iderinfo.readbfr);l-=o;d+=o;var t=Buffer.alloc(o);fs.readSync(f.file,t,0,o,r);S.SendDataToHost(c,0==l,t,1&n);0<l&&0==s?e(n):(f=null,s?(S.SendCommand(71),s=!(u=[])):0<u.length&&(o=u.shift(),f=o.media,c=o.dev,d=o.lba,l=o.len,e(o.fr)))}(r)))}S.xxStateChange=function(e){S.debug&&console.log("IDER-StateChange",e),0==e&&S.Stop(),3==e&&S.Start()},S.Start=function(){S.debug&&(console.log("IDER-Start"),console.log(S.floppy,S.cdrom)),S.bytesToAmt=0,S.bytesFromAmt=0,S.inSequence=0,S.outSequence=0,u=[],S.SendCommand(64,Buffer.concat([ShortToStrX(S.rx_timeout),ShortToStrX(S.tx_timeout),ShortToStrX(S.heartbeat),IntToStrX(S.version)])),S.sectorStats&&(S.sectorStats(0,0,S.floppy?S.floppy.size>>9:0),S.sectorStats(0,1,S.cdrom?S.cdrom.size>>11:0))},S.Stop=function(){S.debug&&console.log("IDER-Stop"),S.parent.Stop()},S.ProcessData=function(e){for(S.bytesFromAmt+=e.length,null==S.acc?S.acc=e:S.acc=Buffer.concat([S.acc,e]),S.debug&&console.log("IDER-ProcessData",S.acc.length,S.acc.toString("hex"));null!=S.acc;){var n=S.ProcessDataEx();if(0==n)return;if(S.inSequence!=ReadIntX(S.acc,4))return S.debug&&console.log("ERROR: Out of sequence",S.inSequence,ReadIntX(S.acc,4)),void S.Stop();S.inSequence++,n==S.acc.length?S.acc=null:S.acc=S.acc.slice(n)}},S.SendCommand=function(e,n,o,r){null==n&&(n=Buffer.alloc(0));o=50<e&&1==o?2:0,r&&(o+=1),r=Buffer.concat([Buffer([e,0,0,o]),IntToStrX(S.outSequence++),n]);S.parent.xxSend(r),S.bytesToAmt+=r.length},S.SendCommandEndResponse=function(e,n,o,r,t){e?S.SendCommand(81,new Buffer([0,0,0,0,0,0,0,0,0,0,0,0,197,0,3,0,0,0,o,80,0,0,0]),!0):S.SendCommand(81,new Buffer([0,0,0,0,0,0,0,0,0,0,0,0,135,n<<4,3,0,0,0,o,81,n,r,t]),!0)},S.SendDataToHost=function(e,n,o,r){var t=r?0:o.length;1==n?S.SendCommand(84,Buffer.concat([new Buffer([0,255&o.length,o.length>>8,0,r?180:181,0,2,0,255&t,t>>8,e,88,133,0,3,0,0,0,e,80,0,0,0,0,0,0]),o]),n,r):S.SendCommand(84,Buffer.concat([new Buffer([0,255&o.length,o.length>>8,0,r?180:181,0,2,0,255&t,t>>8,e,88,0,0,0,0,0,0,0,0,0,0,0,0,0,0]),o]),n,r)},S.SendGetDataFromHost=function(e,n){S.SendCommand(82,new Buffer([0,255&n,n>>8,0,181,0,0,0,255&n,n>>8,e,88,0,0,0,0,0,0,0,0,0,0,0]),!1)},S.SendDisableEnableFeatures=function(e,n){null==n&&(n=""),S.SendCommand(72,Buffer.concat([new Buffer([e]),n]))},S.ProcessDataEx=function(){if(!(S.acc.length<8))switch(S.acc[0]){case 65:return S.acc.length<30?0:(a=S.acc[29],S.acc.length<30+a?0:(S.iderinfo={},S.iderinfo.major=S.acc[8],S.iderinfo.minor=S.acc[9],S.iderinfo.fwmajor=S.acc[10],S.iderinfo.fwminor=S.acc[11],S.iderinfo.readbfr=ReadShortX(S.acc,16),S.iderinfo.writebfr=ReadShortX(S.acc,18),S.iderinfo.proto=S.acc[21],S.iderinfo.iana=ReadIntX(S.acc,25),S.debug&&console.log(S.iderinfo),0!=S.iderinfo.proto&&(S.debug&&console.log("Unknown proto",S.iderinfo.proto),S.Stop()),8192<S.iderinfo.readbfr&&(S.debug&&console.log("Illegal read buffer size",S.iderinfo.readbfr),S.Stop()),8192<S.iderinfo.writebfr&&(S.debug&&console.log("Illegal write buffer size",S.iderinfo.writebfr),S.Stop()),0==S.iderStart?S.SendDisableEnableFeatures(3,IntToStrX(9)):1==S.iderStart?S.SendDisableEnableFeatures(3,IntToStrX(17)):2==S.iderStart&&S.SendDisableEnableFeatures(3,IntToStrX(25)),30+a));case 67:return S.debug&&console.log("CLOSE"),S.Stop(),8;case 68:return S.SendCommand(69),8;case 69:return S.debug&&console.log("PONG"),8;case 70:return S.acc.length<9?0:(e=S.acc[8],null===f?(S.SendCommand(71),S.debug&&console.log("RESETOCCURED1",e)):(s=!0,S.debug&&console.log("RESETOCCURED2",e)),9);case 73:if(S.acc.length<13)return 0;var e=S.acc[8],n=ReadIntX(S.acc,9);switch(S.debug&&console.log("STATUS_DATA",e,n),e){case 1:1&n&&(0==S.iderStart?S.SendDisableEnableFeatures(3,IntToStrX(9)):1==S.iderStart?S.SendDisableEnableFeatures(3,IntToStrX(17)):2==S.iderStart&&S.SendDisableEnableFeatures(3,IntToStrX(25)));break;case 2:S.enabled=!!(2&n),S.debug&&console.log("IDER Status: "+S.enabled);break;case 3:1!=n&&S.debug&&console.log("Register toggle failure")}return 13;case 74:return S.acc.length<11?0:(S.debug&&console.log("IDER: ABORT",S.acc[8]),11);case 75:return 8;case 80:var o,r,t;return S.acc.length<28?0:(e=16&S.acc[14]?176:160,o=S.acc[14],r=S.acc.slice(16,28),t=S.acc[9],S.debug&&console.log("SCSI_CMD",e,r.toString("hex"),t,o),function(e,n,o,r){switch(n[0]){case 0:switch(S.debug&&console.log("SCSI: TEST_UNIT_READY",e),e){case 160:if(null==S.floppy)return S.SendCommandEndResponse(1,2,e,58,0);if(0==S.floppyReady)return S.floppyReady=!0,S.SendCommandEndResponse(1,6,e,40,0);break;case 176:if(null==S.cdrom)return S.SendCommandEndResponse(1,2,e,58,0);if(0==S.cdromReady)return S.cdromReady=!0,S.SendCommandEndResponse(1,6,e,40,0);break;default:return S.debug&&console.log("SCSI Internal error 3",e)}S.SendCommandEndResponse(1,0,e,0,0);break;case 8:l=((31&n[1])<<16)+(n[2]<<8)+n[3],0==(d=n[4])&&(d=256),S.debug&&console.log("SCSI: READ_6",e,l,d),F(e,l,d,o);break;case 10:return l=((31&n[1])<<16)+(n[2]<<8)+n[3],0==(d=n[4])&&(d=256),S.debug&&console.log("SCSI: WRITE_6",e,l,d),S.SendCommandEndResponse(1,2,e,58,0);case 26:if(S.debug&&console.log("SCSI: MODE_SENSE_6",e),63==n[2]&&0==n[3]){var t=0,a=0;switch(e){case 160:if(null==S.floppy)return S.SendCommandEndResponse(1,2,e,58,0);t=0,a=128;break;case 176:if(null==S.cdrom)return S.SendCommandEndResponse(1,2,e,58,0);t=5,a=128;break;default:return S.debug&&console.log("SCSI Internal error 6",e)}return S.SendDataToHost(e,!0,new Buffer([0,t,a,0]),1&o)}S.SendCommandEndResponse(1,5,e,36,0);break;case 27:S.SendCommandEndResponse(1,0,e);break;case 30:if(S.debug&&console.log("SCSI: ALLOW_MEDIUM_REMOVAL",e),160==e&&null==S.floppy)return S.SendCommandEndResponse(1,2,e,58,0);if(176==e&&null==S.cdrom)return S.SendCommandEndResponse(1,2,e,58,0);S.SendCommandEndResponse(1,0,e,0,0);break;case 35:S.debug&&console.log("SCSI: READ_FORMAT_CAPACITIES",e);var c=ReadShort(n,7);switch(e){case 160:if(null==S.floppy||0==S.floppy.size)return S.SendCommandEndResponse(0,5,e,36,0);S.floppy.size;break;case 176:if(null==S.cdrom||0==S.cdrom.size)return S.SendCommandEndResponse(0,5,e,36,0);S.cdrom.size;break;default:return S.debug&&console.log("SCSI Internal error 4",e)}S.SendDataToHost(e,!0,Buffer.concat([IntToStr(8),new Buffer([0,0,11,64,2,0,2,0])]),1&o);break;case 37:S.debug&&console.log("SCSI: READ_CAPACITY",e);var d=0;switch(e){case 160:if(null==S.floppy||0==S.floppy.size)return S.SendCommandEndResponse(0,2,e,58,0);null!=S.floppy&&(d=(S.floppy.size>>9)-1),S.debug&&console.log("DEV_FLOPPY",d);break;case 176:if(null==S.cdrom||0==S.cdrom.size)return S.SendCommandEndResponse(0,2,e,58,0);null!=S.cdrom&&(d=(S.cdrom.size>>11)-1),S.debug&&console.log("DEV_CDDVD",d);break;default:return S.debug&&console.log("SCSI Internal error 4",e)}S.debug&&console.log("SCSI: READ_CAPACITY2",e,r),S.SendDataToHost(r,!0,Buffer.concat([IntToStr(d),new Buffer([0,0,176==e?8:2,0])]),1&o);break;case 40:l=ReadInt(n,2),d=ReadShort(n,7),S.debug&&console.log("SCSI: READ_10",e,l,d),F(e,l,d,o);break;case 42:case 46:l=ReadInt(n,2),d=ReadShort(n,7),S.debug&&console.log("SCSI: WRITE_10",e,l,d),S.SendGetDataFromHost(e,512*d);break;case 67:var c=ReadShort(n,7),l=2&n[1],u=7&n[2];switch(0==u&&(u=n[9]>>6),S.debug&&console.log("SCSI: READ_TOC, dev="+e+", buflen="+c+", msf="+l+", format="+u),e){case 160:return S.SendCommandEndResponse(1,5,e,32,0);case 176:break;default:return S.debug&&console.log("SCSI Internal error 9",e)}1==u?S.SendDataToHost(e,!0,new Buffer([0,10,1,1,0,20,1,0,0,0,0,0]),1&o):0==u&&(l?S.SendDataToHost(e,!0,new Buffer([0,18,1,1,0,20,1,0,0,0,2,0,0,20,170,0,0,0,52,19]),1&o):S.SendDataToHost(e,!0,new Buffer([0,18,1,1,0,20,1,0,0,0,0,0,0,20,170,0,0,0,0,0]),1&o));break;case 70:u=2!=n[1],l=ReadShort(n,2),c=ReadShort(n,7);return S.debug&&console.log("SCSI: GET_CONFIGURATION",e,u,l,c),0==c?S.SendDataToHost(e,!0,Buffer.concat([IntToStr(60),IntToStr(8)]),1&o):((s=null==(s=261==l||u&&l<261?k:256==l||u&&l<256?y:30==l||u&&l<30?h:16==l||u&&l<16?D:3==l||u&&l<3?B:2==l||u&&l<2?w:1==l||u&&l<1?T:0==l?E:null)?Buffer.concat([IntToStr(8),IntToStr(4)]):Buffer.concat([IntToStr(8),IntToStr(s.length+4),s])).length>c&&(s=s.slice(0,c)),S.SendDataToHost(e,!0,s,1&o));case 74:S.debug&&console.log("SCSI: GET_EVENT_STATUS_NOTIFICATION",e,n[1],n[4],n[9]),1!=n[1]&&16!=n[4]?(S.debug&&console.log("SCSI ERROR"),S.SendCommandEndResponse(1,5,e,38,1)):(u=0,(160==e&&null!=S.floppy||176==e&&null!=S.cdrom)&&(u=2),S.SendDataToHost(e,!0,new Buffer([0,u,128,0]),1&o));break;case 76:S.SendCommand(81,Buffer.concat([IntToStrX(0),IntToStrX(0),IntToStrX(0),new Buffer([135,80,3,0,0,0,176,81,5,32,0])]),!0);break;case 81:return S.debug&&console.log("SCSI READ_DISC_INFO",e),S.SendCommandEndResponse(0,5,e,32,0);case 85:return S.debug&&console.log("SCSI ERROR: MODE_SELECT_10",e),S.SendCommandEndResponse(1,5,e,32,0);case 90:S.debug&&console.log("SCSI: MODE_SENSE_10",e,63&n[2]);var c=ReadShort(n,7),s=null;if(0==c)return S.SendDataToHost(e,!0,Buffer.concat([IntToStr(60),IntToStr(8)]),1&o);var f=0;switch(160==e?null!=S.floppy&&(f=S.floppy.size>>9):null!=S.cdrom&&(f=S.cdrom.size>>11),63&n[2]){case 1:s=160==e?f<=2880?_:A:X;break;case 5:160==e&&(s=f<=2880?g:i);break;case 63:s=160==e?f<=2880?b:m:C;break;case 26:176==e&&(s=p);break;case 29:176==e&&(s=R);break;case 42:176==e&&(s=I)}null==s?S.SendCommandEndResponse(0,5,e,32,0):S.SendDataToHost(e,!0,s,1&o);break;case 81:S.SendDataToHost(e,!0,v,1&o);break;case 172:S.SendDataToHost(e,!0,O,1&o);break;default:return S.debug&&console.log("IDER: Unknown SCSI command",n[0]),S.SendCommandEndResponse(0,5,e,32,0)}}(e,r,t,o),28);case 83:var a;return S.acc.length<14?0:(a=ReadShortX(S.acc,9),S.acc.length<14+a?0:(S.debug&&console.log("SCSI_WRITE, len = "+(14+a)),S.SendCommand(81,new Buffer([0,0,0,0,0,0,0,0,0,0,0,0,135,112,3,0,0,0,160,81,7,39,0]),!0),14+a));default:S.debug&&console.log("Unknown IDER command",S.acc[0]),S.Stop()}return 0};var c,d,l,u=[],s=!1,f=null;return S}