mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-11-30 03:16:12 +03:00
1 line
6.6 KiB
JavaScript
1 line
6.6 KiB
JavaScript
var CreateWsmanComm=function(e,t,r,n,a){var u={};function o(){for(i in u.socketState=2,u.pendingAjaxCall)u.sendRequest(u.pendingAjaxCall[i][0],u.pendingAjaxCall[i][3],u.pendingAjaxCall[i][4])}u.PendingAjax=[],u.ActiveAjaxCount=0,u.MaxActiveAjaxCount=1,u.FailAllError=0,u.challengeParams=null,u.noncecounter=1,u.authcounter=0,u.socket=null,u.socketState=0,u.host=e,u.port=t,u.user=r,u.pass=n,u.tls=a,u.tlsv1only=1,u.cnonce=Math.random().toString(36).substring(7),u.PerformAjax=function(e,t,r,n,a,o){u.ActiveAjaxCount<u.MaxActiveAjaxCount&&0==u.PendingAjax.length?u.PerformAjaxEx(e,t,r,a,o):1==n?u.PendingAjax.unshift([e,t,r,a,o]):u.PendingAjax.push([e,t,r,a,o])},u.PerformNextAjax=function(){if(!(u.ActiveAjaxCount>=u.MaxActiveAjaxCount||0==u.PendingAjax.length)){var e=u.PendingAjax.shift();u.PerformAjaxEx(e[0],e[1],e[2],e[3],e[4]),u.PerformNextAjax()}},u.PerformAjaxEx=function(e,t,r,n,a){if(0==u.FailAllError)return e=e||"",u.ActiveAjaxCount++,u.PerformAjaxExNodeJS(e,t,r,n,a);u.gotNextMessagesError({status:u.FailAllError},"error",null,[e,t,r,n,a])},u.pendingAjaxCall=[],u.PerformAjaxExNodeJS=function(e,t,r,n,a){u.PerformAjaxExNodeJS2(e,t,r,n,a,3)},u.PerformAjaxExNodeJS2=function(e,t,r,n,a,o){if(o<=0||0!=u.FailAllError)return u.ActiveAjaxCount--,999!=u.FailAllError&&u.gotNextMessages(null,"error",{status:0==u.FailAllError?408:u.FailAllError},[e,t,r,n,a]),void u.PerformNextAjax();u.pendingAjaxCall.push([e,t,r,n,a,o]),0==u.socketState?u.xxConnectHttpSocket():2==u.socketState&&u.sendRequest(e,n,a)},u.sendRequest=function(e,t,r){var n=(r=r||"POST")+" "+(t=t||"/wsman")+" HTTP/1.1\r\n";if(null!=u.challengeParams){var a=hex_md5(hex_md5(u.user+":"+u.challengeParams.realm+":"+u.pass)+":"+u.challengeParams.nonce+":"+u.noncecounter+":"+u.cnonce+":"+u.challengeParams.qop+":"+hex_md5(r+":"+t+("auth-int"==u.challengeParams.qop?":"+hex_md5(e):"")));n+="Authorization: "+u.renderDigest({username:u.user,realm:u.challengeParams.realm,nonce:u.challengeParams.nonce,uri:t,qop:u.challengeParams.qop,response:a,nc:u.noncecounter++,cnonce:u.cnonce})+"\r\n"}!function(e){if(2==u.socketState&&null!=u.socket&&u.socket.readyState==WebSocket.OPEN){for(var t=new Uint8Array(e.length),r=0;r<e.length;++r)t[r]=e.charCodeAt(r);try{u.socket.send(t.buffer)}catch(e){}}}(n+="Host: "+u.host+":"+u.port+"\r\nTransfer-Encoding: chunked\r\n\r\n"+e.length.toString(16).toUpperCase()+"\r\n"+e+"\r\n0\r\n\r\n")},u.parseDigest=function(e){return function(e){return e.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}(e.substring(7)).reduce(function(e,t){var r=t.trim().split("=");return e[r[0]]=r[1].replace(new RegExp('"',"g"),""),e},{})},u.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)},u.xxConnectHttpSocket=function(){u.socketParseState=0,u.socketAccumulator="",u.socketHeader=null,u.socketData="",u.socketState=1,console.log(u.tlsv1only),u.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="+u.host+"&port="+u.port+"&tls="+u.tls+"&tlsv1only="+u.tlsv1only+("*"==r?"&serverauth=1":"")+(void 0===n?"&serverauth=1&user="+r:"")),u.socket.onopen=o,u.socket.onmessage=f,u.socket.onclose=g};var s=new FileReader,c=!1,l=[];function f(e){if("object"==typeof e.data){if(1==c)return void l.push(e.data);if(s.readAsBinaryString)c=!0,s.readAsBinaryString(new Blob([e.data]));else if(s.readAsArrayBuffer)c=!0,s.readAsArrayBuffer(e.data);else{for(var t="",r=new Uint8Array(e.data),n=r.byteLength,a=0;a<n;a++)t+=String.fromCharCode(r[a]);A(t)}}else A(e.data)}function A(e){if("object"==typeof e){for(var t="",r=new Uint8Array(e),n=r.byteLength,a=0;a<n;a++)t+=String.fromCharCode(r[a]);e=t}else if("string"!=typeof e)return;for(u.socketAccumulator+=e;;){if(0==u.socketParseState){var o=u.socketAccumulator.indexOf("\r\n\r\n");if(o<0)return;for(a in u.socketHeader=u.socketAccumulator.substring(0,o).split("\r\n"),u.socketAccumulator=u.socketAccumulator.substring(o+4),u.socketParseState=1,u.socketData="",u.socketXHeader={Directive:u.socketHeader[0].split(" ")},u.socketHeader)if(0!=a){var s=u.socketHeader[a].indexOf(":");u.socketXHeader[u.socketHeader[a].substring(0,s).toLowerCase()]=u.socketHeader[a].substring(s+2)}}if(1==u.socketParseState){var c=-1;if(null==u.socketXHeader.connection||"close"!=u.socketXHeader.connection.toLowerCase()||null!=u.socketXHeader["transfer-encoding"]&&"chunked"==u.socketXHeader["transfer-encoding"].toLowerCase())if(null!=u.socketXHeader["content-length"]){if(c=parseInt(u.socketXHeader["content-length"]),u.socketAccumulator.length<c)return;e=u.socketAccumulator.substring(0,c);u.socketAccumulator=u.socketAccumulator.substring(c),u.socketData=e,c=0}else{var l=u.socketAccumulator.indexOf("\r\n");if(l<0)return;if(c=parseInt(u.socketAccumulator.substring(0,l),16),isNaN(c))return void(u.websocket&&u.websocket.close());if(u.socketAccumulator.length<l+2+c+2)return;e=u.socketAccumulator.substring(l+2,l+2+c);u.socketAccumulator=u.socketAccumulator.substring(l+2+c+2),u.socketData+=e}else c=0;0==c&&(d(u.socketXHeader,u.socketData),u.socketParseState=0,u.socketHeader=null)}}}function d(e,t){var r=parseInt(e.Directive[1]);if(isNaN(r)&&(r=602),401==r&&++u.authcounter<3){if(u.challengeParams=u.parseDigest(e["www-authenticate"]),null!=u.challengeParams.qop){var n=u.challengeParams.qop.split(",");for(var a in n)n[a]=n[a].trim();0<=n.indexOf("auth-int")?u.challengeParams.qop="auth-int":u.challengeParams.qop="auth"}}else{var o=u.pendingAjaxCall.shift();u.authcounter=0,u.ActiveAjaxCount--,u.gotNextMessages(t,"success",{status:r},o),u.PerformNextAjax()}}function g(e){if(u.socketState=0,null!=u.socket&&(u.socket.close(),u.socket=null),0<u.pendingAjaxCall.length){var t=u.pendingAjaxCall.shift(),r=t[5];u.PerformAjaxExNodeJS2(t[0],t[1],t[2],t[3],t[4],--r)}}return s.readAsBinaryString?s.onload=function(e){A(e.target.result),0==l.length?c=!1:s.readAsBinaryString(new Blob([l.shift()]))}:s.readAsArrayBuffer&&(s.onloadend=function(e){A(e.target.result),0==l.length?c=!1:s.readAsArrayBuffer(l.shift())}),u.gotNextMessages=function(e,t,r,n){999!=u.FailAllError&&(0==u.FailAllError?200==r.status?n[1](e,200,n[2]):n[1](null,r.status,n[2]):n[1](null,u.FailAllError,n[2]))},u.gotNextMessagesError=function(e,t,r,n){999!=u.FailAllError&&(0==u.FailAllError?n[1](u,null,{Header:{HttpError:e.status}},e.status,n[2]):n[1](null,u.FailAllError,n[2]))},u.CancelAllQueries=function(e){for(;0<u.PendingAjax.length;){var t=u.PendingAjax.shift();t[1](null,e,t[2])}null!=u.websocket&&(u.websocket.close(),u.websocket=null,u.socketState=0)},u} |