mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-12-29 08:33:20 -05:00
1 line
6.2 KiB
JavaScript
1 line
6.2 KiB
JavaScript
var CreateWsmanComm=function(e,t,n,r,o){var s={};function a(e){e="00000000"+e.toString(16);return e.substring(e.length-8)}function c(){for(i in s.socketState=2,s.pendingAjaxCall)s.sendRequest(s.pendingAjaxCall[i][0],s.pendingAjaxCall[i][3],s.pendingAjaxCall[i][4])}function l(e){for(s.socketAccumulator+=(e=new Uint8Array(e.data),String.fromCharCode.apply(null,e));;){if(0==s.socketParseState){var t,n=s.socketAccumulator.indexOf("\r\n\r\n");if(n<0)return;if(s.socketHeader=s.socketAccumulator.substring(0,n).split("\r\n"),null==s.amtVersion)for(var r in s.socketHeader)0==s.socketHeader[r].indexOf("Server: Intel(R) Active Management Technology ")&&(s.amtVersion=s.socketHeader[r].substring(46));for(r in s.socketAccumulator=s.socketAccumulator.substring(n+4),s.socketParseState=1,s.socketData="",s.socketXHeader={Directive:s.socketHeader[0].split(" ")},s.socketHeader)0!=r&&(t=s.socketHeader[r].indexOf(":"),s.socketXHeader[s.socketHeader[r].substring(0,t).toLowerCase()]=s.socketHeader[r].substring(t+2))}if(1==s.socketParseState){var o=-1;if(null==s.socketXHeader.connection||"close"!=s.socketXHeader.connection.toLowerCase()||null!=s.socketXHeader["transfer-encoding"]&&"chunked"==s.socketXHeader["transfer-encoding"].toLowerCase())if(null!=s.socketXHeader["content-length"]){if(o=parseInt(s.socketXHeader["content-length"]),s.socketAccumulator.length<o)return;var a=s.socketAccumulator.substring(0,o);s.socketAccumulator=s.socketAccumulator.substring(o),s.socketData=a,o=0}else{n=s.socketAccumulator.indexOf("\r\n");if(n<0)return;if(o=parseInt(s.socketAccumulator.substring(0,n),16),isNaN(o))return void(s.websocket&&s.websocket.close());if(s.socketAccumulator.length<n+2+o+2)return;a=s.socketAccumulator.substring(n+2,n+2+o);s.socketAccumulator=s.socketAccumulator.substring(n+2+o+2),s.socketData+=a}else o=0;0==o&&(function(e,t){var n=parseInt(e.Directive[1]);isNaN(n)&&(n=602);if(401==n&&++s.authcounter<3){if(s.challengeParams=s.parseDigest(e["www-authenticate"]),null!=s.challengeParams.qop){var r,o=s.challengeParams.qop.split(",");for(r in o)o[r]=o[r].trim();0<=o.indexOf("auth-int")?s.challengeParams.qop="auth-int":s.challengeParams.qop="auth"}}else{e=s.pendingAjaxCall.shift();s.authcounter=0,s.ActiveAjaxCount--,s.gotNextMessages(t,"success",{status:n},e),s.PerformNextAjax()}}(s.socketXHeader,s.socketData),s.socketParseState=0,s.socketHeader=null)}}}function u(e){var t,n;s.socketState=0,null!=s.socket&&(s.socket.close(),s.socket=null),0<s.pendingAjaxCall.length&&(n=(t=s.pendingAjaxCall.shift())[5],s.PerformAjaxExNodeJS2(t[0],t[1],t[2],t[3],t[4],--n))}return s.PendingAjax=[],s.ActiveAjaxCount=0,s.MaxActiveAjaxCount=1,s.FailAllError=0,s.challengeParams=null,s.noncecounter=1,s.authcounter=0,s.socket=null,s.socketState=0,s.host=e,s.port=t,s.user=n,s.pass=r,s.tls=o,s.tlsv1only=1,s.cnonce=Math.random().toString(36).substring(7),s.PerformAjax=function(e,t,n,r,o,a){s.ActiveAjaxCount<s.MaxActiveAjaxCount&&0==s.PendingAjax.length?s.PerformAjaxEx(e,t,n,o,a):1==r?s.PendingAjax.unshift([e,t,n,o,a]):s.PendingAjax.push([e,t,n,o,a])},s.PerformNextAjax=function(){var e;s.ActiveAjaxCount>=s.MaxActiveAjaxCount||0==s.PendingAjax.length||(e=s.PendingAjax.shift(),s.PerformAjaxEx(e[0],e[1],e[2],e[3],e[4]),s.PerformNextAjax())},s.PerformAjaxEx=function(e,t,n,r,o){if(0==s.FailAllError)return e=e||"",s.ActiveAjaxCount++,s.PerformAjaxExNodeJS(e,t,n,r,o);s.gotNextMessagesError({status:s.FailAllError},"error",null,[e,t,n,r,o])},s.pendingAjaxCall=[],s.PerformAjaxExNodeJS=function(e,t,n,r,o){s.PerformAjaxExNodeJS2(e,t,n,r,o,3)},s.PerformAjaxExNodeJS2=function(e,t,n,r,o,a){if(a<=0||0!=s.FailAllError)return s.ActiveAjaxCount--,999!=s.FailAllError&&s.gotNextMessages(null,"error",{status:0==s.FailAllError?408:s.FailAllError},[e,t,n,r,o]),void s.PerformNextAjax();s.pendingAjaxCall.push([e,t,n,r,o,a]),0==s.socketState?s.xxConnectHttpSocket():2==s.socketState&&s.sendRequest(e,r,o)},s.sendRequest=function(e,t,n){var r=(n=n||"POST")+" "+(t=t||"/wsman")+" HTTP/1.1\r\n";null!=s.challengeParams&&(n=hex_md5(hex_md5(s.user+":"+s.challengeParams.realm+":"+s.pass)+":"+s.challengeParams.nonce+":"+a(s.noncecounter)+":"+s.cnonce+":"+s.challengeParams.qop+":"+hex_md5(n+":"+t+("auth-int"==s.challengeParams.qop?":"+hex_md5(e):""))),r+="Authorization: "+s.renderDigest({username:s.user,realm:s.challengeParams.realm,nonce:s.challengeParams.nonce,uri:t,qop:s.challengeParams.qop,response:n,nc:a(s.noncecounter++),cnonce:s.cnonce})+"\r\n"),function(e){if(2==s.socketState&&null!=s.socket&&s.socket.readyState==WebSocket.OPEN){for(var t=new Uint8Array(e.length),n=0;n<e.length;++n)t[n]=e.charCodeAt(n);try{s.socket.send(t.buffer)}catch(e){}}}(r+="Host: "+s.host+":"+s.port+"\r\nTransfer-Encoding: chunked\r\n\r\n"+e.length.toString(16).toUpperCase()+"\r\n"+e+"\r\n0\r\n\r\n")},s.parseDigest=function(e){return e.substring(7).split(",").reduce(function(e,t){return e.ic?e.st[e.st.length-1]+=","+t:e.st.push(t),t.split('"').length%2==0&&(e.ic=!e.ic),e},{st:[],ic:!1}).st.reduce(function(e,t){t=t.trim().split("=");return e[t[0]]=t[1].replace(new RegExp('"',"g"),""),e},{})},s.renderDigest=function(n){var e=[];for(i in n)e.push(i);return"Digest "+e.reduce(function(e,t){return e+","+("nc"==t||"qop"==t?t+"="+n[t]:t+'="'+n[t]+'"')},"").substring(1)},s.xxConnectHttpSocket=function(){s.socketParseState=0,s.socketAccumulator="",s.socketHeader=null,s.socketData="",s.socketState=1,s.socket=new WebSocket(window.location.protocol.replace("http","ws")+"//"+window.location.host+window.location.pathname.substring(0,window.location.pathname.lastIndexOf("/"))+"/webrelay.ashx?p=1&host="+s.host+"&port="+s.port+"&tls="+s.tls+"&tlsv1only="+s.tlsv1only+("*"==n?"&serverauth=1":"")+(void 0===r?"&serverauth=1&user="+n:"")),s.socket.binaryType="arraybuffer",s.socket.onopen=c,s.socket.onmessage=l,s.socket.onclose=u},s.gotNextMessages=function(e,t,n,r){999!=s.FailAllError&&(0==s.FailAllError?200==n.status?r[1](e,200,r[2]):r[1](null,n.status,r[2]):r[1](null,s.FailAllError,r[2]))},s.gotNextMessagesError=function(e,t,n,r){999!=s.FailAllError&&(0==s.FailAllError?r[1](s,null,{Header:{HttpError:e.status}},e.status,r[2]):r[1](null,s.FailAllError,r[2]))},s.CancelAllQueries=function(e){for(;0<s.PendingAjax.length;){var t=s.PendingAjax.shift();t[1](null,e,t[2])}null!=s.websocket&&(s.websocket.close(),s.websocket=null,s.socketState=0)},s} |