From 420f12f6375c4267296fd38d126b69d9b7839bac Mon Sep 17 00:00:00 2001 From: Jon Rafkind Date: Thu, 6 Oct 2022 23:41:49 -0700 Subject: [PATCH 1/5] add ssh command to print device info --- ssh.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ssh.go b/ssh.go index 376dd82f6..9157d9841 100644 --- a/ssh.go +++ b/ssh.go @@ -264,6 +264,14 @@ func attachCommands(l *logrus.Logger, c *config.C, ssh *sshd.SSHServer, hostMap }, }) + ssh.RegisterCommand(&sshd.Command{ + Name: "device-info", + ShortDescription: "Prints information about the network device.", + Callback: func(fs interface{}, a []string, w sshd.StringWriter) error { + return sshDeviceInfo(ifce, fs, a, w) + }, + }) + ssh.RegisterCommand(&sshd.Command{ Name: "print-cert", ShortDescription: "Prints the current certificate being used or the certificate for the provided vpn ip", @@ -876,6 +884,10 @@ func sshPrintTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWr return enc.Encode(copyHostInfo(hostInfo, ifce.hostMap.preferredRanges)) } +func sshDeviceInfo(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error { + return w.WriteLine(fmt.Sprintf("name=%v cidr=%v", ifce.inside.Name(), ifce.inside.Cidr().String())) +} + func sshReload(c *config.C, w sshd.StringWriter) error { err := w.WriteLine("Reloading config") c.ReloadConfig() From b733e9487cc6b4cecf5b81849af69d38eae29096 Mon Sep 17 00:00:00 2001 From: Jon Rafkind Date: Thu, 6 Oct 2022 23:46:28 -0700 Subject: [PATCH 2/5] go fmt --- ssh.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ssh.go b/ssh.go index 9157d9841..dbeca69dc 100644 --- a/ssh.go +++ b/ssh.go @@ -264,13 +264,13 @@ func attachCommands(l *logrus.Logger, c *config.C, ssh *sshd.SSHServer, hostMap }, }) - ssh.RegisterCommand(&sshd.Command{ - Name: "device-info", - ShortDescription: "Prints information about the network device.", - Callback: func(fs interface{}, a []string, w sshd.StringWriter) error { - return sshDeviceInfo(ifce, fs, a, w) - }, - }) + ssh.RegisterCommand(&sshd.Command{ + Name: "device-info", + ShortDescription: "Prints information about the network device.", + Callback: func(fs interface{}, a []string, w sshd.StringWriter) error { + return sshDeviceInfo(ifce, fs, a, w) + }, + }) ssh.RegisterCommand(&sshd.Command{ Name: "print-cert", @@ -885,7 +885,7 @@ func sshPrintTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWr } func sshDeviceInfo(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error { - return w.WriteLine(fmt.Sprintf("name=%v cidr=%v", ifce.inside.Name(), ifce.inside.Cidr().String())) + return w.WriteLine(fmt.Sprintf("name=%v cidr=%v", ifce.inside.Name(), ifce.inside.Cidr().String())) } func sshReload(c *config.C, w sshd.StringWriter) error { From c84eeac1de399b8174adea7a5a0c499d4fef7506 Mon Sep 17 00:00:00 2001 From: Jon Rafkind Date: Mon, 27 Feb 2023 06:08:31 +0000 Subject: [PATCH 3/5] add -json flag --- ssh.go | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/ssh.go b/ssh.go index 629fb12d1..1ecac6df7 100644 --- a/ssh.go +++ b/ssh.go @@ -48,6 +48,11 @@ type sshCreateTunnelFlags struct { Address string } +type sshDeviceInfoFlags struct { + Json bool + Pretty bool +} + func wireSSHReload(l *logrus.Logger, ssh *sshd.SSHServer, c *config.C) { c.RegisterReloadCallback(func(c *config.C) { if c.GetBool("sshd.enabled", false) { @@ -267,8 +272,15 @@ func attachCommands(l *logrus.Logger, c *config.C, ssh *sshd.SSHServer, hostMap ssh.RegisterCommand(&sshd.Command{ Name: "device-info", ShortDescription: "Prints information about the network device.", + Flags: func() (*flag.FlagSet, interface{}) { + fl := flag.NewFlagSet("", flag.ContinueOnError) + s := sshDeviceInfoFlags{} + fl.BoolVar(&s.Json, "json", false, "outputs as json with more information") + fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json") + return fl, &s + }, Callback: func(fs interface{}, a []string, w sshd.StringWriter) error { - return sshDeviceInfo(ifce, fs, a, w) + return sshDeviceInfo(ifce, fs, w) }, }) @@ -884,8 +896,31 @@ func sshPrintTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWr return enc.Encode(copyHostInfo(hostInfo, ifce.hostMap.preferredRanges)) } -func sshDeviceInfo(ifce *Interface, fs interface{}, a []string, w sshd.StringWriter) error { - return w.WriteLine(fmt.Sprintf("name=%v cidr=%v", ifce.inside.Name(), ifce.inside.Cidr().String())) +func sshDeviceInfo(ifce *Interface, fs interface{}, w sshd.StringWriter) error { + + data := struct{ + Name string `json:"name"` + Cidr string `json:"cidr"` + }{ + Name: ifce.inside.Name(), + Cidr: ifce.inside.Cidr().String(), + } + + flags, ok := fs.(*sshDeviceInfoFlags) + if !ok { + return fmt.Errorf("internal error: expected flags to be sshDeviceInfoFlags but was %+v", fs) + } + + if flags.Json || flags.Pretty { + js := json.NewEncoder(w.GetWriter()) + if flags.Pretty { + js.SetIndent("", " ") + } + + return js.Encode(data) + } else { + return w.WriteLine(fmt.Sprintf("name=%v cidr=%v", data.Name, data.Cidr)) + } } func sshReload(c *config.C, w sshd.StringWriter) error { From cda1848d287ee5dab4bed80b6e9f3e4267e2d97c Mon Sep 17 00:00:00 2001 From: Jon Rafkind Date: Mon, 27 Feb 2023 06:10:39 +0000 Subject: [PATCH 4/5] gofmt --- ssh.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ssh.go b/ssh.go index 1ecac6df7..ffb1efc5a 100644 --- a/ssh.go +++ b/ssh.go @@ -49,7 +49,7 @@ type sshCreateTunnelFlags struct { } type sshDeviceInfoFlags struct { - Json bool + Json bool Pretty bool } @@ -898,7 +898,7 @@ func sshPrintTunnel(ifce *Interface, fs interface{}, a []string, w sshd.StringWr func sshDeviceInfo(ifce *Interface, fs interface{}, w sshd.StringWriter) error { - data := struct{ + data := struct { Name string `json:"name"` Cidr string `json:"cidr"` }{ From 75f8422aa48dda246fb083ea91be1b10e1794567 Mon Sep 17 00:00:00 2001 From: Jon Rafkind Date: Mon, 29 Apr 2024 13:57:46 -0700 Subject: [PATCH 5/5] update variable name --- ssh.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ssh.go b/ssh.go index 9000d3d71..0f4f65194 100644 --- a/ssh.go +++ b/ssh.go @@ -302,7 +302,7 @@ func attachCommands(l *logrus.Logger, c *config.C, ssh *sshd.SSHServer, f *Inter return fl, &s }, Callback: func(fs interface{}, a []string, w sshd.StringWriter) error { - return sshDeviceInfo(ifce, fs, w) + return sshDeviceInfo(f, fs, w) }, })