Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ test
.env
.env.*
!.env.example

# Local credential and tooling files. A remote/cloud build uploads the whole
# context before the Dockerfile is evaluated, so these must never be sent even
# though the current Dockerfile copies explicit paths only.
.npmrc
*.pem
*.key
*.p12
.netrc
.git-credentials
coverage
.vscode
30 changes: 24 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@
# cp .env.example .env
#
# The gateway loads `.env` from the project root (and from the working directory)
# automatically at startup. Every variable below is read by `src/config.ts`;
# the values shown are the built-in defaults.
# automatically at startup. Every variable below is read by `src/config.ts`; the
# uncommented values are the built-in defaults and the commented lines are
# examples or optional settings.

# ---------------------------------------------------------------------------
# Authentication
# ---------------------------------------------------------------------------
# API keys callers must present. The gateway accepts them from
# `Authorization: Bearer <key>`, `x-api-key: <key>` or
# `Authorization: Basic <base64(key:)>`.
# `Authorization: Basic <base64(key:)>`. A bare value in `Authorization` with no
# scheme is not accepted.
#
# Default: sk-dummy-123. When neither OPENCODE_API_KEYS nor OPENCODE_API_KEY is
# set, the gateway uses that built-in development key and logs a warning, so it
# starts out of the box. It is public knowledge — anyone who can reach the port
# can use it. Replace it before exposing the gateway beyond localhost.
# can use it. Replace it before exposing the gateway beyond localhost: the
# gateway refuses to start when the built-in key is still in use on a
# non-loopback HOST.
#
# Comma-separated list. Generate a strong key with:
# node -e "console.log(require('node:crypto').randomBytes(32).toString('base64url'))"
Expand All @@ -35,6 +39,11 @@ OPENCODE_API_KEYS=sk-dummy-123
# Set to true to disable authentication entirely. Only for a trusted loopback host.
# OPENCODE_ALLOW_NO_AUTH=false

# Optional operator credential tier: when this list is non-empty, `/admin/*`
# requires one of these keys and no longer accepts a general API key. Unset keeps
# today's behaviour (any configured API key can call the admin routes).
# OPENCODE_ADMIN_API_KEYS=

# ---------------------------------------------------------------------------
# HTTP listener
# ---------------------------------------------------------------------------
Expand All @@ -46,9 +55,17 @@ PORT=20123
# inflates the body by ~33%, so keep this above OPENCODE_MAX_ATTACHMENT_BYTES.
# OPENCODE_MAX_BODY_BYTES=67108864

# Trust `x-forwarded-*` headers from a reverse proxy. Enable only behind one.
# Trust `x-forwarded-*` headers from a reverse proxy. Accepts `true` (every hop),
# a positive hop count (`1` = the immediate peer only), or a comma-separated list
# of trusted IPs/CIDRs. Enable only behind a proxy you control that overwrites the
# forwarded headers: with a plain `true` the client address, and therefore the
# per-address rate-limit bucket, is caller-controlled.
# OPENCODE_TRUST_PROXY=false

# Wall-clock budget for receiving one request body. A caller that never finishes
# sending is disconnected once it expires. Streamed responses are unaffected.
# OPENCODE_BODY_TIMEOUT_MS=300000

# Comma-separated CORS allow-list. `*` allows every origin.
# OPENCODE_CORS_ORIGINS=

Expand All @@ -63,7 +80,8 @@ PORT=20123
# In-process rate limit per authenticated API key. RPM is requests per minute;
# BURST is the bucket capacity (0 derives max(1, ceil(rpm / 6))). RPM=0 disables
# the limiter. /admin/* uses a separate bucket. Rejections answer 429 with a
# retry-after header.
# retry-after header. Failed authentication is throttled separately (at least 60
# attempts per minute per client address) even when the limiter is disabled here.
# OPENCODE_RATE_LIMIT_RPM=0
# OPENCODE_RATE_LIMIT_BURST=0

Expand Down
33 changes: 25 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ When nothing is found the request fails with:
| `GET` | `/health` | Liveness plus upstream readiness snapshot (public). |
| `GET` | `/ready` | Readiness from cached state: `200` only when an upstream server is resolved and a model catalog is already cached (public, never calls upstream). |
| `GET` | `/healthz` | Minimal liveness alias (public). |
| `GET` | `/metrics` | Prometheus exposition (requires a key unless `OPENCODE_METRICS_PUBLIC=true`). |
| `GET` | `/metrics` | Prometheus exposition (requires a key unless `OPENCODE_METRICS_PUBLIC=true` is set **and** no `OPENCODE_METRICS_TOKEN` is configured). |
| `GET` | `/admin/status` | Config, upstream, catalog and session-cache snapshot. |
| `GET` | `/admin/config` | The effective configuration, with secrets redacted. |
| `POST` | `/admin/models/refresh` | Force a model catalog reload. |
Expand Down Expand Up @@ -219,7 +219,7 @@ curl -N http://127.0.0.1:20123/v1/chat/completions \
```

```
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1764662000,"model":"opencode/deepseek-v4.1-flash-free","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1764662000,"model":"opencode/deepseek-v4.1-flash-free","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1764662000,"model":"opencode/deepseek-v4.1-flash-free","choices":[{"index":0,"delta":{"content":"One"},"finish_reason":null}]}

Expand Down Expand Up @@ -472,9 +472,11 @@ model_disabled`. Query `GET /v1/models` instead of guessing.
| `HOST` / `PORT` | `127.0.0.1` / `20123` | Listen address. |
| `OPENCODE_API_KEYS` | `sk-dummy-123` | Accepted caller keys (comma separated). Replace the built-in default before exposing the port. |
| `OPENCODE_ALLOW_NO_AUTH` | `false` | Start without keys (dangerous). |
| `OPENCODE_ADMIN_API_KEYS` | *(unset)* | Operator keys accepted on `/admin/*`; when set, general API keys are rejected there. |
| `OPENCODE_METRICS_TOKEN` | *(unset)* | Dedicated scrape token for `GET /metrics`; configuring it keeps that endpoint authenticated even with `OPENCODE_METRICS_PUBLIC=true`. |
| `OPENCODE_BODY_TIMEOUT_MS` | `300000` | Wall-clock budget for receiving one request body; a streaming response is never cut short by it. |
| `OPENCODE_RATE_LIMIT_RPM` | `0` (disabled) | Per-minute request budget per API key; `0` turns rate limiting off. |
| `OPENCODE_RATE_LIMIT_BURST` | `0` (disabled) | Burst capacity allowed on top of the per-minute budget. |
| `OPENCODE_RATE_LIMIT_BURST` | `0` | Burst capacity allowed on top of the per-minute budget; `0` derives `max(1, ceil(rpm/6))`. The limiter is off only when `OPENCODE_RATE_LIMIT_RPM=0`. |
| `OPENCODE_CORS_ORIGINS` | *(empty)* | Browser origins allowed to call the gateway; `*` allows every origin. |
| `OPENCODE_SERVER_URL` / `OPENCODE_SERVER_PASSWORD` | *(unset)* | Explicit upstream server. |
| `OPENCODE_AUTOSTART` | `true` | Spawn a private `opencode serve`. |
Expand Down Expand Up @@ -517,7 +519,10 @@ Every variable, with types, defaults and security notes:
`tokens_total`, `completions_total`, `active_streams`, `upstream_restarts_total`,
`session_cache_entries`, `event_stream_connected`, `event_stream_reconnects_total`,
`event_frames_dropped_total`, `rate_limit_rejections_total`, plus process
defaults. Requires a key unless `OPENCODE_METRICS_PUBLIC=true`. `/metrics` is
defaults. Requires a key unless `OPENCODE_METRICS_PUBLIC=true` is set **and** no
`OPENCODE_METRICS_TOKEN` is configured: a configured token is always required,
even when the endpoint is public, so a general API key is never handed to a
scraper. `/metrics` is
never rate limited, so a local scraper cannot be throttled by its own traffic.
* **`/admin/*`** — `/admin/status` (config, upstream binary and URL, catalog
counts, session count), `/admin/config` (effective configuration with API keys
Expand Down Expand Up @@ -558,8 +563,9 @@ Every variable, with types, defaults and security notes:
read-only agent, and constrain tools with `OPENCODE_AGENT_PERMISSIONS`.
* **The default API key is public.** With no `OPENCODE_API_KEYS` configured the
gateway accepts `sk-dummy-123`, which anyone reading this repository knows.
Set your own key before exposing the port; never enable
`OPENCODE_ALLOW_NO_AUTH=true` on a reachable host.
The gateway therefore refuses to start when that key is still accepted on a
non-loopback `HOST`; set your own key before exposing the port, and never
enable `OPENCODE_ALLOW_NO_AUTH=true` on a reachable host.
* **Bridged tools do not remove the agent's own tool set.** A session whose
client tools are bridged still carries native tool *definitions*; the gateway
merely denies their actions, and only when
Expand All @@ -583,7 +589,14 @@ Every variable, with types, defaults and security notes:
traffic without authentication; prefer `OPENCODE_METRICS_TOKEN` and keep the
endpoint closed.
* Put the gateway behind TLS, keep CORS origins explicit, and do not enable
`OPENCODE_TRUST_PROXY` unless a proxy you control sets the forwarded headers.
`OPENCODE_TRUST_PROXY` unless a proxy you control overwrites the forwarded
headers: with a bare `true` the client address is caller-controlled, which also
makes the per-address rate-limit bucket spoofable. Prefer a hop count
(`OPENCODE_TRUST_PROXY=1`) or a CIDR list.
* **Admin routes restart the world.** `/admin/upstream/restart` kills the shared
upstream (every in-flight turn fails) and `DELETE /admin/sessions` drops the
sticky cache for all callers. Set `OPENCODE_ADMIN_API_KEYS` to keep those routes
away from tenant keys, or restrict them at the proxy.

## Docker usage

Expand All @@ -607,7 +620,11 @@ docker run --rm -p 20123:20123 \
[docker-compose.yml](docker-compose.yml):

```bash
cp .env.example .env # defaults to OPENCODE_API_KEYS=sk-dummy-123
cp .env.example .env
# Replace the published default before the container can start: HOST=0.0.0.0 is
# not a loopback address, so the gateway refuses to start while
# OPENCODE_API_KEYS is still sk-dummy-123.
pnpm setup -- --random
docker compose up --build
```

Expand Down
Loading
Loading