mirror of
https://github.com/Merith-TK/simpleproxy.git
synced 2025-02-22 15:50:25 +00:00
default config options
This commit is contained in:
parent
0b300f64cc
commit
8f760582e0
1 changed files with 28 additions and 0 deletions
28
config.go
28
config.go
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue