-
Notifications
You must be signed in to change notification settings - Fork 2
/
request.go
149 lines (117 loc) · 2.82 KB
/
request.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
package doh
import (
"bytes"
"context"
"fmt"
"io"
"net"
"net/http"
"github.com/miekg/dns"
logging "github.com/ipfs/go-log/v2"
)
const (
dohMimeType = "application/dns-message"
)
var log = logging.Logger("doh")
func doRequest(ctx context.Context, url string, m *dns.Msg) (*dns.Msg, error) {
data, err := m.Pack()
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", dohMimeType)
req.Header.Set("Accept", dohMimeType)
req = req.WithContext(ctx)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP error: %q [%d]", resp.Status, resp.StatusCode)
}
if ct := resp.Header.Get("Content-Type"); ct != dohMimeType {
return nil, fmt.Errorf("unexpected Content-Type %q", ct)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
r := new(dns.Msg)
if err := r.Unpack(body); err != nil {
return nil, err
}
return r, nil
}
func doRequestA(ctx context.Context, url string, domain string) ([]net.IPAddr, uint32, error) {
fqdn := dns.Fqdn(domain)
m := new(dns.Msg)
m.SetQuestion(fqdn, dns.TypeA)
r, err := doRequest(ctx, url, m)
if err != nil {
return nil, 0, err
}
var ttl uint32
result := make([]net.IPAddr, 0, len(r.Answer))
for _, rr := range r.Answer {
switch v := rr.(type) {
case *dns.A:
result = append(result, net.IPAddr{IP: v.A})
if ttl == 0 || v.Hdr.Ttl < ttl {
ttl = v.Hdr.Ttl
}
default:
log.Warnf("unexpected DNS resource record %+v", rr)
}
}
return result, ttl, nil
}
func doRequestAAAA(ctx context.Context, url string, domain string) ([]net.IPAddr, uint32, error) {
fqdn := dns.Fqdn(domain)
m := new(dns.Msg)
m.SetQuestion(fqdn, dns.TypeAAAA)
r, err := doRequest(ctx, url, m)
if err != nil {
return nil, 0, err
}
var ttl uint32
result := make([]net.IPAddr, 0, len(r.Answer))
for _, rr := range r.Answer {
switch v := rr.(type) {
case *dns.AAAA:
result = append(result, net.IPAddr{IP: v.AAAA})
if ttl == 0 || v.Hdr.Ttl < ttl {
ttl = v.Hdr.Ttl
}
default:
log.Warnf("unexpected DNS resource record %+v", rr)
}
}
return result, ttl, nil
}
func doRequestTXT(ctx context.Context, url string, domain string) ([]string, uint32, error) {
fqdn := dns.Fqdn(domain)
m := new(dns.Msg)
m.SetQuestion(fqdn, dns.TypeTXT)
r, err := doRequest(ctx, url, m)
if err != nil {
return nil, 0, err
}
var ttl uint32
var result []string
for _, rr := range r.Answer {
switch v := rr.(type) {
case *dns.TXT:
result = append(result, v.Txt...)
if ttl == 0 || v.Hdr.Ttl < ttl {
ttl = v.Hdr.Ttl
}
default:
log.Warnf("unexpected DNS resource record %+v", rr)
}
}
return result, ttl, nil
}