Cloudflare Skill 用于连接 Cloudflare API,进行 DNS 管理、隧道管理和区域(Zone)管理。当用户需要管理域名、DNS 记录或创建隧道时可以使用该技能。
配置
-
获取 API Token:访问 dash.cloudflare.com/profile/api-tokens,创建具有所需权限的 Token:Zone:Read(列出域名)、DNS:Edit(管理 DNS 记录)、Account:Cloudflare Tunnel:Edit(管理隧道),然后复制该 Token。
-
配置方式:
方式 A:存入文件(推荐)
echo "YOUR_API_TOKEN" > ~/.cloudflare_token
chmod 600 ~/.cloudflare_token
方式 B:环境变量
export CLOUDFLARE_API_TOKEN="YOUR_API_TOKEN"
- 测试连接:
./scripts/setup.sh
命令
区域(域名)
./scripts/zones/list.sh # 列出所有区域
./scripts/zones/list.sh --json # JSON 输出
./scripts/zones/get.sh example.com # 获取区域详情
DNS 记录
# 列出记录
./scripts/dns/list.sh example.com
./scripts/dns/list.sh example.com --type A
./scripts/dns/list.sh example.com --name api
# 创建记录
./scripts/dns/create.sh example.com \
--type A \
--name api \
--content 1.2.3.4 \
--proxied
# 创建 CNAME
./scripts/dns/create.sh example.com \
--type CNAME \
--name www \
--content example.com \
--proxied
# 更新记录
./scripts/dns/update.sh example.com \
--name api \
--type A \
--content 5.6.7.8
# 删除记录
./scripts/dns/delete.sh example.com --name api --type A
隧道
# 列出隧道
./scripts/tunnels/list.sh
# 创建隧道
./scripts/tunnels/create.sh my-tunnel
# 配置隧道入口
./scripts/tunnels/configure.sh my-tunnel \
--hostname app.example.com \
--service http://localhost:3000
# 获取运行令牌
./scripts/tunnels/token.sh my-tunnel
# 删除隧道
./scripts/tunnels/delete.sh my-tunnel
Token 权限
| 功能 | 所需权限 |
|---|---|
| 列出区域 | Zone:Read |
| 管理 DNS | DNS:Edit |
| 管理隧道 | Account:Cloudflare Tunnel:Edit |
创建 Token 地址:dash.cloudflare.com/profile/api-tokens
常见工作流
将子域名指向服务器:
./scripts/dns/create.sh mysite.com --type A --name api --content 1.2.3.4 --proxied
为本地服务设置隧道:
# 1. 创建隧道
./scripts/tunnels/create.sh webhook-tunnel
# 2. 配置入口
./scripts/tunnels/configure.sh webhook-tunnel \
--hostname hook.mysite.com \
--service http://localhost:8080
# 3. 添加 DNS 记录
TUNNEL_ID=$(./scripts/tunnels/list.sh --name webhook-tunnel --quiet)
./scripts/dns/create.sh mysite.com \
--type CNAME \
--name hook \
--content ${TUNNEL_ID}.cfargotunnel.com \
--proxied
# 4. 运行隧道
TOKEN=$(./scripts/tunnels/token.sh webhook-tunnel)
cloudflared tunnel run --token $TOKEN
输出格式
| 参数 | 说明 |
|---|---|
| --json | API 原始 JSON |
| --table | 格式化表格(默认) |
| --quiet | 精简输出(仅 ID) |
故障排查
| 错误 | 解决方案 |
|---|---|
| "No API token found" | 运行 setup 或设置 CLOUDFLARE_API_TOKEN |
| "401 Unauthorized" | 检查 Token 是否有效 |
| "403 Forbidden" | Token 缺少所需权限 |
| "Zone not found" | 确认域名在您的账户中 |