From 00c973e728b1939484f886fa30b3399420815aa1 Mon Sep 17 00:00:00 2001 From: John Shaver Date: Wed, 13 Jun 2018 14:00:08 -0700 Subject: [PATCH 1/3] Moved default sorting-hat value into remote.js --- bin/telebitd.js | 2 +- lib/remote.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/bin/telebitd.js b/bin/telebitd.js index d2ba805..9af49be 100755 --- a/bin/telebitd.js +++ b/bin/telebitd.js @@ -517,7 +517,7 @@ function connectTunnel() { state.otp = getOtp(); state.greenlockConf = state.config.greenlock || {}; - state.sortingHat = state.config.sortingHat || path.resolve(__dirname, '..', 'lib/sorting-hat.js'); + state.sortingHat = state.config.sortingHat; // TODO sortingHat.print(); ? diff --git a/lib/remote.js b/lib/remote.js index 3af3709..c2f8c07 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -23,6 +23,9 @@ function _connect(state) { // they can get a promise that will provide feedback about invalid tokens. var tokens = []; var auth; + if(!state.sortingHat) { + state.sortingHat = "./sorting-hat.js"; + } if (state.token) { tokens.push(state.token); } From 7d1c55314925b0c71776f3fce4c57ba350e680ac Mon Sep 17 00:00:00 2001 From: John Shaver Date: Thu, 14 Jun 2018 14:57:09 -0700 Subject: [PATCH 2/3] Added open/close/error/disconenct/reconenct handlers to remote. --- lib/remote.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/remote.js b/lib/remote.js index c2f8c07..bdc35c3 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -33,6 +33,7 @@ function _connect(state) { var wstunneler; var authenticated = false; var authsent = false; + var initialConnect = true; var localclients = {}; var pausedClients = []; @@ -404,6 +405,7 @@ function _connect(state) { , onOpen: function () { console.info("[open] connected to '" + (state.wss || state.relay) + "'"); wsHandlers.refreshTimeout(); + timeoutId = setTimeout(wsHandlers.checkTimeout, activityTimeout); wstunneler._socket.on('drain', function () { @@ -425,6 +427,14 @@ function _connect(state) { pausedClients.length = 0; }); + + //Call either Open or Reconnect handlers. + if(state.handlers.onOpen && initialConnect) { + state.handlers.onOpen(); + } else if (state.handlers.onReconnect && !initialConnect) { + state.handlers.onReconnect(); + } + intitialConnect = false; } , onClose: function () { @@ -443,22 +453,38 @@ function _connect(state) { } if (!authenticated) { + if(state.handlers.onError) { + let err = new Error('Failed to connect on first attempt... check authentication'); + state.handlers.onError(err); + } + if(state.handlers.onClose) { + state.handlers.onClose() + } console.info('[close] failed on first attempt... check authentication.'); timeoutId = null; } else if (tokens.length) { + if(state.handlers.onDisconnect) { + state.handlers.onDisconnect(); + } console.info('[retry] disconnected and waiting...'); timeoutId = setTimeout(connect, 5000); + } else { + if(state.handlers.onClose) { + state.handlers.onClose() + } } + } , onError: function (err) { - console.error("[tunnel error] " + err.message); - console.error(err); - if (connCallback) { - connCallback(err); - } + console.error("[tunnel error] " + err.message); + console.error(err); + + if (connCallback) { + connCallback(err); } + } , sendMessage: function (msg) { if (wstunneler) { From 9dccf5b0220244952ac5697f9335f7fb5f2896d7 Mon Sep 17 00:00:00 2001 From: John Shaver Date: Mon, 18 Jun 2018 11:01:12 -0700 Subject: [PATCH 3/3] Added support for onOpen/onClose/onDisconnect/onReconnect handlers --- lib/remote.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/remote.js b/lib/remote.js index bdc35c3..4a75363 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -434,11 +434,10 @@ function _connect(state) { } else if (state.handlers.onReconnect && !initialConnect) { state.handlers.onReconnect(); } - intitialConnect = false; + initialConnect = false; } , onClose: function () { - console.log('ON CLOSE'); clearTimeout(timeoutId); wstunneler = null; clientHandlers.closeAll(); @@ -474,17 +473,15 @@ function _connect(state) { state.handlers.onClose() } } - } , onError: function (err) { - console.error("[tunnel error] " + err.message); - console.error(err); - - if (connCallback) { - connCallback(err); + console.error("[tunnel error] " + err.message); + console.error(err); + if (connCallback) { + connCallback(err); + } } - } , sendMessage: function (msg) { if (wstunneler) {