main
1// Package transport provides transport layer abstractions for VNC connections.
2package transport
3
4import (
5 "context"
6 "crypto/tls"
7 "net/http"
8 "time"
9)
10
11// Transport represents a connection transport for RFB protocol messages.
12// Unlike TCP streams, this interface is message-oriented to support WebSocket
13// framing where each RFB message is a discrete WebSocket message.
14type Transport interface {
15 // Read reads a single RFB message from the transport.
16 // Returns the message data or an error.
17 Read(ctx context.Context) ([]byte, error)
18
19 // Write writes a single RFB message to the transport.
20 Write(ctx context.Context, data []byte) error
21
22 // Close closes the transport connection.
23 Close() error
24
25 // SetReadLimit sets the maximum message size for reads.
26 SetReadLimit(limit int64)
27}
28
29// DialOptions configures transport connection establishment.
30type DialOptions struct {
31 // Headers to send with the connection request (for WebSocket).
32 Headers http.Header
33
34 // Cookies to send with the connection request.
35 Cookies []*http.Cookie
36
37 // Proxy URL for upstream proxy (http, https, socks5).
38 Proxy string
39
40 // Timeout for connection establishment.
41 Timeout time.Duration
42
43 // TLSConfig for TLS connections.
44 TLSConfig *tls.Config
45
46 // UserAgent to send with the connection request.
47 UserAgent string
48
49 // Subprotocols for WebSocket negotiation.
50 Subprotocols []string
51}
52
53// Dialer creates new transport connections.
54type Dialer interface {
55 // Dial establishes a new transport connection.
56 Dial(ctx context.Context, target string, opts *DialOptions) (Transport, error)
57}
58
59// NewDialOptions creates DialOptions with sensible defaults.
60func NewDialOptions() *DialOptions {
61 return &DialOptions{
62 Headers: make(http.Header),
63 Timeout: 30 * time.Second,
64 }
65}
66
67// AddHeader adds a header to the dial options.
68func (o *DialOptions) AddHeader(key, value string) {
69 if o.Headers == nil {
70 o.Headers = make(http.Header)
71 }
72 o.Headers.Add(key, value)
73}
74
75// SetCookie sets a cookie string (parsed as Cookie header).
76func (o *DialOptions) SetCookie(cookie string) {
77 o.AddHeader("Cookie", cookie)
78}