Skip to content

Commit

Permalink
add WIREGUARD_STARTING_PORT
Browse files Browse the repository at this point in the history
  • Loading branch information
USA-RedDragon committed Feb 12, 2024
1 parent 7350d59 commit babba79
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
8 changes: 8 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Config struct {
SessionSecret []byte
VTUNStartingAddress string
WireguardStartingAddress string
WireguardStartingPort uint16
PostgresDSN string
postgresUser string
postgresPassword string
Expand Down Expand Up @@ -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),
Expand All @@ -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"),
Expand Down
4 changes: 2 additions & 2 deletions internal/db/models/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/server/api/controllers/v1/tunnels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down

0 comments on commit babba79

Please sign in to comment.