Files
CLIProxyAPI/internal/api/buffered_conn.go
Luis Pater 4787c7a3c4 feat(tunnel): add multi-tenant WebSocket data exchange tunnel (/v1/tunnel)
- Introduce internal/wstunnel implementing a multi-tenant, many-to-many
  opaque WebSocket data exchange hub over GET /v1/tunnel.
- Enforce strict access.api-keys authentication across all five standard
  credential sources, rejecting unconfigured keys with 401, isolating
  tenants per API key, and revoking removed keys on hot reload with
  WebSocket close code 1008.
- Enforce per-tenant cross-role device UUID uniqueness (409 Conflict on
  duplicates, generation-bound handshake reservations) while allowing
  duplicate human-readable device names.
- Multiplex discovery and data exchange over a single WebSocket per
  device: UTF-8 JSON TextMessage control frames (*ojson.Doc single-exit
  serialization for server_list, server_online, server_offline,
  list_servers, and error) and BinaryMessage frames carrying a 4-byte
  header, source/destination UUIDs, and up to 64 MiB opaque payloads
  with anti-spoofing and cross-role routing checks.
- Guard upgraded transports against post-upgrade socket deadlines
  (including wrapped non-ALPN TLS connections and RFC 6455 protocol
  error paths), enforce per-peer and per-tenant outbound byte budgets
  retained across in-flight writes, and bound non-blocking close
  reclamation.
- Mount /v1/tunnel on internal/api.Server (405 Allow: GET for non-GET
  methods while preserving CORS OPTIONS) and add English and Chinese
  integration guides under docs/websocket-tunnel.md and
  docs/websocket-tunnel_CN.md.
2026-11-12 00:03:24 +08:00

47 lines
831 B
Go

package api
import (
"bufio"
"crypto/tls"
"net"
)
type bufferedConn struct {
net.Conn
reader *bufio.Reader
}
func (c *bufferedConn) NetConn() net.Conn {
return c.Conn
}
func (c *bufferedConn) Read(p []byte) (int, error) {
if c == nil {
return 0, net.ErrClosed
}
if c.reader == nil {
return c.Conn.Read(p)
}
return c.reader.Read(p)
}
func (c *bufferedConn) ConnectionState() tls.ConnectionState {
if c == nil || c.Conn == nil {
return tls.ConnectionState{}
}
if stater, ok := c.Conn.(interface{ ConnectionState() tls.ConnectionState }); ok {
return stater.ConnectionState()
}
return tls.ConnectionState{}
}
func (c *bufferedConn) CloseWrite() error {
if c == nil || c.Conn == nil {
return nil
}
if cw, ok := c.Conn.(interface{ CloseWrite() error }); ok {
return cw.CloseWrite()
}
return nil
}