forked from AdguardTeam/dnsproxy
/
server_https.go
265 lines (222 loc) · 7.13 KB
/
server_https.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package proxy
import (
"crypto/tls"
"encoding/base64"
"fmt"
"io"
"net"
"net/http"
"strconv"
"strings"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil"
"github.com/miekg/dns"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
"golang.org/x/net/http2"
)
// listenHTTP creates instances of TLS listeners that will be used to run an
// H1/H2 server. Returns the address the listener actually listens to (useful
// in the case if port 0 is specified).
func (p *Proxy) listenHTTP(addr *net.TCPAddr) (laddr *net.TCPAddr, err error) {
tcpListen, err := net.ListenTCP("tcp", addr)
if err != nil {
return nil, fmt.Errorf("tcp listener: %w", err)
}
log.Info("Listening to https://%s", tcpListen.Addr())
tlsConfig := p.TLSConfig.Clone()
tlsConfig.NextProtos = []string{http2.NextProtoTLS, "http/1.1"}
tlsListen := tls.NewListener(tcpListen, tlsConfig)
p.httpsListen = append(p.httpsListen, tlsListen)
return tcpListen.Addr().(*net.TCPAddr), nil
}
// listenH3 creates instances of QUIC listeners that will be used for running
// an HTTP/3 server.
func (p *Proxy) listenH3(addr *net.UDPAddr) (err error) {
tlsConfig := p.TLSConfig.Clone()
tlsConfig.NextProtos = []string{"h3"}
quicListen, err := quic.ListenAddrEarly(addr.String(), tlsConfig, newServerQUICConfig())
if err != nil {
return fmt.Errorf("quic listener: %w", err)
}
log.Info("Listening to h3://%s", quicListen.Addr())
p.h3Listen = append(p.h3Listen, quicListen)
return nil
}
// createHTTPSListeners creates TCP/UDP listeners and HTTP/H3 servers.
func (p *Proxy) createHTTPSListeners() (err error) {
p.httpsServer = &http.Server{
Handler: p,
ReadHeaderTimeout: defaultTimeout,
WriteTimeout: defaultTimeout,
}
if p.HTTP3 {
p.h3Server = &http3.Server{
Handler: p,
}
}
for _, addr := range p.HTTPSListenAddr {
log.Info("Creating an HTTPS server")
tcpAddr, err := p.listenHTTP(addr)
if err != nil {
return fmt.Errorf("failed to start HTTPS server on %s: %w", addr, err)
}
if p.HTTP3 {
// HTTP/3 server listens to the same pair IP:port as the one HTTP/2
// server listens to.
udpAddr := &net.UDPAddr{IP: tcpAddr.IP, Port: tcpAddr.Port}
err = p.listenH3(udpAddr)
if err != nil {
return fmt.Errorf("failed to start HTTP/3 server on %s: %w", udpAddr, err)
}
}
}
return nil
}
// ServeHTTP is the http.Handler implementation that handles DoH queries.
// Here is what it returns:
//
// - http.StatusBadRequest if there is no DNS request data;
// - http.StatusUnsupportedMediaType if request content type is not
// "application/dns-message";
// - http.StatusMethodNotAllowed if request method is not GET or POST.
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Tracef("Incoming HTTPS request on %s", r.URL)
var buf []byte
var err error
switch r.Method {
case http.MethodGet:
dnsParam := r.URL.Query().Get("dns")
buf, err = base64.RawURLEncoding.DecodeString(dnsParam)
if len(buf) == 0 || err != nil {
log.Tracef("Cannot parse DNS request from %s", dnsParam)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
case http.MethodPost:
contentType := r.Header.Get("Content-Type")
if contentType != "application/dns-message" {
log.Tracef("Unsupported media type: %s", contentType)
http.Error(w, http.StatusText(http.StatusUnsupportedMediaType), http.StatusUnsupportedMediaType)
return
}
buf, err = io.ReadAll(r.Body)
if err != nil {
log.Tracef("Cannot read the request body: %s", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
defer log.OnCloserError(r.Body, log.DEBUG)
default:
log.Tracef("Wrong HTTP method: %s", r.Method)
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
req := &dns.Msg{}
if err = req.Unpack(buf); err != nil {
log.Tracef("msg.Unpack: %s", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
addr, prx, err := remoteAddr(r)
if err != nil {
log.Debug("warning: getting real ip: %s", err)
}
d := p.newDNSContext(ProtoHTTPS, req)
d.Addr = addr
d.HTTPRequest = r
d.HTTPResponseWriter = w
if prx != nil {
ip, _ := netutil.IPAndPortFromAddr(prx)
log.Debug("request came from proxy server %s", prx)
if !p.proxyVerifier.Contains(ip) {
log.Debug("proxy %s is not trusted, using original remote addr", ip)
d.Addr = prx
}
}
err = p.handleDNSRequest(d)
if err != nil {
log.Tracef("error handling DNS (%s) request: %s", d.Proto, err)
}
}
// Writes a response to the DoH client.
func (p *Proxy) respondHTTPS(d *DNSContext) error {
resp := d.Res
w := d.HTTPResponseWriter
if resp == nil {
// If no response has been written, indicate it via a 500 error
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return nil
}
bytes, err := resp.Pack()
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return fmt.Errorf("packing message: %w", err)
}
w.Header().Set("Server", "AdGuard DNS")
w.Header().Set("Content-Type", "application/dns-message")
_, err = w.Write(bytes)
return err
}
// realIPFromHdrs extracts the actual client's IP address from the first
// suitable r's header. It returns nil if r doesn't contain any information
// about real client's IP address. Current headers priority is:
//
// 1. CF-Connecting-IP
// 2. True-Client-IP
// 3. X-Real-IP
// 4. X-Forwarded-For
func realIPFromHdrs(r *http.Request) (realIP net.IP) {
for _, h := range []string{
// Headers set by CloudFlare proxy servers.
"CF-Connecting-IP",
"True-Client-IP",
// Other proxying headers.
"X-Real-IP",
} {
realIP = net.ParseIP(strings.TrimSpace(r.Header.Get(h)))
if realIP != nil {
return realIP
}
}
xff := r.Header.Get("X-Forwarded-For")
firstComma := strings.IndexByte(xff, ',')
if firstComma == -1 {
return net.ParseIP(strings.TrimSpace(xff))
}
return net.ParseIP(strings.TrimSpace(xff[:firstComma]))
}
// remoteAddr returns the real client's address and the IP address of the latest
// proxy server if any.
func remoteAddr(r *http.Request) (addr, prx net.Addr, err error) {
var hostStr, portStr string
if hostStr, portStr, err = net.SplitHostPort(r.RemoteAddr); err != nil {
return nil, nil, err
}
var port int
if port, err = strconv.Atoi(portStr); err != nil {
return nil, nil, err
}
host := net.ParseIP(hostStr)
if host == nil {
return nil, nil, fmt.Errorf("invalid ip: %s", hostStr)
}
h3 := r.Context().Value(http3.ServerContextKey) != nil
if realIP := realIPFromHdrs(r); realIP != nil {
log.Tracef("Using IP address from HTTP request: %s", realIP)
// TODO(a.garipov): Add port if we can get it from headers like
// X-Real-Port, X-Forwarded-Port, etc.
if h3 {
addr = &net.UDPAddr{IP: realIP, Port: 0}
prx = &net.UDPAddr{IP: host, Port: port}
} else {
addr = &net.TCPAddr{IP: realIP, Port: 0}
prx = &net.TCPAddr{IP: host, Port: port}
}
return addr, prx, nil
}
if h3 {
return &net.UDPAddr{IP: host, Port: port}, nil, nil
}
return &net.TCPAddr{IP: host, Port: port}, nil, nil
}