check standard apis

This commit is contained in:
AJ ONeal 2018-06-15 22:45:42 +00:00
parent 130219a758
commit b4a2108942
1 changed files with 61 additions and 16 deletions

View File

@ -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 + "'?");