MeshCentral/public/scripts/agent-redir-ws-0.1.1-min.js

1 line
6.7 KiB
JavaScript
Raw Normal View History

2020-05-07 02:23:27 -04:00
var CreateAgentRedirect=function(e,t,n,o,a,r){var c={};function s(){1==c.webSwitchOk&&1==c.webRtcActive&&(c.latency.current=-1,c.sendCtrlMsg('{"ctrlChannel":"102938","type":"webrtc0"}'),c.sendCtrlMsg('{"ctrlChannel":"102938","type":"webrtc1"}'),null!=c.onStateChanged&&c.onStateChanged(c,c.State))}((c.m=t).parent=c).meshserver=e,c.authCookie=o,c.rauthCookie=a,c.State=0,c.nodeid=null,c.options=null,c.socket=null,c.connectstate=-1,c.tunnelid=Math.random().toString(36).substring(2),c.protocol=t.protocol,c.onStateChanged=null,c.ctrlMsgAllowed=!0,c.attemptWebRTC=!1,c.webRtcActive=!1,c.webSwitchOk=!1,c.webchannel=null,c.webrtc=null,c.debugmode=0,c.serverIsRecording=!1,c.latency={lastSend:null,current:-1,callback:null},null==r&&(r="/"),c.consoleMessage=null,c.onConsoleMessageChange=null,c.metadata=null,c.onMetadataChange=null,c.Start=function(e){var t=window.location.protocol.replace("http","ws")+"//"+window.location.host+window.location.pathname.substring(0,window.location.pathname.lastIndexOf("/"))+"/meshrelay.ashx?browser=1&p="+c.protocol+"&nodeid="+e+"&id="+c.tunnelid;null!=o&&""!=o&&(t+="&auth="+o),null!=urlargs&&null!=urlargs.slowrelay&&(t+="&slowrelay="+urlargs.slowrelay),c.nodeid=e,c.connectstate=0,c.socket=new WebSocket(t),c.socket.onopen=c.xxOnSocketConnected,c.socket.onmessage=c.xxOnMessage,c.socket.onerror=function(e){},c.socket.onclose=c.xxOnSocketClosed,c.xxStateChange(1);var n="*"+r+"meshrelay.ashx?p="+c.protocol+"&nodeid="+e+"&id="+c.tunnelid;null!=a&&""!=a&&(n+="&rauth="+a),c.meshserver.send({action:"msg",type:"tunnel",nodeid:c.nodeid,value:n,usage:c.protocol})},c.xxOnSocketConnected=function(){1==c.debugmode&&console.log("onSocketConnected"),c.xxStateChange(2)},c.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?c.setConsoleMessage(t.msg,t.msgid,t.msgargs,t.timeout):"metadata"==t.type?(c.metadata=t,c.onMetadataChange&&c.onMetadataChange(c.metadata)):"rtt"==t.type&&"number"==typeof t.time?(c.latency.current=(new Date).getTime()-t.time,null!=c.latency.callbacks&&c.latency.callback(c.latency.current)):null!=c.webrtc&&("answer"==t.type?c.webrtc.setRemoteDescription(new RTCSessionDescription(t),function(){},c.xxCloseWebRTC):"webrtc0"==t.type?(c.webSwitchOk=!0,s()):"webrtc1"==t.type?c.sendCtrlMsg('{"ctrlChannel":"102938","type":"webrtc2"}'):t.type)):c.xxOnSocketData(e)},c.setConsoleMessage=function(e,t,n,o){c.consoleMessage!=e&&(c.consoleMessage=e,c.consoleMessageId=t,c.consoleMessageArgs=n,c.consoleMessageTimeout=o,c.onConsoleMessageChange&&c.onConsoleMessageChange(c,c.consoleMessage,c.consoleMessageId))},c.sendCtrlMsg=function(e){if(1==c.ctrlMsgAllowed){"undefined"!=typeof args&&args.redirtrace&&console.log("RedirSend",typeof e,e);try{c.socket.send(e)}catch(e){}}},c.xxOnMessage=function(e){if(c.State<3&&("c"==e.data||"cr"==e.data)){if("cr"==e.data&&(c.serverIsRecording=!0),null!=c.options){delete c.options.action,c.options.type="options";try{c.sendCtrlMsg(JSON.stringify(c.options))}catch(e){}}try{c.socket.send(c.protocol)}catch(e){}if(c.xxStateChange(3),1==c.attemptWebRTC){"undefined"!=typeof RTCPeerConnection?c.webrtc=new RTCPeerConnection(null):"undefined"!=typeof webkitRTCPeerConnection&&(c.webrtc=new webkitRTCPeerConnection(null)),null!=c.webrtc&&c.webrtc.createDataChannel&&(c.webchannel=c.webrtc.createDataChannel("DataChannel",{}),c.webchannel.onmessage=c.xxOnMessage,c.webchannel.onopen=function(){c.webRtcActive=!0,s()},c.webchannel.onclose=function(e){c.webRtcActive&&c.Stop()},c.webrtc.onicecandidate=function(e){if(null==e.candidate)try{c.sendCtrlMsg(JSON.stringify(c.webrtcoffer))}catch(e){}else c.webrtcoffer.sdp+="a="+e.candidate.candidate+"\r\n"},c.webrtc.oniceconnectionstatechange=function(){null!=c.webrtc&&("disconnected"==c.webrtc.iceConnectionState?1==c.webRtcActive?c.Stop():c.xxCloseWebRTC():"failed"==c.webrtc.iceConnectionState&&c.xxCloseWebRTC())},c.webrtc.createOffer(function(e){c.webrtcoffer=e,c.webrtc.setLocalDescription(e,function(){},c.xxCloseWebRTC)},c.xxClose