Tweak to the support script

This commit is contained in:
David Carley
2022-09-13 00:08:38 +00:00
parent ca70091347
commit fcc4ed8b03

View File

@@ -7,42 +7,48 @@ const fetch = require("node-fetch");
let ssm; let ssm;
const Commands = [
"code",
"ssh",
"web",
"disconnect"
];
initSignalHandlers(); initSignalHandlers();
main(); main();
async function main() { async function main() {
await getAWSCredentials(); await getAWSCredentials();
// eslint-disable-next-line no-constant-condition
while (true) {
const { command } = await inquirer.prompt({ const { command } = await inquirer.prompt({
type: "list", type: "list",
name: "command", name: "command",
choices: Commands choices: [
"code",
"info",
"disconnect"
]
}); });
switch (command) { switch (command) {
case "code": case "code":
return await commandCode(); await commandCode();
break;
case "ssh": case "info":
return await commandSsh(); await commandInfo();
break;
case "web":
return await commandWeb();
case "disconnect": case "disconnect":
return await commandDisconnect(); await commandDisconnect();
break;
}
} }
} }
async function commandCode() { async function commandCode() {
await closeTunnels(); const tunnels = await loadTunnels();
if (tunnels?.length) {
console.log("There are active tunnels. Disconnect first.");
return;
}
await updateNgrokAuthToken(); await updateNgrokAuthToken();
const code = `000000${Math.random() * 999999}`.slice(-6); const code = `000000${Math.random() * 999999}`.slice(-6);
@@ -51,7 +57,7 @@ async function commandCode() {
console.log(`The code is: ${code}`); console.log(`The code is: ${code}`);
} }
async function commandSsh() { async function commandInfo() {
const tunnels = await loadTunnels(); const tunnels = await loadTunnels();
if (!tunnels.length) { if (!tunnels.length) {
@@ -59,28 +65,22 @@ async function commandSsh() {
return; return;
} }
const webTunnel = tunnels.find(tunnel => tunnel.proto === "https");
const sshTunnel = tunnels.find(tunnel => tunnel.proto === "tcp"); const sshTunnel = tunnels.find(tunnel => tunnel.proto === "tcp");
const [ , host, port ] = sshTunnel.public_url.match(/tcp:\/\/([^:]+):(\d+)/); const [ , host, port ] = sshTunnel.public_url.match(/tcp:\/\/([^:]+):(\d+)/);
console.log("Run this:"); console.log("Connection info:");
console.log(); console.log(webTunnel.public_url);
console.log(`ssh bbmc@${host} -p ${port}`); console.log(`ssh bbmc@${host} -p ${port}`);
console.log(); console.log();
} }
async function commandWeb() {
const url = await getWebUrl();
console.log(`Web interface: ${url}`);
console.log();
}
async function commandDisconnect() { async function commandDisconnect() {
const tunnels = await loadTunnels(); const tunnels = await loadTunnels();
if (!tunnels.length) { if (!tunnels.length) {
console.log("There are no tunnels!"); console.log("There are no tunnels!");
process.exit(1); return;
} }
const webTunnel = tunnels.find(tunnel => tunnel.proto === "https"); const webTunnel = tunnels.find(tunnel => tunnel.proto === "https");
@@ -212,32 +212,3 @@ async function loadTunnels() {
return tunnels; return tunnels;
} }
async function getWebUrl() {
const tunnels = await loadTunnels();
if (!tunnels.length) {
console.log("There are no tunnels!");
process.exit(1);
}
const webTunnel = tunnels.find(tunnel => tunnel.proto === "https");
const url = new URL(webTunnel.public_url);
url.username = "onefinity";
url.password = "onefinity";
url.protocol = "http";
return url.toString();
}
async function closeTunnels() {
const tunnels = await loadTunnels();
if (!tunnels?.length) {
return;
}
console.error("There are tunnels open:", JSON.stringify(tunnels, null, 4));
console.error("Giving up");
process.exit(1);
}