-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.go
More file actions
658 lines (547 loc) · 15.4 KB
/
Copy pathproxy.go
File metadata and controls
658 lines (547 loc) · 15.4 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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
package sprites
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"log/slog"
"net"
"net/http"
"net/url"
"strconv"
"sync"
"time"
"github.com/gorilla/websocket"
)
const (
// TODO: should this be configurable?
proxyCloseDeadline = 1 * time.Second
)
// proxyConn wraps a WebSocket connection and implements [net.Conn].
type proxyConn struct {
conn *websocket.Conn
remoteAddr *proxyAddr
closeOnce sync.Once
readReader io.Reader // current message reader
readMu sync.Mutex
writeMu sync.Mutex
}
// proxyAddr implements [net.Addr].
type proxyAddr struct {
network string
address string
}
func (a *proxyAddr) Network() string {
return a.network
}
func (a *proxyAddr) String() string {
return a.address
}
func (conn *proxyConn) Read(p []byte) (n int, err error) {
conn.readMu.Lock()
defer conn.readMu.Unlock()
for {
// if we have a reader, read from it
if conn.readReader != nil {
n, err = conn.readReader.Read(p)
if errors.Is(err, io.EOF) {
// the current reader is empty, fetch the next frame
conn.readReader = nil
continue
}
return n, err
}
messageType, reader, err := conn.conn.NextReader()
if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
return 0, io.EOF
}
return 0, err
}
// only handle binary messages
if messageType != websocket.BinaryMessage {
continue
}
conn.readReader = reader
}
}
func (conn *proxyConn) Write(p []byte) (n int, err error) {
conn.writeMu.Lock()
defer conn.writeMu.Unlock()
err = conn.conn.WriteMessage(websocket.BinaryMessage, p)
if err != nil {
return 0, err
}
return len(p), nil
}
func (conn *proxyConn) Close() error {
var closeErr error
conn.closeOnce.Do(func() {
// send close message with deadline
deadline := time.Now().Add(proxyCloseDeadline)
conn.conn.SetWriteDeadline(deadline)
conn.conn.WriteControl(
websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""),
deadline,
)
closeErr = conn.conn.Close()
})
return closeErr
}
func (conn *proxyConn) LocalAddr() net.Addr {
// there is no local address, return nil
return nil
}
func (conn *proxyConn) RemoteAddr() net.Addr {
return conn.remoteAddr
}
func (conn *proxyConn) SetDeadline(t time.Time) error {
if err := conn.SetReadDeadline(t); err != nil {
return err
}
return conn.SetWriteDeadline(t)
}
func (conn *proxyConn) SetReadDeadline(t time.Time) error {
return conn.conn.SetReadDeadline(t)
}
func (conn *proxyConn) SetWriteDeadline(t time.Time) error {
return conn.conn.SetWriteDeadline(t)
}
// controlProxyConn wraps a WebSocket connection and implements [net.Conn].
//
// It is identical to [proxyConn], except [controlProxyConn.Close] returns the
// WebSocket to the pool instead of closing it.
type controlProxyConn struct {
*proxyConn
control *controlConn
pool *controlPool
}
func (c *controlProxyConn) Close() error {
c.closeOnce.Do(func() {
// keep the socket open, but return it to the pool
c.control.sendRelease()
c.pool.checkin(c.control)
})
return nil
}
// ProxySocket establishes a proxied connection to a port on a sprite.
//
// The only known network is "tcp".
func (c *Client) ProxySocket(ctx context.Context, network, spriteName, addr string) (net.Conn, error) {
switch network {
case "tcp":
wsConn, err := c.dialProxyWebSocket(ctx, spriteName)
if err != nil {
return nil, err
}
return initSocketTCP(ctx, wsConn, addr)
default:
return nil, fmt.Errorf("unsupported network type %q", network)
}
}
// parseProxyAddr parses a proxy address into host and port components.
// Returns host (defaults to "localhost" if empty) and port (1-65535).
func parseProxyAddr(addr string) (host string, port int, err error) {
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
return "", 0, fmt.Errorf("invalid address %q: %w", addr, err)
}
portUint, err := strconv.ParseUint(portStr, 10, 16)
if err != nil || portUint < 1 {
return "", 0, fmt.Errorf("invalid port in address %q: must be 1-65535", addr)
}
if host == "" {
host = "localhost"
}
return host, int(portUint), nil
}
// wsProxyHandshake sends a proxy initialization message and waits for a
// response. On successful connection, it returns the wrapped proxy.
func wsProxyHandshake(
ctx context.Context, wsConn *websocket.Conn, initMsg any,
) (*proxyConn, error) {
var response ProxyResponseMessage
errChan := make(chan error, 1)
// Race with ctx.Done in a goroutine
go func() {
var err error
if writeErr := wsConn.WriteJSON(initMsg); writeErr != nil {
err = fmt.Errorf("failed to send init message: %w", writeErr)
} else if readErr := wsConn.ReadJSON(&response); readErr != nil {
err = fmt.Errorf("failed to read response: %w", readErr)
} else if response.Status != "connected" {
err = fmt.Errorf("unexpected status: %s", response.Status)
}
errChan <- err
}()
select {
case <-ctx.Done():
return nil, ctx.Err()
case err := <-errChan:
if err != nil {
return nil, err
}
return &proxyConn{
remoteAddr: &proxyAddr{
network: "tcp",
address: response.Target,
},
conn: wsConn,
readReader: nil,
}, nil
}
}
// initSocketTCP initializes a TCP proxy session over a WebSocket.
func initSocketTCP(ctx context.Context, wsConn *websocket.Conn, addr string) (net.Conn, error) {
host, port, err := parseProxyAddr(addr)
if err != nil {
return nil, err
}
initMsg := &ProxyInitMessage{
Host: host,
Port: port,
}
return wsProxyHandshake(ctx, wsConn, initMsg)
}
// initControlSocketTCP initializes a TCP proxy session over a pooled control
// WebSocket.
func initControlSocketTCP(
ctx context.Context,
wsConn *websocket.Conn,
addr string,
control *controlConn,
pool *controlPool,
) (net.Conn, error) {
host, port, err := parseProxyAddr(addr)
if err != nil {
return nil, err
}
initMsg := map[string]any{
"type": "start",
"operation": "proxy",
"params": map[string]any{
"host": host,
"port": port,
"keep_alive": true,
},
}
proxyConn, err := wsProxyHandshake(ctx, wsConn, initMsg)
if err != nil {
return nil, err
}
return &controlProxyConn{
proxyConn: proxyConn,
control: control,
pool: pool,
}, nil
}
func (c *Client) dialProxyWebSocket(ctx context.Context, spriteName string) (*websocket.Conn, error) {
// Build WebSocket URL for proxy
wsURL, err := c.buildProxyURL(spriteName)
if err != nil {
return nil, err
}
// Set up WebSocket dialer
dialer := &websocket.Dialer{
ReadBufferSize: 1024 * 1024, // 1MB
WriteBufferSize: 1024 * 1024, // 1MB
}
// Add TLS config if needed
if wsURL.Scheme == "wss" {
dialer.TLSClientConfig = &tls.Config{
InsecureSkipVerify: false,
}
}
// Set headers including auth
header := http.Header{}
header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
header.Set("User-Agent", "sprites-go-sdk/1.0")
header.Set("Sprite-Client-Features", "control")
applyClientSignalHeaders(header, c.clientSignals)
// Connect to WebSocket
wsConn, resp, err := dialer.DialContext(ctx, wsURL.String(), header)
if err != nil {
// On success, resp.Body must be left alone: gorilla/websocket hands
// back the *http.Response tied to the now-live connection, and
// closing its body here would close the WebSocket too. Only close
// it in the error path, where resp (if non-nil) is just a
// diagnostic response body, not the live connection.
if resp != nil {
resp.Body.Close()
}
return nil, fmt.Errorf("failed to connect: %w", err)
}
return wsConn, nil
}
// ProxySession represents an active port proxy session
type ProxySession struct {
LocalPort int
RemotePort int
RemoteHost string // Optional: specific host to connect to (e.g., "10.0.0.1", "fdf::1"). Defaults to "localhost" if empty.
listener net.Listener
ctx context.Context
cancel context.CancelFunc
closeOnce sync.Once
closed chan struct{}
client *Client
spriteName string
}
// PortMapping represents a local to remote port mapping
type PortMapping struct {
LocalPort int
RemotePort int
RemoteHost string // Optional: specific host to connect to (e.g., "10.0.0.1", "fdf::1"). Defaults to "localhost" if empty.
}
// ProxyPort creates a proxy session for a single port
func (c *Client) ProxyPort(ctx context.Context, spriteName string, localPort, remotePort int) (*ProxySession, error) {
mapping := PortMapping{
LocalPort: localPort,
RemotePort: remotePort,
}
sessions, err := c.ProxyPorts(ctx, spriteName, []PortMapping{mapping})
if err != nil {
return nil, err
}
if len(sessions) != 1 {
return nil, fmt.Errorf("unexpected number of proxy sessions created")
}
return sessions[0], nil
}
// ProxyPorts creates proxy sessions for multiple port mappings
func (c *Client) ProxyPorts(ctx context.Context, spriteName string, mappings []PortMapping) ([]*ProxySession, error) {
var sessions []*ProxySession
for _, mapping := range mappings {
session, err := c.createProxySession(ctx, spriteName, mapping)
if err != nil {
// Clean up any sessions we already created
for _, s := range sessions {
s.Close()
}
return nil, fmt.Errorf("failed to create proxy for port %d: %w", mapping.LocalPort, err)
}
sessions = append(sessions, session)
}
return sessions, nil
}
// createProxySession creates a single proxy session
func (c *Client) createProxySession(ctx context.Context, spriteName string, mapping PortMapping) (*ProxySession, error) {
// Start local listener
listener, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", mapping.LocalPort))
if err != nil {
return nil, fmt.Errorf("failed to listen on port %d: %w", mapping.LocalPort, err)
}
sessionCtx, cancel := context.WithCancel(ctx)
session := &ProxySession{
LocalPort: mapping.LocalPort,
RemotePort: mapping.RemotePort,
RemoteHost: mapping.RemoteHost,
listener: listener,
ctx: sessionCtx,
cancel: cancel,
closed: make(chan struct{}),
client: c,
spriteName: spriteName,
}
// Log start of proxy listening
slog.Default().Debug("Starting proxy listener",
"sprite", spriteName,
"local", fmt.Sprintf("localhost:%d", session.LocalPort),
"remote_host", func() string {
if session.RemoteHost != "" {
return session.RemoteHost
}
return "localhost"
}(),
"remote_port", session.RemotePort,
)
// Start accepting connections
go session.acceptLoop()
return session, nil
}
// ProxySocket establishes a proxied connection to a port on this sprite
func (s *Sprite) ProxySocket(ctx context.Context, network, addr string) (net.Conn, error) {
return s.client.ProxySocket(ctx, network, s.name, addr)
}
// ProxyPort creates a proxy session for a single port on this sprite
func (s *Sprite) ProxyPort(ctx context.Context, localPort, remotePort int) (*ProxySession, error) {
return s.client.ProxyPort(ctx, s.name, localPort, remotePort)
}
// ProxyPorts creates proxy sessions for multiple port mappings on this sprite
func (s *Sprite) ProxyPorts(ctx context.Context, mappings []PortMapping) ([]*ProxySession, error) {
return s.client.ProxyPorts(ctx, s.name, mappings)
}
// acceptLoop accepts incoming connections and handles them
func (ps *ProxySession) acceptLoop() {
defer ps.Close()
for {
select {
case <-ps.ctx.Done():
return
case <-ps.closed:
return
default:
}
conn, err := ps.listener.Accept()
if err != nil {
select {
case <-ps.ctx.Done():
return
case <-ps.closed:
return
default:
// Log error but continue accepting
continue
}
}
// Handle connection in goroutine
go ps.handleConnection(conn)
}
}
// handleConnection handles a single proxy connection
func (ps *ProxySession) handleConnection(localConn net.Conn) {
defer localConn.Close()
// Check if sprite supports control connections (lazy check on first use)
sprite := ps.client.Sprite(ps.spriteName)
sprite.ensureControlSupport(ps.ctx)
var remoteConn net.Conn
var err error
addr := net.JoinHostPort(ps.RemoteHost, strconv.Itoa(ps.RemotePort))
if sprite.supportsControl {
// Try to use control connection
pool := ps.client.getOrCreatePool(ps.spriteName)
controlConn, checkoutErr := pool.checkout(ps.ctx)
if checkoutErr == nil && controlConn != nil {
// Successfully got a control connection
remoteConn, err = initControlSocketTCP(ps.ctx, controlConn.ws, addr, controlConn, pool)
if err != nil {
slog.Default().Debug("Pooled proxy initialization failed",
"sprite", ps.spriteName,
"error", err,
)
controlConn.sendRelease()
pool.checkin(controlConn)
return
}
}
}
if remoteConn == nil {
// Fall back to direct WebSocket connection (legacy path or control unavailable)
wsConn, dialErr := ps.client.dialProxyWebSocket(ps.ctx, ps.spriteName)
if dialErr != nil {
slog.Default().Debug("Proxy connection failed",
"sprite", ps.spriteName,
"error", dialErr,
)
return
}
remoteConn, err = initSocketTCP(ps.ctx, wsConn, addr)
if err != nil {
slog.Default().Debug("Proxy initialization failed",
"sprite", ps.spriteName,
"error", err,
)
wsConn.Close()
return
}
}
// Log established proxy connection with resolved target
slog.Default().Debug("Proxy connection established",
"sprite", ps.spriteName,
"local", localConn.LocalAddr().String(),
"remote_host", ps.RemoteHost,
"remote_port", ps.RemotePort,
"target", remoteConn.RemoteAddr(),
)
// Start bidirectional copy
var wg sync.WaitGroup
wg.Add(2)
// Copy from local to remote
go func() {
defer wg.Done()
defer remoteConn.Close()
io.Copy(remoteConn, localConn)
}()
// Copy from remote to local
go func() {
defer wg.Done()
defer localConn.Close()
io.Copy(localConn, remoteConn)
}()
wg.Wait()
}
// buildProxyURL builds the WebSocket URL for the proxy endpoint
func (c *Client) buildProxyURL(spriteName string) (*url.URL, error) {
baseURL := c.baseURL
// Convert HTTP(S) to WS(S)
if len(baseURL) >= 4 && baseURL[:4] == "http" {
baseURL = "ws" + baseURL[4:]
}
// Parse base URL
u, err := url.Parse(baseURL)
if err != nil {
return nil, fmt.Errorf("invalid base URL: %w", err)
}
// Build path
u.Path = fmt.Sprintf("/v1/sprites/%s/proxy", spriteName)
return u, nil
}
// Close closes the proxy session
func (ps *ProxySession) Close() error {
ps.closeOnce.Do(func() {
close(ps.closed)
ps.cancel()
if ps.listener != nil {
ps.listener.Close()
}
})
return nil
}
// LocalAddr returns the local address of the proxy listener
func (ps *ProxySession) LocalAddr() net.Addr {
if ps.listener != nil {
return ps.listener.Addr()
}
return nil
}
// Wait waits for the proxy session to close
func (ps *ProxySession) Wait() {
<-ps.closed
}
// ProxyManager manages multiple proxy sessions
type ProxyManager struct {
sessions []*ProxySession
mu sync.Mutex
}
// NewProxyManager creates a new proxy manager
func NewProxyManager() *ProxyManager {
return &ProxyManager{
sessions: make([]*ProxySession, 0),
}
}
// AddSession adds a session to the manager
func (pm *ProxyManager) AddSession(session *ProxySession) {
pm.mu.Lock()
defer pm.mu.Unlock()
pm.sessions = append(pm.sessions, session)
}
// CloseAll closes all managed proxy sessions
func (pm *ProxyManager) CloseAll() {
pm.mu.Lock()
defer pm.mu.Unlock()
for _, session := range pm.sessions {
session.Close()
}
pm.sessions = pm.sessions[:0]
}
// WaitAll waits for all proxy sessions to close
func (pm *ProxyManager) WaitAll() {
pm.mu.Lock()
sessions := make([]*ProxySession, len(pm.sessions))
copy(sessions, pm.sessions)
pm.mu.Unlock()
for _, session := range sessions {
session.Wait()
}
}