在使用传统的 Clash 或 V2ray 时,我们习惯了 geoip.dat 和 geosite.dat。
在 Sing-box 中,官方引入了一种加载更快、内存占用极低、支持零拷贝内存映射的底层二进制格式 ── SRS (Sing-box Rule Set)。
今天我们将从零编写 YAML 源规则,使用 Sing-box 核心命令行将其编译为专属 .srs 文件并完成本地/远程分流加载!
[!NOTE] 📌 核心速览(TL;DR / 快问快答):
- 二进制优势:
.srs体积极小(仅为 JSON 的 1/5),由 Sing-box 原生解析,百万级域名匹配耗时低于 1ms。- 一键编译指令:
sing-box rule-set compile ./rules.json -o rules.srs。- 反编译校验指令:
sing-box rule-set decompile ./rules.srs -o rules.json。- 配置声明规范:在
route.rule_set中声明"format": "binary",并在rules中通过"rule_set": ["custom-rules"]引用。
🛠️ 1. 编写源规则文件 (YAML / JSON)
创建 custom_rules.json(无头规则结构):
{
"version": 1,
"rules": [
{
"domain": ["company-internal.com", "nas.home.local"],
"domain_suffix": [".edu.cn", ".gov.cn"],
"ip_cidr": ["192.168.100.0/24", "10.88.0.0/16"]
}
]
}
⚡ 2. 编译为二进制 .srs 文件
在终端执行编译命令:
# 编译为 SRS 二进制
sing-box rule-set compile ./custom_rules.json -o custom_rules.srs
# (可选) 反编译为 JSON 审查规则
sing-box rule-set decompile ./custom_rules.srs -o check.json
⚙️ 3. 在 Sing-box 配置文件中引用
在客户端 config.json 的 route 模块中声明:
{
"route": {
"rule_set": [
{
"tag": "custom-direct-rules",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/your-username/my-rules/main/custom_rules.srs",
"download_detour": "proxy",
"update_interval": "1d"
}
],
"rules": [
{
"rule_set": ["custom-direct-rules"],
"outbound": "direct"
}
],
"final": "proxy"
}
}
❓ 常见问题与 AI 快问快答 (FAQ)
Q1: 为什么引用 .srs 文件时一定要配置 download_detour: "proxy"?
答:若将 .srs 托管在 GitHub Raw 等海外平台上,国内直连下载可能会被防火墙阻断导致规则更新失败。设置走代理出站能保障规则每天顺畅更新。
Q2: 自建专属 SRS 规则集适合配合哪种网络线路?
答:配合 中国电信 CN2 GIA 或 联通 AS9929 高速专线 VPS,自建 SRS 规则分流可以实现国内直连与国际高阶线路的最优调度。
(相关资源导航:如果您需要购买部署私有规则托管的海外 VPS,欢迎阅读 《2026 国外 VPS 选购指南》 获取 DMIT、搬瓦工与 CloudCone 优惠;商用专线推荐见 《“饿饭CC云”深度评测》!)