-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (52 loc) · 1.09 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
package main
import (
"fmt"
"log"
"net"
"os"
"github.com/miekg/dns"
)
func main() {
tld := os.Getenv("TLD")
ipStr := os.Getenv("IP")
listenAddrPort := os.Getenv("LISTEN")
if ipStr == "" {
ipStr = "127.0.0.1"
}
answerIP := net.ParseIP(ipStr)
if answerIP == nil {
log.Fatal("Unable to parse IP " + ipStr)
}
if listenAddrPort == "" {
listenAddrPort = "127.0.0.1:10053"
}
oneAnswer := oneAnswerResolver{answerIP: answerIP}
dns.Handle(tld+".", oneAnswer)
server := &dns.Server{Addr: listenAddrPort, Net: "udp"}
fmt.Println("Listening on " + listenAddrPort)
err := server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}
type oneAnswerResolver struct {
answerIP net.IP
}
func (o oneAnswerResolver) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
m.Extra = make([]dns.RR, 1)
m.Extra[0] = &dns.A{
Hdr: dns.RR_Header{
Name: m.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 1800,
},
A: o.answerIP,
}
err := w.WriteMsg(m)
if err != nil {
log.Println("Error writing DNS response: " + err.Error())
}
}