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