-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (76 loc) · 1.88 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
package main
import (
"fmt"
"log"
"net"
"strconv"
"strings"
"github.com/miekg/dns"
)
func parseQuery(m *dns.Msg, ip string) {
for _, q := range m.Question {
switch q.Qtype {
case dns.TypeA:
log.Printf("Query for %s\n", q.Name)
s := strings.Split(ip, ".")
i, _ := strconv.Atoi(s[3])
s[3] = strconv.Itoa(i - 1)
rr, err := dns.NewRR(fmt.Sprintf("%s A %s", q.Name, strings.Join(s, ".")))
if err == nil {
m.Answer = append(m.Answer, rr)
}
}
}
}
func handleGeegleDnsRequest(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Compress = false
switch r.Opcode {
case dns.OpcodeQuery:
parseQuery(m, strings.Split(w.RemoteAddr().String(), ":")[0])
}
w.WriteMsg(m)
}
func handleOtherDnsRequest(resp dns.ResponseWriter, req *dns.Msg) {
if len(req.Question) == 0 {
respond(resp, req, dns.RcodeFormatError)
return
}
recursor := "8.8.8.8:53"
network := "udp"
if _, ok := resp.RemoteAddr().(*net.TCPAddr); ok {
network = "tcp"
}
c := &dns.Client{Net: network}
r, _, err := c.Exchange(req, recursor)
if err == nil {
log.Printf("[info] using %s to answer %s", recursor, req.Question[0].Name)
if err := resp.WriteMsg(r); err != nil {
log.Printf("[WARN] dns: failed to respond: %v", err)
}
return
}
respond(resp, req, dns.RcodeServerFailure)
}
func respond(resp dns.ResponseWriter, req *dns.Msg, code int) {
m := &dns.Msg{}
m.SetReply(req)
m.RecursionAvailable = true
m.SetRcode(req, code)
resp.WriteMsg(m)
}
func main() {
// attach request handler func
dns.HandleFunc("geegle.org.", handleGeegleDnsRequest)
dns.HandleFunc(".", handleOtherDnsRequest)
// start server
port := 53
server := &dns.Server{Addr: ":" + strconv.Itoa(port), Net: "udp"}
log.Printf("Starting at %d\n", port)
err := server.ListenAndServe()
defer server.Shutdown()
if err != nil {
log.Fatalf("Failed to start server: %s\n ", err.Error())
}
}