forked from urnetwork/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet_http_doh.go
More file actions
204 lines (173 loc) · 4.14 KB
/
net_http_doh.go
File metadata and controls
204 lines (173 loc) · 4.14 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package connect
import (
"context"
"strings"
// "time"
// "crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/netip"
"net/url"
"golang.org/x/net/idna"
// "golang.org/x/exp/maps"
)
func DefaultDohSettings() *DohSettings {
return &DohSettings{
ConnectSettings: *DefaultConnectSettings(),
}
}
// see:
// https://developers.cloudflare.com/1.1.1.1/ip-addresses/
// https://www.quad9.net/
// https://support.opendns.com/hc/en-us/articles/360038086532-Using-DNS-over-HTTPS-DoH-with-OpenDNS
func dohUrlsIpv4() []string {
return []string{
"https://1.1.1.1/dns-query",
"https://1.0.0.1/dns-query",
"https://9.9.9.9:5053/dns-query",
"https://149.112.112.112:5053/dns-query",
"https://208.67.222.222/dns-query",
"https://208.67.220.220/dns-query",
}
}
func dohUrlsIpv6() []string {
return []string{
"https://[2606:4700:4700::1111]/dns-query",
"https://[2606:4700:4700::1001]/dns-query",
"https://[2620:fe::fe]:5053/dns-query",
"https://[2620:fe::9]:5053/dns-query",
"https://[2620:119:35::35]/dns-query",
"https://[2620:119:53::53]/dns-query",
}
}
type DohSettings struct {
ConnectSettings
}
func DohQuery(ctx context.Context, ipVersion int, recordType string, settings *DohSettings, domains ...string) map[netip.Addr]bool {
// run all the queries in parallel to all servers
switch recordType {
case "A", "AAAA":
default:
return map[netip.Addr]bool{}
}
netDialer := &net.Dialer{
Timeout: settings.ConnectTimeout,
}
httpClient := &http.Client{
Timeout: settings.RequestTimeout,
Transport: &http.Transport{
DialContext: netDialer.DialContext,
TLSHandshakeTimeout: settings.TlsTimeout,
TLSClientConfig: settings.TlsConfig,
},
}
query := func(dohUrl string, domain string) []netip.Addr {
name, err := Punycode(domain)
if err != nil {
return nil
}
params := url.Values{}
params.Add("name", name)
params.Add("type", recordType)
requestUrl := fmt.Sprintf("%s?%s", dohUrl, params.Encode())
request, err := http.NewRequestWithContext(ctx, "GET", requestUrl, nil)
if err != nil {
return nil
}
request.Header.Set("Accept", "application/dns-json")
// note, we do not set the User-Agent for DoH requests
// see https://bugzilla.mozilla.org/show_bug.cgi?id=1543201#c4
response, err := httpClient.Do(request)
if err != nil {
return nil
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil
}
data, err := io.ReadAll(response.Body)
if err != nil {
return nil
}
dohResponse := &DohResponse{}
err = json.Unmarshal(data, dohResponse)
if err != nil {
return nil
}
if dohResponse.Status != 0 {
return nil
}
ips := []netip.Addr{}
for _, answer := range dohResponse.Answer {
if ip, err := netip.ParseAddr(answer.Data); err == nil {
ips = append(ips, ip)
}
}
return ips
}
var dohUrls []string
switch ipVersion {
case 4:
dohUrls = dohUrlsIpv4()
case 6:
dohUrls = dohUrlsIpv6()
default:
dohUrls = []string{}
}
out := make(chan []netip.Addr)
for _, dohUrl := range dohUrls {
for _, domain := range domains {
go func() {
ips := query(dohUrl, domain)
select {
case out <- ips:
case <-ctx.Done():
}
}()
}
}
mergedIps := map[netip.Addr]bool{}
for range dohUrls {
for range domains {
select {
case ips := <-out:
for _, ip := range ips {
mergedIps[ip] = true
}
case <-ctx.Done():
}
}
}
return mergedIps
}
type DohQuestion struct {
Name string `json:"name"`
Type int `json:"type"`
}
type DohAnswer struct {
Name string `json:"name"`
Type int `json:"type"`
TTL int `json:"TTL"`
Data string `json:"data"`
}
type DohResponse struct {
Status int `json:"Status"`
TC bool `json:"TC"`
RD bool `json:"RD"`
RA bool `json:"RA"`
AD bool `json:"AD"`
CD bool `json:"CD"`
Question []DohQuestion `json:"Question"`
Answer []DohAnswer `json:"Answer"`
}
func Punycode(domain string) (string, error) {
name := strings.TrimSpace(domain)
return idna.New(
idna.MapForLookup(),
idna.Transitional(true),
idna.StrictDomainName(false),
).ToASCII(name)
}