1. sing-box 核心设计哲学
在现代翻墙与网络代理技术栈中,sing-box 是继 Shadowsocks、V2Ray、Xray 和 Clash 之后的划时代通用代理平台。由知名开发者 SagerNet / nekohasekai 主导开发,sing-box 的核心设计哲学可以概括为三点:模块化架构、协议全覆盖与极致性能。
+-------------------------------------------------------------------------+
| sing-box 流量处理流水线 |
+-------------------------------------------------------------------------+
|
[1. Inbound: Mixed / TUN / SOCKS5 / TProxy]
|
[2. Sniffer (域名探测)]
|
+-------------------+-------------------+
| |
v v
[DNS 独立分流引擎] [Route 路由匹配引擎]
- Rule-based DNS Server - Rule-Sets (.srs)
- Independent Outbound Detour - GeoIP / GeoSite / Process
- Fake-IP / Direct / DoH - Domain / IP-CIDR / Port
| |
+-------------------+-------------------+
|
[3. Outbound: Reality / Hy2 / TUIC / Direct]
|
[4. 目标服务器]
2. 核心架构四大支柱剖析
2.1 Inbounds(入站网关)
Inbounds 定义了流量如何进入 sing-box 内核。现代 sing-box 支持多种入站并存:
mixed: 同时监听 HTTP 与 SOCKS5 协议。tun: 创建操作系统虚拟网卡,捕获全系统或全设备流量。tproxy/redirect: 用于 Linux / OpenWrt 软路由透明网关。
2.2 Outbounds(出站协议与策略组)
Outbounds 定义了流量如何流出 sing-box。主要包含两类:
- 协议端点:
vless(Reality),hysteria2,tuic,shadowsocks,trojan,wireguard,direct,block。 - 复合策略组:
selector: 手动选择节点。urltest: 自动根据 HTTP 延迟测速选择最快节点。fallback: 主备冗余容灾。
2.3 Route & Rule-Sets(高性能路由与规则集)
传统客户端每次加载成千上万行文本规则都需要经历耗时的 JSON/YAML 解析,而 sing-box 引入了 Headless Rule-Set 架构:
- 规则以预编译的二进制
.srs格式发布与分发。 - 内核使用基于内存对齐的 Radix Tree / 二分搜索,内存占用减少 80%,匹配吞吐量突破数百万 QPS。
2.4 独立的 DNS 路由子系统
在传统代理架构中,DNS 往往与代理出站混为一谈。sing-box 将 DNS 完全解耦:
- 可以为不同域名指定不同的上游 DNS 服务器(如国内走腾讯/阿里 DoH,国外走 Cloudflare/Google DoH)。
- 可以为每个 DNS 请求指定独立的
detour(即该 DNS 查询本身走哪条出站链路),从根本上避免了 DNS 查询被 GFW 拦截或被伪造。
3. 生产级 sing-box 1.10+ 完整配置文件实战
以下是一份精心调试、适用于日常生产与高性能出海场景的 config.json 模板:
{
"log": {
"level": "warn",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "dns_proxy",
"address": "https://1.1.1.1/dns-query",
"address_resolver": "dns_direct",
"detour": "proxy"
},
{
"tag": "dns_direct",
"address": "https://223.5.5.5/dns-query",
"detour": "direct"
},
{
"tag": "dns_fakeip",
"address": "fakeip"
},
{
"tag": "dns_block",
"address": "rcode://success"
}
],
"rules": [
{
"outbound": "any",
"server": "dns_direct"
},
{
"rule_set": "geosite-category-ads-all",
"server": "dns_block"
},
{
"rule_set": "geosite-cn",
"server": "dns_direct"
},
{
"query_type": ["A", "AAAA"],
"server": "dns_fakeip"
}
],
"fakeip": {
"enabled": true,
"inet4_range": "198.18.0.0/15",
"inet6_range": "fc00::/18"
},
"independent_cache": true
},
"inbounds": [
{
"type": "mixed",
"tag": "mixed-in",
"listen": "127.0.0.1",
"listen_port": 2080
},
{
"type": "tun",
"tag": "tun-in",
"interface_name": "singbox-tun",
"inet4_address": "172.19.0.1/30",
"auto_route": true,
"strict_route": true,
"stack": "mixed",
"sniff": true
}
],
"outbounds": [
{
"type": "selector",
"tag": "proxy",
"outbounds": ["auto-fallback", "node-vless-reality", "node-hysteria2", "direct"]
},
{
"type": "urltest",
"tag": "auto-fallback",
"outbounds": ["node-vless-reality", "node-hysteria2"],
"url": "https://www.gstatic.com/generate_204",
"interval": "3m",
"tolerance": 50
},
{
"type": "vless",
"tag": "node-vless-reality",
"server": "us-node.example.com",
"server_port": 443,
"uuid": "a7b3c2d1-e5f6-4a8b-9c0d-1e2f3a4b5c6d",
"flow": "xtls-rprx-vision",
"tls": {
"enabled": true,
"server_name": "gateway.icloud.com",
"utls": {
"enabled": true,
"fingerprint": "chrome"
},
"reality": {
"enabled": true,
"public_key": "YOUR_REALITY_PUBLIC_KEY",
"short_id": "0123456789abcdef"
}
}
},
{
"type": "hysteria2",
"tag": "node-hysteria2",
"server": "hk-hy2.example.com",
"server_port": 8443,
"password": "YOUR_STRONG_PASSWORD",
"up_mbps": 100,
"down_mbps": 500,
"tls": {
"enabled": true,
"server_name": "hk-hy2.example.com",
"insecure": false
}
},
{
"type": "direct",
"tag": "direct"
},
{
"type": "block",
"tag": "block"
},
{
"type": "dns",
"tag": "dns-out"
}
],
"route": {
"rule_set": [
{
"tag": "geosite-cn",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-cn.srs",
"download_detour": "proxy"
},
{
"tag": "geoip-cn",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-geoip/rule-set/geoip-cn.srs",
"download_detour": "proxy"
},
{
"tag": "geosite-category-ads-all",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-category-ads-all.srs",
"download_detour": "proxy"
}
],
"rules": [
{
"protocol": "dns",
"outbound": "dns-out"
},
{
"rule_set": "geosite-category-ads-all",
"outbound": "block"
},
{
"ip_is_private": true,
"outbound": "direct"
},
{
"rule_set": ["geosite-cn", "geoip-cn"],
"outbound": "direct"
}
],
"auto_detect_interface": true
}
}
4. 三大核心对比:sing-box vs Mihomo (Clash) vs Xray
| 特性维度 | sing-box | Mihomo (Clash.Meta) | Xray-core |
|---|---|---|---|
| 开发语言 | Go (高度轻量优化) | Go (基于 Clash 扩展) | Go (基于 V2Ray 衍生) |
| 内存常驻占用 | 15 MB ~ 40 MB (极低) | 40 MB ~ 120 MB | 30 MB ~ 80 MB |
| 配置文件格式 | 严格结构化 JSON | 宽松 YAML / JSON | 模块化 JSON |
| 规则集技术 | .srs 二进制预编译基数树 | GeoIP.dat / Rule-Provider | GeoIP.dat / GeoSite.dat |
| 协议支持完整度 | 100% (Reality/Hy2/TUIC/WireGuard) | 98% (全面) | 90% (TUIC/Hy2非原生) |
| 图形前端生态 | Hiddify, sing-box GUI, Karing | Clash Verge Rev, Nyanpasu, Flclash | v2rayN, v2rayU, v2rayNG |
| 透明网关与路由集成 | 原生极简 TUN/TProxy | 需配置复杂 Redir/TUN | 需结合 Iptables 外部脚本 |
5. 调试与排错技巧
1. 使用命令行验证配置合法性
在启动 sing-box 之前,务必通过以下指令进行静态语法检查与规则解析验证:
# 验证配置文件语法有效性
sing-box check -c /etc/sing-box/config.json
# 格式化或转换配置
sing-box format -c config.json -w
2. 规则集下载超时问题排错
当遇到 rule-set download error: context deadline exceeded 错误时:
- 检查
download_detour字段是否设置为了可用出站代理(如"download_detour": "proxy")。 - 确保在 initial 启动时至少有一个不依赖远程 rule-set 的基础 direct / proxy 出站可以联网。
6. 结语
sing-box 凭借其严谨的架构分层、领先的协议整合能力以及高效的二进制规则集,已经成为当今最具生命力的高性能代理核心。通过掌握 Inbound、Outbound、DNS 与 Route 四大模块的配合,你可以轻松搭建起一套坚不可摧、低延迟、零污染的现代化网络路由中枢。
本文基于实验室特定硬件环境及网络拓扑进行客观记录与测试,网络指标会随地域运营商、骨干网 QoS 波动而变化。所有内容仅供技术探索与合规网络研究。
需要稳定低延迟的 2026 优质跨境网络支持?
ClashLab 实验室针对全球 30+ 主流机场进行多轮 20:00-23:00 晚高峰吞吐与抗丢包实测,严选真 IPLC 专线与多线 BGP 容灾节点。