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
24
config.go
24
config.go
|
|
@ -4,18 +4,26 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config holds all WAF plugin settings, persisted as JSON.
|
// Config holds all Firewall plugin settings, persisted as JSON.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Enabled bool `json:"enabled"`
|
Enabled bool `json:"enabled"`
|
||||||
NodeURL string `json:"node_url"` // e.g. http://192.168.1.50:8080
|
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
|
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 (
|
var (
|
||||||
cfg Config
|
cfg Config
|
||||||
|
|
@ -35,7 +43,7 @@ func loadConfig() {
|
||||||
cfgMu.Lock()
|
cfgMu.Lock()
|
||||||
defer cfgMu.Unlock()
|
defer cfgMu.Unlock()
|
||||||
|
|
||||||
f, err := os.Open(configFile)
|
f, err := os.Open(configPath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cfg = defaultConfig()
|
cfg = defaultConfig()
|
||||||
return
|
return
|
||||||
|
|
@ -55,7 +63,7 @@ func saveConfig() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return os.WriteFile(configFile, data, 0644)
|
return os.WriteFile(configPath(), data, 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfig() Config {
|
func getConfig() Config {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue