-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhandler.go
More file actions
92 lines (83 loc) · 2.42 KB
/
handler.go
File metadata and controls
92 lines (83 loc) · 2.42 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
package main
import (
"github.com/miekg/dns"
"go.uber.org/zap"
"net"
"time"
)
// Handler represents how DNS requests be handled
type Handler struct {
Upstreams map[string]Upstream
Rules []Rule
}
// ServeDNS actually handle the DNS requests
func (handler *Handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
defer w.Close()
isMatched := false
ruleSearchStartTime := time.Now()
// log fields
fields := make([]zap.Field, 6)
fields[0] = zap.String("from", w.RemoteAddr().Network()+"://"+w.RemoteAddr().String())
fields[1] = zap.String("to", w.LocalAddr().Network()+"://"+w.LocalAddr().String())
fields[3] = zap.String("question", r.Question[0].String())
fields[5] = zap.Uint16("id", r.Id)
// find in cache
if msg, found := GetCache(r.Question[0].String(), r.Id); found {
fields[2] = zap.String("upstream", "cache")
fields[4] = zap.Duration("searchtime", time.Since(ruleSearchStartTime))
zap.L().Named("query").Info("routing request", fields[:]...)
w.WriteMsg(msg)
return
}
// find in rules
for _, rule := range handler.Rules {
if rule.Upstream() == nil && r.Question[0].Qtype != dns.TypeA {
continue
}
if rule.Matches(r.Question[0].Name) {
isMatched = true
if rule.Upstream() == nil {
fields[2] = zap.String("upstream", "static")
} else {
fields[2] = zap.String("upstream", rule.Upstream().Name())
}
fields[4] = zap.Duration("searchtime", time.Since(ruleSearchStartTime))
zap.L().Named("query").Info("routing request", fields[:]...)
query(rule, w, r)
break
}
}
if !isMatched {
fields[2] = zap.String("upstream", "nil")
fields[4] = zap.Duration("searchtime", time.Since(ruleSearchStartTime))
zap.L().Named("query").Info("routing request", fields[:]...)
}
}
func query(r Rule, w dns.ResponseWriter, req *dns.Msg) {
if len(req.Question) > 1 {
zap.L().Debug("question number > 1", zap.Int("length", len(req.Question))) // what
}
if r.Upstream() != nil {
r.Upstream().Query(w, req)
return
}
respMsg := &dns.Msg{}
respMsg.SetReply(req)
switch req.Question[0].Qtype {
case dns.TypeA:
respMsg.Authoritative = true
domain := req.Question[0].Name
respMsg.Answer = append(respMsg.Answer, &dns.A{
Hdr: dns.RR_Header{
Name: domain,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 60,
},
A: net.ParseIP(r.StaticResult()),
})
w.WriteMsg(respMsg)
default:
zap.L().Named("query").Error("static type request qtype must be A at this time")
}
}