-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (155 loc) · 4.4 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"log"
"math/rand"
"net"
"os"
"strings"
"time"
"github.com/miekg/dns"
"github.com/urfave/cli/v2"
)
type options struct {
upstreamNormal []string
upstreamSpecial []string
upstreamDomains []string
masquedDomain string
upstreamDomain string
}
func getRandomUpstream(upstreams []string) (string, string) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
index := r.Intn(len(upstreams))
upstream := upstreams[index]
parts := strings.Split(upstream, ":")
if len(parts) != 2 {
log.Fatalf("Invalid upstream: %s", upstream)
}
return parts[0], parts[1]
}
func HasSuffixInSlice(str string, suffixes []string) bool {
for _, suffix := range suffixes {
if strings.HasSuffix(str, suffix) {
return true
}
}
return false
}
func handleDNSRequest(w dns.ResponseWriter, r *dns.Msg, opts options) {
var upstreamHost, upstreamPort string
var special bool
defer func() {
if err := w.Close(); err != nil {
log.Println("Error closing connection:", err)
}
}()
c := new(dns.Client)
m := new(dns.Msg)
// log.Println(r.Question[0].Name)
if strings.HasSuffix(r.Question[0].Name, opts.masquedDomain) ||
HasSuffixInSlice(r.Question[0].Name, opts.upstreamDomains) {
m.SetQuestion(strings.Replace(r.Question[0].Name, opts.masquedDomain, opts.upstreamDomain, 1), r.Question[0].Qtype)
upstreamHost, upstreamPort = getRandomUpstream(opts.upstreamSpecial)
special = true
} else {
m.SetQuestion(r.Question[0].Name, r.Question[0].Qtype)
upstreamHost, upstreamPort = getRandomUpstream(opts.upstreamNormal)
}
in, _, err := c.Exchange(m, net.JoinHostPort(upstreamHost, upstreamPort))
if special && (in == nil || len(in.Answer) == 0) {
log.Println(r.Question[0].String(), "not found retrying with normal upstream")
m.SetQuestion(r.Question[0].Name, r.Question[0].Qtype)
upstreamHost, upstreamPort = getRandomUpstream(opts.upstreamNormal)
in, _, err = c.Exchange(m, net.JoinHostPort(upstreamHost, upstreamPort))
}
if err != nil {
log.Println(err)
return
}
res := new(dns.Msg)
res.SetReply(r)
if strings.HasSuffix(r.Question[0].Name, opts.masquedDomain) {
for _, ans := range in.Answer {
if ans.Header().Rrtype == dns.TypeA {
if a, ok := ans.(*dns.A); ok {
a.Hdr.Name = strings.Replace(a.Hdr.Name, opts.upstreamDomain, opts.masquedDomain, 1)
}
}
}
}
res.Answer = in.Answer
// log.Println(in.Answer)
res.RecursionAvailable = true
w.WriteMsg(res)
}
func appendPeriods(str string) string {
if !strings.HasPrefix(str, ".") {
str = "." + str
}
if !strings.HasSuffix(str, ".") {
str = str + "."
}
return str
}
func main() {
app := &cli.App{
Name: "DNS Proxy",
Usage: "A DNS proxy server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "listen",
Aliases: []string{"l"},
Value: ":1053",
Usage: "Address and port to listen on",
},
&cli.StringFlag{
Name: "masqued-domain",
Aliases: []string{"m"},
Value: ".example.com.",
Usage: "Domain to be masqued",
},
&cli.StringFlag{
Name: "upstream-domain",
Aliases: []string{"u"},
Value: ".service.consul.",
Usage: "Upstream domain to be masqued",
},
&cli.StringSliceFlag{
Name: "upstream-domains",
Aliases: []string{"uds"},
Value: cli.NewStringSlice("service.consul.", ".consul."),
Usage: "Upstream domains to be resolved",
},
&cli.StringSliceFlag{
Name: "upstream-special",
Aliases: []string{"uc"},
Value: cli.NewStringSlice("localhost:8600"),
Usage: "Special upstream host:port",
},
&cli.StringSliceFlag{
Name: "upstream-normal",
Aliases: []string{"un"},
Value: cli.NewStringSlice("1.1.1.1:53"),
Usage: "Normal resolution upstream host:port",
},
},
Action: func(c *cli.Context) error {
var opts options
listenAddr := c.String("listen")
opts.upstreamSpecial = c.StringSlice("upstream-special")
opts.upstreamNormal = c.StringSlice("upstream-normal")
opts.masquedDomain = appendPeriods(c.String("masqued-domain"))
opts.upstreamDomain = appendPeriods(c.String("upstream-domain"))
opts.upstreamDomains = c.StringSlice("upstream-domains")
server := &dns.Server{Addr: listenAddr, Net: "udp"}
dns.HandleFunc(".", func(w dns.ResponseWriter, r *dns.Msg) {
handleDNSRequest(w, r, opts)
})
log.Printf("DNS proxy listening on %s\n", listenAddr)
return server.ListenAndServe()
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}