zoraxy-waf/config.go

65 lines
1.2 KiB
Go

package main
import (
"encoding/json"
"log"
"os"
"sync"
)
// Config holds all WAF 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
TimeoutMs int `json:"timeout_ms"` // inspection timeout in milliseconds
}
const configFile = "waf_config.json"
var (
cfg Config
cfgMu sync.RWMutex
)
func defaultConfig() Config {
return Config{
Enabled: false,
NodeURL: "http://127.0.0.1:8080",
ReturnPort: 9090,
TimeoutMs: 3000,
}
}
func loadConfig() {
cfgMu.Lock()
defer cfgMu.Unlock()
f, err := os.Open(configFile)
if err != nil {
cfg = defaultConfig()
return
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&cfg); err != nil {
log.Printf("WARN: corrupt config, using defaults: %v", err)
cfg = defaultConfig()
}
}
func saveConfig() error {
cfgMu.RLock()
data, err := json.MarshalIndent(cfg, "", " ")
cfgMu.RUnlock()
if err != nil {
return err
}
return os.WriteFile(configFile, data, 0644)
}
func getConfig() Config {
cfgMu.RLock()
defer cfgMu.RUnlock()
return cfg
}