-
Notifications
You must be signed in to change notification settings - Fork 5
/
dns.go
113 lines (100 loc) · 2.55 KB
/
dns.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
package main
import (
"fmt"
"log"
"strconv"
"github.com/miekg/dns"
)
func answerQuery(m *dns.Msg) {
for _, q := range m.Question {
switch q.Qtype {
case dns.TypeA:
answerQuestionType(m, q, "A", *publicIP, "127.0.0.1")
case dns.TypeAAAA:
// If this isn't set, and a query doesn't have an A type question that we answer here,
// the query will be forwarded upstream which might not be the result we intended.
if mitmAAAA != nil {
answerQuestionType(m, q, "AAAA", *mitmAAAA, "::1")
}
}
}
}
func answerQuestionType(m *dns.Msg, q dns.Question, _type, mitmHost, blockHost string) {
host := q.Name[:len(q.Name)-1]
// Check if question is for the info host or on unpaywall list
options := getHostOptions(host)
if compareBase(host, config.InfoHost) || options != nil {
record := fmt.Sprintf("%s %s %s", q.Name, _type, mitmHost)
rr, err := dns.NewRR(record)
if err != nil {
log.Println("[ERR]", err)
return
}
m.Answer = append(m.Answer, rr)
return
}
// Check if host is on blocklist
for _, blocked := range blockList {
if host == blocked {
record := fmt.Sprintf("%s %s %s", q.Name, _type, blockHost)
rr, err := dns.NewRR(record)
if err != nil {
log.Println("[ERR]", err)
continue
}
m.Answer = append(m.Answer, rr)
break
}
}
}
func handleDnsRequest(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
m.Compress = false
switch req.Opcode {
case dns.OpcodeQuery:
answerQuery(m)
}
// If we dont know what to do forward request to upstream dns
if len(m.Answer) == 0 {
c := &dns.Client{Net: "udp"}
res, _, err := c.Exchange(req, config.UpstreamDNS)
if err != nil {
dns.HandleFailed(w, req)
log.Println("[ERR]", err)
return
}
w.WriteMsg(res)
return
}
log.Printf("[DNS] Response: %s", m.Answer)
w.WriteMsg(m)
}
func serveDNS() {
// attach request handler func
dns.HandleFunc(".", handleDnsRequest)
// start server
go func() {
log.Printf("[DNS] Listening on 0.0.0.0:%d(udp only)", *dnsPort)
server := &dns.Server{Addr: ":" + strconv.Itoa(*dnsPort), Net: "udp"}
err := server.ListenAndServe()
defer server.Shutdown()
if err != nil {
log.Fatalf("[ERR] Failed to start DNS server: %s\n ", err.Error())
}
}()
}
func serveDNSoverTLS() error {
log.Printf("[DNS-TLS] Listening on %s:%d(tcp/tls)", *dotDomain, *dnsTlsPort)
server := &dns.Server{
Addr: ":" + strconv.Itoa(*dnsTlsPort),
Net: "tcp-tls",
TLSConfig: tlsDoTServerConfig,
}
err := server.ListenAndServe()
defer server.Shutdown()
if err != nil {
return err
}
return nil
}