How vi/websocat works
1. What this repo is
- websocat is a single Rust CLI binary plus supporting library code. Architecturally it is a “connector/composer” tool: it takes two endpoint specifications, builds a pipeline between them, and copies data bidirectionally with optional transformations.
2. Top-level execution path
- src/main.rs is the entry point for the executable.
- src/lib.rs exposes the reusable modules that implement parsing, peer construction, overlays, session handling, and utility code.
- The program flow is: parse CLI/options → parse address/specifier strings → instantiate two endpoints (or a listening/serving endpoint plus a client endpoint) → run the transfer loop.
3. Command-line and configuration boundary
- src/options.rs defines the runtime options and flags seen in README.md and --help.
- src/help.rs generates the help text, which is large because the tool supports many address types and overlays.
- The options layer is not just cosmetic: it controls framing, TLS behavior, server behavior, reconnection, buffering, JSON-RPC formatting, Prometheus metrics, and many protocol-specific toggles.
4. Specifier parsing and composition model
- src/specifier.rs and src/specparse.rs are the key architectural pieces. They turn a textual endpoint like ws://…, tcp-l:…, broadcast:mirror:, or ssl-connect:tcp:… into a structured plan.
- This is the main abstraction boundary in the project: “addresses” are not hardcoded cases in main, but composable specifiers.
- The system supports both base peers (actual I/O endpoints) and overlays (transformations or wrappers around a peer).
5. Core data flow: peer-to-peer copying
- Most sessions ultimately move bytes/messages between two “peers” via a common copy engine.
- src/my_copy.rs, src/readdebt.rs, and src/util.rs support the transfer logic, buffering, shutdown, backpressure handling, and message/byte accounting.
- The design is duplex-oriented: the tool can copy in both directions unless flags like -u, -U, or --one-message constrain it.
6. WebSocket subsystem
- WebSocket support is split across multiple modules: src/ws_peer.rs, src/ws_client_peer.rs, src/ws_server_peer.rs, and src/ws_lowlevel_peer.rs.
- src/http_peer.rs and src/http_serve.rs handle the HTTP/1 upgrade and/or HTTP request/response side of the WebSocket handshake and related HTTP serving behavior.
- The README’s limitation is reflected in the design: it speaks HTTP/1 WebSockets, not RFC 8441/9220 style extended WebSocket support.
7. Transport adapters
- A major part of the architecture is the set of concrete transport peers:
- src/net_peer.rs for TCP
- src/unix_peer.rs and src/unix_seqpacket_peer.rs for Unix domain sockets
- src/stdio_peer.rs and src/stdio_threaded_peer.rs for console/stdin/stdout
- src/file_peer.rs, src/process_peer.rs, and src/foreachmsg_peer.rs for file/process-style I/O
- src/udp_peer-style behavior is embedded through the UDP-related peer logic
- src/windows_np_peer.rs for Windows named pipes
- These modules form the “raw I/O endpoints” layer underneath WebSocket and overlay logic.
8. Protocol and security wrappers
- src/ssl_peer.rs and src/crypto_peer.rs provide transport security and message encryption boundaries.
- SSL/TLS is used both as a direct connection type and as an overlay atop another transport.
- PKCS#12 certificates are first-class in the CLI, matching the repository’s examples and 1234.pkcs12 / test.pkcs12 artifacts.
9. Message-shaping overlays
- Several overlays convert between byte streams and message-oriented flows:
- src/line_peer.rs for line-delimited records
- src/lengthprefixed_peer.rs for framed messages
- src/jsonrpc_peer.rs for JSON-RPC formatting
- src/timestamp_peer.rs for timestamp injection
- src/trivial_peer.rs, src/mirror_peer.rs, and src/literalreply-style behavior for simple transformation/testing
- These overlays are the main way the tool changes semantics without changing the underlying transport.
10. Connection-management overlays
- src/reconnect_peer.rs, src/broadcast_reuse_peer.rs, src/primitive_reuse_peer.rs, and src/all_peers.rs implement multiplexing/reuse/reconnect behavior.
- These are important architectural boundaries because they manage lifecycle and fan-out rather than transport bytes directly.
- broadcast: and reuse-raw: behaviors from the README map to these modules.
11. Server/session orchestration
- src/sessionserve.rs and src/http_serve.rs coordinate server-side acceptance, routing, and session lifecycle.
- This is where listening modes, -s, ws-l:, static-file fallback, URI restriction, and mixed HTTP/WebSocket serving come together.
- The server side is therefore not a single-purpose WebSocket listener; it can multiplex between WebSocket sessions and non-WebSocket responses.
12. Prometheus and observability
- src/prometheus_peer.rs exposes metrics on a separate endpoint while the main session continues running.
- Metrics are an additive side channel, not part of the data path, so they sit as a companion subsystem beside the main connector graph.
13. Cross-platform and runtime concerns
- src/asyncstdio/threaded stdio behavior is split because console I/O differs across UNIX and other platforms.
- src/lints.rs and src/util.rs support compatibility and housekeeping around old dependencies and platform quirks.
- The repository also includes Dockerfiles and GitHub Actions workflows, so builds are packaged both as native Rust artifacts and container images.
14. Testing and examples
- tests/test.rs, test.sh, and test_help.sh indicate integration-style validation rather than isolated unit-only coverage.
- The README and moreexamples.md are effectively part of the architecture documentation: they demonstrate how specifiers compose into real pipelines and how the subsystems fit together.
15. How to diagram it
- A good architecture diagram should center on three layers:
- CLI/spec parsing at the top
- Composable peer/overlay graph in the middle
- Concrete transports and protocol adapters at the bottom
- Then add side subsystems for server/session orchestration, metrics, and platform-specific I/O.
Main entry points: specifier.rs specifier · all_peers.rs all peers · main.rs main · options.rs options
Components 27
CLI boundary
Entry point, options, and generated help
Core composition
Specifier parsing and peer orchestration
Transports
Concrete endpoint implementations
- ws server · WebSocket server-side peer
src/ws_server_peer.rs - ws client · WebSocket client-side peer
src/ws_client_peer.rs - ws lowlevel · Shared low-level WebSocket logic
src/ws_lowlevel_peer.rs - http peer · HTTP upgrade and request/response side
src/http_peer.rs - tcp · TCP endpoint adapter
src/net_peer.rs - unix · Unix domain socket adapter
src/unix_peer.rs - stdio · Standard input/output adapter
src/stdio_peer.rs
Overlays
Message shaping, reuse, and security wrappers
- tls · TLS/SSL transport wrapper
src/ssl_peer.rs - crypto · Message encryption boundary
src/crypto_peer.rs - lines · Line-delimited message framing
src/line_peer.rs - jsonrpc · JSON-RPC message shaping
src/jsonrpc_peer.rs - length-prefixed · Length-prefixed message framing
src/lengthprefixed_peer.rs - reconnect · Reconnect and retry behavior
src/reconnect_peer.rs - broadcast reuse · Broadcast and reuse behavior
src/broadcast_reuse_peer.rs
Runtime services
Session serving, copy engine, and observability
- session serve · Accepts and routes server-side sessions
src/sessionserve.rs - copy loop · Bidirectional data movement between peers
src/my_copy.rs - read debt · Backpressure and buffering accounting
src/readdebt.rs - util · Shared helpers for transfer and runtime behavior
src/util.rs - metrics · Prometheus metrics endpoint
src/prometheus_peer.rs - http serve · HTTP-side serving and fallback behavior
src/http_serve.rs
Connections 32
- main → options: reads · Loads runtime flags and mode switches
- main → specifier: builds · Creates endpoint plans from CLI input
- main → copy loop: runs · Starts the main transfer loop
- main → session serve: serves · Enters server mode when requested
- lib → options: exports · Makes configuration types reusable
- lib → specifier: exports · Makes the composition model reusable
- lib → all peers: exports · Exposes supported peer constructors
- options → help: drives · Help text reflects supported flags
- options → specparse: configures · Options influence parsing and peer setup
- specifier → specparse: uses · Turns textual specs into structured plans
- specparse → all peers: resolves · Maps specifiers to concrete peer types
- specifier → tls: wraps · Builds security overlays around transports
- specifier → lines: wraps · Builds message-shaping overlays
- specifier → jsonrpc: wraps · Builds protocol-format overlays
- specifier → reconnect: wraps · Builds lifecycle overlays
- specifier → broadcast reuse: wraps · Builds fan-out and reuse overlays
- all peers → tcp: includes · Registers TCP transport support
- all peers → unix: includes · Registers Unix socket support
- all peers → stdio: includes · Registers stdio support
- all peers → ws client: includes · Registers WebSocket client support
- all peers → ws server: includes · Registers WebSocket server support
- all peers → http peer: includes · Registers HTTP upgrade support
- ws client → ws lowlevel: shares · Uses common low-level WebSocket logic
- ws server → ws lowlevel: shares · Uses common low-level WebSocket logic
- ws client → http peer: handshakes · Relies on HTTP-side upgrade behavior
- ws server → http serve: serves · Integrates with HTTP serving and fallback
- session serve → http serve: routes · Coordinates listening and HTTP/WebSocket responses
- session serve → metrics: exposes · Publishes metrics alongside sessions
- copy loop → read debt: manages · Tracks buffering and backpressure
- copy loop → util: uses · Shares helper routines for transfer logic
- tls → crypto: composes · Security layers can be stacked with encryption
- lines → length-prefixed: alternative · Different framing strategies for message mode