config.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package config
  2. import (
  3. "encoding/json"
  4. "os"
  5. )
  6. type Config struct {
  7. DHCP DHCPConfig `json:"dhcp"`
  8. DNS DNSConfig `json:"dns"`
  9. Web WebConfig `json:"web"`
  10. Database DatabaseConfig `json:"database"`
  11. }
  12. type DHCPConfig struct {
  13. Enabled bool `json:"enabled"`
  14. Interface string `json:"interface"`
  15. Network string `json:"network"`
  16. Netmask string `json:"netmask"`
  17. Gateway string `json:"gateway"`
  18. DNSServers []string `json:"dns_servers"`
  19. NTPServers []string `json:"ntp_servers"`
  20. BroadcastAddress string `json:"broadcast_address"`
  21. LeaseTime int `json:"lease_time"` // seconds
  22. IPPoolStart string `json:"ip_pool_start"`
  23. IPPoolEnd string `json:"ip_pool_end"`
  24. DomainName string `json:"domain_name"`
  25. ExcludedIPs []string `json:"excluded_ips"`
  26. }
  27. type DNSConfig struct {
  28. Enabled bool `json:"enabled"`
  29. ListenAddr string `json:"listen_addr"`
  30. ListenPort int `json:"listen_port"`
  31. Upstream []string `json:"upstream"`
  32. CacheSize int `json:"cache_size"`
  33. CacheTTL int `json:"cache_ttl"`
  34. Recursion bool `json:"recursion"`
  35. AllowQuery []string `json:"allow_query"`
  36. DNSSECValidation bool `json:"dnssec_validation"`
  37. }
  38. type WebConfig struct {
  39. Host string `json:"host"`
  40. Port int `json:"port"`
  41. SessionKey string `json:"session_key"`
  42. EnableHTTPS bool `json:"enable_https"`
  43. CertFile string `json:"https_cert"`
  44. KeyFile string `json:"https_key"`
  45. }
  46. type DatabaseConfig struct {
  47. Path string `json:"path"`
  48. }
  49. func LoadConfig(path string) (*Config, error) {
  50. data, err := os.ReadFile(path)
  51. if err != nil {
  52. return nil, err
  53. }
  54. var cfg Config
  55. if err := json.Unmarshal(data, &cfg); err != nil {
  56. return nil, err
  57. }
  58. return &cfg, nil
  59. }
  60. func (c *Config) Save(path string) error {
  61. data, err := json.MarshalIndent(c, "", " ")
  62. if err != nil {
  63. return err
  64. }
  65. return os.WriteFile(path, data, 0644)
  66. }