VPS 科学上网教程 | 07 订阅转换服务(原创)

接下来将分享由我原创的方法

3x-ui 自带的订阅格式只是原始的 vless:// 链接,Clash 需要 YAML 格式。这一步部署一个 Python 脚本,自动完成格式转换,同时集成 Cloudflare 优选 IP。


服务功能

  • GET /GET /clash → 返回 Clash YAML 格式订阅
  • GET /v2ray → 返回 base64 编码的 v2ray 格式订阅(vless:// 链接)

部署脚本

在服务器上创建脚本文件:

1
nano /usr/local/bin/clash-sub.py

脚本内容见 GitHub 仓库 scripts/clash-sub.py


修改脚本配置

编辑脚本顶部的配置项:

1
2
3
4
5
6
7
8
9
10
SUB_URL    = 'https://127.0.0.1:2096/sub/你的订阅ID'
SUB_HOST = 'tunnel.yourdomain.xyz'
DIRECT_IP = '你的VPS公网IP'
TOP_N = 10

CARRIERS = {
'unicom': {'file': '/opt/cfst/result_unicom.csv', 'name': '联通'},
'mobile': {'file': '/opt/cfst/result_mobile.csv', 'name': '移动'},
'telecom': {'file': '/opt/cfst/result_telecom.csv', 'name': '电信'},
}

如何找到订阅 ID:

在 3x-ui 面板 → 设置 → 订阅设置 中可以看到订阅地址,类似:

1
2
3
https://127.0.0.1:2096/sub/uwb7cegzppwny4uq
^^^^^^^^^^^^^^^^
这就是订阅 ID

创建 systemd 服务

1
nano /etc/systemd/system/clash-sub.service

内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=Clash Subscription Converter
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/local/bin/clash-sub.py
Restart=always
RestartSec=5
User=root

[Install]
WantedBy=multi-user.target

启用并启动:

1
2
3
4
systemctl daemon-reload
systemctl enable clash-sub
systemctl start clash-sub
systemctl status clash-sub

🟢小绿灯亮起来


测试服务

直接在你自己的电脑上测试,这样才能验证 Cloudflare 转发是否正常:

1
2
3
4
5
# 测试 Clash 订阅
curl -s http://tunnel.yourdomain.xyz | head -20

# 测试 v2ray 订阅
curl -s http://tunnel.yourdomain.xyz/v2ray | base64 -d | head -5

这里的域名可以换成自己配的 tunnel 域名,反正 CF 会解析的

或者直接用浏览器打开 http://tunnel.yourdomain.xyz,会自动下载一个 YAML 文件,打开看看内容对不对就行。

Clash 订阅应该返回类似:

1
2
3
4
5
6
7
8
9
10
11
12
13
proxies:
- name: "CF-WS-TLS"
- name: "REALITY-TCP"
- name: "联通优选-1"
...

proxy-groups:
- name: "Auto Pilot"
type: url-test
- name: "联通 Best"
type: url-test
- name: "Take The Wheel"
type: select