package config import ( "errors" "fmt" "net" "net/url" "os" "strconv" "strings" ) // ServerSeed is a config-managed EasyDNS instance. Token contents are held // only in process memory and never included in HTML, JSON or logs. type ServerSeed struct { Slot int Name string Role string URL string Token string TokenFile string } type Config struct { Listen string APIURL string APISocket string TokenFile string APIToken string PasswordFile string Password string SessionKeyFile string SessionAuto bool CookieSecure bool Username string MultiServer bool MultiServerExplicit bool LocalRoleExplicit bool AllowInsecureHTTP bool NodesDir string LocalName string LocalRole string Servers []ServerSeed } func env(key, fallback string) string { if v := strings.TrimSpace(os.Getenv(key)); v != "" { return v } return fallback } func parseBool(key string, fallback bool) (bool, error) { v := strings.TrimSpace(os.Getenv(key)) if v == "" { return fallback, nil } value, err := strconv.ParseBool(v) if err != nil { return false, fmt.Errorf("invalid %s: must be true or false", key) } return value, nil } // Load applies defaults, then the optional TOML file, then explicit environment // variables. EASYDNS_UI_CONFIG_FILE selects a required file; otherwise Debian's // /etc/easydns-ui/config.toml is used if present (Docker .env works without it). // A specified multi-server enabled value in TOML or the environment is // authoritative. The UI preference is available only when neither sets it. func Load() (Config, error) { c := Config{ Listen: "127.0.0.1:8088", APIURL: "http://127.0.0.1:9953", TokenFile: "/etc/easydns/data/control.token", PasswordFile: "/etc/easydns-ui/panel.password", SessionKeyFile: "/etc/easydns-ui/session.key", Username: "admin", NodesDir: "/var/lib/easydns-ui", LocalName: "Local EasyDNS", LocalRole: "primary", } path, explicit := os.LookupEnv("EASYDNS_UI_CONFIG_FILE") if !explicit { path = "/etc/easydns-ui/config.toml" } if path == "" { return c, errors.New("EASYDNS_UI_CONFIG_FILE must be an absolute file path") } if !strings.HasPrefix(path, "/") { return c, errors.New("EASYDNS_UI_CONFIG_FILE must be an absolute file path") } if err := readTOMLConfig(&c, path); err != nil { if !errors.Is(err, os.ErrNotExist) || explicit { return c, fmt.Errorf("load panel configuration %s: %w", path, err) } } c.Listen = env("EASYDNS_UI_LISTEN", c.Listen) c.APIURL = env("EASYDNS_API_URL", c.APIURL) c.APISocket = env("EASYDNS_API_SOCKET", c.APISocket) c.TokenFile = env("EASYDNS_API_TOKEN_FILE", c.TokenFile) c.APIToken = env("EASYDNS_API_TOKEN", c.APIToken) c.PasswordFile = env("EASYDNS_UI_PASSWORD_FILE", c.PasswordFile) if p, ok := os.LookupEnv("EASYDNS_UI_PASSWORD"); ok && p != "" { c.Password = p } c.SessionKeyFile = env("EASYDNS_UI_SESSION_KEY_FILE", c.SessionKeyFile) c.Username = env("EASYDNS_UI_USERNAME", c.Username) c.NodesDir = env("EASYDNS_UI_NODES_DIR", c.NodesDir) c.LocalName = env("EASYDNS_UI_LOCAL_NAME", c.LocalName) c.LocalRole = env("EASYDNS_UI_LOCAL_ROLE", c.LocalRole) if value, ok := os.LookupEnv("EASYDNS_UI_LOCAL_ROLE"); ok && strings.TrimSpace(value) != "" { c.LocalRoleExplicit = true } var err error if c.CookieSecure, err = parseBool("EASYDNS_UI_COOKIE_SECURE", c.CookieSecure); err != nil { return c, err } if c.SessionAuto, err = parseBool("EASYDNS_UI_SESSION_AUTO", c.SessionAuto); err != nil { return c, err } if c.MultiServer, err = parseBool("EASYDNS_UI_MULTISERVER", c.MultiServer); err != nil { return c, err } if _, ok := os.LookupEnv("EASYDNS_UI_MULTISERVER"); ok && strings.TrimSpace(os.Getenv("EASYDNS_UI_MULTISERVER")) != "" { c.MultiServerExplicit = true } if c.AllowInsecureHTTP, err = parseBool("EASYDNS_UI_ALLOW_INSECURE_HTTP", c.AllowInsecureHTTP); err != nil { return c, err } if _, portText, e := net.SplitHostPort(c.Listen); e != nil { return c, fmt.Errorf("invalid listen address: %w", e) } else { port, pErr := strconv.Atoi(portText) if pErr != nil || port < 1 || port > 65535 { return c, errors.New("invalid listen port") } } if c.APISocket == "" { u, e := url.Parse(c.APIURL) if e != nil || u == nil || u.Host == "" || u.User != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Path != "" || u.RawQuery != "" || u.Fragment != "" { return c, errors.New("EasyDNS API URL must be an http(s) base URL without credentials or path") } if u.Scheme == "http" && !c.AllowInsecureHTTP { h := u.Hostname() ip := net.ParseIP(h) if h != "localhost" && (ip == nil || !ip.IsLoopback()) { return c, errors.New("EasyDNS API URL must use HTTPS or loopback HTTP (set EASYDNS_UI_ALLOW_INSECURE_HTTP=true only for trusted networks)") } } } if c.Password == "" { if _, err := os.Stat(c.PasswordFile); err != nil { return c, fmt.Errorf("required credential file %s (or panel.password / EASYDNS_UI_PASSWORD): %w", c.PasswordFile, err) } } else if len(c.Password) < 16 { return c, errors.New("panel password must have at least 16 characters") } if !c.SessionAuto { if _, err := os.Stat(c.SessionKeyFile); err != nil { return c, fmt.Errorf("required session key file %s (or panel.session_auto / EASYDNS_UI_SESSION_AUTO=true): %w", c.SessionKeyFile, err) } } // Environment-declared node slots override file-declared slots by slot ID; // each environment slot must be fully defined (never mix token fragments). bySlot := make(map[int]ServerSeed) for _, seed := range c.Servers { if seed.Slot < 1 || seed.Slot > 15 { return c, errors.New("invalid config.toml server slot") } if _, ok := bySlot[seed.Slot]; ok { return c, errors.New("duplicate config.toml server slot") } bySlot[seed.Slot] = seed } for i := 1; i <= 15; i++ { prefix := fmt.Sprintf("EASYDNS_UI_SERVER_%d_", i) seed := ServerSeed{Slot: i, Name: strings.TrimSpace(os.Getenv(prefix + "NAME")), URL: strings.TrimSpace(os.Getenv(prefix + "URL")), Role: env(prefix+"ROLE", "secondary"), Token: strings.TrimSpace(os.Getenv(prefix + "TOKEN")), TokenFile: strings.TrimSpace(os.Getenv(prefix + "TOKEN_FILE"))} if seed.Name == "" && seed.URL == "" && seed.Token == "" && seed.TokenFile == "" { continue } bySlot[i] = seed } c.Servers = nil for i := 1; i <= 15; i++ { seed, ok := bySlot[i] if !ok { continue } if seed.Name == "" || seed.URL == "" || (seed.Token == "" && seed.TokenFile == "") { return c, fmt.Errorf("incomplete server slot %d: NAME, URL and TOKEN or TOKEN_FILE are required", i) } if seed.Token != "" && seed.TokenFile != "" { return c, fmt.Errorf("server slot %d: use TOKEN or TOKEN_FILE, not both", i) } c.Servers = append(c.Servers, seed) } return c, nil }