6.30 Tailscale集成

OpenClaw 集成 Tailscale VPN 提供安全的跨网络 Gateway 访问,支持 Tailnet 内部访问和公共 HTTPS 暴露两种模式。

概述

Tailscale 是一个基于 WireGuard 的零配置 VPN 解决方案,为 OpenClaw Gateway 提供:

  • 零配置: 无需手动配置防火墙或端口转发
  • 点对点加密: 端到端加密的通信
  • 跨网络访问: 穿透 NAT 和防火墙
  • 团队协作: 基于身份的访问控制
  • 公共暴露: 可选的 HTTPS 公网访问(Funnel)

Tailscale 模式

模式对比

模式 可见性 HTTPS 安全级别
off 本地 N/A
serve 仅 Tailnet
funnel 公网

配置示例

{
  "gateway": {
    "port": 18789,
    "tailscale": {
      "mode": "serve"  // off | serve | funnel
    }
  }
}

安装 Tailscale

macOS

# 使用 Homebrew 安装
brew install tailscale

# 或下载 GUI 应用
# https://tailscale.com/download/mac

# 启动 Tailscale
sudo tailscale up

Linux

# 使用安装脚本
curl -fsSL https://tailscale.com/install.sh | sh

# 启动 Tailscale
sudo tailscale up

# 启用用户空间网络(可选,无需 root)
tailscale up --operator=$USER

Windows

# 下载并安装 Tailscale
# https://tailscale.com/download/windows

# 使用 WSL2 时,在 WSL 内安装
curl -fsSL https://tailscale.com/install.sh | sh
sudo tailscale up

验证安装

# 查看 Tailscale 状态
tailscale status

# 查看本机 Tailscale IP
tailscale ip -4

# 查看网络中的设备
tailscale status --peers

模式一: Serve (Tailnet 内访问)

最推荐的模式,仅允许 Tailnet 内的设备访问 Gateway。

基本配置

{
  "gateway": {
    "port": 18789,
    "bind": "loopback",  // 保持 loopback 绑定
    "tailscale": {
      "mode": "serve"
    }
  }
}

启动 Gateway

# 启动 Gateway
openclaw gateway

# Tailscale 自动配置 HTTPS 访问
# Gateway 可通过以下方式访问:
# https://hostname.tailnet-name.ts.net:18789

从其他设备连接

# 在 Tailnet 内的任何设备上
export OPENCLAW_GATEWAY=wss://hostname.tailnet.ts.net:18789
openclaw node start

# 或使用 IP 地址
export OPENCLAW_GATEWAY=wss://100.x.x.x:18789

模式二: Funnel (公网访问)

公网暴露警告
Funnel 模式将 Gateway 暴露到公网。务必配置强认证速率限制

配置

{
  "gateway": {
    "port": 18789,
    "bind": "loopback",
    "tailscale": {
      "mode": "funnel"
    },
    "auth": {
      "token": "${VERY_STRONG_TOKEN}"  // 必须使用强 token
    },
    "rateLimit": {
      "enabled": true,
      "maxRequests": 100,
      "windowMs": 60000
    }
  }
}

启动和访问

# 启动 Gateway
openclaw gateway

# 公网 HTTPS 访问
# https://hostname.tailnet-name.ts.net:18789

# 任何人都可以访问(需要认证)
curl -H "Authorization: Bearer $TOKEN" \
     https://hostname.tailnet.ts.net:18789/v1/chat/completions

认证配置

Token 认证

{
  "gateway": {
    "tailscale": {
      "mode": "serve",
      "auth": {
        "mode": "token"
      }
    },
    "auth": {
      "token": "${OPENCLAW_GATEWAY_TOKEN}"
    }
  }
}

密码认证

{
  "gateway": {
    "tailscale": {
      "mode": "serve",
      "auth": {
        "mode": "password"
      }
    },
    "auth": {
      "password": "${OPENCLAW_GATEWAY_PASSWORD}"
    }
  }
}

Tailscale 身份认证

使用 Tailscale 的身份系统进行认证:

{
  "gateway": {
    "tailscale": {
      "mode": "serve",
      "auth": {
        "enabled": true,
        "allowedUsers": [
          "user1@github",
          "user2@google"
        ]
      }
    }
  }
}
身份提供商
Tailscale 支持多种身份提供商: GitHub, Google, Microsoft, Okta 等。在 Tailscale 管理后台配置。

绑定类型

Loopback 绑定 (推荐)

{
  "gateway": {
    "bind": "loopback",  // Gateway 仍绑定 127.0.0.1
    "tailscale": {
      "mode": "serve"    // Tailscale 代理到 loopback
    }
  }
}

这是最安全的配置,Gateway 仅监听 loopback,Tailscale 负责转发。

Tailnet 绑定

{
  "gateway": {
    "bind": "tailnet",   // 直接绑定到 Tailscale 接口
    "tailscale": {
      "mode": "serve"
    }
  }
}

Gateway 直接监听 Tailscale 接口,跳过 loopback 代理。

CLI 使用

使用 Tailscale Serve

# 启动 Gateway 并启用 Tailscale serve
openclaw gateway --tailscale serve

# 查看 Tailscale serve 状态
tailscale serve status

# 停止 serve
tailscale serve off

使用 Tailscale Funnel

# 启动 Gateway 并启用 funnel
openclaw gateway --tailscale funnel

# 查看 funnel 状态
tailscale funnel status

# 停止 funnel
tailscale funnel off

高级配置

自定义域名

# 在 Tailscale 管理后台配置 MagicDNS
# 然后使用自定义域名访问
https://gateway.mycompany.ts.net:18789

多端口暴露

{
  "gateway": {
    "port": 18789,
    "tailscale": {
      "mode": "serve",
      "exposePorts": [
        18789,  // Gateway 主端口
        18793   // Canvas 端口
      ]
    }
  }
}

ACL 策略

在 Tailscale 管理后台配置 ACL:

// Tailscale ACL 示例
{
  "acls": [
    {
      "action": "accept",
      "src": ["group:developers"],
      "dst": ["tag:openclaw:18789"]
    }
  ],
  "tagOwners": {
    "tag:openclaw": ["group:admins"]
  }
}

安全建议

Serve 模式安全清单

  • ✅ 使用 serve 而非 funnel
  • ✅ 保持 Gateway bind: loopback
  • ✅ 配置 Tailscale 用户白名单
  • ✅ 启用 Gateway 认证
  • ✅ 配置 Tailscale ACL

Funnel 模式安全清单

  • ✅ 使用超强 token (64+ 字符)
  • ✅ 启用严格速率限制
  • ✅ 启用沙箱 (mode: all)
  • ✅ 限制工具白名单
  • ✅ 配置 DM 策略为 pairing
  • ✅ 启用审计日志
  • ✅ 监控异常流量

故障排查

Tailscale 连接问题

# 检查 Tailscale 状态
tailscale status

# 测试连通性
ping $(tailscale ip -4)

# 查看 Tailscale 日志
sudo journalctl -u tailscaled -f

# 重启 Tailscale
sudo systemctl restart tailscaled

Serve/Funnel 不工作

# 检查 serve 状态
tailscale serve status

# 查看详细错误
tailscale debug serve

# 手动配置 serve
tailscale serve https:18789 http://localhost:18789

# 手动配置 funnel
tailscale funnel 18789

认证失败

# 检查 Tailscale 用户
tailscale status | grep user

# 验证 ACL
tailscale debug acls

# 测试连接
curl -v https://hostname.ts.net:18789

性能问题

# 检查 Tailscale 连接类型
tailscale status --peers

# 优选直连(DERP 表示中继,不理想)
# 期望: direct connection

# 优化 MTU
sudo tailscale set --accept-routes=false

监控和日志

Tailscale 日志

# 查看连接日志
tailscale debug daemon-logs

# 查看网络日志
tailscale debug netcheck

# 导出日志
tailscale debug logs > tailscale.log

Gateway 日志

# 查看 Tailscale 相关日志
openclaw logs --filter tailscale

# 监控连接
openclaw gateway list-connections

使用场景

场景 1: 团队协作

{
  "gateway": {
    "tailscale": {
      "mode": "serve",
      "auth": {
        "enabled": true,
        "allowedUsers": [
          "alice@github",
          "bob@github",
          "carol@github"
        ]
      }
    }
  }
}

// 团队成员通过 Tailscale 安全访问

场景 2: 远程开发

# 家中开发机器
{
  "gateway": {
    "tailscale": {
      "mode": "serve"
    }
  }
}

# 从任何地方访问
# 笔记本、平板、手机等

场景 3: 演示和集成

{
  "gateway": {
    "tailscale": {
      "mode": "funnel"  // 临时公网访问
    },
    "auth": {
      "token": "demo-token-12345"
    }
  }
}

// 提供给客户或合作伙伴测试

与 SSH 隧道对比

特性 Tailscale SSH 隧道
配置复杂度 零配置 需要手动配置
NAT 穿透 自动 需要公网 IP
HTTPS 自动证书 需要手动配置
团队管理 内置 需要额外工具
性能 点对点 通过 SSH 服务器

常用命令参考

# Tailscale 管理
tailscale up                    # 启动 Tailscale
tailscale down                  # 停止 Tailscale
tailscale status                # 查看状态
tailscale ip                    # 查看 IP 地址

# Serve/Funnel
tailscale serve https:18789 http://localhost:18789
tailscale funnel 18789
tailscale serve status
tailscale funnel status
tailscale serve off
tailscale funnel off

# OpenClaw 集成
openclaw gateway --tailscale serve
openclaw gateway --tailscale funnel
openclaw status

# 调试
tailscale ping hostname
tailscale debug daemon-logs
tailscale debug netcheck

相关资源