From babba790cd6579f1775d165c007b91fb23e4a578 Mon Sep 17 00:00:00 2001 From: Jacob McSwain Date: Mon, 12 Feb 2024 03:27:34 -0600 Subject: [PATCH] add WIREGUARD_STARTING_PORT --- internal/config/config.go | 8 ++++++++ internal/db/models/tunnel.go | 4 ++-- internal/server/api/controllers/v1/tunnels.go | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index e9d0df83..c403a3b3 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -34,6 +34,7 @@ type Config struct { SessionSecret []byte VTUNStartingAddress string WireguardStartingAddress string + WireguardStartingPort uint16 PostgresDSN string postgresUser string postgresPassword string @@ -66,6 +67,12 @@ func loadConfig() Config { metricsPort = 0 } + portStr = os.Getenv("WIREGUARD_STARTING_PORT") + wireguardStartingPort, err := strconv.ParseInt(portStr, 10, 0) + if err != nil { + wireguardStartingPort = 5527 + } + tmpConfig := Config{ Debug: os.Getenv("DEBUG") != "", Port: int(httpPort), @@ -82,6 +89,7 @@ func loadConfig() Config { strSessionSecret: os.Getenv("SESSION_SECRET"), VTUNStartingAddress: os.Getenv("VTUN_STARTING_ADDRESS"), WireguardStartingAddress: os.Getenv("WIREGUARD_STARTING_ADDRESS"), + WireguardStartingPort: uint16(wireguardStartingPort), postgresUser: os.Getenv("PG_USER"), postgresPassword: os.Getenv("PG_PASSWORD"), postgresHost: os.Getenv("PG_HOST"), diff --git a/internal/db/models/tunnel.go b/internal/db/models/tunnel.go index 422f0fae..859e6e98 100644 --- a/internal/db/models/tunnel.go +++ b/internal/db/models/tunnel.go @@ -204,7 +204,7 @@ func GetNextWireguardIP(db *gorm.DB, config *config.Config) (string, error) { return highestIP.String(), nil } -func GetNextWireguardPort(db *gorm.DB) (uint16, error) { +func GetNextWireguardPort(db *gorm.DB, config *config.Config) (uint16, error) { // Each tunnel is added with a port starting from 51820 and incrementing by 1 for each tunnel // We need to find the next available port. var tunnels []Tunnel @@ -214,7 +214,7 @@ func GetNextWireguardPort(db *gorm.DB) (uint16, error) { } // We need to find the next available port. // We can do this by finding the highest port, and adding 1 to it. - var highestPort uint16 = 5525 + var highestPort uint16 = config.WireguardStartingPort - 1 for _, tunnel := range tunnels { if tunnel.WireguardPort > highestPort { highestPort = tunnel.WireguardPort diff --git a/internal/server/api/controllers/v1/tunnels.go b/internal/server/api/controllers/v1/tunnels.go index caf0bdf7..371ce076 100644 --- a/internal/server/api/controllers/v1/tunnels.go +++ b/internal/server/api/controllers/v1/tunnels.go @@ -272,7 +272,7 @@ func POSTTunnel(c *gin.Context) { return } - tunnel.WireguardPort, err = models.GetNextWireguardPort(db) + tunnel.WireguardPort, err = models.GetNextWireguardPort(db, config) if err != nil { fmt.Printf("POSTTunnel: Error getting next port: %v\n", err) c.JSON(http.StatusInternalServerError, gin.H{"error": "Error getting next port"})