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
|
//go:build integration
// Integration tests for Anubis, using Playwright.
//
// These tests require an already running Anubis and Playwright server.
//
// Anubis must be configured to redirect to the server started by the test suite.
// The bind address and the Anubis server can be specified using the flags `-bind` and `-anubis` respectively.
//
// Playwright must be started in server mode using `npx playwright@1.50.1 run-server --port 3000`.
// The version must match the minor used by the playwright-go package.
//
// On unsupported systems you may be able to use a container instead: https://playwright.dev/docs/docker#remote-connection
//
// In that case you may need to set the `-playwright` flag to the container's URL, and specify the `--host` the run-server command listens on.
package test
import (
"context"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"testing"
"time"
"github.com/playwright-community/playwright-go"
)
var (
anubisServer = flag.String("anubis", "http://localhost:8923", "Anubis server URL")
serverBindAddr = flag.String("bind", "localhost:3923", "test server bind address")
playwrightServer = flag.String("playwright", "ws://localhost:3000", "Playwright server URL")
playwrightMaxTime = flag.Duration("playwright-max-time", 5*time.Second, "maximum time for Playwright requests")
playwrightMaxHardTime = flag.Duration("playwright-max-hard-time", 5*time.Minute, "maximum time for hard Playwright requests")
testCases = []testCase{
{name: "firefox", action: actionChallenge, realIP: placeholderIP, userAgent: "Mozilla/5.0 (X11; Linux x86_64; rv:136.0) Gecko/20100101 Firefox/136.0"},
{name: "headlessChrome", action: actionDeny, realIP: placeholderIP, userAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/120.0.6099.28 Safari/537.36"},
{name: "kagiBadIP", action: actionChallenge, isHard: true, realIP: placeholderIP, userAgent: "Mozilla/5.0 (compatible; Kagibot/1.0; +https://kagi.com/bot)"},
{name: "kagiGoodIP", action: actionAllow, realIP: "216.18.205.234", userAgent: "Mozilla/5.0 (compatible; Kagibot/1.0; +https://kagi.com/bot)"},
{name: "unknownAgent", action: actionAllow, realIP: placeholderIP, userAgent: "AnubisTest/0"},
}
)
const (
actionAllow action = "ALLOW"
actionDeny action = "DENY"
actionChallenge action = "CHALLENGE"
placeholderIP = "fd11:5ee:bad:c0de::"
)
type action string
type testCase struct {
name string
action action
isHard bool
realIP, userAgent string
}
func TestPlaywrightBrowser(t *testing.T) {
pw := setupPlaywright(t)
spawnTestServer(t)
browsers := []playwright.BrowserType{pw.Chromium, pw.Firefox, pw.WebKit}
for _, typ := range browsers {
for _, tc := range testCases {
name := fmt.Sprintf("%s@%s", tc.name, typ.Name())
t.Run(name, func(t *testing.T) {
_, hasDeadline := t.Deadline()
if tc.isHard && hasDeadline {
t.Skip("skipping hard challenge with deadline")
}
perfomedAction := executeTestCase(t, tc, typ)
if perfomedAction != tc.action {
t.Errorf("unexpected test result, expected %s, got %s", tc.action, perfomedAction)
} else {
t.Logf("test passed")
}
})
}
}
}
func buildBrowserConnect(name string) string {
u, _ := url.Parse(*playwrightServer)
q := u.Query()
q.Set("browser", name)
u.RawQuery = q.Encode()
return u.String()
}
func executeTestCase(t *testing.T, tc testCase, typ playwright.BrowserType) action {
deadline, _ := t.Deadline()
browser, err := typ.Connect(buildBrowserConnect(typ.Name()), playwright.BrowserTypeConnectOptions{
ExposeNetwork: playwright.String("<loopback>"),
})
if err != nil {
t.Fatalf("could not connect to remote browser: %v", err)
}
defer browser.Close()
ctx, err := browser.NewContext(playwright.BrowserNewContextOptions{
AcceptDownloads: playwright.Bool(false),
ExtraHttpHeaders: map[string]string{
"X-Real-Ip": tc.realIP,
},
UserAgent: playwright.String(tc.userAgent),
})
if err != nil {
t.Fatalf("could not create context: %v", err)
}
defer ctx.Close()
page, err := ctx.NewPage()
if err != nil {
t.Fatalf("could not create page: %v", err)
}
defer page.Close()
// Attempt challenge.
start := time.Now()
_, err = page.Goto(*anubisServer, playwright.PageGotoOptions{
Timeout: pwTimeout(tc, deadline),
})
if err != nil {
pwFail(t, page, "could not navigate to test server: %v", err)
}
hadChallenge := false
switch tc.action {
case actionChallenge:
// FIXME: This could race if challenge is completed too quickly.
checkImage(t, tc, deadline, page, "#image[src*=pensive], #image[src*=happy]")
hadChallenge = true
case actionDeny:
checkImage(t, tc, deadline, page, "#image[src*=sad]")
return actionDeny
}
// Ensure protected resource was provided.
res, err := page.Locator("#anubis-test").TextContent(playwright.LocatorTextContentOptions{
Timeout: pwTimeout(tc, deadline),
})
end := time.Now()
if err != nil {
pwFail(t, page, "could not get text content: %v", err)
}
var tm int64
if _, err := fmt.Sscanf(res, "%d", &tm); err != nil {
pwFail(t, page, "unexpected output: %s", res)
}
if tm < start.Unix() || end.Unix() < tm {
pwFail(t, page, "unexpected timestamp in output: %d not in range %d..%d", tm, start.Unix(), end.Unix())
}
if hadChallenge {
return actionChallenge
} else {
return actionAllow
}
}
func checkImage(t *testing.T, tc testCase, deadline time.Time, page playwright.Page, locator string) {
image := page.Locator(locator)
err := image.WaitFor(playwright.LocatorWaitForOptions{
Timeout: pwTimeout(tc, deadline),
})
if err != nil {
pwFail(t, page, "could not wait for result: %v", err)
}
failIsVisible, err := image.IsVisible()
if err != nil {
pwFail(t, page, "could not check result image: %v", err)
}
if !failIsVisible {
pwFail(t, page, "expected result image not visible")
}
}
func pwFail(t *testing.T, page playwright.Page, format string, args ...any) {
t.Helper()
saveScreenshot(t, page)
t.Fatalf(format, args...)
}
func pwTimeout(tc testCase, deadline time.Time) *float64 {
max := *playwrightMaxTime
if tc.isHard {
max = *playwrightMaxHardTime
}
d := deadline.Sub(time.Now())
if d <= 0 || d > max {
return playwright.Float(float64(max.Milliseconds()))
}
return playwright.Float(float64(d.Milliseconds()))
}
func saveScreenshot(t *testing.T, page playwright.Page) {
t.Helper()
data, err := page.Screenshot()
if err != nil {
t.Logf("could not take screenshot: %v", err)
return
}
f, err := os.CreateTemp("",
|