fix(main): hostname not set

This commit is contained in:
nullcat
2026-05-18 04:25:30 +08:00
parent 15bda1a807
commit ab2addd893
+12 -7
View File
@@ -51,20 +51,16 @@ type Config struct {
} }
func LoadConfig(path string) (*Config, error) { func LoadConfig(path string) (*Config, error) {
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
cfg := &Config{ cfg := &Config{
Core: Core{ Core: Core{
Hostname: hostname, Hostname: "",
Ephemeral: true, Ephemeral: true,
AcceptRoutes: true, AcceptRoutes: true,
}, },
Forward: make(map[string][]ForwardRule), Forward: make(map[string][]ForwardRule),
Connect: make(map[string][]ConnectRule), Connect: make(map[string][]ConnectRule),
} }
if _, err = os.Stat(path); err != nil { if _, err := os.Stat(path); err != nil {
// write a basic config // write a basic config
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err = toml.NewEncoder(buf).Encode(cfg) err = toml.NewEncoder(buf).Encode(cfg)
@@ -73,9 +69,18 @@ func LoadConfig(path string) (*Config, error) {
} }
err = os.WriteFile(path, buf.Bytes(), 0644) err = os.WriteFile(path, buf.Bytes(), 0644)
} }
_, err = toml.DecodeFile(path, cfg) _, err := toml.DecodeFile(path, cfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if cfg.Core.Hostname == "" {
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
cfg.Core.Hostname = hostname
}
return cfg, nil return cfg, nil
} }