Architecture
Agents communicate with the Rexec server using a bidirectional WebSocket protocol:- Registration: Create an agent record and receive an API token
- Connection: Agent connects via WebSocket using the token
- Communication: Bidirectional message exchange for terminal I/O
- Horizontal Scaling: Multi-instance support via Redis pub/sub
Authentication
Agents authenticate using API tokens with theagent scope:
- JWT Tokens: Short-lived session tokens from web UI
- API Tokens: Long-lived tokens (format:
rexec_*) for agent processes
Authorization: Bearer <token>header- WebSocket subprotocol:
Sec-WebSocket-Protocol: rexec.token.<token> - Query parameter:
?token=<token>
Agent Lifecycle
1. Registration
Create an agent record and receive credentials:2. Connection
Agent establishes WebSocket connection:3. Heartbeat
Bidirectional ping/pong keeps connection alive:- Server sends
PingMessageevery 15 seconds - Agent must respond with
PongMessage - 45-second timeout for disconnect detection
4. Disconnection
On disconnect (graceful or timeout):- Agent marked as “offline” in database
- Redis location registration removed
- All active sessions notified
Session Management
Agents support multiple concurrent shell sessions:- Main Session: Default shared session (ID:
"main") - Split Sessions: Independent sessions (ID:
"split-<uuid>")
- Shell process
- Terminal state
- I/O streams
System Information
Agents report system metrics:- Connected user sessions
- WebSocket events hub
- Database (persisted)
Plan Limits
Agents are subject to tier-based limits:| Plan | Max Registered Agents | Concurrent Terminals |
|---|---|---|
| Free | Varies by tier | Matches registered |
| Pro | Varies by tier | Matches registered |
Error Handling
Registration Errors
"agent limit reached for your plan"- Exceeded plan limits"agent registration not available for your plan"- Plan doesn’t support agents
Connection Errors
"invalid token"- Authentication failed"agent not found"- Agent ID doesn’t exist"not authorized"- Agent belongs to different user
User Connection Errors
"agent not online"- Agent is offline"agent terminal limit reached for your plan"- Too many concurrent connections
WebSocket Protocol
See Agent Registration for detailed protocol documentation.Multi-Instance Support
Rexec supports horizontal scaling:- Agent Location: Redis stores which server instance hosts each agent
- Cross-Instance Routing: Terminal I/O proxied via Redis pub/sub
- Session Tracking: Remote sessions tracked separately from local
Security Features
- CSWSH Prevention: Origin validation on WebSocket upgrade
- MFA Locking: Agents can be locked pending MFA verification
- Token Caching: 5-minute Redis cache for token validation
- Ownership Verification: All operations verify user ownership
Next Steps
Registration
Register agents and establish WebSocket connections
File Operations
Upload and download files from containers