-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (63 loc) · 1.61 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
package main
import (
"flag"
"log"
"os"
"os/signal"
"syscall"
"github.com/miekg/dns"
)
const (
defaultRelaySrv = "8.8.8.8:53"
defaultListenAddr = ":53"
)
var (
netflixNames = []string{"netflix.com", "netflix.net", "nflximg.com", "nflxext.com", "nflxvideo.net", "nflxso.net"}
relaySrv = flag.String("relay", defaultRelaySrv, "DNS server to relay requests to")
listenAddr = flag.String("listen", defaultListenAddr, "Address to listen for DNS requests")
help = flag.Bool("h", false, "Show this help message")
)
func main() {
flag.Parse()
if *help {
flag.Usage()
return
}
log.Printf("Running Netflix AAAA skipping DNS server on %s and relaying to %s", *listenAddr, *relaySrv)
srv := startServer()
sch := make(chan os.Signal)
signal.Notify(sch, syscall.SIGTERM, syscall.SIGINT)
<-sch
srv.Shutdown()
}
func startServer() *dns.Server {
srv := &dns.Server{Addr: *listenAddr, Net: "udp", Handler: newMuxedHandler()}
if e := srv.ListenAndServe(); e != nil {
log.Fatalf("Failed to start %s, error: %s", os.Args[0], e.Error())
}
return srv
}
func newMuxedHandler() *dns.ServeMux {
mux := dns.NewServeMux()
mux.HandleFunc(".", defaultHandler)
for _, n := range netflixNames {
mux.HandleFunc(n, netflixHandler)
}
return mux
}
func defaultHandler(rw dns.ResponseWriter, m *dns.Msg) {
r, e := dns.Exchange(m, *relaySrv)
if e != nil {
r = new(dns.Msg)
r.SetRcode(m, dns.RcodeServerFailure)
}
rw.WriteMsg(r)
}
func netflixHandler(rw dns.ResponseWriter, m *dns.Msg) {
if m.Question[0].Qtype == dns.TypeAAAA {
var r dns.Msg
rw.WriteMsg(r.SetReply(m))
} else {
defaultHandler(rw, m)
}
}