From 3ffad3718040f0ed60c518d69d24ae2e5a1fee61 Mon Sep 17 00:00:00 2001 From: greysoh Date: Tue, 7 May 2024 20:58:35 -0400 Subject: [PATCH] feature: Implements tunnel starting and stopping. --- lom/src/commands/connections.ts | 61 ++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/lom/src/commands/connections.ts b/lom/src/commands/connections.ts index 2113196..455aad4 100644 --- a/lom/src/commands/connections.ts +++ b/lom/src/commands/connections.ts @@ -14,6 +14,8 @@ export async function run( axios: Axios, token: string, ) { + if (argv.length == 1) return println("error: no arguments specified! run %s --help to see commands.\n", argv[0]); + let resolve: (value: unknown) => void; let reject: (value: unknown) => void; @@ -43,7 +45,6 @@ export async function run( ); addCommand.argument("", "Destination port to use"); - addCommand.option("--description, -d", "Description for the tunnel"); const lookupCommand = new SSHCommand(println, "find"); @@ -80,10 +81,68 @@ export async function run( startTunnel.description("Starts a tunnel"); startTunnel.argument("", "Tunnel ID to start"); + startTunnel.action(async(idStr: string) => { + const id = parseInt(idStr); + + if (Number.isNaN(id)) { + println("ID (%s) is not a number\n", idStr); + return resolve(null); + }; + + const response = await axios.post("/api/v1/forward/start", { + token, + id + }); + + if (response.status != 200) { + if (process.env.NODE_ENV != "production") console.log(response); + + if (response.data.error) { + println(`Error: ${response.data.error}\n`); + } else { + println("Error requesting connections!\n"); + } + + return resolve(null); + } + + println("Successfully started tunnel.\n"); + return resolve(null); + }); + const stopTunnel = new SSHCommand(println, "stop"); stopTunnel.description("Stops a tunnel"); stopTunnel.argument("", "Tunnel ID to stop"); + stopTunnel.action(async(idStr: string) => { + const id = parseInt(idStr); + + if (Number.isNaN(id)) { + println("ID (%s) is not a number\n", idStr); + return resolve(null); + }; + + const response = await axios.post("/api/v1/forward/stop", { + token, + id + }); + + if (response.status != 200) { + if (process.env.NODE_ENV != "production") console.log(response); + + if (response.data.error) { + println(`Error: ${response.data.error}\n`); + } else { + println("Error requesting connections!\n"); + } + + return resolve(null); + } + + println("Successfully stopped tunnel.\n"); + return resolve(null); + }); + const getInbound = new SSHCommand(println, "get-inbound"); getInbound.description("Shows all current connections"); getInbound.argument("", "Tunnel ID to view inbound connections of");