-
Notifications
You must be signed in to change notification settings - Fork 248
/
upstream.go
173 lines (149 loc) · 5.22 KB
/
upstream.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
168
169
170
171
172
173
// Package upstream implements DNS clients for all known DNS encryption protocols
package upstream
import (
"fmt"
"net"
"net/url"
"strings"
"time"
"github.com/AdguardTeam/golibs/log"
"github.com/ameshkov/dnsstamps"
"github.com/joomcode/errorx"
"github.com/miekg/dns"
)
// Upstream is an interface for a DNS resolver
type Upstream interface {
Exchange(m *dns.Msg) (*dns.Msg, error)
Address() string
}
// Options for AddressToUpstream func
type Options struct {
// Bootstrap is a list of DNS servers to be used to resolve DOH/DOT hostnames (if any)
// You can use plain DNS, DNSCrypt, or DOT/DOH with IP addresses (not hostnames)
Bootstrap []string
// Timeout is the default upstream timeout. Also, it is used as a timeout for bootstrap DNS requests.
// timeout=0 means infinite timeout.
Timeout time.Duration
// ServerIP allows specifying the resolver's IP address. In the case if it's specified,
// bootstrap DNS servers won't be used at all.
ServerIP net.IP
}
// AddressToUpstream converts the specified address to an Upstream instance
// * 8.8.8.8:53 -- plain DNS
// * tcp://8.8.8.8:53 -- plain DNS over TCP
// * tls://1.1.1.1 -- DNS-over-TLS
// * https://dns.adguard.com/dns-query -- DNS-over-HTTPS
// * sdns://... -- DNS stamp (see https://dnscrypt.info/stamps-specifications)
func AddressToUpstream(address string, opts Options) (Upstream, error) {
if strings.Contains(address, "://") {
upstreamURL, err := url.Parse(address)
if err != nil {
return nil, errorx.Decorate(err, "failed to parse %s", address)
}
return urlToUpstream(upstreamURL, opts)
}
// we don't have scheme in the url, so it's just a plain DNS host:port
_, _, err := net.SplitHostPort(address)
if err != nil {
// doesn't have port, default to 53
address = net.JoinHostPort(address, "53")
}
return &plainDNS{address: address, timeout: opts.Timeout}, nil
}
// urlToBoot creates an instance of the bootstrapper with the specified options
func urlToBoot(resolverURL string, opts Options) (*bootstrapper, error) {
if opts.ServerIP == nil {
return toBoot(resolverURL, opts.Bootstrap, opts.Timeout), nil
}
return toBootResolved(resolverURL, opts.ServerIP, opts.Timeout)
}
// urlToUpstream converts a URL to an Upstream
func urlToUpstream(upstreamURL *url.URL, opts Options) (Upstream, error) {
switch upstreamURL.Scheme {
case "sdns":
return stampToUpstream(upstreamURL.String(), opts)
case "dns":
return &plainDNS{address: getHostWithPort(upstreamURL, "53"), timeout: opts.Timeout}, nil
case "tcp":
return &plainDNS{address: getHostWithPort(upstreamURL, "53"), timeout: opts.Timeout, preferTCP: true}, nil
case "tls":
if upstreamURL.Port() == "" {
upstreamURL.Host += ":853"
}
resolverURL := upstreamURL.String()
b, err := urlToBoot(resolverURL, opts)
if err != nil {
return nil, errorx.Decorate(err, "couldn't create tls bootstrapper")
}
return &dnsOverTLS{boot: b}, nil
case "https":
if upstreamURL.Port() == "" {
upstreamURL.Host += ":443"
}
resolverURL := upstreamURL.String()
b, err := urlToBoot(resolverURL, opts)
if err != nil {
return nil, errorx.Decorate(err, "couldn't create tls bootstrapper")
}
return &dnsOverHTTPS{boot: b}, nil
default:
// assume it's plain DNS
return &plainDNS{address: getHostWithPort(upstreamURL, "53"), timeout: opts.Timeout}, nil
}
}
// stampToUpstream converts a DNS stamp to an Upstream
func stampToUpstream(address string, opts Options) (Upstream, error) {
stamp, err := dnsstamps.NewServerStampFromString(address)
if err != nil {
return nil, errorx.Decorate(err, "failed to parse %s", address)
}
if stamp.ServerAddrStr != "" {
host, _, err := net.SplitHostPort(stamp.ServerAddrStr)
if err != nil {
host = stamp.ServerAddrStr
}
// Parse and add to options
opts.ServerIP = net.ParseIP(host)
if opts.ServerIP == nil {
return nil, fmt.Errorf("invalid server address in the stamp: %s", stamp.ServerAddrStr)
}
}
switch stamp.Proto {
case dnsstamps.StampProtoTypePlain:
return &plainDNS{address: stamp.ServerAddrStr, timeout: opts.Timeout}, nil
case dnsstamps.StampProtoTypeDNSCrypt:
return &dnsCrypt{boot: toBoot(address, opts.Bootstrap, opts.Timeout)}, nil
case dnsstamps.StampProtoTypeDoH:
return AddressToUpstream(fmt.Sprintf("https://%s%s", stamp.ProviderName, stamp.Path), opts)
case dnsstamps.StampProtoTypeTLS:
return AddressToUpstream(fmt.Sprintf("tls://%s", stamp.ProviderName), opts)
}
return nil, fmt.Errorf("unsupported protocol %v in %s", stamp.Proto, address)
}
// getHostWithPort is a helper function that appends port if needed
func getHostWithPort(upstreamURL *url.URL, defaultPort string) string {
if upstreamURL.Port() == "" {
return upstreamURL.Host + ":" + defaultPort
}
return upstreamURL.Host
}
// Write to log DNS request information that we are going to send
func logBegin(upstreamAddress string, req *dns.Msg) {
qtype := ""
target := ""
if len(req.Question) != 0 {
qtype = dns.TypeToString[req.Question[0].Qtype]
target = req.Question[0].Name
}
log.Debug("%s: sending request %s %s",
upstreamAddress, qtype, target)
}
// Write to log about the result of DNS request
func logFinish(upstreamAddress string, err error) {
status := "ok"
if err != nil {
status = err.Error()
}
log.Debug("%s: response: %s",
upstreamAddress, status)
}