From 553c385e34b0cc4bd2f2c4efd4ef5a1e113a4c1b Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 7 Jun 2018 00:08:26 -0600 Subject: [PATCH] move intentional user messages and client data to CLI/UI --- bin/telebit.js | 32 ++++++++++++++++++++++++++++++++ lib/remote.js | 43 ++++++++++++++++--------------------------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/bin/telebit.js b/bin/telebit.js index 2d92b88..04757ae 100755 --- a/bin/telebit.js +++ b/bin/telebit.js @@ -97,6 +97,7 @@ function connectTunnel() { if (!state.config.sortingHat) { state.config.sortingHat = path.resolve(__dirname, '..', 'lib/sorting-hat.js'); } + // TODO sortingHat.print(); // TODO Check undefined vs false for greenlock config var tun = remote.connect({ @@ -107,6 +108,37 @@ function connectTunnel() { , net: state.net , insecure: state.config.relay_ignore_invalid_certificates , token: state.token + , handlers: { + grant: function (grants) { + console.info(""); + console.info("Connect to your device by any of the following means:"); + console.info(""); + grants.forEach(function (arr) { + if ('ssh+https' === arr[0]) { + console.info("SSH+HTTPS"); + } else if ('ssh' === arr[0]) { + console.info("SSH"); + } else if ('tcp' === arr[0]) { + console.info("TCP"); + } else if ('https' === arr[0]) { + console.info("HTTPS"); + } + console.log('\t' + arr[0] + '://' + arr[1] + (arr[2] ? (':' + arr[2]) : '')); + if ('ssh+https' === arr[0]) { + console.info("\tex: ssh -o ProxyCommand='openssl s_client -connect %h:%p -quiet' " + arr[1] + " -p 443\n"); + } else if ('ssh' === arr[0]) { + console.info("\tex: ssh " + arr[1] + " -p " + arr[2] + "\n"); + } else if ('tcp' === arr[0]) { + console.info("\tex: netcat " + arr[1] + " " + arr[2] + "\n"); + } else if ('https' === arr[0]) { + console.info("\tex: curl https://" + arr[1] + "\n"); + } + }); + } + , access_token: function (jwt) { + console.info("Received updated access_token:", jwt); + } + } , greenlockConfig: { version: state.greenlock.version || 'draft-11' , server: state.greenlock.server || 'https://acme-v02.api.letsencrypt.org/directory' diff --git a/lib/remote.js b/lib/remote.js index 653c44d..4eb5961 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -217,32 +217,9 @@ function _connect(state) { }); } - function displayGrants(grants) { - // TODO sortingHat.print(); - console.log(""); - console.log("Connect to your device by any of the following means:"); - console.log(""); - grants.forEach(function (arr) { - if ('ssh+https' === arr[0]) { - console.log("SSH+HTTPS"); - } else if ('ssh' === arr[0]) { - console.log("SSH"); - } else if ('tcp' === arr[0]) { - console.log("TCP"); - } else if ('https' === arr[0]) { - console.log("HTTPS"); - } - console.log('\t' + arr[0] + '://' + arr[1] + (arr[2] ? (':' + arr[2]) : '')); - if ('ssh+https' === arr[0]) { - console.log("\tex: ssh -o ProxyCommand='openssl s_client -connect %h:%p -quiet' " + arr[1] + " -p 443\n"); - } else if ('ssh' === arr[0]) { - console.log("\tex: ssh " + arr[1] + " -p " + arr[2] + "\n"); - } else if ('tcp' === arr[0]) { - console.log("\tex: netcat " + arr[1] + " " + arr[2] + "\n"); - } else if ('https' === arr[0]) { - console.log("\tex: curl https://" + arr[1] + "\n"); - } - }); + function noHandler(cmd) { + console.warn("[telebit] state.handlers['" + cmd[1] + "'] not set"); + console.warn(cmd[2]); } var connCallback; @@ -287,7 +264,19 @@ function _connect(state) { err = null; } else if (cmd[1] === 'grant') { authenticated = true; - displayGrants(cmd[2]); + if (state.handlers[cmd[1]]) { + state.handlers[cmd[1]](cmd[2]); + } else { + noHandler(cmd); + } + return; + } else if (cmd[1] === 'access_token') { + authenticated = true; + if (state.handlers[cmd[1]]) { + state.handlers[cmd[1]](cmd[2]); + } else { + noHandler(cmd); + } return; } else { err = { message: 'unknown command "'+cmd[1]+'"', code: 'E_UNKNOWN_COMMAND' };