Treat mtu as int and dont overwrite from websocket

Former-commit-id: 228bddcf79
This commit is contained in:
Owen
2025-10-25 17:15:25 -07:00
parent 2d34c6c8b2
commit 3fa1073f49
2 changed files with 16 additions and 40 deletions

View File

@@ -7,6 +7,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv"
"time" "time"
) )
@@ -18,7 +19,7 @@ type OlmConfig struct {
Secret string `json:"secret"` Secret string `json:"secret"`
// Network settings // Network settings
MTU string `json:"mtu"` MTU int `json:"mtu"`
DNS string `json:"dns"` DNS string `json:"dns"`
InterfaceName string `json:"interface"` InterfaceName string `json:"interface"`
@@ -58,7 +59,7 @@ const (
// DefaultConfig returns a config with default values // DefaultConfig returns a config with default values
func DefaultConfig() *OlmConfig { func DefaultConfig() *OlmConfig {
config := &OlmConfig{ config := &OlmConfig{
MTU: "1280", MTU: 1280,
DNS: "8.8.8.8", DNS: "8.8.8.8",
LogLevel: "INFO", LogLevel: "INFO",
InterfaceName: "olm", InterfaceName: "olm",
@@ -175,8 +176,12 @@ func loadConfigFromEnv(config *OlmConfig) {
config.sources["secret"] = string(SourceEnv) config.sources["secret"] = string(SourceEnv)
} }
if val := os.Getenv("MTU"); val != "" { if val := os.Getenv("MTU"); val != "" {
config.MTU = val if mtu, err := strconv.Atoi(val); err == nil {
config.sources["mtu"] = string(SourceEnv) config.MTU = mtu
config.sources["mtu"] = string(SourceEnv)
} else {
fmt.Printf("Invalid MTU value: %s, keeping current value\n", val)
}
} }
if val := os.Getenv("DNS"); val != "" { if val := os.Getenv("DNS"); val != "" {
config.DNS = val config.DNS = val
@@ -236,7 +241,7 @@ func loadConfigFromCLI(config *OlmConfig, args []string) (bool, bool, error) {
serviceFlags.StringVar(&config.Endpoint, "endpoint", config.Endpoint, "Endpoint of your Pangolin server") serviceFlags.StringVar(&config.Endpoint, "endpoint", config.Endpoint, "Endpoint of your Pangolin server")
serviceFlags.StringVar(&config.ID, "id", config.ID, "Olm ID") serviceFlags.StringVar(&config.ID, "id", config.ID, "Olm ID")
serviceFlags.StringVar(&config.Secret, "secret", config.Secret, "Olm secret") serviceFlags.StringVar(&config.Secret, "secret", config.Secret, "Olm secret")
serviceFlags.StringVar(&config.MTU, "mtu", config.MTU, "MTU to use") serviceFlags.IntVar(&config.MTU, "mtu", config.MTU, "MTU to use")
serviceFlags.StringVar(&config.DNS, "dns", config.DNS, "DNS server to use") serviceFlags.StringVar(&config.DNS, "dns", config.DNS, "DNS server to use")
serviceFlags.StringVar(&config.LogLevel, "log-level", config.LogLevel, "Log level (DEBUG, INFO, WARN, ERROR, FATAL)") serviceFlags.StringVar(&config.LogLevel, "log-level", config.LogLevel, "Log level (DEBUG, INFO, WARN, ERROR, FATAL)")
serviceFlags.StringVar(&config.InterfaceName, "interface", config.InterfaceName, "Name of the WireGuard interface") serviceFlags.StringVar(&config.InterfaceName, "interface", config.InterfaceName, "Name of the WireGuard interface")
@@ -264,7 +269,7 @@ func loadConfigFromCLI(config *OlmConfig, args []string) (bool, bool, error) {
if config.Secret != origValues["secret"].(string) { if config.Secret != origValues["secret"].(string) {
config.sources["secret"] = string(SourceCLI) config.sources["secret"] = string(SourceCLI)
} }
if config.MTU != origValues["mtu"].(string) { if config.MTU != origValues["mtu"].(int) {
config.sources["mtu"] = string(SourceCLI) config.sources["mtu"] = string(SourceCLI)
} }
if config.DNS != origValues["dns"].(string) { if config.DNS != origValues["dns"].(string) {
@@ -343,7 +348,7 @@ func mergeConfigs(dest, src *OlmConfig) {
dest.Secret = src.Secret dest.Secret = src.Secret
dest.sources["secret"] = string(SourceFile) dest.sources["secret"] = string(SourceFile)
} }
if src.MTU != "" && src.MTU != "1280" { if src.MTU != 0 && src.MTU != 1280 {
dest.MTU = src.MTU dest.MTU = src.MTU
dest.sources["mtu"] = string(SourceFile) dest.sources["mtu"] = string(SourceFile)
} }
@@ -396,19 +401,6 @@ func SaveConfig(config *OlmConfig) error {
return os.WriteFile(configPath, data, 0644) return os.WriteFile(configPath, data, 0644)
} }
// UpdateFromWebsocket updates config with values received from websocket client
func (c *OlmConfig) UpdateFromWebsocket(id, secret, endpoint string) {
if id != "" {
c.ID = id
}
if secret != "" {
c.Secret = secret
}
if endpoint != "" {
c.Endpoint = endpoint
}
}
// ShowConfig prints the configuration and the source of each value // ShowConfig prints the configuration and the source of each value
func (c *OlmConfig) ShowConfig() { func (c *OlmConfig) ShowConfig() {
configPath := getOlmConfigPath() configPath := getOlmConfigPath()
@@ -456,7 +448,7 @@ func (c *OlmConfig) ShowConfig() {
// Network settings // Network settings
fmt.Println("\nNetwork:") fmt.Println("\nNetwork:")
fmt.Printf(" mtu = %s [%s]\n", c.MTU, getSource("mtu")) fmt.Printf(" mtu = %d [%s]\n", c.MTU, getSource("mtu"))
fmt.Printf(" dns = %s [%s]\n", c.DNS, getSource("dns")) fmt.Printf(" dns = %s [%s]\n", c.DNS, getSource("dns"))
fmt.Printf(" interface = %s [%s]\n", c.InterfaceName, getSource("interface")) fmt.Printf(" interface = %s [%s]\n", c.InterfaceName, getSource("interface"))

22
main.go
View File

@@ -222,7 +222,6 @@ func runOlmMainWithArgs(ctx context.Context, args []string) {
id = config.ID id = config.ID
secret = config.Secret secret = config.Secret
mtu = config.MTU mtu = config.MTU
mtuInt int
logLevel = config.LogLevel logLevel = config.LogLevel
interfaceName = config.InterfaceName interfaceName = config.InterfaceName
enableHTTP = config.EnableHTTP enableHTTP = config.EnableHTTP
@@ -324,15 +323,6 @@ func runOlmMainWithArgs(ctx context.Context, args []string) {
if err != nil { if err != nil {
logger.Fatal("Failed to create olm: %v", err) logger.Fatal("Failed to create olm: %v", err)
} }
// Update config with values from websocket client (which may have loaded from its config file)
config.UpdateFromWebsocket(
olm.GetConfig().ID,
olm.GetConfig().Secret,
olm.GetConfig().Endpoint,
)
endpoint = config.Endpoint
id = config.ID
secret = config.Secret
// wait until we have a client id and secret and endpoint // wait until we have a client id and secret and endpoint
waitCount := 0 waitCount := 0
@@ -360,12 +350,6 @@ func runOlmMainWithArgs(ctx context.Context, args []string) {
} }
} }
// parse the mtu string into an int
mtuInt, err = strconv.Atoi(mtu)
if err != nil {
logger.Fatal("Failed to parse MTU: %v", err)
}
privateKey, err = wgtypes.GeneratePrivateKey() privateKey, err = wgtypes.GeneratePrivateKey()
if err != nil { if err != nil {
logger.Fatal("Failed to generate private key: %v", err) logger.Fatal("Failed to generate private key: %v", err)
@@ -486,12 +470,12 @@ func runOlmMainWithArgs(ctx context.Context, args []string) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return tun.CreateTUN(interfaceName, mtuInt) return tun.CreateTUN(interfaceName, mtu)
} }
if tunFdStr := os.Getenv(ENV_WG_TUN_FD); tunFdStr != "" { if tunFdStr := os.Getenv(ENV_WG_TUN_FD); tunFdStr != "" {
return createTUNFromFD(tunFdStr, mtuInt) return createTUNFromFD(tunFdStr, mtu)
} }
return tun.CreateTUN(interfaceName, mtuInt) return tun.CreateTUN(interfaceName, mtu)
}() }()
if err != nil { if err != nil {