From e0f80fb2f94f316029eaeb934edd07691cc530bc Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 09:39:09 -0700 Subject: [PATCH 01/18] Remove an obsolete webrtc parameter When creating a webrtc connection, do not pass a 'url' property to in the iceservers parameter, as per https://developer.mozilla.org/en-US/docs/Web/API/RTCIceServer --- lib/ssh-session.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/ssh-session.js b/lib/ssh-session.js index c35ffdf..8baa1a8 100644 --- a/lib/ssh-session.js +++ b/lib/ssh-session.js @@ -92,12 +92,10 @@ SSHSession.prototype.connect = function(connectTimeout, closeCallBack) { debug('Will open peer connection with stun', this.stun, 'turn', this.turn); this.wrtc.RTCPeerConnection({ iceServers: [{ - urls: this.stun, - url: this.stun + urls: this.stun }, { urls: this.turn, - url: this.turn, username: this.username, credential: this.password } From ee2552c08254f64aa7f4dac276529a605517456a Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 09:50:32 -0700 Subject: [PATCH 02/18] Update webrtc-request.js --- lib/webrtc-request.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/webrtc-request.js b/lib/webrtc-request.js index 81e1d9f..ec2fed5 100644 --- a/lib/webrtc-request.js +++ b/lib/webrtc-request.js @@ -124,6 +124,7 @@ class WRTC { } this.log('WEBRTC_SET_REMOTEDESC', desc); let w = new this.webrtc.RTCSessionDescription(desc); + wait(this.waiter).then(() => { this.peer.setRemoteDescription( w, @@ -448,4 +449,4 @@ class WRTC { } } -module.exports = WRTC; \ No newline at end of file +module.exports = WRTC; From a5f76e4fc0e958ef9f702df8f9312a20980b08db Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 09:56:30 -0700 Subject: [PATCH 03/18] Update ssh-session.js --- lib/ssh-session.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ssh-session.js b/lib/ssh-session.js index 8baa1a8..1d5e515 100644 --- a/lib/ssh-session.js +++ b/lib/ssh-session.js @@ -92,7 +92,7 @@ SSHSession.prototype.connect = function(connectTimeout, closeCallBack) { debug('Will open peer connection with stun', this.stun, 'turn', this.turn); this.wrtc.RTCPeerConnection({ iceServers: [{ - urls: this.stun + urls: [ this.stun ] }, { urls: this.turn, From d3ebf37fb366d82d8ce6f193232d4a567ddb572e Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 10:31:41 -0700 Subject: [PATCH 04/18] Update ssh-session.js --- lib/ssh-session.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ssh-session.js b/lib/ssh-session.js index 1d5e515..07991c6 100644 --- a/lib/ssh-session.js +++ b/lib/ssh-session.js @@ -1,6 +1,7 @@ let wrtc = require('./webrtc-request'); let Uuid = require('uuid'); let debug = require('debug')('Unifi SSH'); +debug.log = console.log.bind(console); function SSHSession(unifi, mac, uuid, stun, turn, username, password, site, autoclose, webrtc, waiter) { this.unifi = unifi; From 143afd71f7ee6c4ffef7dcc3fdcd4134e3c17b72 Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 10:49:20 -0700 Subject: [PATCH 05/18] Update ssh-session.js --- lib/ssh-session.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ssh-session.js b/lib/ssh-session.js index 07991c6..5eaf0ae 100644 --- a/lib/ssh-session.js +++ b/lib/ssh-session.js @@ -108,8 +108,8 @@ SSHSession.prototype.connect = function(connectTimeout, closeCallBack) { ] }); // ICE Servers let connStateChange = () => { - debug('CAREFUL, Connection state changed'); let state = this.wrtc.peer.iceConnectionState; + debug('CAREFUL, Connection state changed: ' + state); if (state == 'disconnected' || state == 'failed') { debug('We are notified for session disconnection'); let rej = this.state != "open"; From 812cc5aa0069b5642cee64233eb7cac4c5c6868d Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 11:34:34 -0700 Subject: [PATCH 06/18] Update ssh-session.js --- lib/ssh-session.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ssh-session.js b/lib/ssh-session.js index 5eaf0ae..41b136f 100644 --- a/lib/ssh-session.js +++ b/lib/ssh-session.js @@ -93,7 +93,7 @@ SSHSession.prototype.connect = function(connectTimeout, closeCallBack) { debug('Will open peer connection with stun', this.stun, 'turn', this.turn); this.wrtc.RTCPeerConnection({ iceServers: [{ - urls: [ this.stun ] + urls: this.stun }, { urls: this.turn, From e916ab49594307acb0edaf448d40ee518090bfb2 Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 12:55:33 -0700 Subject: [PATCH 07/18] Update ssh-session.js --- lib/ssh-session.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/ssh-session.js b/lib/ssh-session.js index 41b136f..90b69c8 100644 --- a/lib/ssh-session.js +++ b/lib/ssh-session.js @@ -81,11 +81,13 @@ SSHSession.prototype.connect = function(connectTimeout, closeCallBack) { this.password = d.password; } } + debug("Building ssh session with params: mac=' + this.mac + ' uuid=' + this.uuid + ' ttl=-1 stun=' + (this.stun instanceof Array && this.stun[0] ? this.stun[0].replace(/stun:/, '') : this.stun) + ' turn=' + (this.turn instanceof Array && this.turn[0] ? this.turn[0].replace(/turn:/, '') : this.turn) + ' username='+this.username + ' password=' + this.password + ' site=' + this.site); return this.unifi.buildSSHSession(this.mac, this.uuid, "-1", (this.stun instanceof Array && this.stun[0] ? this.stun[0].replace(/stun:/, '') : this.stun), (this.turn instanceof Array && this.turn[0] ? this.turn[0].replace(/turn:/, '') : this.turn), this.username, this.password, this.site); }); } firstCall - .then(() => { + .then((r) => { + debug("first call response: " + stringify(r)); let o = { debug: this.debug }; if (this.waiter) o.waiter = this.waiter; if (this.webrtc) o.webrtc = this.webrtc; From fb4619078473c13cb4236550bc86d84212f8d142 Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 12:57:51 -0700 Subject: [PATCH 08/18] Update ssh-session.js --- lib/ssh-session.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ssh-session.js b/lib/ssh-session.js index 90b69c8..c1978a4 100644 --- a/lib/ssh-session.js +++ b/lib/ssh-session.js @@ -81,7 +81,7 @@ SSHSession.prototype.connect = function(connectTimeout, closeCallBack) { this.password = d.password; } } - debug("Building ssh session with params: mac=' + this.mac + ' uuid=' + this.uuid + ' ttl=-1 stun=' + (this.stun instanceof Array && this.stun[0] ? this.stun[0].replace(/stun:/, '') : this.stun) + ' turn=' + (this.turn instanceof Array && this.turn[0] ? this.turn[0].replace(/turn:/, '') : this.turn) + ' username='+this.username + ' password=' + this.password + ' site=' + this.site); + debug('Building ssh session with params: mac=' + this.mac + ' uuid=' + this.uuid + ' ttl=-1 stun=' + (this.stun instanceof Array && this.stun[0] ? this.stun[0].replace(/stun:/, '') : this.stun) + ' turn=' + (this.turn instanceof Array && this.turn[0] ? this.turn[0].replace(/turn:/, '') : this.turn) + ' username='+this.username + ' password=' + this.password + ' site=' + this.site); return this.unifi.buildSSHSession(this.mac, this.uuid, "-1", (this.stun instanceof Array && this.stun[0] ? this.stun[0].replace(/stun:/, '') : this.stun), (this.turn instanceof Array && this.turn[0] ? this.turn[0].replace(/turn:/, '') : this.turn), this.username, this.password, this.site); }); } From fcbc88a002b509b9591a1148f5d1563895aa34ae Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 13:03:48 -0700 Subject: [PATCH 09/18] Update ssh-session.js --- lib/ssh-session.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ssh-session.js b/lib/ssh-session.js index c1978a4..ec00f8f 100644 --- a/lib/ssh-session.js +++ b/lib/ssh-session.js @@ -87,7 +87,7 @@ SSHSession.prototype.connect = function(connectTimeout, closeCallBack) { } firstCall .then((r) => { - debug("first call response: " + stringify(r)); + debug("first call response: " + JSON.stringify(r)); let o = { debug: this.debug }; if (this.waiter) o.waiter = this.waiter; if (this.webrtc) o.webrtc = this.webrtc; From ba777b93d7d6c0f96c926f7331816d4987e84549 Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 13:18:22 -0700 Subject: [PATCH 10/18] Update ssh-session.js --- lib/ssh-session.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/ssh-session.js b/lib/ssh-session.js index ec00f8f..b54a11e 100644 --- a/lib/ssh-session.js +++ b/lib/ssh-session.js @@ -160,12 +160,15 @@ SSHSession.prototype.connect = function(connectTimeout, closeCallBack) { }); }) .then((data) => { + debug("Remote description was set: " + JSON.stringify(data) + "; creating answer"); return this.wrtc.createAnswer(data); }) .then((data) => { + debug("Answer was created: " + JSON.stringify(data) + "; setting local description"); return this.wrtc.setLocalDescription(data); }) .then((sdpData) => { + debug("Local description was set: " + JSON.stringify(sdpData) + "; collecting ICE candidates"); return this.wrtc.collectIceCandidates(sdpData); }) .then((data) => { @@ -181,6 +184,7 @@ SSHSession.prototype.connect = function(connectTimeout, closeCallBack) { return x > y; }).shift(); let ip = line.match(/udp\s+\d+\s+(\S+)\s/)[1]; + debug("Executing ssh-sdp-answer with sdp=" + sdp); return this.unifi.sshSDPAnswer(this.mac, this.uuid, /*sdp.replace("c=IN IP4 0.0.0.0", "c=IN IP4 " + ip) */ sdp, this.site); }) // .then((data) => { @@ -188,6 +192,7 @@ SSHSession.prototype.connect = function(connectTimeout, closeCallBack) { // }) .then((data) => { debug('Channel is supposed to be open now. Lets wait'); + debug('Set timeout: ' + (connectTimeout || this.connectTimeout || 15000)); timeoutChannel = setTimeout(() => { debug('Timeout has passed without response, the channel is not open'); this.unifi.closeSSHSession(this.mac, this.uuid, this.site) From 3c0977f3d82f7ed063521d0d31702590709fb3c4 Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 13:45:33 -0700 Subject: [PATCH 11/18] Update webrtc-request.js --- lib/webrtc-request.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/webrtc-request.js b/lib/webrtc-request.js index ec2fed5..2f67180 100644 --- a/lib/webrtc-request.js +++ b/lib/webrtc-request.js @@ -17,6 +17,7 @@ class WRTC { this._q = {}; this._channel = {}; this.log = require('debug')(this.debugName || 'WRTCRequest'); + this.log.log = console.log.bind(console); if (this.debug) this.log.enabled = true; if (this.webrtc && this.webrtc.on) { this.webrtc.on('error', error => { From 6fe3e2d71fbae38ea74393e4e0f9250e629a4432 Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 14:25:47 -0700 Subject: [PATCH 12/18] Update webrtc-request.js --- lib/webrtc-request.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/webrtc-request.js b/lib/webrtc-request.js index 2f67180..45f219c 100644 --- a/lib/webrtc-request.js +++ b/lib/webrtc-request.js @@ -130,9 +130,13 @@ class WRTC { this.peer.setRemoteDescription( w, (data) => { // Create Answer + this.log('received answer from setting remote description: ' + JSON.stringify(data)); resolve(data /*|| desc*/ ); }, - reject + (err) => { + this.log('error while trying to set remote description: ' + err); + reject(err); + } ); }); }); From 2b841f8dee5aac6e8ab104114b5d30a0d6d9cdbb Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 14:41:34 -0700 Subject: [PATCH 13/18] Update webrtc-request.js --- lib/webrtc-request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/webrtc-request.js b/lib/webrtc-request.js index 45f219c..ec3a26d 100644 --- a/lib/webrtc-request.js +++ b/lib/webrtc-request.js @@ -200,7 +200,7 @@ class WRTC { //if (cand.candidate.match(/fd13/)) return; // Ignore IPv6 for the moment sdp = sdp.replace(/\r\n/g, '\n'); for (var b, c, d = sdp, e = 0, f = 0, g = /m=[^\n]*\n/g; null !== (b = g.exec(d));) { - if (f === cand.sdpMLineIndex) { + if (f >= cand.sdpMLineIndex) { return c = b.index, b = g.exec(d), e = null !== b ? b.index : -1, From 9aceaf725690863e9015efc93613a736e235d52c Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 14:57:20 -0700 Subject: [PATCH 14/18] Update webrtc-request.js --- lib/webrtc-request.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/webrtc-request.js b/lib/webrtc-request.js index ec3a26d..a76cf68 100644 --- a/lib/webrtc-request.js +++ b/lib/webrtc-request.js @@ -192,6 +192,11 @@ class WRTC { this.log('Candidate is empty, terminate the gathering'); data.sdp = sdp; return resolve(data); + } + if (candidate.candidate.type === 'relay') { + this.log('Candidate type is relay, terminate the gathering'); + data.sdp = sdp; + return resolve(data); } // TODO: implement reject if (candidate && candidate.candidate) { let cand = candidate.candidate; @@ -200,7 +205,7 @@ class WRTC { //if (cand.candidate.match(/fd13/)) return; // Ignore IPv6 for the moment sdp = sdp.replace(/\r\n/g, '\n'); for (var b, c, d = sdp, e = 0, f = 0, g = /m=[^\n]*\n/g; null !== (b = g.exec(d));) { - if (f >= cand.sdpMLineIndex) { + if (f === cand.sdpMLineIndex) { return c = b.index, b = g.exec(d), e = null !== b ? b.index : -1, From 14cadff2e5eec440d2803602a21930cd6b27a25e Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 15:07:45 -0700 Subject: [PATCH 15/18] Update ssh-session.js --- lib/ssh-session.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/ssh-session.js b/lib/ssh-session.js index b54a11e..7b802c8 100644 --- a/lib/ssh-session.js +++ b/lib/ssh-session.js @@ -187,9 +187,10 @@ SSHSession.prototype.connect = function(connectTimeout, closeCallBack) { debug("Executing ssh-sdp-answer with sdp=" + sdp); return this.unifi.sshSDPAnswer(this.mac, this.uuid, /*sdp.replace("c=IN IP4 0.0.0.0", "c=IN IP4 " + ip) */ sdp, this.site); }) - // .then((data) => { - // return this.wrtc.openDataChannel('ssh'); - // }) + .then((data) => { + debug('ssh-sdp-answer is completed with response: ' + JSON.stringify(data) + '; openning data channel'); + return this.wrtc.openDataChannel('ssh'); + }) .then((data) => { debug('Channel is supposed to be open now. Lets wait'); debug('Set timeout: ' + (connectTimeout || this.connectTimeout || 15000)); From e1399fe3d374a8557e6054d76b21867b0ce2fbfa Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 15:18:57 -0700 Subject: [PATCH 16/18] Update ssh-session.js --- lib/ssh-session.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ssh-session.js b/lib/ssh-session.js index 7b802c8..2c896c5 100644 --- a/lib/ssh-session.js +++ b/lib/ssh-session.js @@ -192,7 +192,7 @@ SSHSession.prototype.connect = function(connectTimeout, closeCallBack) { return this.wrtc.openDataChannel('ssh'); }) .then((data) => { - debug('Channel is supposed to be open now. Lets wait'); + debug('Channel is supposed to be open now. Lets wait: ' + JSON.stringify(data)); debug('Set timeout: ' + (connectTimeout || this.connectTimeout || 15000)); timeoutChannel = setTimeout(() => { debug('Timeout has passed without response, the channel is not open'); From 602d5d5c7d7d585a4fca8a70d3f5677b9929e17a Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 16:09:32 -0700 Subject: [PATCH 17/18] Update ssh-session.js --- lib/ssh-session.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/ssh-session.js b/lib/ssh-session.js index 2c896c5..b71365d 100644 --- a/lib/ssh-session.js +++ b/lib/ssh-session.js @@ -149,6 +149,17 @@ SSHSession.prototype.connect = function(connectTimeout, closeCallBack) { this.fireQ('onmessage', event); }; }); + this.wrtc.setCallback('onnegotiationneeded', (event) => { + debug("Handling negotiation needed event."); + this.wrtc.createOffer() + .then((offer) => { + debug("Negotiation offer created: " + JSON.stringify(offer)); + return this.wrtc.setLocalDescription(offer); + }) + .catch((err) => { + debug("Error while negotiating: " + err); + }); + }); return this.unifi.getSDPOffer(this.mac, this.uuid, this.site); }) .then((data) => { From f25711c08517e319d5b3889bf5247df489d4f6dc Mon Sep 17 00:00:00 2001 From: mzinger Date: Wed, 19 Jun 2019 16:22:27 -0700 Subject: [PATCH 18/18] Update webrtc-request.js --- lib/webrtc-request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/webrtc-request.js b/lib/webrtc-request.js index a76cf68..2c53e2e 100644 --- a/lib/webrtc-request.js +++ b/lib/webrtc-request.js @@ -131,7 +131,7 @@ class WRTC { w, (data) => { // Create Answer this.log('received answer from setting remote description: ' + JSON.stringify(data)); - resolve(data /*|| desc*/ ); + resolve(data || desc); // data is always null }, (err) => { this.log('error while trying to set remote description: ' + err);