fix: use absolute config path next to binary — fixes permission denied on save
This commit is contained in:
parent
efb6c813e7
commit
2dc01e9aa0
1 changed files with 16 additions and 8 deletions
18
config.go
18
config.go
|
|
@ -4,18 +4,26 @@ import (
|
|||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Config holds all WAF plugin settings, persisted as JSON.
|
||||
// Config holds all Firewall plugin settings, persisted as JSON.
|
||||
type Config struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
NodeURL string `json:"node_url"` // e.g. http://192.168.1.50:8080
|
||||
ReturnPort int `json:"return_port"` // port for inspection endpoint async callbacks
|
||||
ReturnPort int `json:"return_port"` // port for async callbacks
|
||||
TimeoutMs int `json:"timeout_ms"` // inspection timeout in milliseconds
|
||||
}
|
||||
|
||||
const configFile = "waf_config.json"
|
||||
// configPath resolves to the config file next to the plugin binary.
|
||||
func configPath() string {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return "/opt/zoraxy/plugins/zoraxy-waf/waf_config.json"
|
||||
}
|
||||
return filepath.Join(filepath.Dir(exe), "waf_config.json")
|
||||
}
|
||||
|
||||
var (
|
||||
cfg Config
|
||||
|
|
@ -35,7 +43,7 @@ func loadConfig() {
|
|||
cfgMu.Lock()
|
||||
defer cfgMu.Unlock()
|
||||
|
||||
f, err := os.Open(configFile)
|
||||
f, err := os.Open(configPath())
|
||||
if err != nil {
|
||||
cfg = defaultConfig()
|
||||
return
|
||||
|
|
@ -55,7 +63,7 @@ func saveConfig() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(configFile, data, 0644)
|
||||
return os.WriteFile(configPath(), data, 0644)
|
||||
}
|
||||
|
||||
func getConfig() Config {
|
||||
|
|
|
|||
Loading…
Reference in a new issue