utils/tunnel/tunnel.nim

79 lines
2.2 KiB
Nim
Raw Normal View History

2024-08-16 14:07:25 -05:00
import std/[os, osproc, sequtils, strformat, strutils, sugar]
2024-10-08 12:15:53 -05:00
import hwylterm
2024-08-16 14:07:25 -05:00
proc checkHost(host: seq[string]): string =
case host.len:
of 1: return host[0]
2024-10-08 12:15:53 -05:00
of 0: quit $bb"[red]expected hostname"
else: quit $bb"[red]expected one positional argument"
2024-08-16 14:07:25 -05:00
2024-08-16 15:39:21 -05:00
proc check(name: string): bool = (execCmd &"ssh -O check {name}") == 0
proc startSsh(name: string) = discard execCmd &"ssh -M -f -N {name}"
proc exitSsh(name: string):int = execCmd &"ssh -O exit {name}"
2024-08-16 14:07:25 -05:00
2024-11-06 18:36:42 -06:00
proc activateTunnel(name: string, port: int) =
2024-08-27 13:18:05 -05:00
echo "activating connection to: ", name
2024-08-16 14:07:25 -05:00
if not check(name):
startSsh(name)
2024-11-06 18:36:42 -06:00
quit execCmd &"""ssh -fNL "{port}:localhost:{port}" {name}"""
2024-08-16 14:07:25 -05:00
2024-11-06 18:36:42 -06:00
proc deactivateTunnels(name: string) =
2024-08-27 13:18:05 -05:00
echo "deactivating connection to: ", name
2024-08-16 14:07:25 -05:00
quit exitSsh(name)
2024-10-12 10:58:46 -05:00
proc collectControllers(): seq[string] =
2024-08-16 14:07:25 -05:00
let sshDir = (getEnv "HOME") / ".ssh"
2024-10-12 10:58:46 -05:00
collect(for _,p in walkDir(sshDir): p)
2024-08-16 14:07:25 -05:00
.map(extractFilename)
.filterIt(it.startsWith("control"))
2024-10-12 10:58:46 -05:00
2024-11-06 18:36:42 -06:00
proc killTunnels() =
2024-10-12 10:58:46 -05:00
let controllers = collectControllers()
if controllers.len == 0: quit 0
for c in controllers:
2024-10-31 11:18:23 -05:00
let host = c.lastPathPart().split("-")[2]
let code = exitSsh(host)
if code != 0:
quit $bb"[red][[Error]failed to exit connection for host: " & host
2024-10-12 10:58:46 -05:00
2024-11-06 18:36:42 -06:00
proc showTunnels() =
2024-10-12 10:58:46 -05:00
let controllers = collectControllers()
2024-10-11 12:10:07 -05:00
echo bbfmt"[yellow]{controllers.len}[/] active connections"
2024-08-16 14:07:25 -05:00
if controllers.len == 0: quit 0
echo "hosts:"
echo controllers.mapIt(" " & it.split('-')[1]).join("\n")
2024-08-16 15:39:21 -05:00
2024-10-08 12:15:53 -05:00
when isMainModule:
2024-11-06 18:36:42 -06:00
import hwylterm/[hwylcli]
hwylCli:
name "tunnel"
settings NoArgsShowHelp
subcommands:
--- up
? "[b]tunnel up[/] [green]host[/] [[[faint]-h|-p[/]]"
... "activate a tunnel"
required port
flags:
port:
- p
T int
? "port number"
* 8555
run:
let host = checkHost(args)
activateTunnel(host, port)
--- down
? "[b]tunnel down[/] [green]host[/] [[[faint]-h[/]]"
... "disable all tunnels for a host"
run:
let host = checkHost(args)
deactivateTunnels(host)
--- show
... "show active connections"
run: showTunnels()
--- kill
... "kill all open tunnels"
run: killTunnels()
2024-10-12 10:58:46 -05:00