|
| 1 | +# Redis RESP Proxy |
| 2 | + |
| 3 | +[](https://github.com/redis-developer/cae-resp-proxy/actions/workflows/test.yml) |
| 4 | + |
| 5 | +A Redis protocol proxy with HTTP API for response injection. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Redis Transparent Protocol Proxy**: Forwards Redis connections from clients to target Redis servers |
| 10 | +- **HTTP API**: RESTful API for managing connections and sending arbitrary responses to clients |
| 11 | +- **Connection Management**: Track active connections, view statistics, and close connections |
| 12 | +- **Data Encoding Support**: Send data in base64 or raw binary format |
| 13 | +- **Docker Support**: Containerized deployment with environment variable configuration |
| 14 | +- **Real-time Stats**: Monitor active connections and proxy statistics |
| 15 | + |
| 16 | +## Quick Start |
| 17 | + |
| 18 | +### Prerequisites |
| 19 | +- Docker or [Bun](https://bun.sh/) runtime |
| 20 | +- Running Redis server (target) |
| 21 | + |
| 22 | +### Basic Usage |
| 23 | + |
| 24 | +#### Docker (Recommended) |
| 25 | + |
| 26 | +The easiest way to get started is with Docker : |
| 27 | + |
| 28 | + |
| 29 | +```bash |
| 30 | +# Build the docker image first |
| 31 | +docker build -t resp-proxy . |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +```bash |
| 37 | +# Run with Docker - connects to Redis on host |
| 38 | +docker run -d \ |
| 39 | + -p 6379:6379 \ # the proxy will listen for incoming connections on this port |
| 40 | + -p 3000:3000 \ # the rest api will listen for http requests on this port |
| 41 | + -e TARGET_HOST=host.docker.internal \ #<-- redis server host ( the proxy target ) |
| 42 | + -e TARGET_PORT=6380 \ # redis server port |
| 43 | + -e LISTEN_PORT=6379 \ # proxy listen port |
| 44 | + -e API_PORT = 3000 \ # rest api port |
| 45 | + resp-proxy |
| 46 | +``` |
| 47 | + |
| 48 | +### Local Development |
| 49 | + |
| 50 | +```bash |
| 51 | +# Install dependencies |
| 52 | +bun install |
| 53 | + |
| 54 | +# Start the proxy with CLI arguments |
| 55 | +bun run proxy --listenPort=6379 --targetHost=localhost --targetPort=6380 |
| 56 | + |
| 57 | +# Or use the dev mode with hot reload |
| 58 | +bun run dev --listenPort=6379 --targetHost=localhost --targetPort=6380 |
| 59 | +``` |
| 60 | + |
| 61 | +#### Environment Variables |
| 62 | +You can use env variables instead of cli arguments. |
| 63 | + |
| 64 | +```bash |
| 65 | +# Set required environment variables |
| 66 | +export LISTEN_PORT=6379 |
| 67 | +export TARGET_HOST=localhost |
| 68 | +export TARGET_PORT=6380 |
| 69 | + |
| 70 | +# Optional variables |
| 71 | +export LISTEN_HOST=127.0.0.1 |
| 72 | +export TIMEOUT=30000 |
| 73 | +export ENABLE_LOGGING=true |
| 74 | +export API_PORT=3000 |
| 75 | + |
| 76 | +# Start the proxy |
| 77 | +bun run proxy |
| 78 | +``` |
| 79 | + |
| 80 | +## Configuration |
| 81 | + |
| 82 | +### Required Parameters |
| 83 | + |
| 84 | +| Parameter | CLI Flag | Environment Variable | Description | |
| 85 | +|-----------|----------|---------------------|-------------| |
| 86 | +| Target Host | `--targetHost` | `TARGET_HOST` | Redis server hostname/IP | |
| 87 | +| Target Port | `--targetPort` | `TARGET_PORT` | Redis server port | |
| 88 | + |
| 89 | +### Optional Parameters |
| 90 | + |
| 91 | +| Parameter | CLI Flag | Environment Variable | Default | Description | |
| 92 | +|-----------|----------|---------------------|---------|-------------| |
| 93 | +| Listen Port | `--listenPort` | `LISTEN_PORT` | `6379` | Port for Redis clients to connect to | |
| 94 | +| Listen Host | `--listenHost` | `LISTEN_HOST` | `127.0.0.1` | Host interface to bind to | |
| 95 | +| Timeout | `--timeout` | `TIMEOUT` | - | Connection timeout (ms) | |
| 96 | +| Enable Logging | `--enableLogging` | `ENABLE_LOGGING` | `false` | Verbose logging | |
| 97 | +| API Port | `--apiPort` | `API_PORT` | `3000` | HTTP API port | |
| 98 | + |
| 99 | +## HTTP API Reference |
| 100 | + |
| 101 | +The proxy provides a REST API for managing connections and sending arbitrary responses. |
| 102 | + |
| 103 | + |
| 104 | +### Endpoints |
| 105 | + |
| 106 | + |
| 107 | +#### Get Statistics |
| 108 | +```http |
| 109 | +GET /stats |
| 110 | +``` |
| 111 | +Returns detailed proxy statistics including active connections, total connections, and connection details. |
| 112 | + |
| 113 | +**Response:** |
| 114 | +```json |
| 115 | +{ |
| 116 | + "activeConnections": 2, |
| 117 | + "totalConnections": 5, |
| 118 | + "connections": [ |
| 119 | + { |
| 120 | + "id": "conn_123", |
| 121 | + "clientAddress": "127.0.0.1:54321", |
| 122 | + "connectedAt": "2024-01-01T10:00:00Z" |
| 123 | + } |
| 124 | + ] |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +#### Get Active Connections |
| 129 | +```http |
| 130 | +GET /connections |
| 131 | +``` |
| 132 | +Returns list of active connection IDs. |
| 133 | + |
| 134 | +**Response:** |
| 135 | +```json |
| 136 | +{ |
| 137 | + "connectionIds": ["conn_123", "conn_456"] |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +#### Send Data to Specific Client |
| 142 | +```http |
| 143 | +POST /send-to-client/{connectionId}?encoding={base64|raw} |
| 144 | +``` |
| 145 | +Send Redis protocol data to a specific client connection. |
| 146 | + |
| 147 | +**Parameters:** |
| 148 | +- `connectionId` (path): Target connection ID |
| 149 | +- `encoding` (query): Data encoding format (`base64` or `raw`, default: `base64`) |
| 150 | + |
| 151 | +**Body:** Raw data or base64-encoded data |
| 152 | + |
| 153 | +**Example:** |
| 154 | +```bash |
| 155 | +# Send PING command (base64 encoded) |
| 156 | +curl -X POST "http://localhost:3000/send-to-client/conn_123?encoding=base64" \ |
| 157 | + -d "KjENCiQ0DQpQSU5HDQo=" |
| 158 | + |
| 159 | +# Send raw binary data |
| 160 | +curl -X POST "http://localhost:3000/send-to-client/conn_123?encoding=raw" \ |
| 161 | + --data-binary "*1\r\n$4\r\nPING\r\n" |
| 162 | +``` |
| 163 | + |
| 164 | +**Response:** |
| 165 | +```json |
| 166 | +{ |
| 167 | + "success": true, |
| 168 | + "connectionId": "conn_123" |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +#### Send Data to Multiple Clients |
| 173 | +```http |
| 174 | +POST /send-to-clients?connectionIds={id1,id2}&encoding={base64|raw} |
| 175 | +``` |
| 176 | +Send data to multiple specific client connections. |
| 177 | + |
| 178 | +**Parameters:** |
| 179 | +- `connectionIds` (query): Comma-separated list of connection IDs |
| 180 | +- `encoding` (query): Data encoding format (`base64` or `raw`, default: `base64`) |
| 181 | + |
| 182 | +**Example:** |
| 183 | +```bash |
| 184 | +curl -X POST "http://localhost:3000/send-to-clients?connectionIds=conn_123,conn_456&encoding=base64" \ |
| 185 | + -d "KjENCiQ0DQpQSU5HDQo=" |
| 186 | +``` |
| 187 | + |
| 188 | +#### Send Data to All Clients |
| 189 | +```http |
| 190 | +POST /send-to-all-clients?encoding={base64|raw} |
| 191 | +``` |
| 192 | +Broadcast data to all active client connections. |
| 193 | + |
| 194 | +**Example:** |
| 195 | +```bash |
| 196 | +curl -X POST "http://localhost:3000/send-to-all-clients?encoding=base64" \ |
| 197 | + -d "KjENCiQ0DQpQSU5HDQo=" |
| 198 | +``` |
| 199 | + |
| 200 | +#### Close Connection |
| 201 | +```http |
| 202 | +DELETE /connections/{connectionId} |
| 203 | +``` |
| 204 | +Forcefully close a specific client connection. |
| 205 | + |
| 206 | +**Response:** |
| 207 | +```json |
| 208 | +{ |
| 209 | + "success": true, |
| 210 | + "connectionId": "conn_123" |
| 211 | +} |
| 212 | +``` |
| 213 | + |
| 214 | +## Use Cases |
| 215 | + |
| 216 | +### Testing Redis Applications |
| 217 | +Intercept and inspect Redis commands during development and testing. |
| 218 | + |
| 219 | +### Response Injection |
| 220 | +Send custom Redis responses to specific clients for testing scenarios. |
| 221 | + |
| 222 | +### Protocol Analysis |
| 223 | +Analyze Redis protocol communication patterns. |
| 224 | + |
| 225 | +## Development |
| 226 | + |
| 227 | +### Running Tests |
| 228 | + |
| 229 | +```bash |
| 230 | +bun test |
| 231 | +``` |
| 232 | + |
| 233 | +### Development Mode |
| 234 | + |
| 235 | +```bash |
| 236 | +bun run dev --targetHost=localhost --targetPort=6380 |
| 237 | +``` |
0 commit comments