forked from AdguardTeam/dnsproxy
/
server_https.go
235 lines (198 loc) · 6.18 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
package proxy
import (
"encoding/base64"
"fmt"
"io"
"net"
"net/http"
"strconv"
"strings"
"github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns"
"golang.org/x/net/http2"
)
func (p *Proxy) createHTTPSListeners() error {
for _, a := range p.HTTPSListenAddr {
log.Info("Creating an HTTPS server")
tcpListen, err := net.ListenTCP("tcp", a)
if err != nil {
return fmt.Errorf("starting https listener: %w", err)
}
p.httpsListen = append(p.httpsListen, tcpListen)
log.Info("Listening to https://%s", tcpListen.Addr())
tlsConfig := p.TLSConfig.Clone()
tlsConfig.NextProtos = []string{http2.NextProtoTLS, "http/1.1"}
srv := &http.Server{
TLSConfig: tlsConfig,
Handler: p,
ReadHeaderTimeout: defaultTimeout,
WriteTimeout: defaultTimeout,
}
p.httpsServer = append(p.httpsServer, srv)
}
return nil
}
// serveHttps starts the HTTPS server
func (p *Proxy) listenHTTPS(srv *http.Server, l net.Listener) {
log.Info("Listening to DNS-over-HTTPS on %s", l.Addr())
err := srv.ServeTLS(l, "", "")
if err != http.ErrServerClosed {
log.Info("HTTPS server was closed unexpectedly: %s", err)
} else {
log.Info("HTTPS server was closed")
}
}
// ServeHTTP is the http.RequestHandler 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 r.Body.Close()
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 := ipFromAddr(prx)
log.Debug("request came from proxy server %s", prx)
if !p.proxyVerifier.detect(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)
}
}
// ipFromAddr returns an IP address from addr. If addr is neither
// a *net.TCPAddr nor a *net.UDPAddr, it returns nil.
//
// TODO(a.garipov): Create package netutil in the golibs module and move it
// there.
func ipFromAddr(addr net.Addr) (ip net.IP) {
switch addr := addr.(type) {
case *net.TCPAddr:
return addr.IP
case *net.UDPAddr:
return addr.IP
}
return nil
}
// 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)
}
if realIP := realIPFromHdrs(r); realIP != nil {
log.Tracef("Using IP address from HTTP request: %s", realIP)
// TODO(a.garipov): Use net.UDPAddr here and below when
// necessary when we start supporting HTTP/3.
//
// TODO(a.garipov): Add port if we can get it from headers like
// X-Real-Port, X-Forwarded-Port, etc.
addr = &net.TCPAddr{IP: realIP, Port: 0}
prx = &net.TCPAddr{IP: host, Port: port}
return addr, prx, nil
}
return &net.TCPAddr{IP: host, Port: port}, nil, nil
}