2017-08-28 12:27:45 -04:00
|
|
|
/**
|
|
|
|
* @description Mesh Agent Transport Module - using websocket relay
|
|
|
|
* @author Ylian Saint-Hilaire
|
|
|
|
* @version v0.0.1f
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Construct a MeshServer agent direction object
|
|
|
|
var CreateAgentRedirect = function (meshserver, module, serverPublicNamePort) {
|
|
|
|
var obj = {};
|
|
|
|
obj.m = module; // This is the inner module (Terminal or Desktop)
|
|
|
|
module.parent = obj;
|
|
|
|
obj.meshserver = meshserver;
|
|
|
|
obj.nodeid = null;
|
|
|
|
obj.State = 0;
|
|
|
|
obj.socket = null;
|
|
|
|
obj.connectstate = -1;
|
|
|
|
obj.tunnelid = Math.random().toString(36).substring(2); // Generate a random client tunnel id
|
|
|
|
obj.protocol = module.protocol; // 1 = SOL, 2 = KVM, 3 = IDER, 4 = Files, 5 = FileTransfer
|
2017-10-15 02:22:19 -04:00
|
|
|
obj.attemptWebRTC = false;
|
2018-01-16 20:30:34 -05:00
|
|
|
obj.webRtcActive = false;
|
2017-10-15 02:22:19 -04:00
|
|
|
obj.webrtc = null;
|
|
|
|
obj.webchannel = null;
|
2017-08-28 12:27:45 -04:00
|
|
|
obj.onStateChanged = null;
|
2018-01-09 23:13:41 -05:00
|
|
|
obj.debugmode = 0;
|
2017-08-28 12:27:45 -04:00
|
|
|
|
|
|
|
// Private method
|
|
|
|
//obj.debug = function (msg) { console.log(msg); }
|
|
|
|
|
|
|
|
obj.Start = function (nodeid) {
|
|
|
|
var url2, url = window.location.protocol.replace("http", "ws") + "//" + window.location.host + window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/')) + "/meshrelay.ashx?id=" + obj.tunnelid;
|
2017-09-20 17:44:22 -04:00
|
|
|
//if (serverPublicNamePort) { url2 = window.location.protocol.replace("http", "ws") + "//" + serverPublicNamePort + "/meshrelay.ashx?id=" + obj.tunnelid; } else { url2 = url; }
|
2017-08-28 12:27:45 -04:00
|
|
|
obj.nodeid = nodeid;
|
|
|
|
obj.connectstate = 0;
|
|
|
|
obj.socket = new WebSocket(url);
|
|
|
|
obj.socket.onopen = obj.xxOnSocketConnected;
|
|
|
|
obj.socket.onmessage = obj.xxOnMessage;
|
2017-09-01 14:23:22 -04:00
|
|
|
obj.socket.onerror = function (e) { console.error(e); }
|
2017-08-28 12:27:45 -04:00
|
|
|
obj.socket.onclose = obj.xxOnSocketClosed;
|
|
|
|
obj.xxStateChange(1);
|
2017-09-20 17:44:22 -04:00
|
|
|
//obj.meshserver.Send({ action: 'msg', type: 'tunnel', nodeid: obj.nodeid, value: url2 });
|
|
|
|
obj.meshserver.Send({ action: 'msg', type: 'tunnel', nodeid: obj.nodeid, value: "*/meshrelay.ashx?id=" + obj.tunnelid });
|
2017-08-28 12:27:45 -04:00
|
|
|
//obj.debug("Agent Redir Start: " + url);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.xxOnSocketConnected = function () {
|
2018-01-09 23:13:41 -05:00
|
|
|
if (obj.debugmode == 1) { console.log('onSocketConnected'); }
|
2017-08-28 12:27:45 -04:00
|
|
|
//obj.debug("Agent Redir Socket Connected");
|
|
|
|
obj.xxStateChange(2);
|
|
|
|
}
|
|
|
|
|
2017-10-15 02:22:19 -04:00
|
|
|
// Called to pass websocket control messages
|
|
|
|
obj.xxOnControlCommand = function (msg) {
|
|
|
|
var controlMsg = JSON.parse(msg);
|
|
|
|
if ((controlMsg.type == 'answer') && (obj.webrtc != null)) {
|
2018-01-16 20:30:34 -05:00
|
|
|
//console.log('gotAnswer', JSON.stringify(controlMsg));
|
|
|
|
obj.webrtc.setRemoteDescription(new RTCSessionDescription(controlMsg), function () { /*console.log('WebRTC remote ok');*/ }, obj.xxCloseWebRTC);
|
2017-10-15 02:22:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the WebRTC connection, should be called if a problem occurs during WebRTC setup.
|
|
|
|
obj.xxCloseWebRTC = function () {
|
|
|
|
if (obj.webchannel != null) { obj.webchannel.close(); obj.webchannel = null; }
|
|
|
|
if (obj.webrtc != null) { obj.webrtc.close(); obj.webrtc = null; }
|
|
|
|
}
|
|
|
|
|
2017-08-28 12:27:45 -04:00
|
|
|
obj.xxOnMessage = function (e) {
|
2018-01-16 20:30:34 -05:00
|
|
|
//if (obj.debugmode == 1) { console.log('Recv', e.data); }
|
2017-10-15 02:22:19 -04:00
|
|
|
if (obj.State < 3) {
|
|
|
|
if (e.data == 'c') {
|
|
|
|
obj.socket.send(obj.protocol);
|
|
|
|
obj.xxStateChange(3);
|
|
|
|
|
|
|
|
if (obj.attemptWebRTC == true) {
|
|
|
|
// Try to get WebRTC setup
|
|
|
|
var configuration = null; //{ "iceServers": [ { 'urls': 'stun:stun.services.mozilla.com' }, { 'urls': 'stun:stun.l.google.com:19302' } ] };
|
|
|
|
if (typeof RTCPeerConnection !== 'undefined') { obj.webrtc = new RTCPeerConnection(configuration); }
|
|
|
|
else if (typeof webkitRTCPeerConnection !== 'undefined') { obj.webrtc = new webkitRTCPeerConnection(configuration); }
|
|
|
|
|
|
|
|
if (obj.webrtc != null) {
|
|
|
|
obj.webchannel = obj.webrtc.createDataChannel("DataChannel", {}); // { ordered: false, maxRetransmits: 2 }
|
2018-01-16 20:30:34 -05:00
|
|
|
obj.webchannel.onmessage = function (event) { console.log("DataChannel - onmessage", event.data); obj.xxOnMessage(event.data); };
|
|
|
|
obj.webchannel.onopen = function () { obj.webRtcActive = true; if (obj.onStateChanged != null) { obj.onStateChanged(obj, obj.State); } /*obj.webchannel.send("Browser WebRTC Hello!!!");*/ };
|
|
|
|
obj.webchannel.onclose = function (event) { obj.Stop(); }
|
2017-10-15 02:22:19 -04:00
|
|
|
obj.webrtc.onicecandidate = function (e) {
|
|
|
|
if (e.candidate == null) {
|
2018-01-16 20:30:34 -05:00
|
|
|
//console.log('createOffer', JSON.stringify(obj.webrtcoffer));
|
|
|
|
obj.socket.send(JSON.stringify(obj.webrtcoffer)); // End of candidates, send the offer
|
2017-10-15 02:22:19 -04:00
|
|
|
} else {
|
|
|
|
obj.webrtcoffer.sdp += ("a=" + e.candidate.candidate + "\r\n"); // New candidate, add it to the SDP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
obj.webrtc.oniceconnectionstatechange = function () {
|
|
|
|
if (obj.webrtc != null) {
|
2018-01-16 20:30:34 -05:00
|
|
|
//console.log('WebRTC ICE', obj.webrtc.iceConnectionState);
|
2017-10-15 02:22:19 -04:00
|
|
|
if ((obj.webrtc.iceConnectionState == 'disconnected') || (obj.webrtc.iceConnectionState == 'failed')) { obj.xxCloseWebRTC(); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
obj.webrtc.createOffer(function (offer) {
|
|
|
|
// Got the offer
|
|
|
|
obj.webrtcoffer = offer;
|
2018-01-16 20:30:34 -05:00
|
|
|
obj.webrtc.setLocalDescription(offer, function () { /*console.log('WebRTC local ok');*/ }, obj.xxCloseWebRTC);
|
2017-10-15 02:22:19 -04:00
|
|
|
}, obj.xxCloseWebRTC, { mandatory: { OfferToReceiveAudio: false, OfferToReceiveVideo: false } });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 20:30:34 -05:00
|
|
|
|
2017-10-15 02:22:19 -04:00
|
|
|
if (typeof e.data == 'string') {
|
|
|
|
// Control messages, most likely WebRTC setup
|
|
|
|
obj.xxOnControlCommand(e.data);
|
2018-01-16 20:30:34 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof e.data == 'object') {
|
2017-08-28 12:27:45 -04:00
|
|
|
var f = new FileReader();
|
|
|
|
if (f.readAsBinaryString) {
|
|
|
|
// Chrome & Firefox (Draft)
|
|
|
|
f.onload = function (e) { obj.xxOnSocketData(e.target.result); }
|
|
|
|
f.readAsBinaryString(new Blob([e.data]));
|
|
|
|
} else if (f.readAsArrayBuffer) {
|
|
|
|
// Chrome & Firefox (Spec)
|
|
|
|
f.onloadend = function (e) { obj.xxOnSocketData(e.target.result); }
|
|
|
|
f.readAsArrayBuffer(e.data);
|
|
|
|
} else {
|
|
|
|
// IE10, readAsBinaryString does not exist, use an alternative.
|
|
|
|
var binary = "";
|
|
|
|
var bytes = new Uint8Array(e.data);
|
|
|
|
var length = bytes.byteLength;
|
|
|
|
for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); }
|
|
|
|
obj.xxOnSocketData(binary);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If we get a string object, it maybe the WebRTC confirm. Ignore it.
|
|
|
|
//obj.debug("Agent Redir Relay - OnData - " + typeof e.data + " - " + e.data.length);
|
|
|
|
obj.xxOnSocketData(e.data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
obj.xxOnSocketData = function (data) {
|
|
|
|
if (!data || obj.connectstate == -1) return;
|
|
|
|
if (typeof data === 'object') {
|
|
|
|
// This is an ArrayBuffer, convert it to a string array (used in IE)
|
|
|
|
var binary = "", bytes = new Uint8Array(data), length = bytes.byteLength;
|
|
|
|
for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); }
|
|
|
|
data = binary;
|
|
|
|
}
|
|
|
|
else if (typeof data !== 'string') return;
|
|
|
|
//console.log("xxOnSocketData", rstr2hex(data));
|
|
|
|
return obj.m.ProcessData(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.Send = function (x) {
|
2018-01-16 20:30:34 -05:00
|
|
|
//obj.debug("Agent Redir Send(" + obj.webRtcActive + ", " + x.length + "): " + rstr2hex(x));
|
|
|
|
//console.log("Agent Redir Send(" + obj.webRtcActive + ", " + x.length + "): " + rstr2hex(x));
|
2017-08-28 12:27:45 -04:00
|
|
|
if (obj.socket != null && obj.socket.readyState == WebSocket.OPEN) {
|
|
|
|
if (typeof x == 'string') {
|
2018-01-09 23:13:41 -05:00
|
|
|
if (obj.debugmode == 1) {
|
|
|
|
var b = new Uint8Array(x.length), c = [];
|
|
|
|
for (var i = 0; i < x.length; ++i) { b[i] = x.charCodeAt(i); c.push(x.charCodeAt(i)); }
|
2018-01-16 20:30:34 -05:00
|
|
|
if (obj.webRtcActive == true) { obj.webchannel.send(b.buffer); } else { obj.socket.send(b.buffer); }
|
|
|
|
//console.log('Send', c);
|
2018-01-09 23:13:41 -05:00
|
|
|
} else {
|
|
|
|
var b = new Uint8Array(x.length);
|
|
|
|
for (var i = 0; i < x.length; ++i) { b[i] = x.charCodeAt(i); }
|
2018-01-16 20:30:34 -05:00
|
|
|
if (obj.webRtcActive == true) { obj.webchannel.send(b.buffer); } else { obj.socket.send(b.buffer); }
|
2018-01-09 23:13:41 -05:00
|
|
|
}
|
2017-08-28 12:27:45 -04:00
|
|
|
} else {
|
2018-01-16 20:30:34 -05:00
|
|
|
//if (obj.debugmode == 1) { console.log('Send', x); }
|
|
|
|
if (obj.webRtcActive == true) { obj.webchannel.send(x); } else { obj.socket.send(x); }
|
2017-08-28 12:27:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.xxOnSocketClosed = function () {
|
|
|
|
//obj.debug("Agent Redir Socket Closed");
|
2018-01-16 20:30:34 -05:00
|
|
|
//if (obj.debugmode == 1) { console.log('onSocketClosed'); }
|
2018-01-09 23:13:41 -05:00
|
|
|
obj.Stop(1);
|
2017-08-28 12:27:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
obj.xxStateChange = function(newstate) {
|
|
|
|
if (obj.State == newstate) return;
|
|
|
|
obj.State = newstate;
|
|
|
|
obj.m.xxStateChange(obj.State);
|
|
|
|
if (obj.onStateChanged != null) obj.onStateChanged(obj, obj.State);
|
|
|
|
}
|
|
|
|
|
2018-01-09 23:13:41 -05:00
|
|
|
obj.Stop = function (x) {
|
|
|
|
if (obj.debugmode == 1) { console.log('stop', x); }
|
2017-08-28 12:27:45 -04:00
|
|
|
//obj.debug("Agent Redir Socket Stopped");
|
2018-01-16 20:30:34 -05:00
|
|
|
obj.webRtcActive = false;
|
|
|
|
obj.webrtc = null;
|
|
|
|
obj.webchannel = null;
|
2017-08-28 12:27:45 -04:00
|
|
|
obj.xxStateChange(0);
|
|
|
|
obj.connectstate = -1;
|
2017-10-15 02:22:19 -04:00
|
|
|
obj.xxCloseWebRTC();
|
2017-08-28 12:27:45 -04:00
|
|
|
if (obj.socket != null) { obj.socket.close(); obj.socket = null; }
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|