mirror of
https://github.com/fosrl/olm.git
synced 2025-12-05 19:17:35 -06:00
Treat mtu as int and dont overwrite from websocket
This commit is contained in:
34
config.go
34
config.go
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -18,7 +19,7 @@ type OlmConfig struct {
|
||||
Secret string `json:"secret"`
|
||||
|
||||
// Network settings
|
||||
MTU string `json:"mtu"`
|
||||
MTU int `json:"mtu"`
|
||||
DNS string `json:"dns"`
|
||||
InterfaceName string `json:"interface"`
|
||||
|
||||
@@ -58,7 +59,7 @@ const (
|
||||
// DefaultConfig returns a config with default values
|
||||
func DefaultConfig() *OlmConfig {
|
||||
config := &OlmConfig{
|
||||
MTU: "1280",
|
||||
MTU: 1280,
|
||||
DNS: "8.8.8.8",
|
||||
LogLevel: "INFO",
|
||||
InterfaceName: "olm",
|
||||
@@ -175,8 +176,12 @@ func loadConfigFromEnv(config *OlmConfig) {
|
||||
config.sources["secret"] = string(SourceEnv)
|
||||
}
|
||||
if val := os.Getenv("MTU"); val != "" {
|
||||
config.MTU = val
|
||||
config.sources["mtu"] = string(SourceEnv)
|
||||
if mtu, err := strconv.Atoi(val); err == nil {
|
||||
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 != "" {
|
||||
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.ID, "id", config.ID, "Olm ID")
|
||||
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.LogLevel, "log-level", config.LogLevel, "Log level (DEBUG, INFO, WARN, ERROR, FATAL)")
|
||||
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) {
|
||||
config.sources["secret"] = string(SourceCLI)
|
||||
}
|
||||
if config.MTU != origValues["mtu"].(string) {
|
||||
if config.MTU != origValues["mtu"].(int) {
|
||||
config.sources["mtu"] = string(SourceCLI)
|
||||
}
|
||||
if config.DNS != origValues["dns"].(string) {
|
||||
@@ -343,7 +348,7 @@ func mergeConfigs(dest, src *OlmConfig) {
|
||||
dest.Secret = src.Secret
|
||||
dest.sources["secret"] = string(SourceFile)
|
||||
}
|
||||
if src.MTU != "" && src.MTU != "1280" {
|
||||
if src.MTU != 0 && src.MTU != 1280 {
|
||||
dest.MTU = src.MTU
|
||||
dest.sources["mtu"] = string(SourceFile)
|
||||
}
|
||||
@@ -396,19 +401,6 @@ func SaveConfig(config *OlmConfig) error {
|
||||
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
|
||||
func (c *OlmConfig) ShowConfig() {
|
||||
configPath := getOlmConfigPath()
|
||||
@@ -456,7 +448,7 @@ func (c *OlmConfig) ShowConfig() {
|
||||
|
||||
// Network settings
|
||||
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(" interface = %s [%s]\n", c.InterfaceName, getSource("interface"))
|
||||
|
||||
|
||||
22
main.go
22
main.go
@@ -222,7 +222,6 @@ func runOlmMainWithArgs(ctx context.Context, args []string) {
|
||||
id = config.ID
|
||||
secret = config.Secret
|
||||
mtu = config.MTU
|
||||
mtuInt int
|
||||
logLevel = config.LogLevel
|
||||
interfaceName = config.InterfaceName
|
||||
enableHTTP = config.EnableHTTP
|
||||
@@ -324,15 +323,6 @@ func runOlmMainWithArgs(ctx context.Context, args []string) {
|
||||
if err != nil {
|
||||
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
|
||||
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()
|
||||
if err != nil {
|
||||
logger.Fatal("Failed to generate private key: %v", err)
|
||||
@@ -486,12 +470,12 @@ func runOlmMainWithArgs(ctx context.Context, args []string) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tun.CreateTUN(interfaceName, mtuInt)
|
||||
return tun.CreateTUN(interfaceName, mtu)
|
||||
}
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user