To solve the problem of scattered configuration management logic, we created a unified configuration management module that centralizes all configuration-related operations in one place.
type ConfigManager struct {
configPath string
mutex sync.RWMutex
config *types.GlobalConfig
}
Defines a unified interface for configuration management, facilitating testing and extension.
Provides singleton access through the GetConfigManager() function.
LoadConfig() - Load configuration from fileSaveConfig() - Save configuration to fileUpdateConfig() - Update current configurationDedicated getters and setters for each configuration section:
GetBaseConfig() / SetBaseConfig()GetWebConfig() / SetWebConfig()GetMetricsConfig() / SetMetricsConfig()Uses read-write locks to ensure safe access in multi-goroutine environments.
cfgManager := config.GetConfigManager()
err := cfgManager.LoadConfig()
if err != nil {
// Handle error
}
cfg := cfgManager.GetConfig()
baseCfg := cfgManager.GetBaseConfig()
newBaseCfg := types.BaseConfig{...}
cfgManager.SetBaseConfig(newBaseCfg)
err := cfgManager.SaveConfig() // Save to file
The following files have been updated to use the new configuration manager:
/internal/api/server.go - API server configuration loading/cmd/netxfw/commands/agent/*.go - CLI command configuration access/internal/plugins/manager.go - Plugin manager configuration# Base configuration
base:
default_deny: true
allow_return_traffic: false
allow_icmp: true
persist_rules: true
cleanup_interval: "1m"
# Connection tracking
conntrack:
enabled: true
max_entries: 100000
tcp_timeout: "1h"
udp_timeout: "5m"
# Rate limiting
rate_limit:
enabled: true
auto_block: true
auto_block_expiry: "10m"
# Web interface
web:
enabled: true
port: 11811
# Metrics
metrics:
enabled: true
server_enabled: false
port: 11812
# BPF Map capacity
capacity:
lock_list: 2000000
dyn_lock_list: 2000000
whitelist: 65536
ip_port_rules: 65536
Always use GetConfigManager() to get the configuration manager instance, do not create new instances.
After modifying configuration, call SaveConfig() to persist changes.
Always check for errors when loading or saving configuration.
The configuration manager is thread-safe, but avoid frequent configuration updates in hot paths.