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

1 line
6.8 KiB
JavaScript

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