hwylcli for the win

This commit is contained in:
Daylin Morgan 2024-11-06 18:36:42 -06:00
parent d86dff8360
commit 367d2e5523
Signed by: daylin
GPG key ID: 950D13E9719334AD
4 changed files with 38 additions and 36 deletions

View file

@ -8,5 +8,5 @@ buildNimblePackage {
src = lib.cleanSource ./.; src = lib.cleanSource ./.;
doCheck = false; doCheck = false;
nimbleDepsHash = "sha256-frbWVz7ow3wAbQCVfDk9KWN8Va2jdkW9hp7OKxzltis="; nimbleDepsHash = "sha256-68ausfWeGbiy/zcZYADarO7b2tusEnRSkFKYlcZ6boE=";
} }

View file

@ -1,24 +1,14 @@
{ {
"version": 2, "version": 2,
"packages": { "packages": {
"cligen": {
"version": "1.7.7",
"vcsRevision": "f50f887eacfe33866e22f8a0d322e29a1c830cf9",
"url": "https://github.com/c-blake/cligen.git",
"downloadMethod": "git",
"dependencies": [],
"checksums": {
"sha1": "237596b6be88dcf9ee0bd9732a7baaac526f0c07"
}
},
"hwylterm": { "hwylterm": {
"version": "0.1.0", "version": "0.1.0",
"vcsRevision": "56bb2bb7c1da402c2c52fb3f7f10620c7c2fca7e", "vcsRevision": "f1c3fdfe78dea7285cb759b4962d6d1848fce760",
"url": "https://github.com/daylinmorgan/hwylterm", "url": "https://github.com/daylinmorgan/hwylterm",
"downloadMethod": "git", "downloadMethod": "git",
"dependencies": [], "dependencies": [],
"checksums": { "checksums": {
"sha1": "8810cf69a74de580e1ab08f0c153afbf33c8a98e" "sha1": "885ef6dbddf0e4cb3a16e97943d2298fdd053569"
} }
} }
}, },

View file

@ -10,20 +10,15 @@ proc checkHost(host: seq[string]): string =
proc check(name: string): bool = (execCmd &"ssh -O check {name}") == 0 proc check(name: string): bool = (execCmd &"ssh -O check {name}") == 0
proc startSsh(name: string) = discard execCmd &"ssh -M -f -N {name}" proc startSsh(name: string) = discard execCmd &"ssh -M -f -N {name}"
proc activateTunnel(name: string, port: int): int = execCmd &"""ssh -fNL "{port}:localhost:{port}" {name}"""
proc exitSsh(name: string):int = execCmd &"ssh -O exit {name}" proc exitSsh(name: string):int = execCmd &"ssh -O exit {name}"
proc up(port: int, host: seq[string]) = proc activateTunnel(name: string, port: int) =
## activate a tunnel
let name = checkHost host
echo "activating connection to: ", name echo "activating connection to: ", name
if not check(name): if not check(name):
startSsh(name) startSsh(name)
quit activateTunnel(name, port) quit execCmd &"""ssh -fNL "{port}:localhost:{port}" {name}"""
proc down(host: seq[string]) = proc deactivateTunnels(name: string) =
## disable all tunnels for a host
let name = checkHost host
echo "deactivating connection to: ", name echo "deactivating connection to: ", name
quit exitSsh(name) quit exitSsh(name)
@ -33,8 +28,7 @@ proc collectControllers(): seq[string] =
.map(extractFilename) .map(extractFilename)
.filterIt(it.startsWith("control")) .filterIt(it.startsWith("control"))
proc killCmd() = proc killTunnels() =
## kill all open tunnels
let controllers = collectControllers() let controllers = collectControllers()
if controllers.len == 0: quit 0 if controllers.len == 0: quit 0
for c in controllers: for c in controllers:
@ -43,8 +37,7 @@ proc killCmd() =
if code != 0: if code != 0:
quit $bb"[red][[Error]failed to exit connection for host: " & host quit $bb"[red][[Error]failed to exit connection for host: " & host
proc show() = proc showTunnels() =
## show active connections
let controllers = collectControllers() let controllers = collectControllers()
echo bbfmt"[yellow]{controllers.len}[/] active connections" echo bbfmt"[yellow]{controllers.len}[/] active connections"
if controllers.len == 0: quit 0 if controllers.len == 0: quit 0
@ -52,14 +45,34 @@ proc show() =
echo controllers.mapIt(" " & it.split('-')[1]).join("\n") echo controllers.mapIt(" " & it.split('-')[1]).join("\n")
when isMainModule: when isMainModule:
import cligen, hwylterm/cligen import hwylterm/[hwylcli]
hwylCli(clCfg) hwylCli:
let hostUse = name "tunnel"
$bb("$command [green]hostname[/] [[[i]flags[/]]\n${doc}[b]Options[/]:\n$options") settings NoArgsShowHelp
dispatchMulti( subcommands:
[up , usage=hostUse ], --- up
[down , usage=hostUse ], ? "[b]tunnel up[/] [green]host[/] [[[faint]-h|-p[/]]"
[show , usage=clCfg.use ], ... "activate a tunnel"
[killCmd, usage=clCfg.use, cmdName = "kill"], 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()

View file

@ -10,6 +10,5 @@ bin = @["tunnel"]
# Dependencies # Dependencies
requires "nim >= 2.0.8" requires "nim >= 2.0.8"
requires "cligen >= 1.7.6"
requires "https://github.com/daylinmorgan/hwylterm#HEAD" requires "https://github.com/daylinmorgan/hwylterm#HEAD"