meshcentral
Version:
Web based remote computer management server
1 lines • 6.14 kB
JavaScript
var CreateAmtRedirect=function(e,t){var n={};function r(e){return String.fromCharCode.apply(null,e)}function a(e){for(var t="",n=0;n<e;n++)t+="abcdef0123456789".charAt(Math.floor(16*Math.random()));return t}return n.m=e,e.parent=n,n.authCookie=t,n.State=0,n.socket=null,n.host=null,n.port=0,n.user=null,n.pass=null,n.authuri="/RedirectionService",n.tlsv1only=0,n.inDataCount=0,n.connectstate=0,n.protocol=e.protocol,n.acc=null,n.amtsequence=1,n.amtkeepalivetimer=null,n.onStateChanged=null,n.Start=function(e,r,a,o,c){n.host=e,n.port=r,n.user=a,n.pass=o,n.tls=c,n.connectstate=0,n.inDataCount=0;var s=window.location.protocol.replace("http","ws")+"//"+window.location.host+window.location.pathname.substring(0,window.location.pathname.lastIndexOf("/"))+"/webrelay.ashx?p=2&host="+e+"&port="+r+"&tls="+c+("*"==a?"&serverauth=1":"")+(void 0===o?"&serverauth=1&user="+a:"");null!=t&&""!=t&&(s+="&auth="+t),n.socket=new WebSocket(s),n.socket.binaryType="arraybuffer",n.socket.onopen=n.xxOnSocketConnected,n.socket.onmessage=n.xxOnMessage,n.socket.onclose=n.xxOnSocketClosed,n.xxStateChange(1)},n.xxOnSocketConnected=function(){n.xxStateChange(2),1==n.protocol&&n.directSend(new Uint8Array([16,0,0,0,83,79,76,32])),2==n.protocol&&n.directSend(new Uint8Array([16,1,0,0,75,86,77,82])),3==n.protocol&&n.directSend(new Uint8Array([16,0,0,0,73,68,69,82]))},n.xxOnMessage=function(e){if(e.data&&-1!=n.connectstate){if(n.inDataCount++,1==n.connectstate&&(2==n.protocol||3==n.protocol))return n.m.ProcessBinaryData?n.m.ProcessBinaryData(e.data):n.m.ProcessData(r(e.data));if(null==n.acc)n.acc=e.data;else{var t=new Uint8Array(n.acc.byteLength+e.data.byteLength);t.set(new Uint8Array(n.acc),0),t.set(new Uint8Array(e.data),n.acc.byteLength),n.acc=t.buffer}for(;null!=n.acc&&n.acc.byteLength>=1;){var o=0,c=new Uint8Array(n.acc);switch(c[0]){case 17:if(c.byteLength<4)return;if(0===c[1]){if(c.byteLength<13)return;var s=c[12];if(c.byteLength<13+s)return;n.directSend(new Uint8Array([19,0,0,0,0,0,0,0,0])),o=13+s}else n.Stop(1);break;case 20:if(c.byteLength<9)return;var l=new DataView(n.acc).getUint32(5,!0);if(c.byteLength<9+l)return;var h=c[1],u=c[4],S=[];for(i=0;i<l;i++)S.push(c[9+i]);var f=new Uint8Array(n.acc.slice(9,9+l));if(o=9+l,0==u)S.indexOf(4)>=0?n.xxSend(String.fromCharCode(19,0,0,0,4)+IntToStrX(n.user.length+n.authuri.length+8)+String.fromCharCode(n.user.length)+n.user+String.fromCharCode(0,0)+String.fromCharCode(n.authuri.length)+n.authuri+String.fromCharCode(0,0,0,0)):n.Stop(2);else if(3!=u&&4!=u||1!=h)if(0==h)switch(n.protocol){case 1:n.xxSend(String.fromCharCode(32,0,0,0)+IntToStrX(n.amtsequence++)+ShortToStrX(1e4)+ShortToStrX(100)+ShortToStrX(0)+ShortToStrX(1e4)+ShortToStrX(100)+ShortToStrX(0)+IntToStrX(0));break;case 2:n.directSend(new Uint8Array([64,0,0,0,0,0,0,0]));break;case 3:n.connectstate=1,n.xxStateChange(3)}else n.Stop(3);else{var d=0,g=f[d],C=r(new Uint8Array(f.buffer.slice(d+1,d+1+g))),m=f[d+=g+1],x=r(new Uint8Array(f.buffer.slice(d+1,d+1+m)));d+=m+1;var y=0,b=null,p=a(32),k="00000002",w="";4==u&&(y=f[d],b=r(new Uint8Array(f.buffer.slice(d+1,d+1+y))),d+=y+1,w=k+":"+p+":"+b+":");var v=hex_md5(hex_md5(n.user+":"+C+":"+n.pass)+":"+x+":"+w+hex_md5("POST:"+n.authuri)),A=n.user.length+C.length+x.length+n.authuri.length+p.length+8+v.length+7;4==u&&(A+=b.length+1);var U=String.fromCharCode(19,0,0,0,u)+IntToStrX(A)+String.fromCharCode(n.user.length)+n.user+String.fromCharCode(C.length)+C+String.fromCharCode(x.length)+x+String.fromCharCode(n.authuri.length)+n.authuri+String.fromCharCode(p.length)+p+String.fromCharCode(8)+k+String.fromCharCode(v.length)+v;4==u&&(U+=String.fromCharCode(b.length)+b),n.xxSend(U)}break;case 33:if(c.byteLength<23)break;o=23,n.xxSend(String.fromCharCode(39,0,0,0)+IntToStrX(n.amtsequence++)+String.fromCharCode(0,0,27,0,0,0)),1==n.protocol&&(n.amtkeepalivetimer=setInterval(n.xxSendAmtKeepAlive,2e3)),n.connectstate=1,n.xxStateChange(3);break;case 41:if(c.byteLength<10)break;o=10;break;case 42:if(c.byteLength<10)break;var L=10+(c[9]<<8)+c[8];if(c.byteLength<L)break;n.m.ProcessBinaryData?n.m.ProcessBinaryData(new Uint8Array(c.buffer.slice(10,L))):n.m.ProcessData(r(new Uint8Array(c.buffer.slice(10,L)))),o=L;break;case 43:if(c.byteLength<8)break;o=8;break;case 65:if(c.byteLength<8)break;n.connectstate=1,n.m.Start(),c.byteLength>8&&(n.m.ProcessBinaryData?n.m.ProcessBinaryData(new Uint8Array(c.buffer.slice(8))):n.m.ProcessData(r(new Uint8Array(c.buffer.slice(8))))),o=c.byteLength;break;case 240:n.serverIsRecording=!0,o=1;break;default:return console.log("Unknown Intel AMT command: "+c[0]+" acclen="+c.byteLength),void n.Stop(4)}if(0==o)return;o!=n.acc.byteLength?n.acc=n.acc.slice(o):n.acc=null}}},n.directSend=function(e){try{n.socket.send(e.buffer)}catch(e){}},n.xxSend=function(e){if(null!=n.socket&&n.socket.readyState==WebSocket.OPEN){for(var t=new Uint8Array(e.length),r=0;r<e.length;++r)t[r]=e.charCodeAt(r);try{n.socket.send(t.buffer)}catch(e){}}},n.Send=n.send=function(e){null!=n.socket&&1==n.connectstate&&(1==n.protocol?n.xxSend(String.fromCharCode(40,0,0,0)+IntToStrX(n.amtsequence++)+ShortToStrX(e.length)+e):n.xxSend(e))},n.xxSendAmtKeepAlive=function(){null!=n.socket&&n.xxSend(String.fromCharCode(43,0,0,0)+IntToStrX(n.amtsequence++))},n.xxOnSocketClosed=function(){0==n.inDataCount&&0==n.tlsv1only?(n.tlsv1only=1,n.socket=new WebSocket(window.location.protocol.replace("http","ws")+"//"+window.location.host+window.location.pathname.substring(0,window.location.pathname.lastIndexOf("/"))+"/webrelay.ashx?p=2&host="+n.host+"&port="+n.port+"&tls="+n.tls+"&tls1only=1"+("*"==n.user?"&serverauth=1":"")+("undefined"==typeof pass?"&serverauth=1&user="+n.user:"")),n.socket.binaryType="arraybuffer",n.socket.onopen=n.xxOnSocketConnected,n.socket.onmessage=n.xxOnMessage,n.socket.onclose=n.xxOnSocketClosed):n.Stop(5)},n.xxStateChange=function(e){n.State!=e&&(n.State=e,n.m.xxStateChange(n.State),null!=n.onStateChanged&&n.onStateChanged(n,n.State))},n.Stop=function(e){n.xxStateChange(0),n.connectstate=-1,n.acc=null,null!=n.socket&&(n.socket.close(),n.socket=null),null!=n.amtkeepalivetimer&&(clearInterval(n.amtkeepalivetimer),n.amtkeepalivetimer=null)},n}