# vi/websocat architecture

> Architecture diagram of the GitHub repository vi/websocat, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vi/websocat
- Repository: https://github.com/vi/websocat

## Overview

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: [`src/specifier.rs`](https://github.com/vi/websocat/blob/master/src/specifier.rs) (specifier), [`src/all_peers.rs`](https://github.com/vi/websocat/blob/master/src/all_peers.rs) (all peers), [`src/main.rs`](https://github.com/vi/websocat/blob/master/src/main.rs) (main), [`src/options.rs`](https://github.com/vi/websocat/blob/master/src/options.rs) (options)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI boundary"]
  node_main["main<br/>binary entry<br/>[main.rs]"]
  node_lib["lib<br/>module façade<br/>[lib.rs]"]
  node_options["options<br/>CLI config<br/>[options.rs]"]
  node_help["help<br/>[help.rs]"]
end

subgraph group_core["Core composition"]
  node_specifier{{"specifier<br/>address model<br/>[specifier.rs]"}}
  node_specparse["specparse<br/>parser<br/>[specparse.rs]"]
  node_all_peers["all peers<br/>peer registry<br/>[all_peers.rs]"]
end

subgraph group_transports["Transports"]
  node_ws_server["ws server<br/>websocket server<br/>[ws_server_peer.rs]"]
  node_ws_client["ws client<br/>websocket client<br/>[ws_client_peer.rs]"]
  node_ws_lowlevel["ws lowlevel<br/>websocket core"]
  node_http_peer["http peer<br/>HTTP handshake<br/>[http_peer.rs]"]
  node_net_peer["tcp<br/>transport<br/>[net_peer.rs]"]
  node_unix_peer["unix<br/>transport<br/>[unix_peer.rs]"]
  node_stdio_peer["stdio<br/>transport<br/>[stdio_peer.rs]"]
end

subgraph group_wrappers["Overlays"]
  node_ssl_peer["tls<br/>security wrapper<br/>[ssl_peer.rs]"]
  node_crypto_peer["crypto<br/>encryption wrapper<br/>[crypto_peer.rs]"]
  node_line_peer["lines<br/>framing overlay<br/>[line_peer.rs]"]
  node_jsonrpc_peer["jsonrpc<br/>format overlay<br/>[jsonrpc_peer.rs]"]
  node_lengthprefixed_peer["length-prefixed<br/>framing overlay"]
  node_reconnect_peer["reconnect<br/>lifecycle overlay<br/>[reconnect_peer.rs]"]
  node_broadcast_reuse_peer["broadcast reuse<br/>fan-out overlay"]
end

subgraph group_runtime["Runtime services"]
  node_sessionserve["session serve<br/>server/session<br/>[sessionserve.rs]"]
  node_my_copy["copy loop<br/>transfer engine<br/>[my_copy.rs]"]
  node_readdebt["read debt<br/>flow control<br/>[readdebt.rs]"]
  node_util["util<br/>support code<br/>[util.rs]"]
  node_prometheus(("metrics<br/>observability<br/>[prometheus_peer.rs]"))
  node_http_serve["http serve<br/>[http_serve.rs]"]
end

node_main -->|"reads"| node_options
node_main -->|"builds"| node_specifier
node_main -->|"runs"| node_my_copy
node_main -->|"serves"| node_sessionserve
node_lib -->|"exports"| node_options
node_lib -->|"exports"| node_specifier
node_lib -->|"exports"| node_all_peers
node_options -->|"drives"| node_help
node_options -->|"configures"| node_specparse
node_specifier -->|"uses"| node_specparse
node_specparse -->|"resolves"| node_all_peers
node_specifier -->|"wraps"| node_ssl_peer
node_specifier -->|"wraps"| node_line_peer
node_specifier -->|"wraps"| node_jsonrpc_peer
node_specifier -->|"wraps"| node_reconnect_peer
node_specifier -->|"wraps"| node_broadcast_reuse_peer
node_all_peers -->|"includes"| node_net_peer
node_all_peers -->|"includes"| node_unix_peer
node_all_peers -->|"includes"| node_stdio_peer
node_all_peers -->|"includes"| node_ws_client
node_all_peers -->|"includes"| node_ws_server
node_all_peers -->|"includes"| node_http_peer
node_ws_client -->|"shares"| node_ws_lowlevel
node_ws_server -->|"shares"| node_ws_lowlevel
node_ws_client -->|"handshakes"| node_http_peer
node_ws_server -->|"serves"| node_http_serve
node_sessionserve -->|"routes"| node_http_serve
node_sessionserve -->|"exposes"| node_prometheus
node_my_copy -->|"manages"| node_readdebt
node_my_copy -->|"uses"| node_util
node_ssl_peer -->|"composes"| node_crypto_peer
node_line_peer -.->|"alternative"| node_lengthprefixed_peer

click node_main "https://github.com/vi/websocat/blob/master/src/main.rs"
click node_lib "https://github.com/vi/websocat/blob/master/src/lib.rs"
click node_options "https://github.com/vi/websocat/blob/master/src/options.rs"
click node_help "https://github.com/vi/websocat/blob/master/src/help.rs"
click node_specifier "https://github.com/vi/websocat/blob/master/src/specifier.rs"
click node_specparse "https://github.com/vi/websocat/blob/master/src/specparse.rs"
click node_all_peers "https://github.com/vi/websocat/blob/master/src/all_peers.rs"
click node_sessionserve "https://github.com/vi/websocat/blob/master/src/sessionserve.rs"
click node_my_copy "https://github.com/vi/websocat/blob/master/src/my_copy.rs"
click node_readdebt "https://github.com/vi/websocat/blob/master/src/readdebt.rs"
click node_util "https://github.com/vi/websocat/blob/master/src/util.rs"
click node_prometheus "https://github.com/vi/websocat/blob/master/src/prometheus_peer.rs"
click node_ws_server "https://github.com/vi/websocat/blob/master/src/ws_server_peer.rs"
click node_ws_client "https://github.com/vi/websocat/blob/master/src/ws_client_peer.rs"
click node_ws_lowlevel "https://github.com/vi/websocat/blob/master/src/ws_lowlevel_peer.rs"
click node_http_peer "https://github.com/vi/websocat/blob/master/src/http_peer.rs"
click node_http_serve "https://github.com/vi/websocat/blob/master/src/http_serve.rs"
click node_net_peer "https://github.com/vi/websocat/blob/master/src/net_peer.rs"
click node_unix_peer "https://github.com/vi/websocat/blob/master/src/unix_peer.rs"
click node_stdio_peer "https://github.com/vi/websocat/blob/master/src/stdio_peer.rs"
click node_ssl_peer "https://github.com/vi/websocat/blob/master/src/ssl_peer.rs"
click node_crypto_peer "https://github.com/vi/websocat/blob/master/src/crypto_peer.rs"
click node_line_peer "https://github.com/vi/websocat/blob/master/src/line_peer.rs"
click node_jsonrpc_peer "https://github.com/vi/websocat/blob/master/src/jsonrpc_peer.rs"
click node_lengthprefixed_peer "https://github.com/vi/websocat/blob/master/src/lengthprefixed_peer.rs"
click node_reconnect_peer "https://github.com/vi/websocat/blob/master/src/reconnect_peer.rs"
click node_broadcast_reuse_peer "https://github.com/vi/websocat/blob/master/src/broadcast_reuse_peer.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_lib,node_options,node_help toneBlue
class node_specifier,node_specparse,node_all_peers toneAmber
class node_ws_server,node_ws_client,node_ws_lowlevel,node_http_peer,node_net_peer,node_unix_peer,node_stdio_peer toneMint
class node_ssl_peer,node_crypto_peer,node_line_peer,node_jsonrpc_peer,node_lengthprefixed_peer,node_reconnect_peer,node_broadcast_reuse_peer toneRose
class node_sessionserve,node_my_copy,node_readdebt,node_util,node_prometheus,node_http_serve toneIndigo
```

## Components

### CLI boundary

Entry point, options, and generated help

- [main](https://github.com/vi/websocat/blob/master/src/main.rs): binary entry · Executable entry point · `src/main.rs`
- [lib](https://github.com/vi/websocat/blob/master/src/lib.rs): module façade · Exports the reusable architecture · `src/lib.rs`
- [options](https://github.com/vi/websocat/blob/master/src/options.rs): CLI config · Runtime flags and behavior switches · `src/options.rs`
- [help](https://github.com/vi/websocat/blob/master/src/help.rs): help text · Generated command reference · `src/help.rs`

### Core composition

Specifier parsing and peer orchestration

- [specifier](https://github.com/vi/websocat/blob/master/src/specifier.rs): address model · Composable endpoint and overlay model · `src/specifier.rs`
- [specparse](https://github.com/vi/websocat/blob/master/src/specparse.rs): parser · Parses textual endpoint specs · `src/specparse.rs`
- [all peers](https://github.com/vi/websocat/blob/master/src/all_peers.rs): peer registry · Central registry of available peer types · `src/all_peers.rs`

### Transports

Concrete endpoint implementations

- [ws server](https://github.com/vi/websocat/blob/master/src/ws_server_peer.rs): websocket server · WebSocket server-side peer · `src/ws_server_peer.rs`
- [ws client](https://github.com/vi/websocat/blob/master/src/ws_client_peer.rs): websocket client · WebSocket client-side peer · `src/ws_client_peer.rs`
- [ws lowlevel](https://github.com/vi/websocat/blob/master/src/ws_lowlevel_peer.rs): websocket core · Shared low-level WebSocket logic · `src/ws_lowlevel_peer.rs`
- [http peer](https://github.com/vi/websocat/blob/master/src/http_peer.rs): HTTP handshake · HTTP upgrade and request/response side · `src/http_peer.rs`
- [tcp](https://github.com/vi/websocat/blob/master/src/net_peer.rs): transport · TCP endpoint adapter · `src/net_peer.rs`
- [unix](https://github.com/vi/websocat/blob/master/src/unix_peer.rs): transport · Unix domain socket adapter · `src/unix_peer.rs`
- [stdio](https://github.com/vi/websocat/blob/master/src/stdio_peer.rs): transport · Standard input/output adapter · `src/stdio_peer.rs`

### Overlays

Message shaping, reuse, and security wrappers

- [tls](https://github.com/vi/websocat/blob/master/src/ssl_peer.rs): security wrapper · TLS/SSL transport wrapper · `src/ssl_peer.rs`
- [crypto](https://github.com/vi/websocat/blob/master/src/crypto_peer.rs): encryption wrapper · Message encryption boundary · `src/crypto_peer.rs`
- [lines](https://github.com/vi/websocat/blob/master/src/line_peer.rs): framing overlay · Line-delimited message framing · `src/line_peer.rs`
- [jsonrpc](https://github.com/vi/websocat/blob/master/src/jsonrpc_peer.rs): format overlay · JSON-RPC message shaping · `src/jsonrpc_peer.rs`
- [length-prefixed](https://github.com/vi/websocat/blob/master/src/lengthprefixed_peer.rs): framing overlay · Length-prefixed message framing · `src/lengthprefixed_peer.rs`
- [reconnect](https://github.com/vi/websocat/blob/master/src/reconnect_peer.rs): lifecycle overlay · Reconnect and retry behavior · `src/reconnect_peer.rs`
- [broadcast reuse](https://github.com/vi/websocat/blob/master/src/broadcast_reuse_peer.rs): fan-out overlay · Broadcast and reuse behavior · `src/broadcast_reuse_peer.rs`

### Runtime services

Session serving, copy engine, and observability

- [session serve](https://github.com/vi/websocat/blob/master/src/sessionserve.rs): server/session · Accepts and routes server-side sessions · `src/sessionserve.rs`
- [copy loop](https://github.com/vi/websocat/blob/master/src/my_copy.rs): transfer engine · Bidirectional data movement between peers · `src/my_copy.rs`
- [read debt](https://github.com/vi/websocat/blob/master/src/readdebt.rs): flow control · Backpressure and buffering accounting · `src/readdebt.rs`
- [util](https://github.com/vi/websocat/blob/master/src/util.rs): support code · Shared helpers for transfer and runtime behavior · `src/util.rs`
- [metrics](https://github.com/vi/websocat/blob/master/src/prometheus_peer.rs): observability · Prometheus metrics endpoint · `src/prometheus_peer.rs`
- [http serve](https://github.com/vi/websocat/blob/master/src/http_serve.rs): HTTP server · HTTP-side serving and fallback behavior · `src/http_serve.rs`

## Connections

- 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
