OpenCode 内置 Web 界面,由驱动 TUI 的同一台服务器提供服务。默认即可用,并受密码保护。

访问

$ opencode pair

  Open a link to connect. Links work once and expire in 5 minutes.

  http://127.0.0.1:49374/auth/connect/...

  █▀▀▀▀▀█ ...
在浏览器中打开链接,即会通过会话(session)cookie 完成登录并加载 Web 界面。从 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 远程地址进行配对:
$ 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