-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
181 lines (164 loc) · 4.15 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
// rdns is a DNS server that serves PTR and AAAA records for an IPv6 subnet, as
// well as the associated authoritative NS records.
package main
import (
"encoding/hex"
"flag"
"fmt"
"log"
"net"
"regexp"
"strings"
"github.com/miekg/dns"
)
var (
networkFlag = flag.String("network", "fe80::/64", "`subnet` for which to serve ip6.arpa records")
hostPrefixFlag = flag.String("host_prefix", "ip-", "prefix for generated host name")
domainFlag = flag.String("domain", ".v6.example.com.", "domain suffix for generated domain name")
nsFlag = flag.String("ns", "ns.example.com.", "name `server` for NS responses")
ttlFlag = flag.Int("ttl", 3600, "answer TTL in `seconds`")
addrFlag = flag.String("listen", ":53", "DNS server listen `address`")
)
func main() {
flag.Parse()
if !strings.HasSuffix(*domainFlag, ".") {
log.Fatal("bad -domain flag: must have trailing dot")
}
if !strings.HasPrefix(*domainFlag, ".") {
log.Fatal("bad -domain flag: must have leading dot")
}
if !strings.HasSuffix(*nsFlag, ".") {
log.Fatal("bad -ns flag: must have trailing dot")
}
_, network, err := net.ParseCIDR(*networkFlag)
if err != nil {
log.Fatalf("bad -network flag: %v", err)
}
ones, _ := network.Mask.Size()
if ones%8 != 0 {
log.Fatal("bad -network flag: mask must be a multiple of 8")
}
prefix := fmt.Sprintf("%x", []byte(network.IP[:ones/8]))
s := &server{
prefix: prefix,
ns: regexp.MustCompile(`^((?:[[:xdigit:]]\.){` + fmt.Sprint(len(prefix)) + `,32})ip6\.arpa\.$`),
ptr: regexp.MustCompile(`^((?:[[:xdigit:]]\.){32})ip6\.arpa\.$`),
aaaa: regexp.MustCompile(`^` +
regexp.QuoteMeta(*hostPrefixFlag) +
`([[:xdigit:]]{1,` + fmt.Sprint(32-len(prefix)) + `})` +
regexp.QuoteMeta(*domainFlag) +
`$`),
}
log.Fatal((&dns.Server{
Addr: *addrFlag,
Net: "udp",
Handler: s,
}).ListenAndServe())
}
type server struct {
prefix string
ns, ptr, aaaa *regexp.Regexp
}
func (s *server) ServeDNS(w dns.ResponseWriter, m *dns.Msg) {
if len(m.Question) != 1 {
log.Printf("len(question) = %d", len(m.Question))
return
}
q := m.Question[0]
reply := new(dns.Msg)
reply.SetReply(m)
switch q.Qtype {
case dns.TypeNS:
s.toNS(reply, q.Name)
case dns.TypePTR:
s.toPTR(reply, q.Name)
case dns.TypeAAAA:
s.toAAAA(reply, q.Name)
}
var h *dns.RR_Header
if len(reply.Answer) == 1 {
h = reply.Answer[0].Header()
} else if len(reply.Ns) == 1 {
h = reply.Ns[0].Header()
}
if h != nil {
reply.Authoritative = true
h.Class = dns.ClassINET
h.Name = q.Name
h.Ttl = uint32(*ttlFlag)
} else {
reply.Rcode = dns.RcodeNameError
}
log.Printf("reply to %v:\n%v", w.RemoteAddr(), reply)
if err := w.WriteMsg(reply); err != nil {
log.Printf("WriteMsg error: %v", err)
}
}
func (s *server) toNS(reply *dns.Msg, name string) {
if _, ok := s.hexMatch(s.ns, name); !ok {
return
}
reply.Ns = []dns.RR{&dns.NS{
Hdr: dns.RR_Header{
Rrtype: dns.TypeNS,
Rdlength: uint16(len(*nsFlag)),
},
Ns: *nsFlag,
}}
}
func (s *server) toPTR(reply *dns.Msg, name string) {
x, ok := s.hexMatch(s.ptr, name)
if !ok {
return
}
x = strings.TrimLeft(x, "0")
ptr := *hostPrefixFlag + x + *domainFlag
reply.Answer = []dns.RR{&dns.PTR{
Hdr: dns.RR_Header{
Rrtype: dns.TypePTR,
Rdlength: uint16(len(ptr)),
},
Ptr: ptr,
}}
}
func (s *server) hexMatch(re *regexp.Regexp, name string) (suffix string, _ bool) {
m := re.FindStringSubmatch(name)
if m == nil {
return "", false
}
d := strings.Split(m[1][:len(m[1])-1], ".")
n := len(d)
for i := 0; i < n/2; i++ {
d[i], d[n-1-i] = d[n-1-i], d[i]
}
x := strings.Join(d, "")
if !strings.HasPrefix(x, s.prefix) {
return "", false
}
return x[len(s.prefix):], true
}
func (s *server) toAAAA(reply *dns.Msg, name string) {
m := s.aaaa.FindStringSubmatch(name)
if m == nil {
return
}
x := m[1]
if strings.HasPrefix(x, "0") {
return
}
if pad := 32 - len(x) - len(s.prefix); pad > 0 {
x = strings.Repeat("0", pad) + x
}
ip, err := hex.DecodeString(s.prefix + x)
if err != nil {
return
}
aaaa := net.IP(ip)
reply.Answer = []dns.RR{&dns.AAAA{
Hdr: dns.RR_Header{
Rrtype: dns.TypeAAAA,
Rdlength: uint16(len(aaaa)),
},
AAAA: aaaa,
}}
}