这是一个自动化的Clash订阅管理服务,可以从网络下载订阅内容,应用自定义修改脚本,然后提供处理后的配置文件下载。
- 根据URL参数下载最新的订阅内容
- 应用JavaScript修改脚本处理配置(如添加自定义规则、代理组等), 参考https://www.clashverge.dev/guide/script.html
- 支持配置名称参数,传递给修改脚本使用
- 支持缓存功能,当下载失败时使用缓存内容
- 通过Web界面提供RESTful API
- 支持安装为systemd系统服务
- 支持HTTPS安全访问,可配置强制HTTPS
- 支持传递Clash订阅响应头,如订阅流量信息、更新间隔等
- 安装依赖
npm install- 修改配置文件
service-config.yaml
# 修改脚本路径
scriptPath: ./ai-rules-script.js
# 服务端口
port: 3000
# 是否使用缓存
useCache: true
# HTTPS配置
https:
# 是否启用HTTPS
enabled: false
# 是否强制使用HTTPS(当同时启用HTTP和HTTPS时,将HTTP请求重定向到HTTPS)
forceHttps: false
# HTTPS端口,默认为443
port: 443
# SSL证书路径
certPath: ./cert/server.crt
# SSL私钥路径
keyPath: ./cert/server.key- 运行服务
npm start服务默认在 http://localhost:3000 启动。
-
准备SSL证书
您可以使用自签名证书(仅用于测试)或从Let's Encrypt等机构获取免费证书:
# 创建证书目录 mkdir -p cert # 生成自签名证书(仅用于测试) openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout cert/server.key -out cert/server.crt
-
修改配置文件中的HTTPS设置
# HTTPS配置 https: enabled: true forceHttps: true # 设置为true可以强制所有HTTP请求重定向到HTTPS port: 443 certPath: ./cert/server.crt keyPath: ./cert/server.key
-
重启服务
修改配置后重启服务,HTTPS服务将在指定端口启动。如果启用了forceHttps选项,所有HTTP请求将自动重定向到HTTPS端口。
- 安装依赖
npm install- 运行安装脚本 (需要sudo权限)
sudo node install-service.js- 按照提示操作,设置服务描述和运行用户
安装完成后,服务将自动启动并设置为开机自启动。您可以使用以下命令管理服务:
# 启动服务
sudo systemctl start clash-subscription-converter
# 停止服务
sudo systemctl stop clash-subscription-converter
# 重启服务
sudo systemctl restart clash-subscription-converter
# 查看服务状态
sudo systemctl status clash-subscription-converter
# 查看日志
sudo journalctl -u clash-subscription-converter- 卸载服务
sudo node install-service.js uninstall- 构建Docker镜像
docker build -t clash-subscription-converter .- 运行容器
docker run -d -p 3000:3000 --name clash-converter clash-subscription-converter- 使用卷挂载配置和缓存(可选)
docker run -d -p 3000:3000 \
-v /path/to/config:/app/service-config.yaml \
-v /path/to/cache:/app/cache \
--name clash-converter clash-subscription-converterGET /convert?url=订阅链接&name=配置名称&noCache=0
参数说明:
url: (必需) 要转换的订阅链接name: (可选) 配置名称,用于传递给修改脚本noCache: (可选) 设置为1时不使用缓存
POST /convert
Content-Type: application/json
{
"url": "订阅链接",
"name": "配置名称",
"noCache": false
}
修改脚本需要定义一个main函数,接收两个参数:
config: Clash配置对象profileName: 配置文件名称(如果未提供name参数则为空字符串)
脚本示例 (rules-script.js):
function main(config, profileName) {
console.log(`处理配置: ${profileName}`);
// 添加自定义规则
if (!config.rules) {
config.rules = [];
}
// 添加AI相关规则
const aiRules = [
'DOMAIN-SUFFIX,example.com,AI'
// 更多规则...
];
config.rules = aiRules.concat(config.rules);
// 返回修改后的配置
return config;
}在service-config.yaml中可以配置以下选项:
| 选项 | 说明 | 默认值 |
|---|---|---|
| scriptPath | 修改脚本的路径 | ./rules-script.js |
| port | 服务运行的端口 | 3000 |
| useCache | 是否启用缓存功能 | true |
| https.enabled | 是否启用HTTPS | false |
| https.forceHttps | 是否强制使用HTTPS | false |
| https.port | HTTPS服务端口 | 443 |
| https.certPath | SSL证书路径 | ./cert/server.crt |
| https.keyPath | SSL私钥路径 | ./cert/server.key |
- 每次下载订阅内容后会自动缓存
- 当下载失败且
useCache为true时,会使用缓存内容 - 可以通过
noCache=1请求参数强制忽略缓存 - 缓存文件保存在
cache目录中
本服务支持透传原始订阅链接的以下Clash特有响应头,方便Clash客户端使用:
-
content-disposition
- 设置配置文件名
- 示例:
content-disposition: attachment; filename="my-config.yaml" - 支持UTF-8编码的中文文件名
-
profile-update-interval
- 设置配置更新间隔(单位:小时)
- 示例:
profile-update-interval: 24
-
subscription-userinfo
- 显示流量使用情况和到期时间
- 示例:
subscription-userinfo: upload=1234; download=2234; total=1024000; expire=2218532293
-
profile-web-page-url
- 提供订阅的Web页面链接
- 示例:
profile-web-page-url: https://example.com
- 当检测到请求的User-Agent中包含"clash"字样时,服务会自动传递这些响应头
- 订阅响应头也会随内容一起缓存,在使用缓存时同样会传递
- 使用Clash订阅链接时,我们会自动使用包含"clash"字样的User-Agent请求原始链接
使用Clash客户端(如Clash Verge)添加订阅时,使用本服务的转换链接即可:
http://your-server:3000/convert?url=原始订阅链接&name=配置名称
客户端将自动接收并处理这些响应头,显示相应的流量信息、更新频率等。
- 如果不提供
name参数,脚本中的profileName参数将为空字符串,基于订阅文件名的规则脚本请务必设置。 - 作为systemd服务运行时,请确保运行用户对应用目录有足够的权限。
- 使用HTTPS时,请确保证书文件路径正确且有效,否则HTTPS服务将无法启动。
- 如果使用自签名证书,浏览器可能会显示安全警告,这是正常现象。在生产环境中,建议使用受信任的SSL证书。
- 启用
forceHttps选项后,所有HTTP请求将自动重定向到HTTPS。请确保HTTPS服务正常运行,否则将无法访问服务。