-
Notifications
You must be signed in to change notification settings - Fork 0
/
mserver.go
254 lines (220 loc) · 6.1 KB
/
mserver.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
package mserver
import (
"fmt"
"log"
"net"
"os"
"path/filepath"
"strings"
"time"
"context"
"github.com/fsnotify/fsnotify"
"github.com/go-redis/redis/v8"
"github.com/miekg/dns"
"github.com/patrickmn/go-cache"
)
var serverurl string
var c *cache.Cache
var question *cache.Cache
var redisdb *redis.Client
var ctx context.Context
var resolvconf string
// For blocklist
var blocklist map[string]bool
// We will read and fill up our blocklist
func fillBlockList() {
blocklist = make(map[string]bool)
var filenames []string
// Find all the files in the blocklists directory
filepath.Walk("blocklists", func(path string, info os.FileInfo, err error) error {
// Need files ready for simple loading to our tool
if !info.IsDir() && strings.Contains(path, "clean-") && strings.HasSuffix(path, ".txt") {
filenames = append(filenames, path)
}
return nil
})
for _, filename := range filenames {
data, err := os.ReadFile(filename)
if err == nil {
lines := strings.Split(string(data), "\n")
for _, line := range lines {
if len(line) > 0 {
blocklist[line+"."] = true
}
}
fmt.Println("Loaded:", filename)
} else {
fmt.Println("Error reading blocklist")
}
}
}
// This function monitors the /etc/resolv.conf for any change
func watchDNSConfiguration(watcher fsnotify.Watcher) {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
//log.Println("event:", event)
if event.Has(fsnotify.Write) || event.Has(fsnotify.Remove) {
// Now first read and see if any modification
data, err := os.ReadFile("/etc/resolv.conf")
if err == nil {
if string(data) != resolvconf {
// Now we have a different file on disk, so rewrite
file, err := os.Create("/etc/resolv.conf")
if err != nil {
log.Panic("Can not write the file.")
}
defer file.Close()
file.WriteString(resolvconf)
}
} else {
fmt.Println("We could not read /etc/resolv.conf")
log.Println(err)
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}
func Listen(port int, serveraddr string) {
resolvconf = `# Generated by cdns
nameserver 127.0.0.1
`
// Now we will monitor the /etc/resolv.conf
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
err = watcher.Add("/etc/resolv.conf")
if err != nil {
log.Fatal(err)
}
go watchDNSConfiguration(*watcher)
// End of monitoring code
// Let us read all the blocklists
fillBlockList()
serverurl = serveraddr
serveMux := dns.NewServeMux()
serveMux.HandleFunc(".", func(w dns.ResponseWriter, req *dns.Msg) {
handleRequest(w, req)
})
c = cache.New(5*time.Minute, 10*time.Minute)
question = cache.New(2*time.Second, 2*time.Minute)
// Now for redis
ctx = context.Background()
// TODO: Get this from configuration file
redisdb = redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
Password: "", // no password set
DB: 0, // use default DB
})
server := &dns.Server{Addr: fmt.Sprintf("0.0.0.0:%d", port), Net: "udp", Handler: serveMux}
err = server.ListenAndServe()
if err != nil {
fmt.Fprintf(os.Stderr, "Error while starting the server: %s\n", err)
os.Exit(127)
}
}
func handleRequest(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
if r.MsgHdr.Opcode == dns.OpcodeQuery {
if len(r.Question) > 0 {
// Record the query
go recordQuery(r)
// Check if blocklisted domain
blocked := blocklist[r.Question[0].Name]
if !blocked {
// First check the cache
res, found := c.Get(r.Question[0].String())
if found {
//fmt.Printf("From cache: %+v\n", r.Question)
m.Answer = append(m.Answer, res.(dns.Msg).Answer...)
m.Ns = append(m.Ns, res.(dns.Msg).Ns...)
m.Extra = append(m.Extra, res.(dns.Msg).Extra...)
w.WriteMsg(m)
return
}
// Check if we already asked and waiting for an Answer
_, found = question.Get(r.Question[0].String())
if found {
// Then don't ask again, fail happily
// TODO: I don't know how bad is this idea :)
// Set NXDOMAIN
m.SetRcode(r, dns.RcodeNameError)
w.WriteMsg(m)
return
}
}
// Now do a real query
d := net.Dialer{Timeout: 500 * time.Millisecond}
conn, err := d.Dial("udp", serverurl)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
return
}
defer conn.Close()
// Now we record that we asked this question
question.Set(r.Question[0].String(), true, cache.DefaultExpiration)
//fmt.Printf("%+v\n", r.Question)
dnsConn := &dns.Conn{Conn: conn}
if err = dnsConn.WriteMsg(r); err != nil {
// Set NXDOMAIN
m.SetRcode(r, dns.RcodeNameError)
w.WriteMsg(m)
fmt.Fprintf(os.Stderr, "Error while talking to the server %s\n", err)
return
}
resp, err := dnsConn.ReadMsg()
if err == nil {
// First delete from the question
question.Delete(r.Question[0].String())
// Now if the domain is not blocked then return real answer
if !blocked {
m.Answer = append(m.Answer, resp.Answer...)
m.Ns = append(m.Ns, resp.Ns...)
m.Extra = append(m.Extra, resp.Extra...)
} else {
// Means blocked domain, so return NXDOMAIN
m.SetRcode(r, dns.RcodeNameError)
}
// We need to record the real IP address anyway
if len(resp.Answer) > 0 {
//fmt.Printf("TTL: %+v\n", resp.Answer[0].Header().Ttl)
c.Set(r.Question[0].String(), *resp, cache.DefaultExpiration)
go pushToRedis(r, resp)
}
}
}
}
w.WriteMsg(m)
}
func pushToRedis(r *dns.Msg, answer *dns.Msg) {
// For each IP, record the DNS name
for _, ans := range answer.Answer {
if ip, ok := ans.(*dns.A); ok {
rname := fmt.Sprintf("ip:%s", ip.A.String())
redisdb.SAdd(ctx, rname, r.Question[0].Name)
}
if ip, ok := ans.(*dns.AAAA); ok {
rname := fmt.Sprintf("ip:%s", ip.AAAA.String())
redisdb.SAdd(ctx, rname, r.Question[0].Name)
}
}
}
func recordQuery(r *dns.Msg) {
name := r.Question[0].Name
qtype := r.Question[0].Qtype
now := time.Now()
value := now.Unix()
entry := fmt.Sprintf("%s:%d:%d", name, qtype, value)
redisdb.RPush(ctx, "cdns:queries", entry)
}