-
Notifications
You must be signed in to change notification settings - Fork 179
/
dns.go
163 lines (126 loc) · 3.12 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
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
package router
import (
"context"
"fmt"
"net"
"os"
"strings"
"github.com/miekg/dns"
)
type DNS struct {
internal bool
mux *dns.ServeMux
router DNSRouter
server *dns.Server
upstream string
}
type DNSRouter interface {
RouterIP(internal bool) string
TargetList(host string) ([]string, error)
Upstream() (string, error)
}
func NewDNS(conn net.PacketConn, router DNSRouter, internal bool) (*DNS, error) {
mux := dns.NewServeMux()
d := &DNS{
internal: internal,
mux: mux,
router: router,
server: &dns.Server{
PacketConn: conn,
Handler: mux,
},
upstream: "1.1.1.1:53",
}
us, err := router.Upstream()
if err != nil {
return nil, err
}
d.upstream = us
fmt.Printf("ns=dns at=new upstream=%s\n", d.upstream)
mux.Handle(".", d)
return d, nil
}
func (d *DNS) ListenAndServe() error {
fmt.Printf("ns=dns at=serve\n")
return d.server.ActivateAndServe()
}
func (d *DNS) Shutdown(ctx context.Context) error {
fmt.Printf("ns=dns at=shutdown\n")
return d.server.Shutdown()
}
func (d *DNS) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
//soa, err := dns.NewRR("$ORIGIN .\n$TTL 0\n@ SOA ns.convox. support.convox.com. 2018042500 0 0 0 0")
//if err != nil {
// dnsError(w, r)
// return
//}
if len(r.Question) < 1 {
dnsError(w, r, fmt.Errorf("no question"))
return
}
fmt.Printf("ns=dns at=query remote=%s\n", w.RemoteAddr())
q := r.Question[0]
host := strings.TrimSuffix(q.Name, ".")
internal := d.internal
ts, err := d.router.TargetList(host)
if err != nil {
dnsError(w, r, err)
return
}
if len(ts) > 0 {
fmt.Printf("ns=dns at=resolve internal=%t type=route host=%q\n", internal, q.Name)
a := &dns.Msg{}
if r.IsEdns0() != nil {
a.SetEdns0(4096, true)
}
a.SetReply(r)
//a.Authoritative = true
a.Compress = false
//a.Ns = []dns.RR{soa}
a.RecursionAvailable = true
ip := d.router.RouterIP(internal)
if parts := strings.Split(host, "."); len(parts) == 2 && parts[0] == "registry" {
switch os.Getenv("PLATFORM") {
case "darwin":
ip = "0.0.0.0"
}
}
switch q.Qtype {
case dns.TypeA:
fmt.Printf("ns=dns at=answer internal=%t type=A value=%s\n", internal, ip)
rr, err := dns.NewRR(fmt.Sprintf("%s A %s", q.Name, ip))
if err != nil {
dnsError(w, r, err)
return
}
a.Answer = append(a.Answer, rr)
// case dns.TypeAAAA:
// fmt.Printf("ns=dns at=answer internal=%t type=AAAA value=%s\n", internal, ip)
// rr, err := dns.NewRR(fmt.Sprintf("%s AAAA %s", q.Name, ip))
// if err != nil {
// dnsError(w, r, err)
// return
// }
// a.Answer = append(a.Answer, rr)
default:
fmt.Printf("ns=dns at=answer internal=%t type=%s value=nx\n", internal, dns.TypeToString[q.Qtype])
}
w.WriteMsg(a)
return
}
fmt.Printf("ns=dns at=resolve type=forward host=%q\n", q.Name)
c := dns.Client{Net: "udp"}
rs, _, err := c.Exchange(r, d.upstream)
if err != nil {
dnsError(w, r, err)
return
}
//rs.Ns = []dns.RR{soa}
w.WriteMsg(rs)
}
func dnsError(w dns.ResponseWriter, r *dns.Msg, err error) {
fmt.Printf("ns=dns at=error error=%s\n", err)
m := &dns.Msg{}
m.SetRcode(r, dns.RcodeServerFailure)
w.WriteMsg(m)
}