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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
package config
import (
"flag"
"fmt"
"github.com/BurntSushi/toml"
)
type Config struct {
Server ServerConfig `toml:"server"`
Cache CacheConfig `toml:"cache"`
Resolver ResolverConfig `toml:"resolver"`
Blocklist BlocklistConfig `toml:"blocklist"`
Admin AdminConfig `toml:"admin"`
ACL ACLConfig `toml:"acl"`
Log LogConfig `toml:"log"`
}
type ServerConfig struct {
ListenUDP string `toml:"listen_udp"`
ListenTCP string `toml:"listen_tcp"`
ListenDOH string `toml:"listen_doh"`
}
type CacheConfig struct {
MaxEntries int `toml:"max_entries"`
DBPath string `toml:"db_path"`
}
type ResolverConfig struct {
Mode string `toml:"mode"`
Timeout string `toml:"timeout"`
MaxDelegations int `toml:"max_delegations"`
Forwarders []string `toml:"forwarders"`
}
type BlocklistConfig struct {
Response string `toml:"response"`
Files []string `toml:"files"`
URLs []string `toml:"urls"`
}
type LogConfig struct {
Level string `toml:"level"`
}
type CLIFlags struct {
Config string
LogLevel string
ListenUDP string
ListenTCP string
ListenDOH string
}
type ACLConfig struct {
Allow []string `toml:"allow"`
RateLimitQPS int `toml:"rate_limit_qps"`
RateLimitBurst int `toml:"rate_limit_burst"`
}
type AdminConfig struct {
Listen string `toml:"listen"`
}
func ParseFlags() CLIFlags {
var f CLIFlags
flag.StringVar(&f.Config, "config", "linum.toml", "path to config file")
flag.StringVar(&f.LogLevel, "loglevel", "", "log level (debug|info|warn|error)")
flag.StringVar(&f.ListenUDP, "udp", "", "UDP listen address")
flag.StringVar(&f.ListenTCP, "tcp", "", "TCP listen address")
flag.StringVar(&f.ListenDOH, "doh", "", "DoH listen address")
flag.Parse()
return f
}
func Default() Config {
return Config{
Server: ServerConfig{
ListenUDP: ":5353",
ListenTCP: ":5353",
ListenDOH: ":8443",
},
Cache: CacheConfig{
MaxEntries: 100000,
},
Resolver: ResolverConfig{
Mode: "recursive",
Timeout: "2s",
MaxDelegations: 30,
},
Blocklist: BlocklistConfig{
Response: "zero_ip",
},
Log: LogConfig{
Level: "info",
},
Admin: AdminConfig{
Listen: "127.0.0.1:8080",
},
ACL: ACLConfig{
Allow: []string{},
RateLimitQPS: 50,
RateLimitBurst: 10,
},
}
}
func LoadFile(path string) (Config, error) {
var cfg Config
_, err := toml.DecodeFile(path, &cfg)
return cfg, err
}
func Merge(dst, src Config) Config {
if src.Server.ListenUDP != "" {
dst.Server.ListenUDP = src.Server.ListenUDP
}
if src.Server.ListenTCP != "" {
dst.Server.ListenTCP = src.Server.ListenTCP
}
if src.Server.ListenDOH != "" {
dst.Server.ListenDOH = src.Server.ListenDOH
}
if src.Cache.MaxEntries > 0 {
dst.Cache.MaxEntries = src.Cache.MaxEntries
}
if src.Cache.DBPath != "" {
dst.Cache.DBPath = src.Cache.DBPath
}
if src.Resolver.Timeout != "" {
dst.Resolver.Timeout = src.Resolver.Timeout
}
if src.Resolver.MaxDelegations > 0 {
dst.Resolver.MaxDelegations = src.Resolver.MaxDelegations
}
if src.Resolver.Mode != "" {
dst.Resolver.Mode = src.Resolver.Mode
}
if len(src.Resolver.Forwarders) > 0 {
dst.Resolver.Forwarders = src.Resolver.Forwarders
}
if src.Blocklist.Response != "" {
dst.Blocklist.Response = src.Blocklist.Response
}
if len(src.Blocklist.Files) > 0 {
dst.Blocklist.Files = src.Blocklist.Files
}
if len(src.Blocklist.URLs) > 0 {
dst.Blocklist.URLs = src.Blocklist.URLs
}
if src.Log.Level != "" {
dst.Log.Level = src.Log.Level
}
if src.ACL.Allow != nil {
dst.ACL.Allow = src.ACL.Allow
}
if src.ACL.RateLimitQPS != 0 {
dst.ACL.RateLimitQPS = src.ACL.RateLimitQPS
}
if src.ACL.RateLimitBurst != 0 {
dst.ACL.RateLimitBurst = src.ACL.RateLimitBurst
}
if src.Admin.Listen != "" {
dst.Admin.Listen = src.Admin.Listen
}
return dst
}
func (f CLIFlags) Apply(cfg Config) Config {
if f.ListenUDP != "" {
cfg.Server.ListenUDP = f.ListenUDP
}
if f.ListenTCP != "" {
cfg.Server.ListenTCP = f.ListenTCP
}
if f.ListenDOH != "" {
cfg.Server.ListenDOH = f.ListenDOH
}
if f.LogLevel != "" {
cfg.Log.Level = f.LogLevel
}
return cfg
}
func (c Config) Validate() error {
switch c.Blocklist.Response {
case "zero_ip", "nxdomain", "":
default:
return fmt.Errorf("invalid blocklist response %q (want zero_ip or nxdomain)", c.Blocklist.Response)
}
switch c.ACL.Allow {
default:
}
switch c.Resolver.Mode {
case "recursive", "forward", "":
// nothing happened lol
default:
return fmt.Errorf("invalid resolver mode %q (recursive or forward)", c.Resolver.Mode)
}
if c.Resolver.Mode == "forward" && len(c.Resolver.Forwarders) == 0 {
return fmt.Errorf("resolver mode=forward requires at least one forwarder")
}
return nil
}
|