前言

  • Sing-box 虽然直接改 config.json 很方便,但是节点大批量变化的时候,手改就显得太傻逼了
  • 用别人的订阅转换怕泄露节点信息,不如直接自建

部署

Ref: https://github.com/Toperlock/sing-box-subscribe

Docker

Ref: https://github.com/hestudy-blog/sing-box-subscribe-docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
version: '3'

services:
nginx:
image: hestudy/sing-box-subscribe:latest
container_name: sub
networks:
- reverse-network # 和 Nginx 在同一网桥
restart: unless-stopped
volumes:
- ./config_template:/sing-box-subscribe/config_template

networks:
reverse-network:
external: true

本地安装

  1. 安装 Python 3.10 及以上版本,并确保添加到环境变量中image

  2. 在终端中,输入以下命令来安装依赖项(在 Mac 上,替换 pippip3

    1
    pip install -r requirements.txt
  3. 下载 sing-box-subscribe,并打开终端导航到项目目录(可以直接在文件路径输入 cmdimage

  4. 将订阅链接放入 providers.json,在 config_template 文件夹中编辑模版,或者直接使用默认模版,使用以下命令运行订阅转换脚本

    1
    python main.py
  5. 其余具体还是看官方说明吧,下面会贴一下。这里顺便提一下注意的点

    1. providers.json 中一般需要改的只有 urlUser-Agent 一般默认为 v2rayng 即可,如果你的机场只有 Clash 订阅或者 sing-box 订阅,请相应修改为 clashmeta 或者 sing-box
    2. 默认不支持 SSR,不过都 2024 年了,谁还在用这个协议啊

    Put your subscription links in providers.json, edit config_template_groups_tun.json file and use the following command to run the script after editing the template:

    1
    python main.py

    or you can directly select the template with the template_index, 0 means the first template

    1
    python main.py --template_index=0

    For Windows systems, it’s recommended to add the commands to a batch program for execution.

    Before using, make sure to edit the providers.json file and the .json template files in the config_template directory.

    A lazy configuration config_template_groups_rule_set_tun file is included, which allows filtering nodes based on different categories:

    • Implement Openai routing rules
    • Implement Youtube routing rules
    • Implement Google routing rules
    • Implement Github routing rules
    • Implement Telegram routing rules
    • Implement Twitter routing rules
    • Implement Facebook routing rules
    • Implement Instagram routing rules
    • Implement Bilibili routing rules
    • Implement Bahamut routing rules
    • Implement Spotify routing rules
    • Implement TikTok routing rules
    • Implement Netflix routing rules
    • Implement Disney+ routing rules
    • Implement Apple routing rules
    • Implement Amazon routing rules
    • Implement Microsoft routing rules
    • Implement Game routing rules
    • Implement Hbo routing rules
    • Implement Prime Video routing rules

    providers.json File

    In this file, you can add subscription links and basic settings.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    {
    "subscribes":[
    {
    "url": "https://4gviet.com/api/v1/client/subscribe?token=xx",
    "tag": "airport1_tag", //You can keep the default without modification
    "enabled": true, //Enable conversion of this link
    "emoji": 1, //Add flag emoji
    "subgroup": "",
    "prefix": "", //Do not add node name prefix
    "ex-node-name": "网站|流量|过期", //Filter nodes containing keywords
    "User-Agent":"clashmeta" //Set browser UA
    },
    {
    "url": "https://5gtocdocao.com/api/v1/client/subscribe?token=xx",
    "tag": "airport2_tag", //You can keep the default without modification
    "enabled": false, //Disable conversion of this link
    "emoji": 0, //Do not add flag emoji
    "subgroup": "named", //Name the subscription link
    "prefix": "❤️node_name prefix - ", //Add node name prefix
    "User-Agent":"clashmeta" //Set browser UA
    }
    ],
    "auto_set_outbounds_dns":{
    "proxy": "",
    "direct": ""
    },
    "save_config_path": "./config.json",
    "auto_backup": false,
    "exclude_protocol": "ssr", //Not parsing ssr nodes!!!
    "config_template": "", //Customize the correct web page json configuration template link
    "Only-nodes": false //Output the complete sing-box configuration
    }
    • url: Required.

    Supports setting up a regular V2 subscription link (content in base64 encoding)

    Supports setting up a clash subscription link

    Supports setting up a sing-box subscription link

    Supports setting up a local file paths (content as standard URI links or Clash field)

    1
    2
    3
    4
    5
    Local files with `.txt` suffix need to add single node share links one per line in the file, e.g. `ss://` at the beginning (non-subscription links).

    Local files with `.yaml` suffix, with the correct clash proxies fields filled in.

    Local files need to be saved on the same drive. Local path formats: `/Desktop/sing-box-subscribe/xx.txt` or relative path formats in the same folder as `main.py`: `./xx.txt`.
    • tag: Required. Just keep the default.

    Fill in this tag in the config template to add this subscription. The “airport1_tag” here corresponds to “{机场1}” in the config template. Specific usage can be found in the config template section below.

    tag screenshot reference
    download
    • enabled: Optional. Set it to false, and the subscription will be ignored.
    • emoji: Optional. Set it to false or 0, and the node name will not have a country flag emoji.
    • subgroup: Optional. Name the subscription link and generate an outbound.
    • prefix: Optional. Set a custom prefix that will be added to the beginning of the node names. If not set, no prefix will be added.
    • ex-node-name: Optional. Filter nodes containing keywords. Multiple keywords are separated by “|”
    • User-Agent: Optional. You can customize UA, such as setting UA to “clash.meta” or “sing-box”
    prefix effect reference

    Snipaste_2023-05-02_12-53-27

    • auto_set_outbounds_dns: Optional.

    Includes proxy and direct settings.

    proxy and direct should be set to the tag of the dns server in the config template file.

    With this option set, the script will automatically adapt routing rules to DNS rules.

    DNS servers for outbound rules with direct setting in the routing rules will be set to the specified direct outbound.

    Outbound rules that need to be proxied in the routing rules will be set to the corresponding proxy outbound, and the script will automatically create a corresponding dns server for the proxy outbound, using the dns server specified in the proxy setting.

    • save_config_path: Required. Set the path for the generated configuration file.
    • auto_backup: Optional.

    When set to true, the script will rename the currently used sing-box configuration file to original_filename.current_time.bak for backup purposes, in case an incorrect configuration file is generated and needs to be restored.

    • exclude_protocol: Optional.

    Set the protocols to exclude, separated by commas, e.g., ssr, vmess.

    Sharing links using protocols in this setting will be ignored.

    The sing-box release program does not support ssr (needs additional parameters to build), so this setting might be useful.

    • config_template:Optional. Enter a correct webpage json configuration template link to generate sing-box configuration from this template.
    • Only-nodes: Optional.

    When it is set to true or 1, only the node information in sing-box format of the subscription link is output.

    config Template Files

    The script will search for JSON template files in the config_template directory, and you can select which template file to use when the script runs.

    For example, if there are tun.json and socks.json template files in the directory.

    Snipaste_2023-03-24_22-16-49

    The script does not validate the correctness of the template files. If the template file is incorrect, errors will occur, and the script won’t run.

    The template files are similar to sing-box configs, but with some new parameters like {all}, {机场tag} (translated as {airport_tag}), filter, which only work with clash_mode in urltest and selector outbounds.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
      >{
    "tag":"proxy",
    "type":"selector",
    "outbounds":[
    "auto",
    "{all}"//All nodes of all subscriptions are added to the location of this tag
    ],
    "filter":[
    //This filter will remove nodes containing ˣ² in airport1_tag
    {"action":"exclude","keywords":["ˣ²"],"for":["机场1"]}
    ]
    },
    {
    "tag":"netflix",
    "type":"selector",
    "outbounds":[
    "{机场1}",//Tag with the airport1_tag will be added to this tagged location
    "{机场2}"//Tag with the airport2_tag will be added to this tagged location
    ],
    "filter":[
    //If airport1_tag and airport2_tag have nodes with these names 'sg','新加坡','tw','台湾' they collectively form the netflix group
    {"action":"include","keywords":["sg|新加坡|tw|台湾"]},
    //The "for" is set to airport1_tag, which means that this rule only works on airport1_tag
    {"action":"exclude","keywords":["ˣ²"],"for":["机场1"]}
    //This filter will remove nodes containing ˣ² in airport1_tag
    ]
    }
    • {all}: Represents all nodes in all subscriptions. The script will add all nodes to the outbounds with this identifier.
    • {机场tag} (translated as {airport_tag}): The airport tag set in providers.json can be used here, representing all nodes in this subscription.
    • filter: Optional. Node filtering, an array object where you can add any number of rules, formatted as:
    1
    2
    3
    4
    "filter": [
    {"action": "include", "keywords": ["keyword1|keyword2"]},
    {"action": "exclude", "keywords": ["keyword1|keyword2"], "for": ["airport1_tag", "airport2_tag"]}
    ]
    • Keyword case-sensitive
    • include: Add the keywords to be retained, use ‘|’ to connect multiple keywords. Nodes with names containing these keywords will be retained, and other nodes will be deleted.
    • exclude: Add the keywords to be excluded, use ‘|’ to connect multiple keywords. Nodes with names containing these keywords will be deleted, and other nodes will be retained.
    • for: Optional. Set the airport tag, can be multiple. This rule will only apply to the specified airports, and other airports will ignore this rule.

    Multiple rules will be executed in order.

Github 部署

细看官方文档吧,我用不到,不再赘述

结语

  • 通过 Docker 和 Github 在服务端部署我们都能拿到订阅直链,而本地的话我们只能拿到一个 config.json,当然把这个文件利用 Nginx 等工具放出去也是可以的
  • 我主推还是在服务器上用 Docker 部署,拿到直链全平台都能用,就是改配置稍微有些麻烦
  • 我的配置就不放了,还在持续完善中,贴一下作者的

模版的名称里不要带URL需要转义的特殊符号,比如 +