-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (76 loc) · 2.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
package main
import (
"encoding/base32"
"flag"
"fmt"
"log"
"net"
"strings"
"github.com/miekg/dns"
)
func main() {
dnsbase := flag.String("domain", "tor6.flm.me.uk", "the domain you want to top on")
listen := flag.String("listen", "127.0.0.1:553", "the port to listen on")
flag.Parse()
listens, err := net.ListenPacket("udp4", *listen)
if err != nil {
log.Fatalf("failed to listen on UDP %s / %s", *listen, err.Error())
}
for {
dnsin := make([]byte, 1500)
inbytes, inaddr, err := listens.ReadFrom(dnsin)
inmsg := &dns.Msg{}
if unpackErr := inmsg.Unpack(dnsin[0:inbytes]); unpackErr != nil {
log.Printf("Unable to unpack DNS request %s", err.Error())
continue
}
if len(inmsg.Question) != 1 {
log.Printf("More than one quesion in query (%d), droppin %+v", len(inmsg.Question), inmsg)
continue
}
iqn := strings.ToLower(inmsg.Question[0].Name)
if !strings.Contains(iqn, *dnsbase) {
log.Printf("question is not for us '%s' vs expected '%s'", iqn, *dnsbase)
continue
}
outmsg := &dns.Msg{}
iqn = strings.ToUpper(inmsg.Question[0].Name)
queryname := strings.Replace(
iqn, fmt.Sprintf(".%s.", strings.ToUpper(*dnsbase)), "", 1)
b32, err := base32.StdEncoding.DecodeString(queryname)
if err != nil || len(b32) != 10 {
outmsg.Id = inmsg.Id
outmsg = inmsg.SetReply(outmsg)
outmsg.Rcode = dns.RcodeServerFailure
outputb, err := outmsg.Pack()
if err != nil {
log.Printf("unable to pack response to thing")
continue
}
listens.WriteTo(outputb, inaddr)
continue
}
barr := make([]byte, 0)
barr = append(barr, 0x2a, 0x07, 0x15, 0x00, 0xFE, 0xD5)
barr = append(barr, b32...)
bip := net.IP(barr)
outmsg.Id = inmsg.Id
outmsg = inmsg.SetReply(outmsg)
iqn = inmsg.Question[0].Name
outmsg.Answer = make([]dns.RR, 1)
outmsg.Answer[0] = &dns.AAAA{
Hdr: dns.RR_Header{
Name: iqn,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: 2147483646},
AAAA: bip,
}
outputb, err := outmsg.Pack()
if err != nil {
log.Printf("unable to pack response to thing %s", err.Error())
continue
}
listens.WriteTo(outputb, inaddr)
}
}