-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathproxy.go
More file actions
482 lines (428 loc) · 13.5 KB
/
proxy.go
File metadata and controls
482 lines (428 loc) · 13.5 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package devtoolsproxy
import (
"bufio"
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"net/url"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/coder/websocket"
"github.com/kernel/kernel-images/server/lib/scaletozero"
"github.com/kernel/kernel-images/server/lib/wsproxy"
)
var devtoolsListeningRegexp = regexp.MustCompile(`DevTools listening on (ws://\S+)`)
// UpstreamManager tails the Chromium supervisord log and extracts the current DevTools
// websocket URL, updating it whenever Chromium restarts and emits a new line.
type UpstreamManager struct {
logFilePath string
logger *slog.Logger
currentURL atomic.Value // string
startOnce sync.Once
stopOnce sync.Once
cancelTail context.CancelFunc
subsMu sync.RWMutex
subs map[chan string]struct{}
}
func NewUpstreamManager(logFilePath string, logger *slog.Logger) *UpstreamManager {
um := &UpstreamManager{logFilePath: logFilePath, logger: logger}
um.currentURL.Store("")
return um
}
// Start begins background tailing and updating the upstream URL until ctx is done.
func (u *UpstreamManager) Start(ctx context.Context) {
u.startOnce.Do(func() {
ctx, cancel := context.WithCancel(ctx)
u.cancelTail = cancel
go u.tailLoop(ctx)
})
}
// Stop cancels the background tailer.
func (u *UpstreamManager) Stop() {
u.stopOnce.Do(func() {
if u.cancelTail != nil {
u.cancelTail()
}
})
}
// WaitForInitial blocks until an initial upstream URL has been discovered or the timeout elapses.
func (u *UpstreamManager) WaitForInitial(timeout time.Duration) (string, error) {
deadline := time.Now().Add(timeout)
for {
if url := u.Current(); url != "" {
return url, nil
}
if time.Now().After(deadline) {
return "", fmt.Errorf("devtools upstream not found within %s", timeout)
}
time.Sleep(100 * time.Millisecond)
}
}
// Current returns the current upstream websocket URL if known, or empty string.
func (u *UpstreamManager) Current() string {
val, _ := u.currentURL.Load().(string)
return val
}
func (u *UpstreamManager) setCurrent(url string) {
prev := u.Current()
if url != "" && url != prev {
u.logger.Info("devtools upstream updated", slog.String("url", url))
u.currentURL.Store(url)
// Broadcast update to subscribers without blocking. If a subscriber's
// channel buffer (size 1) is full, replace the buffered value with the
// latest update to avoid dropping notifications entirely.
u.subsMu.RLock()
for ch := range u.subs {
select {
case ch <- url:
// sent successfully
default:
// channel is full; drop one stale value if present and try again
select {
case <-ch:
default:
}
select {
case ch <- url:
default:
// still full; give up to remain non-blocking
}
}
}
u.subsMu.RUnlock()
}
}
// Subscribe returns a channel that receives new upstream URLs as they are discovered.
// The returned cancel function should be called to unsubscribe and release resources.
func (u *UpstreamManager) Subscribe() (<-chan string, func()) {
// use channel size 1 to avoid setCurrent blocking/stalling on slow subscribers
// also provides "latest-wins" semantics: only one notification can sit in the channel
ch := make(chan string, 1)
u.subsMu.Lock()
if u.subs == nil {
u.subs = make(map[chan string]struct{})
}
u.subs[ch] = struct{}{}
u.subsMu.Unlock()
cancel := func() {
u.subsMu.Lock()
if _, ok := u.subs[ch]; ok {
delete(u.subs, ch)
close(ch)
}
u.subsMu.Unlock()
}
return ch, cancel
}
func (u *UpstreamManager) tailLoop(ctx context.Context) {
backoff := 250 * time.Millisecond
for {
if ctx.Err() != nil {
return
}
// Run one tail session. If it exits, retry with a small backoff.
u.runTailOnce(ctx)
select {
case <-ctx.Done():
return
case <-time.After(backoff):
}
// cap backoff to 2s
if backoff < 2*time.Second {
backoff *= 2
}
}
}
func (u *UpstreamManager) runTailOnce(ctx context.Context) {
cmd := exec.CommandContext(ctx, "tail", "-f", "-n", "+1", u.logFilePath)
stdout, err := cmd.StdoutPipe()
if err != nil {
u.logger.Error("failed to open tail stdout", slog.String("err", err.Error()))
return
}
if err := cmd.Start(); err != nil {
// Common when file does not exist yet; log at debug level
if strings.Contains(err.Error(), "No such file or directory") {
u.logger.Debug("supervisord log not found yet; will retry", slog.String("path", u.logFilePath))
} else {
u.logger.Error("failed to start tail", slog.String("err", err.Error()))
}
return
}
defer func() {
_ = cmd.Process.Kill()
_, _ = cmd.Process.Wait()
}()
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
select {
case <-ctx.Done():
return
default:
}
line := scanner.Text()
if matches := devtoolsListeningRegexp.FindStringSubmatch(line); len(matches) == 2 {
u.setCurrent(matches[1])
}
}
if err := scanner.Err(); err != nil && !errors.Is(err, context.Canceled) {
u.logger.Error("tail scanner error", slog.String("err", err.Error()))
}
}
func dialUpstreamWithRetry(ctx context.Context, mgr *UpstreamManager, urlCh <-chan string, initialUpstreamURL string, dialOpts *websocket.DialOptions, logger *slog.Logger) (*websocket.Conn, string, error) {
upstreamURL := normalizeUpstreamURL(initialUpstreamURL)
if upstreamURL == "" {
return nil, "", fmt.Errorf("upstream not ready")
}
deadline := time.NewTimer(5 * time.Second)
defer deadline.Stop()
for {
upstreamConn, _, err := websocket.Dial(ctx, upstreamURL, dialOpts)
if err == nil {
return upstreamConn, upstreamURL, nil
}
logger.Warn("dial upstream failed, checking for newer URL",
slog.String("err", err.Error()), slog.String("url", upstreamURL))
latestURL := normalizeUpstreamURL(mgr.Current())
if latestURL != "" && latestURL != upstreamURL {
upstreamURL = latestURL
continue
}
select {
case newURL, ok := <-urlCh:
if !ok {
return nil, "", fmt.Errorf("upstream unavailable")
}
newURL = normalizeUpstreamURL(newURL)
if newURL == "" || newURL == upstreamURL {
continue
}
upstreamURL = newURL
case <-deadline.C:
return nil, "", fmt.Errorf("timed out waiting for new upstream URL")
case <-ctx.Done():
return nil, "", ctx.Err()
}
}
}
func maybePauseAfterCurrentRead(ctx context.Context, logger *slog.Logger, r *http.Request) {
if r.URL.Query().Get("devtoolsProxyTestHook") != "1" {
return
}
// Test-only hook used by e2e to widen the window between reading Current
// and dialing/subscribing so reconnect races can be reproduced reliably.
rawDelayMs := os.Getenv("DEVTOOLS_PROXY_TEST_POST_CURRENT_DELAY_MS")
if rawDelayMs != "" {
delayMs, err := strconv.Atoi(rawDelayMs)
if err != nil || delayMs <= 0 {
logger.Warn("ignoring invalid devtools proxy test delay", slog.String("value", rawDelayMs))
} else {
timer := time.NewTimer(time.Duration(delayMs) * time.Millisecond)
defer timer.Stop()
select {
case <-timer.C:
case <-ctx.Done():
return
}
}
}
blockPath := os.Getenv("DEVTOOLS_PROXY_TEST_POST_CURRENT_BLOCK_FILE")
if blockPath == "" {
return
}
readyPath := blockPath + ".ready"
releasePath := blockPath + ".release"
if err := os.WriteFile(readyPath, []byte("ready\n"), 0o644); err != nil {
logger.Warn("failed to write devtools proxy test ready marker",
slog.String("path", readyPath),
slog.String("err", err.Error()))
return
}
ticker := time.NewTicker(50 * time.Millisecond)
defer ticker.Stop()
for {
if _, err := os.Stat(releasePath); err == nil {
return
} else if !os.IsNotExist(err) {
logger.Warn("failed to read devtools proxy test release marker",
slog.String("path", releasePath),
slog.String("err", err.Error()))
return
}
select {
case <-ticker.C:
case <-ctx.Done():
return
}
}
}
// WebSocketProxyHandler returns an http.Handler that upgrades incoming connections and
// proxies them to the current upstream websocket URL. It expects only websocket requests.
// If logCDPMessages is true, all CDP messages will be logged with their direction.
func WebSocketProxyHandler(mgr *UpstreamManager, logger *slog.Logger, logCDPMessages bool, ctrl scaletozero.Controller) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var transform wsproxy.MessageTransform
if logCDPMessages {
transform = func(direction string, mt websocket.MessageType, msg []byte) []byte {
logCDPMessage(logger, direction, mt, msg)
return msg
}
}
acceptOpts := &websocket.AcceptOptions{
OriginPatterns: []string{"*"},
CompressionMode: websocket.CompressionContextTakeover,
}
dialOpts := &websocket.DialOptions{
CompressionMode: websocket.CompressionContextTakeover,
}
// Subscribe to upstream URL changes so we can tear down stale sessions
// when Chromium restarts and retry if the current URL is already dead.
urlCh, unsub := mgr.Subscribe()
defer unsub()
upstreamCurrent := mgr.Current()
if upstreamCurrent == "" {
http.Error(w, "upstream not ready", http.StatusServiceUnavailable)
return
}
maybePauseAfterCurrentRead(r.Context(), logger, r)
// Accept the client WebSocket connection.
clientConn, err := websocket.Accept(w, r, acceptOpts)
if err != nil {
logger.Error("websocket accept failed", slog.String("err", err.Error()))
return
}
clientConn.SetReadLimit(100 * 1024 * 1024)
// Dial upstream. If the URL is stale (Chromium just restarted), first
// re-check the manager's latest URL in case we missed the notification,
// then wait briefly for the next update from Subscribe.
upstreamConn, upstreamURL, err := dialUpstreamWithRetry(r.Context(), mgr, urlCh, upstreamCurrent, dialOpts, logger)
if err != nil {
switch {
case errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded), errors.Is(r.Context().Err(), context.Canceled), errors.Is(r.Context().Err(), context.DeadlineExceeded):
clientConn.Close(websocket.StatusGoingAway, "request cancelled")
default:
logger.Error("failed to connect to upstream", slog.String("err", err.Error()))
clientConn.Close(websocket.StatusInternalError, "upstream unavailable")
}
return
}
upstreamConn.SetReadLimit(100 * 1024 * 1024)
logger.Debug("proxying websocket", slog.String("url", upstreamURL))
// Cancel the pump when the upstream URL changes (Chromium restarted),
// forcing the client to reconnect with the new upstream.
pumpCtx, pumpCancel := context.WithCancel(r.Context())
go func(currentUpstreamURL string) {
for {
select {
case newURL, ok := <-urlCh:
if !ok {
return
}
newURL = normalizeUpstreamURL(newURL)
if newURL == "" || newURL == currentUpstreamURL {
continue
}
logger.Info("upstream URL changed, closing stale proxy session",
slog.String("old_url", currentUpstreamURL),
slog.String("new_url", newURL))
pumpCancel()
return
case <-pumpCtx.Done():
return
}
}
}(upstreamURL)
var once sync.Once
cleanup := func() {
once.Do(func() {
pumpCancel()
upstreamConn.Close(websocket.StatusNormalClosure, "")
clientConn.Close(websocket.StatusNormalClosure, "")
})
}
wsproxy.Pump(pumpCtx, clientConn, upstreamConn, cleanup, logger, transform)
})
}
// normalizeUpstreamURL parses a raw DevTools URL and returns a clean form.
func normalizeUpstreamURL(raw string) string {
parsed, err := url.Parse(raw)
if err != nil {
return raw
}
return (&url.URL{Scheme: parsed.Scheme, Host: parsed.Host, Path: parsed.Path, RawQuery: parsed.RawQuery}).String()
}
// logCDPMessage logs a CDP message with its direction if logging is enabled
func logCDPMessage(logger *slog.Logger, direction string, mt websocket.MessageType, msg []byte) {
if mt != websocket.MessageText {
return // Only log text messages (CDP messages)
}
// Extract fields using regex from raw message
rawMsg := string(msg)
// Regex patterns to match "key":"val" or "key": "val" for string values
extractStringField := func(key string) string {
pattern := fmt.Sprintf(`"%s"\s*:\s*"([^"]*)"`, key)
re := regexp.MustCompile(pattern)
matches := re.FindStringSubmatch(rawMsg)
if len(matches) > 1 {
return matches[1]
}
return ""
}
// Regex pattern to match "key": number for numeric id
extractNumberField := func(key string) interface{} {
pattern := fmt.Sprintf(`"%s"\s*:\s*(\d+)`, key)
re := regexp.MustCompile(pattern)
matches := re.FindStringSubmatch(rawMsg)
if len(matches) > 1 {
// Try to parse as int first
if val, err := strconv.Atoi(matches[1]); err == nil {
return val
}
// Fall back to float64
if val, err := strconv.ParseFloat(matches[1], 64); err == nil {
return val
}
}
return nil
}
// Extract fields using regex
method := extractStringField("method")
id := extractNumberField("id")
sessionId := extractStringField("sessionId")
targetId := extractStringField("targetId")
frameId := extractStringField("frameId")
// Build log attributes, only including non-empty values
attrs := []slog.Attr{
slog.String("dir", direction),
}
if sessionId != "" {
attrs = append(attrs, slog.String("sessionId", sessionId))
}
if targetId != "" {
attrs = append(attrs, slog.String("targetId", targetId))
}
if id != nil {
attrs = append(attrs, slog.Any("id", id))
}
if frameId != "" {
attrs = append(attrs, slog.String("frameId", frameId))
}
if method != "" {
attrs = append(attrs, slog.String("method", method))
}
attrs = append(attrs, slog.Int("raw_length", len(msg)))
// Convert attrs to individual slog.Attr arguments
args := make([]any, len(attrs))
for i, attr := range attrs {
args[i] = attr
}
logger.Info("cdp", args...)
}