From 9a43ee8f1d60dfc8a974ef07ce6ad0179c3afec1 Mon Sep 17 00:00:00 2001 From: Henry Dollman Date: Sun, 28 Jul 2024 17:53:41 -0400 Subject: [PATCH] Allow address in agent's `PORT` env var --- agent/main.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/agent/main.go b/agent/main.go index 15482fe..7a53461 100644 --- a/agent/main.go +++ b/agent/main.go @@ -279,7 +279,7 @@ func gatherStats() *SystemData { return stats } -func startServer(port string, pubKey []byte) { +func startServer(addr string, pubKey []byte) { sshServer.Handle(func(s sshServer.Session) { stats := gatherStats() var jsonStats []byte @@ -288,8 +288,8 @@ func startServer(port string, pubKey []byte) { s.Exit(0) }) - log.Printf("Starting SSH server on port %s", port) - if err := sshServer.ListenAndServe(":"+port, nil, sshServer.NoPty(), + log.Printf("Starting SSH server on %s", addr) + if err := sshServer.ListenAndServe(addr, nil, sshServer.NoPty(), sshServer.PublicKeyAuth(func(ctx sshServer.Context, key sshServer.PublicKey) bool { data := []byte(pubKey) allowed, _, _, _, _ := sshServer.ParseAuthorizedKey(data) @@ -329,9 +329,13 @@ func main() { initializeNetIoStats() if port, exists := os.LookupEnv("PORT"); exists { + // allow passing an address in the form of "127.0.0.1:45876" + if !strings.Contains(port, ":") { + port = ":" + port + } startServer(port, pubKey) } else { - startServer("45876", pubKey) + startServer(":45876", pubKey) } }