mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-12-27 15:45:53 -05:00
1 line
7.4 KiB
JavaScript
1 line
7.4 KiB
JavaScript
var CreateAmtRedirect=function(t,c){var O={};((O.m=t).parent=O).authCookie=c,O.State=0,O.socket=null,O.host=null,O.port=0,O.user=null,O.pass=null,O.authuri="/RedirectionService",O.tlsv1only=0,O.inDataCount=0,O.connectstate=0,O.protocol=t.protocol,O.debugmode=0,O.amtaccumulator="",O.amtsequence=1,O.amtkeepalivetimer=null,O.onStateChanged=null,O.Start=function(t,e,r,a,o){O.host=t,O.port=e,O.user=r,O.pass=a,O.connectstate=0,O.inDataCount=0;var n=window.location.protocol.replace("http","ws")+"//"+window.location.host+window.location.pathname.substring(0,window.location.pathname.lastIndexOf("/"))+"/webrelay.ashx?p=2&host="+t+"&port="+e+"&tls="+o+("*"==r?"&serverauth=1":"")+(void 0===a?"&serverauth=1&user="+r:"");null!=c&&""!=c&&(n+="&auth="+c),O.socket=new WebSocket(n),O.socket.onopen=O.xxOnSocketConnected,O.socket.onmessage=O.xxOnMessage,O.socket.onclose=O.xxOnSocketClosed,O.xxStateChange(1)},O.xxOnSocketConnected=function(){1==O.debugmode&&console.log("onSocketConnected"),O.xxStateChange(2),1==O.protocol&&O.xxSend(O.RedirectStartSol),2==O.protocol&&O.xxSend(O.RedirectStartKvm),3==O.protocol&&O.xxSend(O.RedirectStartIder)};var n=new FileReader,l=!1,u=[];return n.readAsBinaryString?n.onload=function(t){O.xxOnSocketData(t.target.result),0==u.length?l=!1:n.readAsBinaryString(new Blob([u.shift()]))}:n.readAsArrayBuffer&&(n.onloadend=function(t){O.xxOnSocketData(t.target.result),0==u.length?l=!1:n.readAsArrayBuffer(u.shift())}),O.xxOnMessage=function(t){if(O.inDataCount++,"object"==typeof t.data){if(1==l)return void u.push(t.data);if(n.readAsBinaryString)l=!0,n.readAsBinaryString(new Blob([t.data]));else if(n.readAsArrayBuffer)l=!0,n.readAsArrayBuffer(t.data);else{for(var e="",r=new Uint8Array(t.data),a=r.byteLength,o=0;o<a;o++)e+=String.fromCharCode(r[o]);O.xxOnSocketData(e)}}else O.xxOnSocketData(t.data)},O.xxOnSocketData=function(t){if(t&&-1!=O.connectstate){if("object"==typeof t){for(var e="",r=new Uint8Array(t),a=r.byteLength,o=0;o<a;o++)e+=String.fromCharCode(r[o]);t=e}else if("string"!=typeof t)return;if((2==O.protocol||3==O.protocol)&&1==O.connectstate)return O.m.ProcessData(t);for(O.amtaccumulator+=t;1<=O.amtaccumulator.length;){var n=0;switch(O.amtaccumulator.charCodeAt(0)){case 17:if(O.amtaccumulator.length<4)return;switch(O.amtaccumulator.charCodeAt(1)){case 0:if(O.amtaccumulator.length<13)return;var c=O.amtaccumulator.charCodeAt(12);if(O.amtaccumulator.length<13+c)return;O.xxSend(String.fromCharCode(19,0,0,0,0,0,0,0,0)),n=13+c;break;default:O.Stop(1)}break;case 20:if(O.amtaccumulator.length<9)return;var l=ReadIntX(O.amtaccumulator,5);if(O.amtaccumulator.length<9+l)return;var u=O.amtaccumulator.charCodeAt(1),s=O.amtaccumulator.charCodeAt(4),i=[];for(o=0;o<l;o++)i.push(O.amtaccumulator.charCodeAt(9+o));var h=O.amtaccumulator.substring(9,9+l);if(n=9+l,0==s)0<=i.indexOf(4)?O.xxSend(String.fromCharCode(19,0,0,0,4)+IntToStrX(O.user.length+O.authuri.length+8)+String.fromCharCode(O.user.length)+O.user+String.fromCharCode(0,0)+String.fromCharCode(O.authuri.length)+O.authuri+String.fromCharCode(0,0,0,0)):0<=i.indexOf(3)?O.xxSend(String.fromCharCode(19,0,0,0,3)+IntToStrX(O.user.length+O.authuri.length+7)+String.fromCharCode(O.user.length)+O.user+String.fromCharCode(0,0)+String.fromCharCode(O.authuri.length)+O.authuri+String.fromCharCode(0,0,0)):0<=i.indexOf(1)?O.xxSend(String.fromCharCode(19,0,0,0,1)+IntToStrX(O.user.length+O.pass.length+2)+String.fromCharCode(O.user.length)+O.user+String.fromCharCode(O.pass.length)+O.pass):O.Stop(2);else if(3!=s&&4!=s||1!=u)if(0==u){if(1==O.protocol){O.xxSend(String.fromCharCode(32,0,0,0)+IntToStrX(O.amtsequence++)+ShortToStrX(1e4)+ShortToStrX(100)+ShortToStrX(0)+ShortToStrX(1e4)+ShortToStrX(100)+ShortToStrX(0)+IntToStrX(0))}2==O.protocol&&O.xxSend(String.fromCharCode(64,0,0,0,0,0,0,0)),3==O.protocol&&(O.connectstate=1,O.xxStateChange(3))}else O.Stop(3);else{var d=0,m=h.charCodeAt(d),S=h.substring(d+1,d+1+m);d+=m+1;var g=h.charCodeAt(d),f=h.substring(d+1,d+1+g);d+=g+1;var C=0,x=null,p=O.xxRandomNonce(32),k="00000002",b="";4==s&&(C=h.charCodeAt(d),x=h.substring(d+1,d+1+C),d+=C+1,b=k+":"+p+":"+x+":");var v=hex_md5(hex_md5(O.user+":"+S+":"+O.pass)+":"+f+":"+b+hex_md5("POST:"+O.authuri)),A=O.user.length+S.length+f.length+O.authuri.length+p.length+k.length+v.length+7;4==s&&(A+=x.length+1);var w=String.fromCharCode(19,0,0,0,s)+IntToStrX(A)+String.fromCharCode(O.user.length)+O.user+String.fromCharCode(S.length)+S+String.fromCharCode(f.length)+f+String.fromCharCode(O.authuri.length)+O.authuri+String.fromCharCode(p.length)+p+String.fromCharCode(k.length)+k+String.fromCharCode(v.length)+v;4==s&&(w+=String.fromCharCode(x.length)+x),O.xxSend(w)}break;case 33:if(O.amtaccumulator.length<23)break;n=23,O.xxSend(String.fromCharCode(39,0,0,0)+IntToStrX(O.amtsequence++)+String.fromCharCode(0,0,27,0,0,0)),1==O.protocol&&(O.amtkeepalivetimer=setInterval(O.xxSendAmtKeepAlive,2e3)),O.connectstate=1,O.xxStateChange(3);break;case 41:if(O.amtaccumulator.length<10)break;n=10;break;case 42:if(O.amtaccumulator.length<10)break;var y=10+((255&O.amtaccumulator.charCodeAt(9))<<8)+(255&O.amtaccumulator.charCodeAt(8));if(O.amtaccumulator.length<y)break;O.m.ProcessData(O.amtaccumulator.substring(10,y)),n=y;break;case 43:if(O.amtaccumulator.length<8)break;n=8;break;case 65:if(O.amtaccumulator.length<8)break;O.connectstate=1,O.m.Start(),8<O.amtaccumulator.length&&O.m.ProcessData(O.amtaccumulator.substring(8)),n=O.amtaccumulator.length;break;case 240:O.serverIsRecording=!0,n=1;break;default:return console.log("Unknown Intel AMT command: "+O.amtaccumulator.charCodeAt(0)+" acclen="+O.amtaccumulator.length),void O.Stop(4)}if(0==n)return;O.amtaccumulator=O.amtaccumulator.substring(n)}}},O.xxSend=function(t){if(null!=O.socket&&O.socket.readyState==WebSocket.OPEN){1==O.debugmode&&console.log("Send",t);for(var e=new Uint8Array(t.length),r=0;r<t.length;++r)e[r]=t.charCodeAt(r);try{O.socket.send(e.buffer)}catch(t){}}},O.send=function(t){null!=O.socket&&1==O.connectstate&&(1==O.protocol?O.xxSend(String.fromCharCode(40,0,0,0)+IntToStrX(O.amtsequence++)+ShortToStrX(t.length)+t):O.xxSend(t))},O.xxSendAmtKeepAlive=function(){null!=O.socket&&O.xxSend(String.fromCharCode(43,0,0,0)+IntToStrX(O.amtsequence++))},O.xxRandomNonceX="abcdef0123456789",O.xxRandomNonce=function(t){for(var e="",r=0;r<t;r++)e+=O.xxRandomNonceX.charAt(Math.floor(Math.random()*O.xxRandomNonceX.length));return e},O.xxOnSocketClosed=function(){1==O.debugmode&&console.log("onSocketClosed"),0==O.inDataCount&&0==O.tlsv1only?(O.tlsv1only=1,O.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="+O.host+"&port="+O.port+"&tls="+O.tls+"&tls1only=1"+("*"==O.user?"&serverauth=1":"")+("undefined"==typeof pass?"&serverauth=1&user="+O.user:"")),O.socket.onopen=O.xxOnSocketConnected,O.socket.onmessage=O.xxOnMessage,O.socket.onclose=O.xxOnSocketClosed):O.Stop(5)},O.xxStateChange=function(t){O.State!=t&&(O.State=t,O.m.xxStateChange(O.State),null!=O.onStateChanged&&O.onStateChanged(O,O.State))},O.Stop=function(t){1==O.debugmode&&console.log("onSocketStop",t),O.xxStateChange(0),O.connectstate=-1,O.amtaccumulator="",null!=O.socket&&(O.socket.close(),O.socket=null),null!=O.amtkeepalivetimer&&(clearInterval(O.amtkeepalivetimer),O.amtkeepalivetimer=null)},O.RedirectStartSol=String.fromCharCode(16,0,0,0,83,79,76,32),O.RedirectStartKvm=String.fromCharCode(16,1,0,0,75,86,77,82),O.RedirectStartIder=String.fromCharCode(16,0,0,0,73,68,69,82),O} |