This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquest.go
370 lines (335 loc) · 8.44 KB
/
quest.go
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package main
import (
"bytes"
"container/list"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"os/exec"
"os/signal"
"regexp"
"strings"
"sync"
"syscall"
"time"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/go-yaml/yaml"
"github.com/miekg/dns"
)
func main() {
confPath := flag.String("conf", "quest.yml", "configuration file")
flag.Parse()
conf, err := loadConfig(*confPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}
quest := Quest{conf: conf, cache: NewCache(conf.Server.CacheSize)}
err = quest.Run()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
type Quest struct {
conf Config
upstreams map[string]func() ([]upstream.Upstream, error)
cache *Cache
}
func msgKey(msg *dns.Msg) string {
q := msg.Question[0]
return q.String()
}
var errNotRuleMatch = fmt.Errorf("not rule match with")
func (q *Quest) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
start := time.Now()
domain := req.Question[0].Name
resp, u, cached, err := q.cache.GetOrCreate(req, func() (*dns.Msg, upstream.Upstream, error) {
rule, ok := q.findRule(domain)
if !ok {
log.Printf("W %s %s not rule match with\n", w.RemoteAddr(), req.Question[0].Name)
req.MsgHdr.Rcode = dns.RcodeServerFailure
w.WriteMsg(req)
return req, nil, errNotRuleMatch
}
allUpstreams := []upstream.Upstream{}
for _, addr := range rule.Resolvers {
upstreams, err := q.upstreams[addr]()
if err != nil {
return req, nil, err
}
for _, u := range upstreams {
log.Printf("D %s %s -> %s\n", w.RemoteAddr(), domain, u.Address())
allUpstreams = append(allUpstreams, u)
}
}
resp, u, err := upstream.ExchangeParallel(allUpstreams, req)
if err != nil {
log.Printf("E %s %s %s\n", w.RemoteAddr(), domain, err)
resp = req.Copy()
resp.Rcode = dns.RcodeServerFailure
} else {
if rule.IPSet != "" && hasIPSetCmd() {
for _, rr := range resp.Answer {
var ip string
switch rr := rr.(type) {
case *dns.A:
ip = rr.A.String()
case *dns.AAAA:
ip = rr.AAAA.String()
default:
continue
}
log.Printf("D %s %s +ipset add %s %s\n", w.RemoteAddr(), domain, rule.IPSet, ip)
err := ipsetAdd(rule.IPSet, ip)
if err != nil {
log.Printf("E %s %s ipset add: %s\n", w.RemoteAddr(), domain, err)
}
}
}
}
return resp, u, err
})
duration := time.Now().Sub(start).Round(time.Millisecond)
if err == nil {
if cached {
log.Printf("I %s %s <- %s %s (cached)\n", w.RemoteAddr(), domain, u.Address(), duration)
} else {
log.Printf("I %s %s <- %s %s\n", w.RemoteAddr(), domain, u.Address(), duration)
}
}
w.WriteMsg(resp)
}
func (q *Quest) findRule(domain string) (Rule, bool) {
for _, rule := range q.conf.Rules {
if len(rule.DomainSuffix) == 0 {
return rule, true
}
for _, suffix := range rule.DomainSuffix {
if strings.HasSuffix(domain, suffix) {
return rule, true
}
}
}
return Rule{}, false
}
func (q *Quest) Run() error {
log.Printf("I cahe size is %d\n", q.conf.Server.CacheSize)
q.upstreams = make(map[string]func() ([]upstream.Upstream, error))
for _, rule := range q.conf.Rules {
for _, addr := range rule.Resolvers {
if _, ok := q.upstreams[addr]; ok {
continue
}
var f func() ([]upstream.Upstream, error)
if strings.HasPrefix(addr, "resolvconf://") {
location := strings.TrimPrefix(addr, "resolvconf://")
f = (&resolvconf{location: location}).GetUpstreams
} else {
u, err := upstream.AddressToUpstream(addr, upstream.Options{Timeout: 15 * time.Second})
if err != nil {
return fmt.Errorf("failed to create upstream %s: %s", addr, err)
}
f = func(u upstream.Upstream) func() ([]upstream.Upstream, error) {
return func() ([]upstream.Upstream, error) {
return []upstream.Upstream{u}, nil
}
}(u)
}
q.upstreams[addr] = f
}
}
errs := make(chan error, len(q.conf.Server.Listeners))
servers := make([]*dns.Server, 0, len(q.conf.Server.Listeners))
for _, addr := range q.conf.Server.Listeners {
u, err := url.Parse(addr)
if err != nil {
return err
}
s := &dns.Server{Net: u.Scheme, Addr: u.Host, Handler: q}
go func(s *dns.Server) {
log.Println("I listen on", fmt.Sprintf("%s://%s", s.Net, s.Addr))
err := s.ListenAndServe()
errs <- err
}(s)
go func(s *dns.Server) {
s.Shutdown()
}(s)
servers = append(servers, s)
}
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
defer signal.Reset(os.Interrupt, syscall.SIGTERM)
defer close(stop)
go func() {
sig := <-stop
log.Println("I shutdown by", sig)
for _, s := range servers {
s.Shutdown()
}
}()
var err error
for _ = range q.conf.Server.Listeners {
err = <-errs
}
return err
}
type Rule struct {
DomainSuffix []string `yaml:"domain-suffix"`
Resolvers []string `yaml:"resolvers"`
IPSet string `yaml:"ipset"`
}
type Config struct {
Server struct {
Listeners []string `yaml:"listeners"`
CacheSize int `yaml:"cache-size"`
} `yaml:"server"`
Rules []Rule `yaml:"rules"`
}
func loadConfig(path string) (conf Config, err error) {
content, err := ioutil.ReadFile(path)
if err != nil {
err = fmt.Errorf("failed to load config: %s: %s", path, err)
return
}
err = yaml.Unmarshal(content, &conf)
if err != nil {
err = fmt.Errorf("failed to parse config: %s", err)
return
}
if len(conf.Server.Listeners) == 0 {
err = fmt.Errorf("config error: at least one listener are required")
return
}
if conf.Server.CacheSize <= 0 {
conf.Server.CacheSize = 1000
}
for _, rule := range conf.Rules {
if len(rule.Resolvers) == 0 {
err = fmt.Errorf("config err: every rule must has resolvers")
return
}
for i := range rule.DomainSuffix {
rule.DomainSuffix[i] = strings.TrimRight(rule.DomainSuffix[i], ".") + "."
}
}
return
}
var (
isIPSetCmd = 2
)
func hasIPSetCmd() bool {
if isIPSetCmd == 2 {
_, err := exec.Command("ipset", "version").CombinedOutput()
if err != nil {
log.Println("W ipset command not found")
isIPSetCmd = 0
} else {
isIPSetCmd = 1
}
}
return isIPSetCmd == 1
}
func ipsetAdd(setName, ip string) error {
cmd := exec.Command("ipset", "add", setName, ip)
out, err := cmd.CombinedOutput()
if err != nil {
if bytes.Contains(out, []byte("already added")) {
return nil
}
err = fmt.Errorf("ipset %s: %s", err, out)
return err
}
return nil
}
type Cache struct {
mu sync.Mutex
cacheSize int
items map[string]*cacheItem
lru *list.List
}
func NewCache(cacheSize int) *Cache {
return &Cache{cacheSize: cacheSize, items: make(map[string]*cacheItem), lru: list.New()}
}
func (c *Cache) GetOrCreate(req *dns.Msg, f func() (*dns.Msg, upstream.Upstream, error)) (*dns.Msg, upstream.Upstream, bool, error) {
msg, u, err := f()
return msg, u, false, err
}
func (c *Cache) expireLocked() {
now := time.Now()
for {
back := c.lru.Back()
if back == nil {
break
}
key := back.Value.(string)
item, ok := c.items[key]
if ok && item.expiredAt.Before(now) {
c.lru.Remove(back)
delete(c.items, key)
} else {
break
}
}
}
type cacheItem struct {
lruRef *list.Element
done chan error
msg *dns.Msg
u upstream.Upstream
err error
expiredAt time.Time
}
func (ci *cacheItem) Get() (*dns.Msg, upstream.Upstream, error) {
err := <-ci.done
return ci.msg, ci.u, err
}
type resolvconf struct {
location string
upstreams []upstream.Upstream
lastModify time.Time
}
var nsRegex = regexp.MustCompile(`nameserver\s+([\w.:]+)`)
func (r *resolvconf) GetUpstreams() ([]upstream.Upstream, error) {
stat, err := os.Stat(r.location)
if err != nil {
return nil, err
}
if r.lastModify.Before(stat.ModTime()) {
content, err := ioutil.ReadFile(r.location)
if err != nil {
return nil, err
}
text := removeComments(string(content))
var upstreams []upstream.Upstream
for _, match := range nsRegex.FindAllStringSubmatch(text, -1) {
addr := match[1]
u, err := upstream.AddressToUpstream(addr, upstream.Options{Timeout: 15 * time.Second})
if err != nil {
return nil, err
}
upstreams = append(upstreams, u)
}
r.upstreams = upstreams
}
if len(r.upstreams) == 0 {
return nil, errors.New("no upstream found in file://" + r.location)
}
return r.upstreams, nil
}
func removeComments(content string) string {
lines := strings.Split(content, "\n")
for i := 0; i < len(lines); i++ {
line := lines[i]
commentIdx := strings.Index(line, "#")
if commentIdx > -1 {
lines[0] = line[:commentIdx]
}
}
return strings.Join(lines, "\n")
}