diff --git a/bin/telebitd.js b/bin/telebitd.js index 09d4202..c545246 100755 --- a/bin/telebitd.js +++ b/bin/telebitd.js @@ -835,6 +835,24 @@ function startTelebitRemote(rawCb) { state.insecure = state.config.relay_ignore_invalid_certificates; // { relay, config, servernames, ports, sortingHat, net, insecure, token, handlers, greenlockConfig } + function onError(err) { + // Likely causes: + // * DNS lookup failed (no Internet) + // * Rejected (bad authn) + if ('ENOTFOUND' === err.code) { + // DNS issue, probably network is disconnected + setTimeout(function () { + startTelebitRemote(rawCb); + }, 90 * 1000); + return; + } + if ('function' === typeof rawCb) { + rawCb(err); + } else { + console.error('Unhandled TelebitRemote Error:'); + console.error(err); + } + } console.log("[DEBUG] token", typeof token, token); myRemote = TelebitRemote.createConnection({ relay: state.relay @@ -850,19 +868,10 @@ function startTelebitRemote(rawCb) { , handlers: state.handlers , greenlockConfig: state.greenlockConfig }, function () { + myRemote.removeListener('error', onError); rawCb(null, myRemote); }); - myRemote.once('error', function (err) { - // Likely causes: - // * DNS lookup failed (no Internet) - // * Rejected (bad authn) - if ('function' === typeof rawCb) { - rawCb(err); - } else { - console.error('Unhandled TelebitRemote Error:'); - console.error(err); - } - }); + myRemote.once('error', onError); }); } diff --git a/lib/remote.js b/lib/remote.js index 0e89ae6..bb42cbc 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -477,12 +477,14 @@ function TelebitRemote(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(); } + */ initialConnect = false; } @@ -500,6 +502,9 @@ function TelebitRemote(state) { connCallback(error); } + me.emit('close'); + /* + return; if (!authenticated) { if(state.handlers.onError) { var err = new Error('Failed to connect on first attempt... check authentication'); @@ -522,9 +527,13 @@ function TelebitRemote(state) { state.handlers.onClose(); } } + */ } , onError: function (err) { + me.emit('error', err); + /* + return; if ('ENOTFOUND' === err.code) { // DNS issue, probably network is disconnected timeoutId = setTimeout(connect, 90 * 1000); @@ -535,6 +544,7 @@ function TelebitRemote(state) { if (connCallback) { connCallback(err); } + */ } , sendMessage: function (msg) { diff --git a/package.json b/package.json index aa85237..a13b29a 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,7 @@ "serve-tpl-attachment": "^1.0.4", "sni": "^1.0.0", "socket-pair": "^1.0.3", - "ws": "^2.3.1" + "ws": "^6.0.0" }, "trulyOptionalDependencies": { "bluebird": "^3.5.1"