From b4a210894243f8d46f601c4c56e9c23c7a37fd10 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 15 Jun 2018 22:45:42 +0000 Subject: [PATCH] check standard apis --- bin/telebit.js | 77 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 16 deletions(-) diff --git a/bin/telebit.js b/bin/telebit.js index 219baaa..a4605a8 100755 --- a/bin/telebit.js +++ b/bin/telebit.js @@ -8,6 +8,7 @@ var os = require('os'); //var url = require('url'); var path = require('path'); var http = require('http'); +var https = require('https'); var YAML = require('js-yaml'); var recase = require('recase').create({}); var camelCopy = recase.camelCopy.bind(recase); @@ -128,6 +129,65 @@ function askForConfig(answers, mainCb) { setTimeout(cb, 250); }); } + , function askRelay(cb) { + if (answers.relay) { cb(); return; } + console.info(""); + console.info(""); + console.info("What relay will you be using? (press enter for default)"); + console.info(""); + function parseUrl(hostname) { + var url = require('url'); + var location = url.parse(hostname); + if (!location.protocol || /\./.test(location.protocol)) { + hostname = 'https://' + hostname; + location = url.parse(hostname); + } + hostname = location.hostname + (location.port ? ':' + location.port : ''); + hostname = location.protocol.replace(/https?/, 'https') + '//' + hostname + location.pathname; + return hostname; + } + rl.question('relay [default: telebit.cloud]: ', function (relay) { + // TODO parse and check https://{{relay}}/.well-known/telebit.cloud/directives.json + if (!relay) { relay = 'telebit.cloud'; } + answers.relay = relay.trim(); + var urlstr = parseUrl(answers.relay) + '_apis/telebit.cloud/index.json'; + https.get(urlstr, function (resp) { + var body = ''; + if (200 !== resp.statusCode) { + console.error("[" + resp.statusCode + " Error] Failed to retrieve '" + urlstr + "'"); + askRelay(cb); + return; + } + resp.on('data', function (chunk) { + body += chunk.toString('utf8'); + }); + resp.on('end', function () { + try { + body = JSON.parse(body); + } catch(e) { + console.error("[Parse Error] Failed to retrieve '" + urlstr + "'"); + console.error(e); + askRelay(cb); + return; + } + if (!(body.api_host)) { + console.error("[API Error] API Index '" + urlstr + "' does not describe a known version of telebit.cloud"); + console.error(e); + askRelay(cb); + return; + } + if (body.pair_request) { + answers._can_pair = true; + } + cb(); + }); + }).on('error', function (e) { + console.error("[Network Error] Failed to retrieve '" + urlstr + "'"); + console.error(e); + askRelay(cb); + }); + }); + } , function askAgree(cb) { if (answers.agree_tos) { cb(); return; } console.info(""); @@ -150,21 +210,6 @@ function askForConfig(answers, mainCb) { setTimeout(cb, 250); }); } - , function askRelay(cb) { - if (answers.relay) { cb(); return; } - console.info(""); - console.info(""); - console.info("What relay will you be using? (press enter for default)"); - console.info(""); - rl.question('relay [default: telebit.cloud]: ', function (relay) { - // TODO parse and check https://{{relay}}/.well-known/telebit.cloud/directives.json - if (!relay) { - relay = 'telebit.cloud'; - } - answers.relay = relay.trim(); - setTimeout(cb, 250); - }); - } , function checkRelay(cb) { if (!/\btelebit\.cloud\b/i.test(answers.relay)) { standardSet = standardSet.concat(advancedSet); @@ -240,7 +285,7 @@ function askForConfig(answers, mainCb) { ]; var advancedSet = [ function askTokenOrSecret(cb) { - if (answers.token || answers.secret) { cb(); return; } + if (answers._can_pair || answers.token || answers.secret) { cb(); return; } console.info(""); console.info(""); console.info("What's your authorization for '" + answers.relay + "'?");