一、前置安装

1. 安装 ngrok

brew install ngrok

2. 注册 ngrok 账号并配置 token(只需一次)

注册地址: https://dashboard.ngrok.com/signup
获取 token: https://dashboard.ngrok.com/get-started/your-authtoken

ngrok config add-authtoken <你的token>

二、配置 OpenClaw

配置文件路径: ~/.openclaw/openclaw.json

gateway 部分需要以下配置:

{
  "gateway": {
    "port": 18789,
    "mode": "local",
    "bind": "lan",
    "trustedProxies": ["127.0.0.1/32"],
    "controlUi": {
      "allowedOrigins": ["*"]
    },
    "auth": {
      "mode": "token",
      "token": "你的token"
    }
  }
}

关键配置说明:

bind: "lan" — 允许局域网访问(默认 loopback 只允许本地)

trustedProxies — 信任 ngrok 代理转发的请求头,否则会被当作不可信连接

allowedOrigins: ["*"] — 允许跨域访问

三、启动服务

1. 启动 OpenClaw 网关

openclaw gateway stop
openclaw gateway run --bind lan

或者用 service 方式:

openclaw gateway start

2. 启动 ngrok 转发 18789 端口

ngrok http 18789

ngrok 会输出一个公网 URL,类似: https://xxxx-xxxx.ngrok-free.dev

四、手机/远程浏览器连接

1. 打开 ngrok 给的公网 URL,会看到 OpenClaw 仪表盘

2. 填写连接信息:

• WebSocket URL: wss://xxxx-xxxx.ngrok-free.dev

• 网关令牌: 你的 gateway token

• 点击"连接"

3. 首次连接会提示 "pairing required",需要在电脑上批准设备

五、设备配对审批(关键步骤)

每个新设备/浏览器首次连接需要在电脑上批准,之后同一设备不再需要。

1. 查看待批准设备列表

openclaw devices list

会显示 Pending 列表,包含 Request ID 和设备信息。

2. 批准设备

openclaw devices approve <Request ID>

例如:

openclaw devices approve 89ee7a64-56c2-472d-856e-1db30ffa62db

3. 批准后手机上重新点"连接"即可。

其他设备管理命令:

openclaw devices list — 查看所有设备

openclaw devices reject — 拒绝配对请求

openclaw devices remove — 移除已配对设备

openclaw devices revoke — 撤销设备 token

六、注意事项

• ngrok 免费版每次启动会分配不同的 URL,需要重新在手机上填写

• 每个新设备只需批准一次,配对信息会保存

• ngrok 需要保持运行,关掉后远程就无法访问了

• 如果网关启动报错 "gateway already running",先执行 openclaw gateway stop