MeshCentral/public/scripts/agent-redir-ws-0.1.1-min.js
2020-09-07 20:18:46 -07:00

1 line
6.3 KiB
JavaScript

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