From 012925a684f284efce7643838d07a2d258d8c64b Mon Sep 17 00:00:00 2001 From: Ziggy Date: Thu, 3 Apr 2025 21:50:33 +0300 Subject: [PATCH 1/4] read bind address from env --- src/main.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/main.go b/src/main.go index e1dd09e..d3a2326 100644 --- a/src/main.go +++ b/src/main.go @@ -3,6 +3,13 @@ package main import ( "encoding/json" "flag" + "io/ioutil" + "net/http" + "os" + "plugin" + "strconv" + "strings" + "github.com/bbernhard/signal-cli-rest-api/api" "github.com/bbernhard/signal-cli-rest-api/client" docs "github.com/bbernhard/signal-cli-rest-api/docs" @@ -12,11 +19,6 @@ import ( log "github.com/sirupsen/logrus" swaggerFiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" - "io/ioutil" - "net/http" - "os" - "strconv" - "plugin" ) // @title Signal Cli REST API @@ -422,5 +424,21 @@ func main() { c.Start() } - router.Run() + router.Run(getBindAddress(port)) +} + +func getBindAddress(port string) string { + allowedIPs := utils.GetEnv("ALLOWED_IPS", "") + if allowedIPs == "" { + return ":" + port // Listen to all incoming traffic + } + + // Parse the list of allowed IPs + ipList := strings.Split(allowedIPs, ",") + if len(ipList) == 1 { + return ipList[0] + ":" + port // Bind to a single IP + } + + log.Fatal("Multiple IPs in ALLOWED_IPS are not supported for binding") + return "" } From 44ee59721d5d1552daddcb78bfcb1da249c89c68 Mon Sep 17 00:00:00 2001 From: Ziggy Date: Thu, 3 Apr 2025 21:52:53 +0300 Subject: [PATCH 2/4] BIND_IP --- src/main.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/main.go b/src/main.go index d3a2326..6a7eb49 100644 --- a/src/main.go +++ b/src/main.go @@ -8,7 +8,6 @@ import ( "os" "plugin" "strconv" - "strings" "github.com/bbernhard/signal-cli-rest-api/api" "github.com/bbernhard/signal-cli-rest-api/client" @@ -428,17 +427,10 @@ func main() { } func getBindAddress(port string) string { - allowedIPs := utils.GetEnv("ALLOWED_IPS", "") - if allowedIPs == "" { + bindIP := utils.GetEnv("BIND_IP", "") + if bindIP == "" { return ":" + port // Listen to all incoming traffic } - // Parse the list of allowed IPs - ipList := strings.Split(allowedIPs, ",") - if len(ipList) == 1 { - return ipList[0] + ":" + port // Bind to a single IP - } - - log.Fatal("Multiple IPs in ALLOWED_IPS are not supported for binding") - return "" + return bindIP + ":" + port // Bind to the specified IP } From 0a5e5b79df99966d3b06403888a3867d0d5c1f7b Mon Sep 17 00:00:00 2001 From: Ziggy Date: Thu, 3 Apr 2025 22:08:19 +0300 Subject: [PATCH 3/4] TRUSTED_PROXIES --- src/main.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main.go b/src/main.go index 6a7eb49..75200d3 100644 --- a/src/main.go +++ b/src/main.go @@ -8,6 +8,7 @@ import ( "os" "plugin" "strconv" + "strings" "github.com/bbernhard/signal-cli-rest-api/api" "github.com/bbernhard/signal-cli-rest-api/client" @@ -82,6 +83,17 @@ func main() { })) router.Use(gin.Recovery()) + trustedProxies := utils.GetEnv("TRUSTED_PROXIES", "") + if trustedProxies != "" { + proxiesArr := strings.Split(trustedProxies, ",") + for i, p := range proxiesArr { + proxiesArr[i] = strings.TrimSpace(p) + } + err := router.SetTrustedProxies(proxiesArr) + if err != nil { + log.Fatal("Couldn't set trusted proxies: ", err.Error()) + } + } port := utils.GetEnv("PORT", "8080") if _, err := strconv.Atoi(port); err != nil { From 92fe20b29b42a9604f4f80941aa1d82eb3b985fc Mon Sep 17 00:00:00 2001 From: Ziggy Date: Sat, 5 Apr 2025 19:18:21 +0300 Subject: [PATCH 4/4] add logging --- src/main.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main.go b/src/main.go index 75200d3..18c2540 100644 --- a/src/main.go +++ b/src/main.go @@ -84,6 +84,7 @@ func main() { router.Use(gin.Recovery()) trustedProxies := utils.GetEnv("TRUSTED_PROXIES", "") + log.Info("Debug: TRUSTED_PROXIES env variable = ", trustedProxies) if trustedProxies != "" { proxiesArr := strings.Split(trustedProxies, ",") for i, p := range proxiesArr { @@ -440,9 +441,13 @@ func main() { func getBindAddress(port string) string { bindIP := utils.GetEnv("BIND_IP", "") + log.Info("Debug: BIND_IP env variable = ", bindIP) + var addr string if bindIP == "" { - return ":" + port // Listen to all incoming traffic + addr = ":" + port // Listen to all incoming traffic + } else { + addr = bindIP + ":" + port // Bind to the specified IP } - - return bindIP + ":" + port // Bind to the specified IP + log.Info("Final bind address: ", addr) + return addr }