-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwoozle.go
210 lines (185 loc) · 5.18 KB
/
woozle.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
// Copyright 2015 John Kelley. All rights reserved.
// This source code is licensed under the Simplified BSD License
// Woozle is a simple DNS recursor with the ability to filter out
// certain queries. The author finds this useful for forcing Youtube
// over IPv4 instead of his Hurricane Electric IPv6 tunnel
package main
import (
"fmt"
"github.com/miekg/dns"
"os"
"os/signal"
"sort"
"syscall"
"strings"
"time"
)
// ================================================
// Configurables - TODO: move to CLI arg processing
// ================================================
const upstreamDNS = "10.10.10.1:53"
var filterDomainAAAA = []string{ "youtube.com.", "googlevideo.com." }
// ================================================
// Globals for tracking stats and caching
// ================================================
type DNSQuery struct {
domain string
queryType string
filtered bool
}
type DomainStats struct {
domain string
frequency int
filtered int
queries map[string]int
}
var statPipe chan DNSQuery
var stats = map[string]*DomainStats{}
type PStats *DomainStats
type ByFreq []*DomainStats
var sortedStats = make(ByFreq, 100)[0:0]
func (s ByFreq) Len() int {
return len(s)
}
func (s ByFreq) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByFreq) Less(i, j int) bool {
// fmt.Printf("len: %d. %i > %j\n", len(s), i, j)
return s[i].frequency > s[j].frequency
}
func (s ByFreq) Append(stats *DomainStats) ByFreq {
l := len(s)
// fmt.Printf("len: %d, cap %d\n", l, cap(s))
if l + 1 > cap(s) {
newSlice := make(ByFreq, l*2)
copy(newSlice, s)
s = newSlice
}
s = s[0:l+1]
s[l] = stats
return s
}
var totalQueries = 0
var timeStarted time.Time
// ================================================
// Helper functions
// ================================================
func serve(net string) {
server := &dns.Server{Addr: ":53", Net: net, TsigSecret: nil}
err := server.ListenAndServe()
if err != nil {
fmt.Printf("Failed to setup the "+net+" server: %s\n", err.Error())
os.Exit(1)
}
}
func getRootFromDomain(domain string) string {
var root string
components := strings.Split(domain, ".")
idx := len(components)
if idx > 2 {
root = components[idx-3] + "." + components[idx-2]
}
return root
}
func dispStats() {
fmt.Printf("\nQuery Statistics:\n")
// print up to top 10 domains
l := len(sortedStats)
if l > 10 {
l = 10
}
for i := 0; i < l; i++ {
fmt.Printf("%25s: %3d queries", sortedStats[i].domain, sortedStats[i].frequency)
if sortedStats[i].filtered > 0 {
fmt.Printf(", %3d dropped", sortedStats[i].filtered)
}
fmt.Println()
}
}
// ================================================
// Meat and potatoes
// ================================================
func handleRecurse(w dns.ResponseWriter, m *dns.Msg) {
// collect stats
statPipe <- DNSQuery{m.Question[0].Name, dns.TypeToString[m.Question[0].Qtype], false}
// pass the query on to the upstream DNS server
c := new(dns.Client)
r, _, e := c.Exchange(m, upstreamDNS)
if e != nil {
fmt.Printf("%s retrying query for '%s'\n", time.Now().Format("01/02 15:04"), m.Question[0].Name);
time.Sleep(time.Millisecond * 10);
r, _, e = c.Exchange(m, upstreamDNS)
if e != nil {
fmt.Printf("%s Client query failed for '%s': %s\n", time.Now().Format("01/02 15:04"), m.Question[0].Name, e.Error())
return
}
}
w.WriteMsg(r)
}
func filterAAAA(w dns.ResponseWriter, r *dns.Msg) {
if r.Question[0].Qtype == dns.TypeAAAA {
// collect stats
statPipe <- DNSQuery{r.Question[0].Name, "AAAA", true}
// send a blank reply
m := new(dns.Msg)
m.SetReply(r)
w.WriteMsg(m)
} else {
handleRecurse(w, r)
}
}
func handleStats(queryChan <-chan DNSQuery) {
var domain string
for query := range queryChan {
totalQueries++
domain = getRootFromDomain(query.domain)
if nil == stats[domain] {
stats[domain] = &DomainStats{domain:domain}
stats[domain].queries = make(map[string]int)
sortedStats = sortedStats.Append(stats[domain])
}
stats[domain].frequency++
if query.filtered {
stats[domain].filtered++
}
stats[domain].queries[query.queryType] += 1
if stats[domain].frequency > 1 {
sort.Sort(sortedStats)
}
//fmt.Printf("%s type %s (%d, %d)\n", query.domain, query.queryType, stats[query.domain].frequency, stats[query.domain].queries[query.queryType])
}
fmt.Printf("stats engine stopping\n");
}
func main() {
// init update
timeStarted = time.Now()
lastSigInt := timeStarted.Add(time.Second * -31)
// setup and kickoff stats collection
statPipe = make(chan DNSQuery, 10)
go handleStats(statPipe)
// handler for filtering ipv6 AAAA records
for _, domain := range filterDomainAAAA {
dns.HandleFunc(domain, filterAAAA)
}
// default handler
dns.HandleFunc(".", handleRecurse)
// start server(s)
//go serve("tcp")
go serve("udp")
// handle signals
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
forever:
for s := range sig {
fmt.Printf("\nUptime %s, %d queries performed", time.Since(timeStarted).String(), totalQueries)
if time.Since(lastSigInt).Seconds() < 30 {
fmt.Printf("\nSignal (%d) received, stopping\n", s)
break forever
} else {
fmt.Printf(", send SIGINT again within 30s to quit\n")
lastSigInt = time.Now();
dispStats()
}
}
}