|
| 1 | +# WebSocket Reflector X - AI Coding Agent Instructions |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +**WebSocketReflectorX** is a controlled TCP-over-WebSocket tunneling tool written in Rust. It enables secure forwarding of TCP connections through WebSocket channels for environments with restricted network access (e.g., Ret2Shell, GZCTF platforms). |
| 6 | + |
| 7 | +### Repository Structure |
| 8 | + |
| 9 | +- **Workspace**: Two main crates in `crates/` |
| 10 | + - `wsrx`: Core library + CLI tools (daemon, client, server modes) |
| 11 | + - `desktop`: Cross-platform GUI application |
| 12 | +- **Key directories**: |
| 13 | + - `crates/wsrx/src/`: Core proxy logic, tunnel management, CLI handlers |
| 14 | + - `crates/desktop/src/`: UI setup, daemon integration, native bridges |
| 15 | + - `crates/desktop/ui/`: UI markup and styling (framework-agnostic design) |
| 16 | + - `packages/`: Optional TypeScript client/documentation |
| 17 | + |
| 18 | +## Architecture |
| 19 | + |
| 20 | +### Three-Mode Operation |
| 21 | + |
| 22 | +The project implements three complementary tunneling modes (launched via CLI arguments): |
| 23 | + |
| 24 | +1. **Daemon Mode** (`wsrx daemon`): Server accepting WebSocket connections from clients |
| 25 | +2. **Client Mode** (`wsrx connect <url>`): Lightweight CLI client connecting to daemon |
| 26 | +3. **Serve Mode** (`wsrx serve`): Standalone server mode for direct deployments |
| 27 | + |
| 28 | +Each mode shares the same underlying `proxy::proxy()` function for TCP↔WebSocket conversion. |
| 29 | + |
| 30 | +### Core Abstraction Layer |
| 31 | + |
| 32 | +**Location**: `crates/wsrx/src/proxy.rs` |
| 33 | + |
| 34 | +The `WrappedWsStream` enum abstracts over two incompatible WebSocket implementations: |
| 35 | + |
| 36 | +- **Tungstenite**: Used by client via `tokio-tungstenite` (feature: `client`) |
| 37 | +- **Axum**: Used by server via `axum::extract::ws` (feature: `server`) |
| 38 | + |
| 39 | +This design pattern allows shared proxy logic to work with both without conditional compilation in business logic. The `Message` enum normalizes all message types to Binary variants. |
| 40 | + |
| 41 | +### Feature-Driven Compilation |
| 42 | + |
| 43 | +**Location**: `crates/wsrx/Cargo.toml` |
| 44 | + |
| 45 | +```toml |
| 46 | +features: |
| 47 | + binary = ["server", "client", "log", ...] # CLI tool |
| 48 | + client = ["dep:tokio-tungstenite"] |
| 49 | + server = ["dep:axum"] |
| 50 | +``` |
| 51 | + |
| 52 | +- Library builds with `default` (binary off) for integration |
| 53 | +- Binary requires both `client` and `server` features |
| 54 | +- Desktop app (`wsrx-desktop`) bundles the library with all features |
| 55 | + |
| 56 | +### Desktop App Architecture |
| 57 | + |
| 58 | +**Location**: `crates/desktop/src/` |
| 59 | + |
| 60 | +- **`launcher.rs`**: Single-instance guard via lock file, UI initialization |
| 61 | +- **`daemon/`**: Spawns `wsrx` daemon subprocess with forwarded messages |
| 62 | +- **`bridges/`**: Native bridges connecting UI layer to Rust core: |
| 63 | + - `ui_state.rs`: Reactive state binding for tunnels, connections, logs |
| 64 | + - `settings.rs`: TOML config persistence (platform-specific directories via `directories` crate) |
| 65 | + - `system_info.rs`: System resource monitoring |
| 66 | + - `window_control.rs`: Window lifecycle and menu interactions |
| 67 | + |
| 68 | +## Critical Workflows |
| 69 | + |
| 70 | +### Building |
| 71 | + |
| 72 | +```bash |
| 73 | +# Library only (no features) |
| 74 | +cargo build -p wsrx --lib |
| 75 | + |
| 76 | +# CLI tool with all features |
| 77 | +cargo build -p wsrx --bin wsrx --release |
| 78 | + |
| 79 | +# Desktop application |
| 80 | +cargo build -p wsrx-desktop --release |
| 81 | +``` |
| 82 | + |
| 83 | +### Platform-Specific Build Considerations |
| 84 | + |
| 85 | +- **macOS**: Requires manual code-signing workaround (`sudo xattr -cr` in README). Desktop app uses platform-specific window configuration. |
| 86 | +- **Windows**: Icon compilation via `winres` in `build.rs` |
| 87 | +- **Linux**: AppImage generation via deployments script |
| 88 | + |
| 89 | +### Testing |
| 90 | + |
| 91 | +No dedicated test suite. Validation occurs via: |
| 92 | + |
| 93 | +1. Manual smoke tests of proxy logic |
| 94 | +2. Desktop app functional testing |
| 95 | +3. CI builds on GitHub |
| 96 | + |
| 97 | +## Desktop Application UI/UX Design |
| 98 | + |
| 99 | +### Page Architecture |
| 100 | + |
| 101 | +The desktop application implements a **multi-page layout** with left sidebar navigation: |
| 102 | + |
| 103 | +1. **Get Started Page** |
| 104 | + |
| 105 | + - Onboarding experience for new users |
| 106 | + - Displays application branding and welcome message |
| 107 | + - Update notification banner (if available) |
| 108 | + - Primary action buttons for initial setup |
| 109 | + |
| 110 | +2. **Connections Page** |
| 111 | + |
| 112 | + - Main hub for managing WebSocket tunnels and connections |
| 113 | + - Shows active scopes/instances with status indicators (pending, allowed, syncing) |
| 114 | + - Displays real-time connection statistics |
| 115 | + - Supports creating/editing tunnels with local/remote address binding |
| 116 | + - Visual indicators for connection states (icons + color coding) |
| 117 | + |
| 118 | +3. **Network Logs Page** |
| 119 | + |
| 120 | + - Real-time log stream display with severity levels |
| 121 | + - Log levels: DEBUG (transparent), INFO (semi-opaque), WARN, ERROR |
| 122 | + - Structured log display: level + target + timestamp + message |
| 123 | + - Auto-scrolling to latest entries |
| 124 | + - Uses color-coded severity for quick scanning |
| 125 | + |
| 126 | +4. **Settings Page** |
| 127 | + - Application configuration and metadata |
| 128 | + - Branded header with visual branding elements |
| 129 | + - Settings groups organized vertically |
| 130 | + - Toggle controls for daemon behavior, logging levels |
| 131 | + - Platform-specific options (macOS vs Windows vs Linux) |
| 132 | + |
| 133 | +### UI Component Structure |
| 134 | + |
| 135 | +**Main Window Layout** |
| 136 | + |
| 137 | +- Container: Custom window chrome for all platforms |
| 138 | +- Layout: [SideBar | MainContent] |
| 139 | + - **SideBar**: Tab navigation + status indicators, collapsible |
| 140 | + - **MainContent**: [TitleBar | PageStack] |
| 141 | + - **TitleBar**: Window controls (minimize/maximize/close), platform-aware |
| 142 | + - **PageStack**: Page transitions between 4 pages |
| 143 | + |
| 144 | +**Navigation Model** |
| 145 | + |
| 146 | +- State managed in `bridges/ui_state.rs`: `page` property |
| 147 | +- Page switching is reactive (state-driven, not imperative) |
| 148 | +- SideBar tabs include visual indicators for active scope/instance state |
| 149 | + |
| 150 | +### Design System & Theming |
| 151 | + |
| 152 | +**Core Principles**: |
| 153 | + |
| 154 | +- Consistent visual hierarchy with background layers (layer-1, layer-2, layer-3) |
| 155 | +- Foreground color (window-fg) with semantic colors (error, warn, info, debug) |
| 156 | +- Consistent spacing units across the application |
| 157 | +- Platform-aware styling (different treatment for macOS, Windows, Linux) |
| 158 | + |
| 159 | +**Implementation Guidelines**: |
| 160 | + |
| 161 | +- Use centralized theme/style definitions (NOT scattered throughout components) |
| 162 | +- Support light/dark mode switching (store preference in config) |
| 163 | +- Ensure readable contrast ratios for accessibility |
| 164 | +- Consistent padding, spacing, and border radius across all pages |
| 165 | + |
| 166 | +### State Binding & Reactivity |
| 167 | + |
| 168 | +**Core Principles**: |
| 169 | + |
| 170 | +- Bidirectional binding between UI and Rust state |
| 171 | +- Page content updates reactively when state changes |
| 172 | +- List views auto-update when data changes (no manual refresh) |
| 173 | + |
| 174 | +**Bridge Architecture** (`crates/desktop/src/bridges/`): |
| 175 | + |
| 176 | +- `WindowControlBridge`: Native window control callbacks (close, minimize, maximize) |
| 177 | +- `SystemInfoBridge`: System info + logs stream |
| 178 | +- `InstanceBridge`: Tunnel instance management |
| 179 | +- `ScopeBridge`: WebSocket scope state management |
| 180 | +- `SettingsBridge`: TOML config persistence |
| 181 | + |
| 182 | +### Accessibility & Platform Considerations |
| 183 | + |
| 184 | +- **macOS**: Frameless window with transparent titlebar, custom window chrome |
| 185 | +- **Windows**: Standard window frame with platform conventions |
| 186 | +- **Linux**: Platform-agnostic with window manager compatibility |
| 187 | +- Global keyboard shortcut: Ctrl+Q to quit application |
| 188 | +- Tab navigation and focus management throughout |
| 189 | +- Keyboard-accessible controls (all interactive elements reachable via keyboard) |
| 190 | + |
| 191 | +### Internationalization (i18n) |
| 192 | + |
| 193 | +- Translation system support for UI strings |
| 194 | +- Language auto-detection from system settings |
| 195 | +- Centralized locale management |
| 196 | +- Right-to-left (RTL) language support considerations |
| 197 | + |
| 198 | +## External Dependencies & Integration Points |
| 199 | + |
| 200 | +### Critical Dependencies |
| 201 | + |
| 202 | +- **Tokio**: Async runtime (required by all crates) |
| 203 | +- **Axum**: HTTP + WebSocket server for daemon mode |
| 204 | +- **tokio-tungstenite**: WebSocket client implementation |
| 205 | +- **Rustls**: TLS, uses AWS-LC-RS (with ring fallback) |
| 206 | +- **UI Framework**: GPUI (replacing previous Slint implementation) |
| 207 | + |
| 208 | +### Crypto Backend Selection |
| 209 | + |
| 210 | +**Location**: `crates/wsrx/src/main.rs` lines 66-80 |
| 211 | + |
| 212 | +Priority order (configurable): |
| 213 | + |
| 214 | +1. AWS-LC-RS (preferred) |
| 215 | +2. Ring (fallback) |
| 216 | + |
| 217 | +Log initialization **before** crypto selection. If both backends fail, exit immediately. |
| 218 | + |
| 219 | +### Platform Detection |
| 220 | + |
| 221 | +Uses `build-target` crate for compile-time platform info. Generated constants in `constants.rs` include: |
| 222 | + |
| 223 | +- `WSRX_VERSION`: Semantic version |
| 224 | +- `WSRX_FULL_VERSION`: Version with git commit, arch, OS, environment |
| 225 | + |
| 226 | +## Common Gotchas |
| 227 | + |
| 228 | +1. **Library vs. CLI**: `wsrx` library exports only `proxy::proxy()`, `tunnel::Tunnel`. CLI features gated by feature flags. |
| 229 | + |
| 230 | +2. **Instance Locking**: Desktop app enforces single-instance via lock file in `~/.local/share/wsrx-desktop/`. Subsequent launches notify the existing instance to raise its window. |
| 231 | + |
| 232 | +3. **Subprocess Daemon**: Desktop spawns `wsrx daemon` subprocess. Daemon's stdout/stderr must be captured for log integration into UI. |
| 233 | + |
| 234 | +4. **Cross-Platform Paths**: Use `directories` crate for config paths (ProjectDirs), NOT hardcoded paths. |
| 235 | + |
| 236 | +5. **Heartbeat Timeout**: Daemon supports optional heartbeat; desktop app does not set it by default. Useful for ephemeral deployments. |
| 237 | + |
| 238 | +6. **UI Framework Migration**: The current Slint-based UI is planned for replacement. New implementations must maintain the same page architecture (Get Started, Connections, Network Logs, Settings) and state binding model. |
| 239 | + |
| 240 | +## Key Files to Understand New Features |
| 241 | + |
| 242 | +- **Protocol details**: `docs/PROTOCOL.md` (binary message format) |
| 243 | +- **Daemon CLI parsing**: `crates/wsrx/src/main.rs` (clap Parser) |
| 244 | +- **Proxy core**: `crates/wsrx/src/proxy.rs` (Message handling, Stream/Sink traits) |
| 245 | +- **UI logic**: `crates/desktop/src/bridges/ui_state.rs` (state synchronization) |
| 246 | +- **Build configuration**: `crates/desktop/build.rs` (constants generation, platform resources) |
| 247 | + |
| 248 | +--- |
| 249 | + |
| 250 | +**Last Updated**: 2025-11-09 |
| 251 | +**Rust Edition**: 2024 | **MSRV**: 1.89.0 |
| 252 | +**License**: MIT |
0 commit comments