This repository was archived by the owner on Aug 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.go
More file actions
152 lines (132 loc) · 4.56 KB
/
settings.go
File metadata and controls
152 lines (132 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// File: settings.go
package godns
import (
"flag"
"fmt"
"os"
"github.com/BurntSushi/toml"
"github.com/ProxyFi/GoDNS/internal/log"
)
// LogLevelMap maps log level strings to their integer constants.
// The constants are defined below to ensure type safety and clarity.
var LogLevelMap = map[string]int{
"DEBUG": log.LevelDebug,
"INFO": log.LevelInfo,
"NOTICE": log.LevelNotice,
"WARN": log.LevelWarn,
"ERROR": log.LevelError,
}
// Settings holds all application-wide configuration.
// It is the root struct for the TOML configuration file.
type Settings struct {
Version string
Debug bool
Server DNSServerSettings `toml:"server"`
ResolvConfig ResolvSettings `toml:"resolv"`
Redis RedisSettings `toml:"redis"`
Memcache MemcacheSettings `toml:"memcache"`
Log LogSettings `toml:"log"`
Cache CacheSettings `toml:"cache"`
Hosts HostsSettings `toml:"hosts"`
Blocklist BlocklistSettings `toml:"blocklist"`
}
// ResolvSettings holds resolver-specific configuration.
type ResolvSettings struct {
Timeout int
Interval int
SetEDNS0 bool
ServerListFile string `toml:"server-list-file"`
ResolvFile string `toml:"resolv-file"`
DNSSECEnable bool `toml:"dnssec-enable"`
TrustAnchorFile string `toml:"trust-anchor-file"`
// LoadBalanceEnable controls whether latency-based load balancing is active.
LoadBalanceEnable bool `toml:"load-balance-enable"`
}
// DNSServerSettings holds DNS server-specific configuration.
type DNSServerSettings struct {
Host string
Port int
}
// RedisSettings holds Redis connection configuration.
type RedisSettings struct {
Addr string
Password string
DB int
}
// MemcacheSettings holds Memcache connection configuration.
type MemcacheSettings struct {
Servers []string
}
// LogSettings holds log configuration.
type LogSettings struct {
Stdout bool
File string
Level string
}
// LogLevel returns the integer constant for the configured log level.
// It performs a lookup on the LogLevelMap and panics on an invalid level.
// This design choice is carried over from the original code.
func (ls LogSettings) LogLevel() int {
level, ok := LogLevelMap[ls.Level]
if !ok {
// Use fmt.Errorf to create a structured error message and then panic.
// This is a common pattern for handling critical configuration errors.
panic(fmt.Errorf("config error: invalid log level '%s'", ls.Level))
}
return level
}
// CacheSettings holds cache configuration.
type CacheSettings struct {
Backend string
Expire int
Maxcount int
}
// HostsSettings holds hosts file configuration.
type HostsSettings struct {
Enable bool
HostsFile string `toml:"host-file"`
RedisEnable bool `toml:"redis-enable"`
RedisKey string `toml:"redis-key"`
TTL uint32 `toml:"ttl"`
RefreshInterval uint32 `toml:"refresh-interval"`
}
// BlocklistSettings holds blocklist configuration.
type BlocklistSettings struct {
Enable bool
Backend string
File string
WhitelistFile string `toml:"whitelist-file"`
RefreshInterval int `toml:"refresh-interval"`
RedisEnable bool `toml:"redis-enable"`
RedisKey string `toml:"redis-key"`
RedisWhitelistKey string `toml:"redis-whitelist-key"`
TTL uint32
}
// Global variable to hold the application's configuration.
// It is populated by the `init` function.
var settings Settings
// init is a special Go function that runs automatically before `main`.
// It is used here to parse command-line flags and load the configuration file.
func init() {
var configFile string
var verbose bool
// Define command-line flags for the configuration file path and verbosity.
flag.StringVar(&configFile, "c", "./etc/godns.conf", "Path to the godns toml-formatted config file")
flag.BoolVar(&verbose, "v", false, "Enable verbose logging (sets log level to DEBUG)")
// Parse the command-line flags.
flag.Parse()
// Decode the TOML configuration file into the global settings variable.
// We use a clean file path directly from the flag.
if _, err := toml.DecodeFile(configFile, &settings); err != nil {
// Use a more idiomatic and robust way to handle errors.
// fmt.Fprintf is used to print to standard error (os.Stderr).
// This separates log output from standard output, which is a good practice.
fmt.Fprintf(os.Stderr, "godns: toml decode file failed: %v\n", err)
os.Exit(1)
}
// Override the log level to DEBUG if the -v flag is set.
// This logic is preserved from the original code.
if verbose {
settings.Log.Level = "DEBUG"
}
}