meshcentral
Version:
Web based remote computer management server
1 lines • 7.11 kB
JavaScript
var CreateAgentRedirect=function(e,t,n,a,o,c){var r={};function l(){1==r.webSwitchOk&&1==r.webRtcActive&&(r.latency.current=-1,r.sendCtrlMsg('{"ctrlChannel":"102938","type":"webrtc0"}'),r.sendCtrlMsg('{"ctrlChannel":"102938","type":"webrtc1"}'),null!=r.onStateChanged&&r.onStateChanged(r,r.State))}r.m=t,t.parent=r,r.meshserver=e,r.authCookie=a,r.rauthCookie=o,r.State=0,r.nodeid=null,r.options=null,r.socket=null,r.connectstate=-1,r.tunnelid=Math.random().toString(36).substring(2),r.protocol=t.protocol,r.onStateChanged=null,r.ctrlMsgAllowed=!0,r.attemptWebRTC=!1,r.webRtcActive=!1,r.webrtcconfig=null,r.webSwitchOk=!1,r.webchannel=null,r.webrtc=null,r.debugmode=0,r.serverIsRecording=!1,r.urlname="meshrelay.ashx",r.latency={lastSend:null,current:-1,callback:null},null==c&&(c="/"),r.consoleMessage=null,r.onConsoleMessageChange=null,r.metadata=null,r.onMetadataChange=null,r.Start=function(e){var t=window.location.protocol.replace("http","ws")+"//"+window.location.host+window.location.pathname.substring(0,window.location.pathname.lastIndexOf("/"))+"/"+r.urlname+"?browser=1&p="+r.protocol+(e?"&nodeid="+e:"")+"&id="+r.tunnelid;if(null!=a&&""!=a&&(t+="&auth="+a),null!=urlargs&&null!=urlargs.slowrelay&&(t+="&slowrelay="+urlargs.slowrelay),r.nodeid=e,r.connectstate=0,r.socket=new WebSocket(t),r.socket.binaryType="arraybuffer",r.socket.onopen=r.xxOnSocketConnected,r.socket.onmessage=r.xxOnMessage,r.socket.onerror=function(e){},r.socket.onclose=r.xxOnSocketClosed,r.xxStateChange(1),null!=r.meshserver){var n="*"+c+"meshrelay.ashx?p="+r.protocol+"&nodeid="+e+"&id="+r.tunnelid;null!=o&&""!=o&&(n+="&rauth="+o),r.meshserver.send({action:"msg",type:"tunnel",nodeid:r.nodeid,value:n,usage:r.protocol})}},r.xxOnSocketConnected=function(){1==r.debugmode&&console.log("onSocketConnected"),r.latency.lastSend||(r.latency.lastSend=setInterval((function(){-1==r.latency.current?(clearInterval(r.latency.lastSend),r.latency.lastSend=null):r.sendCtrlMsg(JSON.stringify({ctrlChannel:102938,type:"rtt",time:(new Date).getTime()}))}),1e4)),r.sendCtrlMsg(JSON.stringify({ctrlChannel:102938,type:"rtt",time:(new Date).getTime()})),r.xxStateChange(2)},r.xxOnControlCommand=function(e){var t;try{t=JSON.parse(e)}catch(e){return}"102938"==t.ctrlChannel?("undefined"!=typeof args&&args.redirtrace&&console.log("RedirRecv",t),"console"==t.type?r.setConsoleMessage(t.msg,t.msgid,t.msgargs,t.timeout):"metadata"==t.type?(r.metadata=t,r.onMetadataChange&&r.onMetadataChange(r.metadata)):"rtt"==t.type&&"number"==typeof t.time?(r.latency.current=(new Date).getTime()-t.time,null!=r.latency.callback&&r.latency.callback(r.latency.current)):null!=r.webrtc?"answer"==t.type?r.webrtc.setRemoteDescription(new RTCSessionDescription(t),(function(){}),r.xxCloseWebRTC):"webrtc0"==t.type?(r.webSwitchOk=!0,l()):"webrtc1"==t.type?r.sendCtrlMsg('{"ctrlChannel":"102938","type":"webrtc2"}'):t.type:"ping"==t.type&&r.sendCtrlMsg('{"ctrlChannel":"102938","type":"pong"}')):r.m.ProcessData?r.m.ProcessData(e):console.log(e)},r.setConsoleMessage=function(e,t,n,a){r.consoleMessage!=e&&(r.consoleMessage=e,r.consoleMessageId=t,r.consoleMessageArgs=n,r.consoleMessageTimeout=a,r.onConsoleMessageChange&&r.onConsoleMessageChange(r,r.consoleMessage,r.consoleMessageId))},r.sendCtrlMsg=function(e){if(1==r.ctrlMsgAllowed){"undefined"!=typeof args&&args.redirtrace&&console.log("RedirSend",typeof e,e);try{r.socket.send(e)}catch(e){}}},r.xxOnMessage=function(e){if(r.State<3&&("c"==e.data||"cr"==e.data)){if("cr"==e.data&&(r.serverIsRecording=!0),null!=r.options){delete r.options.action,r.options.type="options";try{r.sendCtrlMsg(JSON.stringify(r.options))}catch(e){}}try{r.socket.send(r.protocol)}catch(e){}if(r.xxStateChange(3),1==r.attemptWebRTC){var t=r.webrtcconfig;"undefined"!=typeof RTCPeerConnection?r.webrtc=new RTCPeerConnection(t):"undefined"!=typeof webkitRTCPeerConnection&&(r.webrtc=new webkitRTCPeerConnection(t)),null!=r.webrtc&&r.webrtc.createDataChannel&&(r.webchannel=r.webrtc.createDataChannel("DataChannel",{}),r.webchannel.binaryType="arraybuffer",r.webchannel.onmessage=r.xxOnMessage,r.webchannel.onopen=function(){r.webRtcActive=!0,l()},r.webchannel.onclose=function(e){r.webRtcActive&&r.Stop()},r.webrtc.onicecandidate=function(e){if(null==e.candidate)try{r.sendCtrlMsg(JSON.stringify(r.webrtcoffer))}catch(e){}else r.webrtcoffer.sdp+="a="+e.candidate.candidate+"\r\n"},r.webrtc.oniceconnectionstatechange=function(){null!=r.webrtc&&("disconnected"==r.webrtc.iceConnectionState?1==r.webRtcActive?r.Stop():r.xxCloseWebRTC():"failed"==r.webrtc.iceConnectionState&&r.xxCloseWebRTC())},r.webrtc.createOffer((function(e){r.webrtcoffer=e,r.webrtc.setLocalDescription(e,(function(){}),r.xxCloseWebRTC)}),r.xxCloseWebRTC,{mandatory:{OfferToReceiveAudio:!1,OfferToReceiveVideo:!1}}))}}else if("string"==typeof e.data)"~"==e.data[0]?r.m.ProcessData(e.data):r.xxOnControlCommand(e.data);else if(r.m.ProcessBinaryCommand){if(0==d&&e.data.byteLength<4)return;if(0!=d){var n=new Uint8Array(e.data);if(g.push(n),d+=n.byteLength,i<=d){var a=new Uint8Array(d),o=0;for(var c in g)a.set(g[c],o),o+=g[c].byteLength;r.m.ProcessBinaryCommand(s,i,a),s=0,i=0,d=0,g=[]}}else{var u=((n=new Uint8Array(e.data))[0]<<8)+n[1],f=(n[2]<<8)+n[3];27==u&&8==f&&(u=(n[8]<<8)+n[9],f=(n[5]<<16)+(n[6]<<8)+n[7],n=n.slice(8)),f!=n.byteLength?(s=u,i=f,d=n.byteLength,g=[n]):r.m.ProcessBinaryCommand(u,f,n)}}else if(r.m.ProcessBinaryData)r.m.ProcessBinaryData(new Uint8Array(e.data));else if(e.data.byteLength<16e3)r.m.ProcessData(String.fromCharCode.apply(null,new Uint8Array(e.data)));else{var b=new Blob([new Uint8Array(e.data)]),y=new FileReader;y.onload=function(e){r.m.ProcessData(e.target.result)},y.readAsBinaryString(b)}};var s=0,i=0,d=0,g=[];return r.sendText=function(e){"string"!=typeof e&&(e=JSON.stringify(e)),r.send(encode_utf8(e))},r.send=function(e){"undefined"!=typeof args&&args.redirtrace&&console.log("RedirSend",typeof e,e.length,"{"==e[0]?e:rstr2hex(e).substring(0,64));try{if(null!=r.socket&&r.socket.readyState==WebSocket.OPEN)if("string"==typeof e)if(1==r.debugmode){for(var t=new Uint8Array(e.length),n=[],a=0;a<e.length;++a)t[a]=e.charCodeAt(a),n.push(e.charCodeAt(a));1==r.webRtcActive?r.webchannel.send(t.buffer):r.socket.send(t.buffer)}else{for(t=new Uint8Array(e.length),a=0;a<e.length;++a)t[a]=e.charCodeAt(a);1==r.webRtcActive?r.webchannel.send(t.buffer):r.socket.send(t.buffer)}else 1==r.webRtcActive?r.webchannel.send(e):r.socket.send(e)}catch(e){}},r.xxOnSocketClosed=function(){r.Stop(1)},r.xxStateChange=function(e){r.State!=e&&(r.State=e,r.m.xxStateChange(r.State),null!=r.onStateChanged&&r.onStateChanged(r,r.State))},r.xxCloseWebRTC=function(){if(null!=r.webchannel){try{r.webchannel.close()}catch(e){}r.webchannel=null}if(null!=r.webrtc){try{r.webrtc.close()}catch(e){}r.webrtc=null}r.webRtcActive=!1},r.Stop=function(e){if(1==r.debugmode&&console.log("stop",e),r.xxCloseWebRTC(),r.latency.current=-1,r.connectstate=-1,null!=r.socket){try{1==r.socket.readyState&&r.sendCtrlMsg('{"ctrlChannel":"102938","type":"close"}')}catch(e){}try{r.socket.readyState<=1&&r.socket.close()}catch(e){}r.socket=null}r.xxStateChange(0)},r}