utils/tunnel-go/main.go

92 lines
2.4 KiB
Go
Raw Permalink Normal View History

2024-08-16 11:02:24 -05:00
package main
import (
"fmt"
"log"
"os"
"os/exec"
2024-08-16 14:07:25 -05:00
"path/filepath"
"strings"
2024-08-16 11:02:24 -05:00
"github.com/spf13/cobra"
)
func logFail(cond bool, s string, args ...interface{}) {
if !cond {
log.Fatalf(s, args...)
}
}
func ok(cmd *exec.Cmd) bool { err := cmd.Run(); return err == nil }
func portString() string { return fmt.Sprintf("%[1]d:localhost:%[1]d", port) }
func checkControl() bool { return ok(exec.Command("ssh", "-O", "check", host)) }
func activateSshControl() bool { return ok(exec.Command("ssh", "-M", "-f", "-N", host)) }
func connectPort() bool { return ok(exec.Command("ssh", "-fNL", portString(), host)) }
func deactivateSshControl() bool { return ok(exec.Command("ssh", "-O", "exit", host)) }
func tunnelUp() {
if !checkControl() {
logFail(activateSshControl(), "failed to activate ssh for host: %s", host)
}
logFail(connectPort(), "failed to connect to host: %s with port: %d", host, port)
}
2024-08-16 14:07:25 -05:00
func tunnelDown() {
logFail(deactivateSshControl(), "failed to disable ssh control %s", host)
}
2024-08-16 14:48:49 -05:00
func getControlSockets() (ret []string) {
files, err := os.ReadDir(sshDir)
if err != nil {
panic(err)
}
2024-08-16 14:07:25 -05:00
for _, f := range files {
if strings.HasPrefix(f.Name(), "control") {
ret = append(ret, f.Name())
}
}
return
}
2024-08-16 11:02:24 -05:00
func tunnelShow() {
2024-08-16 14:48:49 -05:00
controls := getControlSockets()
2024-08-16 14:07:25 -05:00
fmt.Printf("%d active connections\n", len(controls))
if len(controls) > 0 {
fmt.Println("hosts:")
for _, c := range controls {
fmt.Printf(" %s\n", strings.Split(c, "-")[1])
}
}
2024-08-16 11:02:24 -05:00
}
2024-08-16 14:48:49 -05:00
func genSubCmd(use string, short string, run func()) *cobra.Command {
return &cobra.Command{
Use: use, Short: short, Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { host = args[0]; run() },
}
}
2024-08-16 11:02:24 -05:00
var (
2024-08-16 14:07:25 -05:00
sshDir = filepath.Join(os.Getenv("HOME"), ".ssh")
2024-08-16 11:02:24 -05:00
rootCmd = &cobra.Command{Use: "tunnel", Short: "control ssh tunnels"}
upCmd = genSubCmd("up hostname [flags]", "activate ssh tunnel", tunnelUp)
downCmd = genSubCmd("down hostname [flags]", "deactivate ssh tunnel", tunnelDown)
showCmd = &cobra.Command{Use: "show", Short: "show activate tunnels", Run: func(cmd *cobra.Command, args []string) { tunnelShow() }}
2024-08-16 14:07:25 -05:00
port uint64
host string
2024-08-16 11:02:24 -05:00
)
func init() {
rootCmd.CompletionOptions.HiddenDefaultCmd = true
2024-08-16 14:07:25 -05:00
rootCmd.AddCommand(upCmd, downCmd, showCmd)
2024-08-16 11:02:24 -05:00
upCmd.Flags().Uint64VarP(&port, "port", "p", 0, "port number")
upCmd.MarkFlagRequired("port")
}
func main() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}