-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (64 loc) · 1.44 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
package main
import (
"github.com/miekg/dns"
"log"
"strings"
"net"
)
type MyHandler struct {
}
func (handler *MyHandler) ServeDNS(w dns.ResponseWriter, msg *dns.Msg) {
response := new(dns.Msg)
response.SetReply(msg)
for _, question := range msg.Question {
if strings.HasSuffix(question.Name, ".test.") {
rr := &dns.A{
Hdr: dns.RR_Header{
Name: question.Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 60,
},
A: net.ParseIP("127.0.0.1"),
}
response.Answer = append(response.Answer, rr)
continue
}
if strings.HasSuffix(question.Name, ".xip.io.") {
handler.handleDynamic(response, question.Name, "xip.io.")
continue
}
if strings.HasSuffix(question.Name, ".nip.io.") {
handler.handleDynamic(response, question.Name, "nip.io.")
continue
}
}
w.WriteMsg(response)
}
func (handler *MyHandler) handleDynamic(response *dns.Msg, name string, suffix string) {
sa := strings.Split(suffix, ".")
na := strings.Split(name, ".")
address := strings.Join(na[len(na)-len(sa)-4:len(na)-len(sa)], ".")
rr := &dns.A{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 60,
},
A: net.ParseIP(address),
}
response.Answer = append(response.Answer, rr)
}
func main() {
handler := MyHandler{}
server := dns.Server{
Addr: "127.0.0.1:5300",
Net: "udp",
Handler: &handler,
}
err := server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}