Sing-box + Cloudflare WARP 一键自动部署工具
Project description
Automated-sing-box-json-generator
Sing-box + Cloudflare WARP 一键自动部署工具。
支持 AnyTLS (Reality) / TUIC / Hysteria2 三协议可选组合,自动完成依赖安装、DNS 记录创建、TLS 证书签发、配置生成、Watchdog 守护部署。
快速开始
推荐方式:通过 pip 安装
pip install automated-sing-box-generator
automated-sing-box-generator deploy
开发者方式:从源码运行
git clone https://github.com/henryliu443/Automated-sing-box-json-generator.git
cd Automated-sing-box-json-generator
pip install -e .
automated-sing-box-generator deploy
部署过程中会交互式提示输入:
- 主域名(如
example.com) - Cloudflare API Token 和 Zone ID
- 要启用的协议(默认全选)
CLI 子命令
| 命令 | 说明 |
|---|---|
automated-sing-box-generator |
无参数默认执行完整部署 |
automated-sing-box-generator deploy |
完整部署(可选 --domain / --protocols) |
automated-sing-box-generator config |
使用已保存的状态重新生成配置(可选 --protocols 切换协议) |
automated-sing-box-generator export |
导出客户端配置(--format json|link|qr|qr-json,--output 指定文件) |
automated-sing-box-generator decode-qr-json |
还原 qr-json 压缩或分片二维码扫描结果 |
automated-sing-box-generator status |
查看部署状态和服务健康度 |
automated-sing-box-generator doctor |
运行系统和依赖状态诊断检查 |
automated-sing-box-generator validate |
校验生成配置语法的正确性 |
automated-sing-box-generator benchmark |
对代理节点进行速度和延迟测试 |
automated-sing-box-generator install |
仅安装依赖(WARP、sing-box) |
automated-sing-box-generator update |
更新 sing-box 到最新版本 |
automated-sing-box-generator cleanup-dns |
删除由于本工具产生的冗余 Cloudflare 域名解析记录 |
automated-sing-box-generator certs |
TLS 证书强制维护/续签任务,自动配置 ACME |
automated-sing-box-generator uninstall |
完整卸载工具部署的所有组件(可带 --remove-warp) |
automated-sing-box-generator redeploy |
保留域名,重新生成全部随机凭据并重部署 |
示例
# 非交互式部署,仅启用 AnyTLS 和 TUIC
automated-sing-box-generator deploy --domain example.com --protocols anytls,tuic
# 导出分享链接
automated-sing-box-generator export --format link
# 导出客户端 JSON 到文件
automated-sing-box-generator export --format json --output client.json
# 导出二维码 (协议分享链接)
automated-sing-box-generator export --format qr
# 导出二维码 (完整 JSON 配置)
automated-sing-box-generator export --format qr-json
# 还原压缩/分片 qr-json 扫描结果
automated-sing-box-generator decode-qr-json --input scans.txt --output client.json
# 切换到仅 Hysteria2
automated-sing-box-generator config --protocols hy2
核心特性
协议可选化
部署时交互选择或通过 --protocols 指定要启用的协议组合:
- AnyTLS (Reality) — TCP,无需 TLS 证书,使用 Reality 伪装
- TUIC — UDP/QUIC,需要 TLS 证书
- Hysteria2 — UDP/QUIC,需要 TLS 证书
仅启用 AnyTLS 时无需签发证书,部署更快。
路由模式(route-mode)
客户端配置内置三层出站架构,通过顶层 route-mode selector 在客户端 UI 一键切换:
| 模式 | 未匹配流量 | DNS(未匹配域名) | 推荐场景 |
|---|---|---|---|
| route(默认) | 直连 | 1.1.1.1 DoH via 代理 | 日常使用,规则分流 |
| global | 走代理节点(含 direct 规则) | 1.1.1.1 DoH via 代理 | 需要全局翻墙、测试连通性 |
| direct | 直连 | 1.1.1.1 DoH via 代理 | 临时关闭代理、排查网络问题 |
三层结构:
route-mode (selector) ← route.final 指向这里
├── route (direct) ← 默认,规则分流
├── global (selector) ← 全局代理,可手动选节点或 proxy-auto
│ ├── proxy-auto (urltest) ← 自动测速选最优
│ ├── anytls-out / tuic-out / hy2-out
│ └── route ← 兜底回退到直连
└── direct (direct) ← 全部直连
注意:切到
global时,分流规则的结果会收敛到代理出口(等价全局代理);仅保留私网地址直连以保证局域网访问稳定。
排查指南:
| 现象 | 排查步骤 |
|---|---|
| 全局模式下某些网站打不开 | 切到 global → 确认 proxy-auto 已选中且测速通过;若节点全挂,在 global 里手动切到 route 兜底 |
| 规则模式下国内网站走了代理 | 检查 rules.json 的 direct_suffix 是否包含该域名,或确认 GEOSITE-CN 规则集已下载 |
| DNS 解析慢 | DNS final 为 dns-remote(1.1.1.1 DoH via 代理),若代理延迟高会影响首次解析;已匹配的国内域名走 dns-direct(223.5.5.5)不受影响 |
| 想临时绕过大部分代理 | 切 route-mode → direct;显式 proxy 规则仍生效 |
DNS 记录自动管理
部署时自动通过 Cloudflare API:
- 为每个协议生成随机子域名前缀(每次部署唯一,不在代码中硬编码)
- 检测服务器公网 IP
- 自动创建 A 记录,IP 变更时自动更新
- 协议切换时自动清理不再需要的记录
- 所有记录打上
managed:sing-box-deploy标识,不会误删用户其他 DNS 记录
凭据安全
- 每次部署随机生成 UUID、Reality 密钥对、各协议独立密码、随机 short_id
- 凭据存储于 VPS 本地
/etc/sing-box/deploy-state.json(权限0600,仅 root 可读) - 服务端配置
/etc/sing-box/config.json同样0600权限 - 代码仓库中不含任何硬编码凭据或子域名前缀
deploy-state.json已加入.gitignore
客户端导出
- JSON — 完整 sing-box 客户端配置,可直接导入 GUI 客户端
- Share Link — 标准 URI 格式(
tuic://、hy2://、anytls://) - QR Code — 终端 ASCII 二维码,手机扫码导入。支持协议分享链接 (
--format qr) 或完整 JSON 配置 (--format qr-json)。
完整 JSON 二维码恢复格式
export --format qr-json 始终使用完整客户端 JSON,不会为了二维码容量省略路由规则或其他配置内容。输出策略自动选择:
- 原始 JSON 能放入单个二维码时,直接输出可导入客户端的 JSON 二维码。
- 原始 JSON 超出容量时,输出
SBOX:ZLIB45载荷:完整 JSON 先用 zlib 压缩,再用 Base45 编码,便于二维码使用 alphanumeric 模式承载。 - 压缩后仍超出单个二维码容量时,自动输出多个
SBOX:ZLIB45分片二维码。每个分片包含序号、总数和 SHA-256 校验值,解码时会检查缺片、错片和内容损坏。
压缩或分片二维码不能直接导入普通 sing-box 客户端,需要先还原为 JSON。将扫码得到的每个 QR 文本按“一行一个”保存到 scans.txt,然后运行:
automated-sing-box-generator decode-qr-json --input scans.txt --output client.json
也可以从 stdin 读取:
cat scans.txt | automated-sing-box-generator decode-qr-json --input - --output client.json
WARP 集成
- 自动安装官方 Cloudflare WARP(
warp-svc+warp-cli) - 自动识别本地代理模式(
127.0.0.1:40000)与系统隧道模式 - 集成 Watchdog 守护脚本,每分钟检测 WARP 健康状态,自动重连/重注册
其他
- TLS 证书通过 acme.sh + Cloudflare DNS-01 自动签发和续签
- sing-box 自动更新定时任务(每日凌晨检查 GitHub Latest Release)
- 端口冲突自动检测
- 部署状态持久化,支持重新生成配置和导出而无需重新部署
部署流程
输入主域名 + CF 凭据 + 选择协议
│
▼
生成随机子域名前缀
│
▼
检测服务器公网 IP → Cloudflare API 创建 A 记录
│
▼
安装依赖 (WARP + sing-box) + 端口检查
│
▼
签发 TLS 证书 (仅 TUIC/Hy2 需要)
│
▼
生成随机凭据 → 写入服务端配置 + 保存部署状态
│
▼
部署 Watchdog → 重启 sing-box → 输出客户端配置
项目结构
| 文件 | 说明 |
|---|---|
cli.py |
CLI 入口,argparse 子命令分发 |
deploy.py |
核心部署/重配置/状态查看流程 |
config.py |
服务端/客户端 sing-box 配置生成(按协议动态组装) |
cloudflare_dns.py |
Cloudflare API DNS 记录管理(创建/更新/清理) |
credentials.py |
UUID、Reality 密钥、随机密码、子域名前缀生成 |
certs.py |
acme.sh + Cloudflare DNS-01 证书签发 |
installer.py |
WARP、sing-box 安装与端口检查 |
state.py |
部署状态持久化(/etc/sing-box/deploy-state.json) |
export.py |
客户端配置导出(JSON / Share Link / QR) |
watchdog.py |
WARP Watchdog 脚本生成与 crontab 挂载 |
route_profile.py |
客户端路由规则与 DNS 配置 |
ui.py |
终端 UI 输出函数 |
Cloudflare 凭据
需要一个具有 DNS 编辑权限 的 Cloudflare API Token 和对应的 Zone ID。
提供方式(二选一):
- 运行时交互输入(部署时会自动提示)
- 环境变量(运行前导出):
export CF_Token="你的 Cloudflare API Token"
export CF_Zone_ID="你的 Zone ID"
这些凭据不会被持久化到磁盘,仅在部署过程中使用。
安全说明
本项目设计为公开仓库安全存储:
- 所有敏感数据(密码、私钥、子域名前缀)在部署时动态生成,存储于 VPS 的
0600文件中 - 代码中不含任何硬编码凭据
- Cloudflare API Token 仅在内存中使用,不写入磁盘
- DNS 记录通过 API comment 标识,清理时不会误删用户其他记录
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file automated_sing_box_generator-0.3.2.tar.gz.
File metadata
- Download URL: automated_sing_box_generator-0.3.2.tar.gz
- Upload date:
- Size: 47.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
01f8f286aea1cc70d05b9298fe9547b36b4621b377e8b4cd7125d4b74a59e094
|
|
| MD5 |
08dbfffb4c34cb14c85d0d0801a2916b
|
|
| BLAKE2b-256 |
ac9316e02559044bee3683e86cc915cf74f72cb647fb86ca71d171cfa912b840
|
Provenance
The following attestation bundles were made for automated_sing_box_generator-0.3.2.tar.gz:
Publisher:
publish.yml on henryliu443/Automated-sing-box-json-generator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
automated_sing_box_generator-0.3.2.tar.gz -
Subject digest:
01f8f286aea1cc70d05b9298fe9547b36b4621b377e8b4cd7125d4b74a59e094 - Sigstore transparency entry: 1624152649
- Sigstore integration time:
-
Permalink:
henryliu443/Automated-sing-box-json-generator@9fb0163d48cf6b7ad0015aa59b58935563edb64f -
Branch / Tag:
refs/tags/v0.3.2 - Owner: https://github.com/henryliu443
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9fb0163d48cf6b7ad0015aa59b58935563edb64f -
Trigger Event:
release
-
Statement type:
File details
Details for the file automated_sing_box_generator-0.3.2-py3-none-any.whl.
File metadata
- Download URL: automated_sing_box_generator-0.3.2-py3-none-any.whl
- Upload date:
- Size: 49.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a9c4897c7f864195a4878d461549b7ec1551afda57eebff4cc8a05eb7c59050
|
|
| MD5 |
057cce09cacee5f3329da3f23b26698d
|
|
| BLAKE2b-256 |
ea92d4b760382217d3862720f513896a480fdff577b3fde9d8c866db4fef9c6b
|
Provenance
The following attestation bundles were made for automated_sing_box_generator-0.3.2-py3-none-any.whl:
Publisher:
publish.yml on henryliu443/Automated-sing-box-json-generator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
automated_sing_box_generator-0.3.2-py3-none-any.whl -
Subject digest:
5a9c4897c7f864195a4878d461549b7ec1551afda57eebff4cc8a05eb7c59050 - Sigstore transparency entry: 1624152732
- Sigstore integration time:
-
Permalink:
henryliu443/Automated-sing-box-json-generator@9fb0163d48cf6b7ad0015aa59b58935563edb64f -
Branch / Tag:
refs/tags/v0.3.2 - Owner: https://github.com/henryliu443
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9fb0163d48cf6b7ad0015aa59b58935563edb64f -
Trigger Event:
release
-
Statement type: