Skip to content

About

opencode zen 反向代理: 模拟 CLI 特征头 + 会话缓存 (IPv4/IPv6 隔离)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

114 Commits

Folders and files

Repository files navigation

opencode-zen-proxy

opencode-zen-proxy is a lightweight reverse proxy for opencode.ai. It provides dual-stack egress selection and cluster failover for opencode zen backend.

Features

  • Single-port dual-stack listener (IPv6 preferred → IPv4 fallback)
  • Automatic egress IP probing and failure marking (cooldown)
  • Cluster mode with private TLS+frame protocol for multi-node failover
  • opencode CLI feature-header injection (User-Agent, x-opencode-client/project)
  • Missing opencode tools auto-injection (bash, edit, glob, grep, read; chat vs responses format aware, tools:null treated as empty)
  • Stream-to-nonstream conversion: downstream stream:false is forwarded upstream as stream:true, SSE is collected and reassembled into a single JSON response (opt-out via --no-stream-convert)
  • Session caching based on egress IP
  • Inbound and outbound auth
  • Custom DNS server and domain aliases (resolve alias IP, keep original Host/TLS)

Build

go build -o opencode-zen-proxy ./cmd/zen-proxy

Configuration

Parameter Env Default
PORT PORT 9000
BACKEND BACKEND https://opencode.ai/zen/v1
OUTBOUND_AUTH OUTBOUND_AUTH pass-through
INBOUND_AUTH INBOUND_AUTH disable
FWD_INBOUND FWD_INBOUND 0
EGRESS_PREFER EGRESS_PREFER 6
CLUSTER_TOKEN CLUSTER_TOKEN
CLUSTER_LISTEN CLUSTER_LISTEN
CLUSTER_JOIN CLUSTER_JOIN
PEERS PEERS
FAILOVER_ON FAILOVER_ON 429,502,503,504,timeout
CACHE_FILE CACHE_FILE logs/session-cache.json
IP_INTERVAL IP_INTERVAL 5m
DNS_SERVER DNS_SERVER system resolver
DNS_ALIAS DNS_ALIAS
NO_STREAM_CONVERT NO_STREAM_CONVERT 0 (flag: --no-stream-convert)

Usage

One-liner (curl | bash)

curl -fsSL https://raw.githubusercontent.com/scyslz/opencode-forward/master/start.sh -o start.sh && bash start.sh start

Interactive: when run from a terminal, prompts for listen port (default 9003), inbound auth, egress preference, DNS, cluster and model rewrite — Enter accepts defaults. The binary is downloaded automatically if missing.

Foreground with live logs:

bash start.sh run

Non-interactive (pipes/cron skip prompts; use env vars):

PORT=9003 MODEL=gpt-5 bash start.sh start </dev/null

Service management (start.sh)

Command Behavior
./start.sh start Start in background, logs to file
./start.sh run Start in foreground, logs streamed to terminal (also persisted)
./start.sh stop Stop
./start.sh restart Restart
./start.sh status Show status + last 5 log lines

Port conflict handling: if the port is held by an old instance of this binary it is stopped automatically and replaced; if held by another program the script exits with an error.

Tools auto-injection

On /v1/chat/completions and /v1/responses outbound requests, any missing opencode tools (bash, edit, glob, grep, read) are injected; existing tools are never duplicated. Chat uses {type:function, function:{name,description,parameters}}, responses uses the flat {type:function, name,description,parameters} form.

Stream-to-nonstream conversion

When downstream requests stream:false on chat/responses paths, the proxy forwards stream:true upstream, buffers the SSE stream, and reassembles it into a single JSON response (choices/message (+tool_calls) for chat, terminal response object for responses). Reassembly failure falls back to passthrough. Disable with:

NO_STREAM_CONVERT=1 bash start.sh start
# or: ./opencode-zen-proxy 9000 https://opencode.ai/zen/v1 --no-stream-convert

Build from source

go build -o opencode-zen-proxy ./cmd/zen-proxy
OUTBOUND_AUTH="Bearer sk-..." ./start.sh start
# or via scripts/
go build -o opencode-zen-proxy ./cmd/zen-proxy && ./scripts/start.sh start

Cluster

Public listener:

CLUSTER_LISTEN=:9443 CLUSTER_TOKEN=s3 ./start.sh start

Private joiner:

CLUSTER_JOIN=public:9443 CLUSTER_TOKEN=s3 ./start.sh start

Project Layout (Standard Go)

cmd/zen-proxy/main.go          # entry, flag parsing, wiring
internal/proxy/                # HTTP reverse proxy & failover
internal/egress/               # dual-stack egress & probe
internal/cluster/              # private TLS+frame cluster protocol
internal/tunnel/               # tunnel persistence
internal/util/                 # helpers, constants (UpstreamTimeout)
scripts/start.sh               # service script (also ./start.sh shim)
configs/opencode-zen-proxy.service # systemd unit
legacy/                        # previous flat files (reference only)
Path Description
cmd/zen-proxy/main.go CLI args parsing & server
internal/proxy HTTP reverse proxy & failover logic
internal/egress dual-stack egress & probe manager
internal/cluster private TLS+frame cluster protocol
internal/tunnel tunnel persistence
internal/util helpers
scripts/start.sh unified service script
configs/opencode-zen-proxy.service systemd unit

About

opencode zen 反向代理: 模拟 CLI 特征头 + 会话缓存 (IPv4/IPv6 隔离)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages