6.26 远程访问

OpenClaw 支持通过多种方式安全地远程访问 Gateway,包括 SSH 隧道、Tailscale VPN 等方案,同时保持 Gateway 默认的 loopback 绑定安全配置。

概述

Gateway 默认仅监听 127.0.0.1 (loopback),这是最安全的配置。对于远程访问需求,OpenClaw 推荐使用安全隧道技术而非直接暴露 Gateway 端口到公网。

安全原则
始终保持 Gateway 的 loopback 绑定是最安全的默认配置。远程访问应通过加密隧道实现,而不是将 Gateway 直接绑定到公网 IP。

远程访问架构

OpenClaw 的远程访问基于以下架构设计:

  • Gateway: 默认端口 18789,仅监听 127.0.0.1
  • 节点到 Gateway: 通过 WebSocket RPC 连接
  • 安全隧道: SSH、Tailscale 等提供加密传输
  • 认证层: Gateway 层面的 token/password 认证

方案一: SSH 隧道

SSH 隧道是最传统和可靠的远程访问方案,适用于所有支持 SSH 的环境。

基本配置

# 在远程机器上启动 Gateway (保持 loopback 绑定)
openclaw gateway --port 18789

# 从本地机器建立 SSH 隧道
ssh -L 18789:localhost:18789 user@remote-host

# 现在可以通过 localhost:18789 访问远程 Gateway
openclaw status --gateway ws://localhost:18789

后台运行 SSH 隧道

# 后台运行并保持连接
ssh -fN -L 18789:localhost:18789 user@remote-host

# 使用 autossh 自动重连
autossh -M 0 -f -N -L 18789:localhost:18789 user@remote-host

# 查看隧道状态
ps aux | grep ssh

SSH 配置文件

~/.ssh/config 中添加配置简化连接:

Host openclaw-gateway
    HostName remote-host.example.com
    User your-username
    LocalForward 18789 localhost:18789
    ServerAliveInterval 60
    ServerAliveCountMax 3

# 使用配置连接
ssh -fN openclaw-gateway

方案二: Tailscale VPN

Tailscale 提供零配置的点对点 VPN,特别适合团队协作和多设备访问场景。

Tailscale Serve 模式 (私有访问)

仅允许 Tailnet 内设备访问:

{
  "gateway": {
    "port": 18789,
    "bind": "loopback",
    "tailscale": {
      "mode": "serve"  // Tailnet 内可访问
    }
  }
}

访问 Tailnet 上的 Gateway

# 通过 Tailscale 设备名访问
openclaw status --gateway ws://remote-machine:18789

# 通过 Tailscale IP 访问
openclaw status --gateway ws://100.x.x.x:18789

Tailscale 认证配置

{
  "gateway": {
    "auth": {
      "mode": "token",
      "token": "${GATEWAY_TOKEN}"
    },
    "tailscale": {
      "mode": "serve",
      "bind": "tailnet",  // 绑定到 Tailscale 接口
      "auth": {
        "enabled": true,
        "allowedUsers": [
          "user1@github",
          "user2@google"
        ]
      }
    }
  }
}

方案三: 双重绑定 (不推荐)

不推荐
直接绑定到 0.0.0.0 或公网 IP 会增加安全风险,仅在充分理解风险且有其他安全措施时使用。
{
  "gateway": {
    "bind": "0.0.0.0",  // 监听所有接口
    "port": 18789,
    "auth": {
      "token": "${VERY_STRONG_TOKEN}",
      "rateLimit": {
        "enabled": true,
        "maxRequests": 100,
        "windowMs": 60000
      }
    }
  }
}

端口映射

Gateway 使用的相关端口:

端口 用途 计算方式
18789 Gateway 主端口 基础端口
18791 浏览器控制端口 基础端口 + 2
18793 Canvas 画布端口 基础端口 + 4
18800-18899 CDP 调试端口范围 控制端口 + 9 到 + 108
重要提示
永远不要共享 browser.cdpUrl。CDP (Chrome DevTools Protocol) 端口具有完整的浏览器控制权限,必须严格保护。

多 Gateway 隧道

当运行多个 Gateway 实例时,需要为每个实例建立独立的隧道:

使用不同本地端口

# Gateway 1 (默认)
ssh -L 18789:localhost:18789 user@host1

# Gateway 2 (使用 profile)
ssh -L 19789:localhost:19001 user@host2

# 连接到不同的 Gateway
openclaw status --gateway ws://localhost:18789
openclaw status --gateway ws://localhost:19789

安全加固

1. 强制认证

{
  "gateway": {
    "auth": {
      "token": "${GATEWAY_TOKEN}",
      "tokenValidation": {
        "minLength": 32,
        "requireAlphanumeric": true
      }
    }
  }
}

2. 速率限制

{
  "gateway": {
    "rateLimit": {
      "enabled": true,
      "maxRequests": 100,
      "windowMs": 60000,
      "maxConnections": 10
    }
  }
}

3. IP 白名单 (Tailscale)

{
  "gateway": {
    "tailscale": {
      "mode": "serve",
      "allowedIPs": [
        "100.x.x.x",  // 特定 Tailscale IP
        "100.y.y.0/24"  // IP 范围
      ]
    }
  }
}

4. TLS 加密

{
  "gateway": {
    "tls": {
      "enabled": true,
      "cert": "/path/to/cert.pem",
      "key": "/path/to/key.pem"
    }
  }
}

故障排查

SSH 隧道问题

# 检查 SSH 隧道是否活跃
netstat -an | grep 18789

# 测试本地端口
curl http://localhost:18789

# 查看 SSH 调试信息
ssh -v -L 18789:localhost:18789 user@remote-host

Tailscale 连接问题

# 检查 Tailscale 状态
tailscale status

# 测试 Tailnet 连通性
ping remote-machine

# 查看 Tailscale 日志
tailscale debug logs

防火墙检查

# Linux: 检查防火墙规则
sudo iptables -L -n | grep 18789
sudo ufw status

# macOS: 检查防火墙
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate

# Windows: 检查防火墙
netsh advfirewall firewall show rule name=all | findstr 18789

监控和日志

# 查看 Gateway 连接日志
openclaw gateway --log-level debug

# 监控活动连接
openclaw gateway list-connections

# 查看认证日志
openclaw logs --filter auth

# 实时监控
watch -n 1 'openclaw status'

最佳实践

  • 默认 loopback: 始终保持 Gateway 绑定到 127.0.0.1
  • 优先 SSH/Tailscale: 使用加密隧道而非直接暴露
  • 强认证: 使用高强度 token,定期轮换
  • 最小权限: 只授予必要的访问权限
  • 监控审计: 记录所有远程访问日志
  • 定期更新: 保持 OpenClaw 和依赖组件更新

常用命令

# 启动 Gateway (loopback)
openclaw gateway --port 18789

# 检查 Gateway 状态
openclaw status

# 建立 SSH 隧道
ssh -L 18789:localhost:18789 user@remote-host

# 启用 Tailscale serve
openclaw gateway --tailscale serve

# 查看活动连接
openclaw gateway connections

# 测试远程连接
openclaw ping --gateway ws://localhost:18789

相关资源