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 pathhandler.go
More file actions
247 lines (217 loc) · 6.52 KB
/
handler.go
File metadata and controls
247 lines (217 loc) · 6.52 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
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
package godns
import (
"time"
"github.com/miekg/dns"
"github.com/ProxyFi/GoDNS/features/blocklist"
"github.com/ProxyFi/GoDNS/internal/log"
)
const (
notIPQuery = 0
_IP4Query = 4
_IP6Query = 6
)
// Question represents a DNS query question.
type Question struct {
qname string
qtype string
qclass string
}
// String returns a string representation of the Question.
func (q *Question) String() string {
return q.qname + " " + q.qclass + " " + q.qtype
}
// GODNSHandler is the main DNS handler.
type GODNSHandler struct {
resolver *Resolver
cache, negCache Cache
hosts Hosts
// blocklist holds the block and allow lists.
blocklist blocklist.Blocklist
}
// NewHandler creates a new GODNSHandler instance and initializes its components.
func NewHandler() *GODNSHandler {
var (
cacheConfig CacheSettings
resolver *Resolver
cache, negCache Cache
err error // Add a variable to handle the error from NewResolver
)
// Initialize resolver. Check for errors from the new function signature.
resolver, err = NewResolver(settings.ResolvConfig)
if err != nil {
log.Error("Failed to create resolver: %s", err)
// Depending on the application's needs, you might want to panic here
// or return a nil handler and handle the error in the caller.
// For a server application, panicking on a critical initialization error is common.
panic(err)
}
// Initialize cache and negative cache
cacheConfig = settings.Cache
switch cacheConfig.Backend {
case "memory":
cache = &MemoryCache{
Backend: make(map[string]Mesg, cacheConfig.Maxcount),
Expire: time.Duration(cacheConfig.Expire) * time.Second,
Maxcount: cacheConfig.Maxcount,
}
negCache = &MemoryCache{
Backend: make(map[string]Mesg),
Expire: time.Duration(cacheConfig.Expire) * time.Second / 2,
Maxcount: cacheConfig.Maxcount,
}
case "memcache":
cache = NewMemcachedCache(
settings.Memcache.Servers,
int32(cacheConfig.Expire))
negCache = NewMemcachedCache(
settings.Memcache.Servers,
int32(cacheConfig.Expire/2))
case "redis":
cache = NewRedisCache(
settings.Redis,
int64(cacheConfig.Expire))
negCache = NewRedisCache(
settings.Redis,
int64(cacheConfig.Expire/2))
default:
cache = &MemoryCache{
Backend: make(map[string]Mesg, cacheConfig.Maxcount),
Expire: time.Duration(cacheConfig.Expire) * time.Second,
Maxcount: cacheConfig.Maxcount,
}
negCache = &MemoryCache{
Backend: make(map[string]Mesg),
Expire: time.Duration(cacheConfig.Expire) * time.Second / 2,
Maxcount: cacheConfig.Maxcount,
}
}
// Initialize hosts module
hosts := NewHosts(settings.Hosts, settings.Redis)
// Initialize blocklist module
blocklistConfig := &blocklist.Config{
Enable: settings.Blocklist.Enable,
Backend: settings.Blocklist.Backend,
File: settings.Blocklist.File,
WhitelistFile: settings.Blocklist.WhitelistFile,
RefreshInterval: settings.Blocklist.RefreshInterval,
RedisEnable: settings.Blocklist.RedisEnable,
RedisKey: settings.Blocklist.RedisKey,
RedisWhitelistKey: settings.Blocklist.RedisWhitelistKey,
RedisSettings: settings.Redis,
}
blocklist := blocklist.NewBlocklist(blocklistConfig)
return &GODNSHandler{resolver: resolver, cache: cache, negCache: negCache, hosts: hosts, blocklist: blocklist}
}
// DoTCP handles DNS queries over TCP.
func (h *GODNSHandler) DoTCP(w dns.ResponseWriter, req *dns.Msg) {
h.Do("tcp", w, req)
}
// DoUDP handles DNS queries over UDP.
func (h *GODNSHandler) DoUDP(w dns.ResponseWriter, req *dns.Msg) {
h.Do("udp", w, req)
}
// Do performs the DNS query handling logic.
func (h *GODNSHandler) Do(Net string, w dns.ResponseWriter, req *dns.Msg) {
// Only handle A, AAAA, MX and CNAME question
if len(req.Question) == 0 || req.Question[0].Qtype != dns.TypeA && req.Question[0].Qtype != dns.TypeAAAA && req.Question[0].Qtype != dns.TypeMX && req.Question[0].Qtype != dns.TypeCNAME {
dns.HandleFailed(w, req)
return
}
q := req.Question[0]
Q := Question{UnFqdn(q.Name), dns.Type(q.Qtype).String(), dns.Class(q.Qclass).String()}
// --- Blocklist check ---
domain := UnFqdn(q.Name)
if h.blocklist.IsBlocked(domain) {
log.Debug("Domain %s is blocked, returning NXDOMAIN", domain)
m := new(dns.Msg)
m.SetReply(req)
m.SetRcode(req, dns.RcodeNameError) // NXDOMAIN
w.WriteMsg(m)
return
}
// --- End blocklist check ---
if settings.Hosts.Enable {
if ips, ok := h.hosts.Get(Q.qname, h.getQuestionType(q)); ok {
mesg := new(dns.Msg)
mesg.SetReply(req)
if q.Qtype == dns.TypeMX {
mesg.Authoritative = true
rr := new(dns.MX)
rr.Hdr = dns.RR_Header{Name: dns.Fqdn(Q.qname), Rrtype: dns.TypeMX, Class: dns.ClassINET, Ttl: settings.Hosts.TTL}
rr.Preference = 10
rr.Mx = "mail." + dns.Fqdn(Q.qname)
mesg.Answer = []dns.RR{rr}
} else {
mesg.Authoritative = true
var records []dns.RR
for _, ip := range ips {
if ip.To4() != nil {
rr := new(dns.A)
rr.Hdr = dns.RR_Header{Name: dns.Fqdn(Q.qname), Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: settings.Hosts.TTL}
rr.A = ip
records = append(records, rr)
} else {
rr := new(dns.AAAA)
rr.Hdr = dns.RR_Header{Name: dns.Fqdn(Q.qname), Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: settings.Hosts.TTL}
rr.AAAA = ip
records = append(records, rr)
}
}
mesg.Answer = records
}
w.WriteMsg(mesg)
return
}
}
key := KeyGen(Q)
mesg, err := h.cache.Get(key)
if err != nil {
if mesg, err = h.negCache.Get(key); err != nil {
log.Debug("%s didn't hit cache", Q.String())
} else {
log.Debug("%s hit negative cache", Q.String())
dns.HandleFailed(w, req)
return
}
} else {
log.Debug("%s hit cache", Q.String())
// we need this copy against concurrent modification of Id
msg := *mesg
msg.Id = req.Id
w.WriteMsg(&msg)
return
}
mesg, err = h.resolver.Lookup(Net, req)
if err != nil {
log.Warn("Resolve query error %s", err)
dns.HandleFailed(w, req)
// cache the failure, too!
if err = h.negCache.Set(key, nil); err != nil {
log.Warn("Set %s negative cache failed: %v", Q.String(), err)
}
return
}
w.WriteMsg(mesg)
if len(mesg.Answer) > 0 {
err = h.cache.Set(key, mesg)
if err != nil {
log.Warn("Set %s cache failed: %s", Q.String(), err.Error())
}
log.Debug("Insert %s into cache", Q.String())
}
}
func (h *GODNSHandler) getQuestionType(q dns.Question) int {
switch q.Qtype {
case dns.TypeA:
return _IP4Query
case dns.TypeAAAA:
return _IP6Query
}
return notIPQuery
}
func UnFqdn(s string) string {
if dns.IsFqdn(s) {
return s[:len(s)-1]
}
return s
}