MeshCentral/public/scripts/amt-wsman-ws-0.2.0-min.js
2020-09-07 20:18:46 -07:00

1 line
6.1 KiB
JavaScript

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