default config options

This commit is contained in:
Merith-TK 2025-02-04 17:17:16 +00:00
parent 0b300f64cc
commit 8f760582e0

View file

@ -17,7 +17,23 @@ type Proxy struct {
Type string `json:"type,omitempty"` // "tcp", "udp", or "both"
}
// ReadConfig loads the configuration or generates a default one if missing.
func ReadConfig(configPath string) ProxyConfig {
if _, err := os.Stat(configPath); os.IsNotExist(err) {
log.Printf("[INFO] Config file not found, generating default config at %s\n", configPath)
defaultConfig := ProxyConfig{
Proxy: []Proxy{
{
Local: "127.0.0.1:8080",
Remote: "127.0.0.1:8081",
Type: "both",
},
},
}
saveConfig(configPath, defaultConfig)
return defaultConfig
}
configData, err := os.ReadFile(configPath)
if err != nil {
log.Fatalf("[ERROR] Failed to read config file (%s): %v\n", configPath, err)
@ -29,3 +45,15 @@ func ReadConfig(configPath string) ProxyConfig {
}
return config
}
// saveConfig writes the given configuration to a file.
func saveConfig(configPath string, config ProxyConfig) {
configData, err := json5.MarshalIndent(config, "", " ")
if err != nil {
log.Fatalf("[ERROR] Failed to generate default config: %v\n", err)
}
if err := os.WriteFile(configPath, configData, 0644); err != nil {
log.Fatalf("[ERROR] Failed to write default config file: %v\n", err)
}
}