promote tunnel-nim to sole implementation

This commit is contained in:
Daylin Morgan 2024-10-30 16:20:09 -05:00
parent 7a775d5ab9
commit da246de536
Signed by: daylin
GPG key ID: 950D13E9719334AD
12 changed files with 1 additions and 170 deletions

View file

@ -32,15 +32,12 @@
{ {
packages = forAllSystems (pkgs: rec { packages = forAllSystems (pkgs: rec {
utils = pkgs.callPackage ./meta.nix { inherit tunnel; }; utils = pkgs.callPackage ./meta.nix { inherit tunnel; };
tunnel-go = pkgs.callPackage ./tunnel-go { }; tunnel = pkgs.callPackage ./tunnel-nim { };
tunnel-nim = pkgs.callPackage ./tunnel-nim { };
tunnel = tunnel-nim;
default = utils; default = utils;
}); });
devShells = forAllSystems (pkgs: { devShells = forAllSystems (pkgs: {
default = pkgs.mkShell { default = pkgs.mkShell {
packages = with pkgs; [ packages = with pkgs; [
go
nim nim
]; ];
}; };

View file

@ -1,27 +0,0 @@
{
lib,
installShellFiles,
buildGoModule,
makeWrapper,
...
}:
let
inherit (lib) cleanSource;
in
buildGoModule rec {
pname = "tunnel";
version = "unstable";
src = cleanSource ./.;
vendorHash = "sha256-PwZJMEVaPHqZs7bM+9XLxVA36GfV3EN6bja86hkfO90=";
nativeBuildInputs = [
installShellFiles
makeWrapper
];
postInstall = ''
installShellCompletion --cmd ${pname} \
--zsh <($out/bin/${pname} completion zsh)
'';
}

View file

@ -1,16 +0,0 @@
module git.dayl.in/daylin/utils/tunnel
go 1.22.5
require (
github.com/bitfield/script v0.22.1
github.com/spf13/cobra v1.8.1
)
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/itchyny/gojq v0.12.13 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
mvdan.cc/sh/v3 v3.7.0 // indirect
)

View file

@ -1,32 +0,0 @@
github.com/bitfield/script v0.22.1 h1:DphxoC5ssYciwd0ZS+N0Xae46geAD/0mVWh6a2NUxM4=
github.com/bitfield/script v0.22.1/go.mod h1:fv+6x4OzVsRs6qAlc7wiGq8fq1b5orhtQdtW0dwjUHI=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx3GhA=
github.com/frankban/quicktest v1.14.5/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/itchyny/gojq v0.12.13 h1:IxyYlHYIlspQHHTE0f3cJF0NKDMfajxViuhBLnHd/QU=
github.com/itchyny/gojq v0.12.13/go.mod h1:JzwzAqenfhrPUuwbmEz3nu3JQmFLlQTQMUcOdnu/Sf4=
github.com/itchyny/timefmt-go v0.1.5 h1:G0INE2la8S6ru/ZI5JecgyzbbJNs5lG1RcBqa7Jm6GE=
github.com/itchyny/timefmt-go v0.1.5/go.mod h1:nEP7L+2YmAbT2kZ2HfSs1d8Xtw9LY8D2stDBckWakZ8=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8=
golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
mvdan.cc/sh/v3 v3.7.0 h1:lSTjdP/1xsddtaKfGg7Myu7DnlHItd3/M2tomOcNNBg=
mvdan.cc/sh/v3 v3.7.0/go.mod h1:K2gwkaesF/D7av7Kxl0HbF5kGOd2ArupNTX3X44+8l8=

View file

@ -1,91 +0,0 @@
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"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)
}
func tunnelDown() {
logFail(deactivateSshControl(), "failed to disable ssh control %s", host)
}
func getControlSockets() (ret []string) {
files, err := os.ReadDir(sshDir)
if err != nil {
panic(err)
}
for _, f := range files {
if strings.HasPrefix(f.Name(), "control") {
ret = append(ret, f.Name())
}
}
return
}
func tunnelShow() {
controls := getControlSockets()
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])
}
}
}
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() },
}
}
var (
sshDir = filepath.Join(os.Getenv("HOME"), ".ssh")
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() }}
port uint64
host string
)
func init() {
rootCmd.CompletionOptions.HiddenDefaultCmd = true
rootCmd.AddCommand(upCmd, downCmd, showCmd)
upCmd.Flags().Uint64VarP(&port, "port", "p", 0, "port number")
upCmd.MarkFlagRequired("port")
}
func main() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}