forked from AdguardTeam/dnsproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_https_test.go
319 lines (283 loc) · 7.17 KB
/
server_https_test.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package proxy
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"io"
"net"
"net/http"
"strings"
"testing"
"github.com/AdguardTeam/golibs/netutil"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHttpsProxy(t *testing.T) {
// Prepare the proxy server.
tlsConf, caPem := createServerTLSConfig(t)
dnsProxy := createTestProxy(t, tlsConf)
var gotAddr net.Addr
dnsProxy.RequestHandler = func(_ *Proxy, d *DNSContext) (err error) {
gotAddr = d.Addr
return dnsProxy.Resolve(d)
}
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(caPem)
require.True(t, ok)
dialer := &net.Dialer{
Timeout: defaultTimeout,
}
dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
// Route request to the DNS-over-HTTPS server address.
return dialer.DialContext(ctx, network, dnsProxy.Addr(ProtoHTTPS).String())
}
transport := &http.Transport{
TLSClientConfig: &tls.Config{
ServerName: tlsServerName,
RootCAs: roots,
},
DisableCompression: true,
DialContext: dialContext,
}
client := http.Client{
Transport: transport,
Timeout: defaultTimeout,
}
clientIP, proxyIP := net.IP{1, 2, 3, 4}, net.IP{127, 0, 0, 1}
msg := createTestMessage()
doRequest := func(t *testing.T, proxyAddr string) {
dnsProxy.TrustedProxies = []string{proxyAddr}
// Start listening.
serr := dnsProxy.Start()
require.NoError(t, serr)
t.Cleanup(func() {
derr := dnsProxy.Stop()
require.NoError(t, derr)
})
packed, err := msg.Pack()
require.NoError(t, err)
b := bytes.NewBuffer(packed)
req, err := http.NewRequest("POST", "https://test.com", b)
require.NoError(t, err)
req.Header.Set("Content-Type", "application/dns-message")
req.Header.Set("Accept", "application/dns-message")
// IP "1.2.3.4" will be used as a client address in DNSContext.
req.Header.Set("X-Forwarded-For", strings.Join(
[]string{clientIP.String(), proxyIP.String()},
",",
))
resp, err := client.Do(req)
require.NoError(t, err)
if resp != nil && resp.Body != nil {
t.Cleanup(func() {
resp.Body.Close()
})
}
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
reply := &dns.Msg{}
err = reply.Unpack(body)
require.NoError(t, err)
requireResponse(t, msg, reply)
}
t.Run("success", func(t *testing.T) {
doRequest(t, proxyIP.String())
ip, _ := netutil.IPAndPortFromAddr(gotAddr)
assert.True(t, ip.Equal(clientIP))
})
t.Run("not_in_trusted", func(t *testing.T) {
doRequest(t, "127.0.0.2")
ip, _ := netutil.IPAndPortFromAddr(gotAddr)
assert.True(t, ip.Equal(proxyIP))
})
}
func TestAddrsFromRequest(t *testing.T) {
theIP, anotherIP := net.IP{1, 2, 3, 4}, net.IP{1, 2, 3, 5}
theIPStr, anotherIPStr := theIP.String(), anotherIP.String()
testCases := []struct {
name string
hdrs map[string]string
wantIP net.IP
}{{
name: "cf-connecting-ip",
hdrs: map[string]string{
"CF-Connecting-IP": theIPStr,
},
wantIP: theIP,
}, {
name: "true-client-ip",
hdrs: map[string]string{
"True-Client-IP": theIPStr,
},
wantIP: theIP,
}, {
name: "x-real-ip",
hdrs: map[string]string{
"X-Real-IP": theIPStr,
},
wantIP: theIP,
}, {
name: "no_any",
hdrs: map[string]string{
"CF-Connecting-IP": "invalid",
"True-Client-IP": "invalid",
"X-Real-IP": "invalid",
},
wantIP: nil,
}, {
name: "priority",
hdrs: map[string]string{
"X-Forwarded-For": strings.Join([]string{anotherIPStr, theIPStr}, ","),
"True-Client-IP": anotherIPStr,
"X-Real-IP": anotherIPStr,
"CF-Connecting-IP": theIPStr,
},
wantIP: theIP,
}, {
name: "x-forwarded-for_simple",
hdrs: map[string]string{
"X-Forwarded-For": strings.Join([]string{anotherIPStr, theIPStr}, ","),
},
wantIP: anotherIP,
}, {
name: "x-forwarded-for_single",
hdrs: map[string]string{
"X-Forwarded-For": theIPStr,
},
wantIP: theIP,
}, {
name: "x-forwarded-for_invalid_proxy",
hdrs: map[string]string{
"X-Forwarded-For": strings.Join([]string{theIPStr, "invalid"}, ","),
},
wantIP: theIP,
}, {
name: "x-forwarded-for_empty",
hdrs: map[string]string{
"X-Forwarded-For": "",
},
wantIP: nil,
}, {
name: "x-forwarded-for_redundant_spaces",
hdrs: map[string]string{
"X-Forwarded-For": " " + theIPStr + " ,\t" + anotherIPStr,
},
wantIP: theIP,
}, {
name: "cf-connecting-ip_redundant_spaces",
hdrs: map[string]string{
"CF-Connecting-IP": " " + theIPStr + "\t",
},
wantIP: theIP,
}}
for _, tc := range testCases {
r, err := http.NewRequest("GET", "localhost", nil)
require.NoError(t, err)
for h, v := range tc.hdrs {
r.Header.Set(h, v)
}
t.Run(tc.name, func(t *testing.T) {
ip := realIPFromHdrs(r)
assert.True(t, tc.wantIP.Equal(ip))
})
}
}
func TestRemoteAddr(t *testing.T) {
theIP, anotherIP, thirdIP := net.IP{1, 2, 3, 4}, net.IP{1, 2, 3, 5}, net.IP{1, 2, 3, 6}
theIPStr, anotherIPStr, thirdIPStr := theIP.String(), anotherIP.String(), thirdIP.String()
rAddr := &net.TCPAddr{IP: theIP, Port: 1}
testCases := []struct {
name string
remoteAddr string
hdrs map[string]string
wantErr string
wantIP net.IP
wantProxy net.IP
}{{
name: "no_proxy",
remoteAddr: rAddr.String(),
hdrs: nil,
wantErr: "",
wantIP: theIP,
wantProxy: nil,
}, {
name: "proxied_with_cloudflare",
remoteAddr: rAddr.String(),
hdrs: map[string]string{
"CF-Connecting-IP": anotherIPStr,
},
wantErr: "",
wantIP: anotherIP,
wantProxy: theIP,
}, {
name: "proxied_once",
remoteAddr: rAddr.String(),
hdrs: map[string]string{
"X-Forwarded-For": anotherIPStr,
},
wantErr: "",
wantIP: anotherIP,
wantProxy: theIP,
}, {
name: "proxied_multiple",
remoteAddr: rAddr.String(),
hdrs: map[string]string{
"X-Forwarded-For": strings.Join([]string{anotherIPStr, thirdIPStr}, ","),
},
wantErr: "",
wantIP: anotherIP,
wantProxy: theIP,
}, {
name: "no_port",
remoteAddr: theIPStr,
hdrs: nil,
wantErr: "address " + theIPStr + ": missing port in address",
wantIP: nil,
wantProxy: nil,
}, {
name: "bad_port",
remoteAddr: theIPStr + ":notport",
hdrs: nil,
wantErr: "strconv.Atoi: parsing \"notport\": invalid syntax",
wantIP: nil,
wantProxy: nil,
}, {
name: "bad_host",
remoteAddr: "host:1",
hdrs: nil,
wantErr: "invalid ip: host",
wantIP: nil,
wantProxy: nil,
}, {
name: "bad_proxied_host",
remoteAddr: "host:1",
hdrs: map[string]string{
"CF-Connecting-IP": theIPStr,
},
wantErr: "invalid ip: host",
wantIP: nil,
wantProxy: nil,
}}
for _, tc := range testCases {
r, err := http.NewRequest("GET", "localhost", nil)
require.NoError(t, err)
r.RemoteAddr = tc.remoteAddr
for h, v := range tc.hdrs {
r.Header.Set(h, v)
}
t.Run(tc.name, func(t *testing.T) {
addr, prx, err := remoteAddr(r)
if tc.wantErr != "" {
assert.Equal(t, tc.wantErr, err.Error())
return
}
require.NoError(t, err)
ip, _ := netutil.IPAndPortFromAddr(addr)
assert.True(t, ip.Equal(tc.wantIP))
prxIP, _ := netutil.IPAndPortFromAddr(prx)
assert.True(t, tc.wantProxy.Equal(prxIP))
})
}
}