-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (119 loc) · 3.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
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
package main
import (
"bytes"
"fmt"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"io"
"math/rand"
"net"
"net/http"
"net/url"
"time"
)
func buildHostSet(urls []string) (map[string]bool, error) {
hosts := make(map[string]bool)
for _, u := range urls {
parsedURL, err := url.Parse(u)
if err != nil {
return nil, err
}
if parsedURL.Scheme != "https" {
return nil, fmt.Errorf("upstream '%v' is not https", u)
}
if net.ParseIP(parsedURL.Host) == nil {
hosts[parsedURL.Host+"."] = true
}
}
return hosts, nil
}
var listenAddr string
var httpTimeout time.Duration
var fallback string
var verbose bool
func init() {
pflag.StringVarP(&listenAddr, "listen", "l", ":10053", "address to listen on")
pflag.DurationVarP(&httpTimeout, "timeout", "t", 5*time.Second, "HTTP timeout")
pflag.StringVarP(&fallback, "fallback", "f", "1.1.1.1:53", "fallback DNS address for resolving upstreams")
pflag.BoolVarP(&verbose, "verbose", "v", false, "enable verbose logging")
}
func main() {
pflag.Parse()
rootLogger := logrus.New()
rootLogger.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
if verbose {
rootLogger.SetLevel(logrus.DebugLevel)
}
httpClient := http.Client{Timeout: httpTimeout}
rand.Seed(time.Now().UTC().UnixNano())
upstreams := pflag.Args()
if len(upstreams) == 0 {
rootLogger.Fatal("Must include at least one upstream")
}
upstreamHosts, err := buildHostSet(upstreams)
if err != nil {
rootLogger.WithError(err).Fatal("Failed to parse upstreams")
}
proxy := func(w dns.ResponseWriter, r *dns.Msg) (*logrus.Entry, error) {
questionName := r.Question[0].Name
log := rootLogger.WithField("Id", r.Id).WithField("Domain", r.Question[0].Name)
msg, err := r.Pack()
if err != nil {
return log, err
}
reader := bytes.NewReader(msg)
if upstreamHosts[questionName] {
log = log.WithField("upstream", fallback)
conn, netErr := net.Dial("udp", fallback)
defer conn.Close()
if netErr != nil {
return log, netErr
}
if _, copyErr := io.Copy(conn, reader); copyErr != nil {
return log, copyErr
}
if _, copyErr := io.Copy(w, conn); copyErr != nil {
return log, copyErr
}
return log, nil
}
dnsURL := upstreams[rand.Intn(len(upstreams))]
log = log.WithField("upstream", dnsURL)
resp, err := httpClient.Post(dnsURL, "application/dns-message", reader)
if err != nil {
return log, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return log, fmt.Errorf("received HTTP %v from %v", resp.StatusCode, dnsURL)
}
if _, err := io.Copy(w, resp.Body); err != nil {
return log, err
}
return log, nil
}
dns.HandleFunc(".", func(w dns.ResponseWriter, r *dns.Msg) {
defer w.Close()
start := time.Now()
if log, err := proxy(w, r); err != nil {
log.WithError(err).Warnf("Failed to proxy in %v", time.Now().Sub(start).Round(time.Millisecond))
reply := &dns.Msg{}
reply.SetRcode(r, dns.RcodeServerFailure)
if writeErr := w.WriteMsg(reply); writeErr != nil {
log.WithError(writeErr).Warn("Error writing failure response")
}
} else {
log.Debugf("Proxied in %v", time.Now().Sub(start).Round(time.Millisecond))
}
})
server := dns.Server{
Net: "udp",
Addr: listenAddr,
}
rootLogger.Infof("Proxying to %v URL(s) with a %v timeout", len(upstreams), httpClient.Timeout)
rootLogger.Infof("Listening on %v", listenAddr)
if err := server.ListenAndServe(); err != nil {
rootLogger.Fatal(err)
}
}