Web
在浏览器中运行 OpenCode。
OpenCode 内置了一套 Web UI,与 TUI 共用同一个服务端。它默认开启,并受密码保护。
访问方式
$ opencode pair
Open a link to connect. Links work once and expire in 5 minutes.
http://127.0.0.1:49374/auth/connect/...
█▀▀▀▀▀█ ...在浏览器中打开该链接会通过会话 Cookie 完成登录并加载 Web UI。使用 OpenCode 应用扫描二维码,或将该链接粘贴到应用的服务器地址输入框,同样可以完成连接。登录会话有效期为 30 天;修改服务端密码会使所有现有会话立即注销。
通过 SSH 转发访问
当服务端仅监听在 localhost 时,从本机转发其端口并在本地打开输出的链接即可:
$ ssh -L 49374:127.0.0.1:49374 my-server默认情况下服务端仅监听在 localhost,默认端口因平台而异:
| 平台 | 默认端口 |
|---|---|
| macOS、Linux 与 WSL | 49374 |
| Windows | 28894 |
Windows 会保留 49152 及以上的端口,WSL 镜像网络也会保留其下方的一段端口区间。使用 opencode service status 查看当前监听地址:
$ opencode service status
http://127.0.0.1:49374你可以通过 opencode service 命令修改这些配置。
远程访问
如需从任意地点连接,可通过 OpenTunnel (opens in a new tab) 远程地址进行配对:
$ opencode pair --remote首次开启远程访问时,系统会先说明风险并询问确认:
▲ This will expose your OpenCode server to the internet.
│ Traffic is end-to-end encrypted, and access requires your server password.
│ To pair locally instead, run `opencode pair` without the --remote flag.
│
◆ Expose your server to the internet?
│ ● Yes / ○ No在没有交互式终端的环境中,除非传入 --yes 自动确认,否则该命令会失败。一旦确认过,后续运行将跳过提示——即使中间通过 opencode service set remote false 关闭过远程访问也是如此。
配置后台服务
使用 opencode service set 修改任意选项:
# Listen on every network interface
$ opencode service set hostname 0.0.0.0
# Use a fixed port instead of the channel default
$ opencode service set port 49374
# Replace the generated password
$ opencode service set password "a-long-secret"
# Allow a web client served from another origin
$ opencode service set cors https://app.example.com,https://other.example.com
# Pass an environment variable to the server process
$ opencode service set env OPENCODE_LOG_LEVEL DEBUG修改配置会停止后台服务端,请重新启动以应用新配置:
$ opencode service start独立前台模式
opencode serve 会在前台运行同一个服务端,而不是通过共享后台服务托管:
$ opencode serve --hostname 0.0.0.0 --port 4096
server listening on http://0.0.0.0:4096
server password <password>适合以下场景:
- 在共享主机、常驻主机或远程主机上运行 OpenCode,再通过
opencode --server <url>连接客户端。 - 为单个进程独立控制主机名、端口与 CORS 允许来源。
- 在 systemd、Docker 或其他期望前台进程的守护环境中运行。
- 维护专用服务端而非使用共享后台服务。
通过 --server 让客户端连接到该服务端:
$ opencode --server http://127.0.0.1:4096认证方式
设置 OPENCODE_PASSWORD 可使用固定密码替代自动生成的密码,同一个环境变量也可供 opencode --server 读取:
$ OPENCODE_PASSWORD="a-long-secret" opencode serve --port 4096
$ OPENCODE_PASSWORD="a-long-secret" opencode --server http://127.0.0.1:4096其他 HTTP 客户端使用 Basic 认证,用户名固定为 opencode,仅密码可自定义:
$ curl -u opencode:a-long-secret http://127.0.0.1:4096/api/info