/
http.go
122 lines (108 loc) · 2.82 KB
/
http.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
package transport
import (
"bytes"
"crypto/tls"
"encoding/base64"
"fmt"
"io"
"net/http"
"time"
"github.com/miekg/dns"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
log "github.com/sirupsen/logrus"
"golang.org/x/net/http2"
)
// HTTP makes a DNS query over HTTP(s)
type HTTP struct {
Server string
TLSConfig *tls.Config
UserAgent string
Method string
Timeout time.Duration
HTTP2, HTTP3 bool
NoPMTUd bool
ReuseConn bool
conn *http.Client
}
func (h *HTTP) Exchange(m *dns.Msg) (*dns.Msg, error) {
if h.conn == nil || !h.ReuseConn {
h.conn = &http.Client{
Timeout: h.Timeout,
Transport: &http.Transport{
TLSClientConfig: h.TLSConfig,
MaxConnsPerHost: 1,
MaxIdleConns: 1,
Proxy: http.ProxyFromEnvironment,
},
}
if h.HTTP2 {
log.Debug("Using HTTP/2")
h.conn.Transport = &http2.Transport{
TLSClientConfig: h.TLSConfig,
AllowHTTP: true,
}
}
if h.HTTP3 {
log.Debug("Using HTTP/3")
h.conn.Transport = &http3.RoundTripper{
TLSClientConfig: h.TLSConfig,
QuicConfig: &quic.Config{
DisablePathMTUDiscovery: h.NoPMTUd,
},
}
}
}
buf, err := m.Pack()
if err != nil {
return nil, fmt.Errorf("packing message: %w", err)
}
var queryURL string
var req *http.Request
switch h.Method {
case http.MethodGet:
queryURL = h.Server + "?dns=" + base64.RawURLEncoding.EncodeToString(buf)
req, err = http.NewRequest(http.MethodGet, queryURL, nil)
if err != nil {
return nil, fmt.Errorf("creating http request to %s: %w", queryURL, err)
}
case http.MethodPost:
queryURL = h.Server
req, err = http.NewRequest(http.MethodPost, queryURL, bytes.NewReader(buf))
if err != nil {
return nil, fmt.Errorf("creating http request to %s: %w", queryURL, err)
}
req.Header.Set("Content-Type", "application/dns-message")
default:
return nil, fmt.Errorf("unsupported HTTP method: %s", h.Method)
}
req.Header.Set("Accept", "application/dns-message")
if h.UserAgent != "" {
log.Debugf("Setting User-Agent to %s", h.UserAgent)
req.Header.Set("User-Agent", h.UserAgent)
}
log.Debugf("[http] sending %s request to %s", h.Method, queryURL)
resp, err := h.conn.Do(req)
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, fmt.Errorf("requesting %s: %w", queryURL, err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("reading %s: %w", queryURL, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("got status code %d from %s", resp.StatusCode, queryURL)
}
response := dns.Msg{}
if err := response.Unpack(body); err != nil {
return nil, fmt.Errorf("unpacking DNS response from %s: %w", queryURL, err)
}
return &response, nil
}
func (h *HTTP) Close() error {
h.conn.CloseIdleConnections()
return nil
}