This repository has been archived by the owner on Aug 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
249 lines (205 loc) · 5.12 KB
/
main.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
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/elmasy-com/columbus-server/db"
eldns "github.com/elmasy-com/elnet/dns"
"github.com/miekg/dns"
)
var (
ReplyChan chan *dns.Msg
Version string
Commit string
resolvers []string
resolversNum int32
)
func getRandomResolver() string {
if resolversNum == 1 {
return resolvers[0]
}
return resolvers[rand.Int31n(resolversNum)]
}
// isValidResponse checks the type and the content of m.
// If m indicates a valid reply, returns true.
// This function is needed to not rely on RCODE only.
func isValidResponse(m dns.RR) bool {
switch t := m.(type) {
case *dns.A:
return true
case *dns.AAAA:
return true
case *dns.CAA:
return true
case *dns.CNAME:
return true
case *dns.DNAME:
return true
case *dns.MX:
return true
case *dns.NS:
return true
case *dns.SOA:
return true
case *dns.SRV:
return true
case *dns.TXT:
return true
//case *dns.CERT:
// TODO: Implement more type
//return false
case *dns.PTR:
// PTR records are out of context
return false
default:
fmt.Fprintf(os.Stderr, "Unknown reply type: %T\n", t)
return false
}
}
// insertWorker is a goroutine.
// NumWorkers controls the number of workers.
func insertWorker(wg *sync.WaitGroup) {
defer wg.Done()
for r := range ReplyChan {
switch {
case len(r.Question) < 1:
fmt.Fprintf(os.Stderr, "Error: question section is empty\n")
continue
case len(r.Answer) < 1:
fmt.Fprintf(os.Stderr, "Error: message answer is empty\n")
continue
case len(r.Question) > 1:
fmt.Fprintf(os.Stderr, "Error: multiple question\n")
continue
case !isValidResponse(r.Answer[0]):
// Further check requires, see: https://community.cloudflare.com/t/noerror-response-for-not-exist-domain-breaks-nslookup/173897
continue
}
wc, err := eldns.IsWildcard(r.Question[0].Name, r.Question[0].Qtype)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to check if %s is wildcard: %s\n", r.Question[0].Name, err)
continue
}
if wc {
continue
}
ni, err := db.Insert(r.Question[0].Name)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to insert %s: %s\n", r.Question[0].Name, err)
continue
}
if ni {
fmt.Printf("New domain inserted: %s\n", r.Question[0].Name)
}
err = db.RecordsUpdate(r.Question[0].Name, true, false)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to update records for %s: %s\n", r.Question[0].Name, err)
}
}
}
// Return whether r.Question is ANY type.
func isQuestionAny(r *dns.Msg) bool {
// Is ANY?
for i := range r.Question {
if r.Question[i].Qtype == dns.TypeANY {
return true
}
}
return false
}
func handleFunc(w dns.ResponseWriter, q *dns.Msg) {
start := time.Now()
// Refuse ANY questions
if isQuestionAny(q) {
q.MsgHdr.Response = true
q.MsgHdr.Rcode = dns.RcodeRefused
w.WriteMsg(q)
return
}
r, err := dns.Exchange(q, getRandomResolver())
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to exchange message: %s\n", err)
q.MsgHdr.Response = true
q.MsgHdr.Rcode = dns.RcodeServerFailure
w.WriteMsg(q)
return
}
if r == nil {
fmt.Fprintf(os.Stderr, "Error: reply is nil\n")
q.MsgHdr.Response = true
q.MsgHdr.Rcode = 2
w.WriteMsg(q)
return
}
if r.Rcode == dns.RcodeSuccess && len(ReplyChan) < cap(ReplyChan) {
ReplyChan <- r
}
err = w.WriteMsg(r)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to write reply: %s\n", err)
}
fmt.Printf("%s -> %s %s %s %s %s\n",
w.RemoteAddr().String(),
q.Question[0].Name,
dns.ClassToString[q.Question[0].Qclass],
dns.TypeToString[q.Question[0].Qtype],
dns.RcodeToString[r.Rcode],
time.Since(start))
}
func main() {
configPath := flag.String("config", "", "Path to the config file")
printVersion := flag.Bool("version", false, "Print version")
flag.Parse()
// Print version and exit
if *printVersion {
fmt.Printf("Version: %s\n", Version)
fmt.Printf("Git Commit: %s\n", Commit)
os.Exit(0)
}
if *configPath == "" {
fmt.Fprintf(os.Stderr, "-config is empty!\n")
fmt.Printf("Use -help for help\n")
os.Exit(1)
}
conf, err := parseConfig(*configPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse config file: %s\n", err)
os.Exit(1)
}
// Set global resolvers to get random ones
resolvers = conf.Resolvers
resolversNum = int32(len(resolvers))
// Create buff channel
ReplyChan = make(chan *dns.Msg, conf.BuffSize)
// Connect to MongoDB
fmt.Printf("Connecting to MongoDB...\n")
err = db.Connect(conf.MongoURI)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to connect to MongoDB: %s\n", err)
os.Exit(1)
}
defer db.Disconnect()
fmt.Printf("Starting %d workers...\n", conf.NumWorkers)
// Start workers
wg := sync.WaitGroup{}
for i := 0; i < conf.NumWorkers; i++ {
wg.Add(1)
go insertWorker(&wg)
}
stopSignal := make(chan os.Signal, 1)
signal.Notify(stopSignal, os.Interrupt, syscall.SIGTERM)
udpServer := UDPStart(conf.ListenAddress, stopSignal)
tcpServer := TCPStart(conf.ListenAddress, stopSignal)
// Wait for the SIGTERM
<-stopSignal
fmt.Printf("Caught a SIGTERM, closing...\n")
udpServer.Shutdown()
tcpServer.Shutdown()
close(ReplyChan)
wg.Wait()
}