-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkindling_test.go
More file actions
353 lines (317 loc) · 9.7 KB
/
kindling_test.go
File metadata and controls
353 lines (317 loc) · 9.7 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package kindling
import (
"context"
"fmt"
"io"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/getlantern/domainfront"
)
func TestNewKindling(t *testing.T) {
t.Parallel()
t.Run("Success", func(t *testing.T) {
t.Parallel()
cfg := &domainfront.Config{
Providers: map[string]*domainfront.Provider{
"cloudfront": {
HostAliases: map[string]string{"example.com": "d1234.cloudfront.net"},
TestURL: "https://d1234.cloudfront.net/ping",
Masquerades: []*domainfront.Masquerade{
{Domain: "d5678.cloudfront.net", IpAddress: "13.224.0.1"},
},
},
"akamai": {
HostAliases: map[string]string{"api.example.com": "api.dsa.akamai.example.com"},
TestURL: "https://fronted-ping.dsa.akamai.example.com/ping",
Masquerades: []*domainfront.Masquerade{
{Domain: "a248.e.akamai.net", IpAddress: "23.192.228.145"},
},
},
},
}
c, err := domainfront.New(context.Background(), cfg)
if err != nil {
t.Fatalf("domainfront.New() error = %v", err)
}
defer c.Close()
k, err := NewKindling("kindling",
WithDomainFronting(c),
WithPanicListener(func(string) {}),
)
if err != nil {
t.Fatalf("NewKindling() error = %v", err)
}
if k == nil {
t.Error("NewKindling() = nil; want non-nil")
}
})
t.Run("NilFronted_ReturnsError", func(t *testing.T) {
t.Parallel()
_, err := NewKindling("test", WithDomainFronting(nil))
if err == nil {
t.Error("NewKindling(WithDomainFronting(nil)) should return error")
}
})
t.Run("NilDNSTT_ReturnsError", func(t *testing.T) {
t.Parallel()
_, err := NewKindling("test", WithDNSTunnel(nil))
if err == nil {
t.Error("NewKindling(WithDNSTunnel(nil)) should return error")
}
})
t.Run("NilAMP_ReturnsError", func(t *testing.T) {
t.Parallel()
_, err := NewKindling("test", WithAMPCache(nil))
if err == nil {
t.Error("NewKindling(WithAMPCache(nil)) should return error")
}
})
t.Run("NilTransport_ReturnsError", func(t *testing.T) {
t.Parallel()
_, err := NewKindling("test", WithTransport(nil))
if err == nil {
t.Error("NewKindling(WithTransport(nil)) should return error")
}
})
}
func TestLantern(t *testing.T) {
if os.Getenv("KINDLING_INTEGRATION") == "" {
t.Skip("skipping integration test; set KINDLING_INTEGRATION=1 to run")
}
k, err := NewKindling("kindling",
WithProxyless("config.getiantem.org"),
)
if err != nil {
t.Fatalf("NewKindling() error = %v", err)
}
client := k.NewHTTPClient()
r, err := newRequestWithHeaders(context.Background(), "POST", "https://config.getiantem.org:443/proxies.yaml.gz", http.NoBody)
if err != nil {
t.Fatalf("newRequestWithHeaders() error = %v", err)
}
res, err := client.Do(r)
if err != nil {
t.Fatalf("client.Do() error = %v", err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
t.Fatalf("io.ReadAll() error = %v", err)
}
fmt.Printf("Response: %d bytes, status %d\n", len(body), res.StatusCode)
}
// TestLantern_DomainFronting exercises the full Kindling → domainfront path
// against real CDN infrastructure: it fetches the production fronted.yaml.gz
// (which ships both CloudFront and Akamai providers), constructs a Kindling
// with WithDomainFronting, and performs a real request through the fronted
// transport. Passes only when a front actually works end-to-end.
//
// Gated on KINDLING_INTEGRATION=1 because it hits external CDN endpoints.
func TestLantern_DomainFronting(t *testing.T) {
if os.Getenv("KINDLING_INTEGRATION") == "" {
t.Skip("skipping integration test; set KINDLING_INTEGRATION=1 to run")
}
const configURL = "https://raw.githubusercontent.com/getlantern/fronted/refs/heads/main/fronted.yaml.gz"
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, configURL, nil)
if err != nil {
t.Fatalf("new config request: %v", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("fetch fronted.yaml.gz: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected config status: %d", resp.StatusCode)
}
cfgBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("read config: %v", err)
}
cfg, err := domainfront.ParseConfig(cfgBytes)
if err != nil {
t.Fatalf("parse config: %v", err)
}
if _, ok := cfg.Providers["cloudfront"]; !ok {
t.Fatalf("expected cloudfront provider in config")
}
if _, ok := cfg.Providers["akamai"]; !ok {
t.Fatalf("expected akamai provider in config")
}
t.Logf("loaded config with %d providers", len(cfg.Providers))
df, err := domainfront.New(ctx, cfg,
domainfront.WithConfigURL(configURL),
)
if err != nil {
t.Fatalf("domainfront.New: %v", err)
}
defer df.Close()
k, err := NewKindling("kindling-integration",
WithDomainFronting(df),
)
if err != nil {
t.Fatalf("NewKindling: %v", err)
}
// Target a host that the production fronted.yaml.gz maps to a real CDN
// distribution. config.getiantem.org is the canonical mapped origin that
// is used in production.
client := k.NewHTTPClient()
r, err := newRequestWithHeaders(ctx, http.MethodPost, "https://config.getiantem.org/proxies.yaml.gz", http.NoBody)
if err != nil {
t.Fatalf("newRequestWithHeaders: %v", err)
}
res, err := client.Do(r)
if err != nil {
t.Fatalf("client.Do via domain fronting: %v", err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
t.Fatalf("read body: %v", err)
}
t.Logf("domainfront response: status=%d bodyLen=%d", res.StatusCode, len(body))
// Status-code assertions only: a successful domainfront request can legitimately
// return an empty body (204, 304, a 405 with no body, etc). The status codes
// below are the ones that indicate the CDN rejected us (400/403) or the
// origin failed (5xx) — anything else means we tunneled through.
if res.StatusCode >= 500 {
t.Fatalf("origin returned server error: %d", res.StatusCode)
}
if res.StatusCode == http.StatusBadRequest || res.StatusCode == http.StatusForbidden {
t.Fatalf("CDN rejected request (status %d) — domain fronting failed", res.StatusCode)
}
}
const (
appVersionHeader = "X-Lantern-App-Version"
versionHeader = "X-Lantern-Version"
platformHeader = "X-Lantern-Platform"
appNameHeader = "X-Lantern-App"
deviceIdHeader = "X-Lantern-Device-Id"
userIdHeader = "X-Lantern-User-Id"
)
const (
AppName = "kindling"
ClientVersion = "7.6.47"
Version = "7.6.47"
Platform = "linux"
DeviceId = "some-uuid-here"
UserId = "23409"
ProToken = ""
)
func newRequestWithHeaders(ctx context.Context, method, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return nil, err
}
req.Header.Set(appVersionHeader, ClientVersion)
req.Header.Set(versionHeader, Version)
req.Header.Set(userIdHeader, UserId)
req.Header.Set(platformHeader, Platform)
req.Header.Set(appNameHeader, AppName)
req.Header.Set(deviceIdHeader, DeviceId)
return req, nil
}
type dummyRoundTripper struct{}
func (d *dummyRoundTripper) RoundTrip(*http.Request) (*http.Response, error) {
return nil, nil
}
func TestKindling_Suite(t *testing.T) {
t.Parallel()
t.Run("NewKindling", func(t *testing.T) {
t.Parallel()
k, err := NewKindling("kindling")
if err != nil {
t.Fatalf("NewKindling() error = %v", err)
}
if k == nil {
t.Error("NewKindling() = nil; want non-nil")
}
})
t.Run("NewHTTPClient", func(t *testing.T) {
t.Parallel()
k, err := NewKindling("kindling")
if err != nil {
t.Fatalf("NewKindling() error = %v", err)
}
client := k.NewHTTPClient()
if client == nil {
t.Error("NewHTTPClient() = nil; want non-nil")
}
})
}
func TestReplaceTransport(t *testing.T) {
t.Parallel()
makeTransport := func(name string) Transport {
return &namedTransport{
name: name,
maxLength: 100,
isStreamable: true,
newRT: func(ctx context.Context, addr string) (http.RoundTripper, error) {
return &dummyRoundTripper{}, nil
},
}
}
t.Run("Success", func(t *testing.T) {
t.Parallel()
k, err := NewKindling("test", WithTransport(makeTransport("test-transport")))
if err != nil {
t.Fatal(err)
}
err = k.ReplaceTransport("test-transport", func(ctx context.Context, addr string) (http.RoundTripper, error) {
return &dummyRoundTripper{}, nil
})
if err != nil {
t.Errorf("ReplaceTransport() error = %v; want nil", err)
}
})
t.Run("NotFound", func(t *testing.T) {
t.Parallel()
k, err := NewKindling("test", WithTransport(makeTransport("test-transport")))
if err != nil {
t.Fatal(err)
}
err = k.ReplaceTransport("nonexistent", func(ctx context.Context, addr string) (http.RoundTripper, error) {
return &dummyRoundTripper{}, nil
})
if err == nil {
t.Error("ReplaceTransport() should fail for unknown transport")
}
if !strings.Contains(err.Error(), "nonexistent") {
t.Errorf("error should mention transport name, got: %v", err)
}
})
t.Run("Multiple", func(t *testing.T) {
t.Parallel()
k, err := NewKindling("test",
WithTransport(makeTransport("transport-1")),
WithTransport(makeTransport("transport-2")),
)
if err != nil {
t.Fatal(err)
}
err = k.ReplaceTransport("transport-2", func(ctx context.Context, addr string) (http.RoundTripper, error) {
return &dummyRoundTripper{}, nil
})
if err != nil {
t.Errorf("ReplaceTransport() error = %v; want nil", err)
}
})
t.Run("EmptyTransportList", func(t *testing.T) {
t.Parallel()
k, err := NewKindling("test")
if err != nil {
t.Fatal(err)
}
err = k.ReplaceTransport("any", func(ctx context.Context, addr string) (http.RoundTripper, error) {
return &dummyRoundTripper{}, nil
})
if err == nil {
t.Error("ReplaceTransport() should fail with no transports")
}
})
}