From 42d558b85eea76723b585523e721e91863e3e2c8 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sun, 16 Sep 2018 00:16:43 -0600 Subject: [PATCH] v0.20.1: cleanup restart --- bin/telebitd.js | 51 +++++++++++++++++++++++++++++-------------------- lib/remote.js | 4 ++-- package.json | 2 +- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/bin/telebitd.js b/bin/telebitd.js index e3f0484..0b5d0bb 100755 --- a/bin/telebitd.js +++ b/bin/telebitd.js @@ -480,25 +480,31 @@ function serveControlsHelper() { } function restart() { - // failsafe - setTimeout(function () { + console.info("[telebitd.js] server closing..."); + state.keepAlive.state = false; + if (myRemote) { + myRemote.end(); + myRemote.on('end', respondAndClose); + // failsafe + setTimeout(function () { + console.info("[telebitd.js] closing too slowly, force quit"); + respondAndClose(); + }, 5 * 1000); + } else { + respondAndClose(); + } + + function respondAndClose() { res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify({ success: true })); - setTimeout(function () { - process.exit(33); - }, 500); - }, 5 * 1000); - - if (myRemote) { myRemote.end(); } - controlServer.close(function () { - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({ success: true })); - - setTimeout(function () { - // system daemon will restart the process - process.exit(22); // use non-success exit code - }, 500); - }); + controlServer.close(function () { + console.info("[telebitd.js] server closed"); + setTimeout(function () { + // system daemon will restart the process + process.exit(22); // use non-success exit code + }, 100); + }); + } } function invalidConfig() { @@ -689,8 +695,10 @@ function serveControls() { } console.info("[info] connecting with stored token"); - return safeStartTelebitRemote().catch(function (/*err*/) { + return safeStartTelebitRemote().catch(function (err) { // ignore, it'll keep looping anyway + console.warn("[debug] error that (supposedly) shouldn't matter:"); + console.warn(err); }); } @@ -806,18 +814,19 @@ function safeStartTelebitRemote(forceOn) { // this won't restart either trPromise = rawStartTelebitRemote(state.keepAlive); trPromise.then(function () { - //console.log("I'm RIGHT HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + console.log("[debug] success on raw start, keepAlive = true"); state.keepAlive.state = true; trPromise = null; }).catch(function () { - //console.log("I FAILED US ALL!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + console.log("[debug] failure on raw start, { keepAlive = true }"); // this will restart state.keepAlive = { state: true }; trPromise = rawStartTelebitRemote(state.keepAlive); trPromise.then(function () { + console.log("[debug] success on 2nd start keepAlive:", state.keepAlive.state); trPromise = null; }).catch(function () { - //console.log('DEBUG state.keepAlive turned off and remote quit'); + console.log("[debug] failure on 2nd start. keepAlive", state.keepAlive.state); trPromise = null; }); }); diff --git a/lib/remote.js b/lib/remote.js index 989a6e8..0f59c7b 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -472,12 +472,12 @@ function TelebitRemote(state) { priv.timeoutId = null; var machine = Packer.create(packerHandlers); - console.info("[connect] '" + (state.wss || state.relay) + "'"); + console.info("[telebit:lib/remote.js] [connect] '" + (state.wss || state.relay) + "'"); var tunnelUrl = (state.wss || state.relay).replace(/\/$/, '') + '/'; // + auth; wstunneler = new WebSocket(tunnelUrl, { rejectUnauthorized: !state.insecure }); // XXXXXX wstunneler.on('open', function () { - console.info("[open] connected to '" + (state.wss || state.relay) + "'"); + console.info("[telebit:lib/remote.js] [open] connected to '" + (state.wss || state.relay) + "'"); me.emit('connect'); priv.refreshTimeout(); priv.timeoutId = setTimeout(priv.checkTimeout, activityTimeout); diff --git a/package.json b/package.json index 1735da0..9380595 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telebit", - "version": "0.20.0-wip", + "version": "0.20.1-wip", "description": "Break out of localhost. Connect to any device from anywhere over any tcp port or securely in a browser. A secure tunnel. A poor man's reverse VPN.", "main": "lib/remote.js", "files": [