客户端指南 #sing-box #VLESS

通用代理核心 sing-box 架构拆解与配置指南:Inbounds 至 Rule-Set 详解

系统梳理通用网络代理平台 sing-box 的模块化结构

1. sing-box 的核心设计思路

在当前的跨网代理技术体系里,sing-box 是继 Shadowsocks、V2Ray、Xray 与 Clash 之后出现的新一代通用代理内核。由开发者 nekohasekai 发起,其整体架构理念主要围绕三个方向展开:模块化设计、广泛协议兼容及高效运行表现。

+-------------------------------------------------------------------------+
|                           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 内核的方式,支持多类型同时启用:

  • mixed:同时处理 HTTP 与 SOCKS5 请求。
  • tun:建立系统级虚拟网卡接口,接管全局流量。
  • tproxy 与 redirect:常用于 Linux 及 OpenWrt 设备的透明代理网关场景。

2.2 配置出站策略组和 Outbounds

Outbounds 负责管理流量离开内核后的去向,通常划分为两类:

  1. 基础协议节点:vless (Reality), hysteria2, tuic, shadowsocks, trojan, wireguard, direct, block。
  2. 策略选择组:
    • selector:由用户手动指定目标节点。
    • urltest:通过 HTTP 速率检测自动优选低延迟节点。
    • fallback:用于主备节点之间的故障切换。

2.3 通过 Rule-Sets 为 Route 提供规则

旧式代理软件往往需要解析海量纯文本规则,而 sing-box 则采用了 Rule-Set 机制:

  • 将分流规则预编译为二进制 .srs 文件进行下发。
  • 内核借助内存对齐的 Radix Tree 与二分查找算法完成匹配,显著压缩内存开销并提升规则检索效率。

2.4 独立的 DNS 路由模块

区别于早期工具将域名解析与出站逻辑耦合的做法,sing-box 实现了 DNS 子系统的独立编排:

  • 支持按域名归属分配对应的上游解析器(例如境内域名交由国内 DoH,境外域名指向 Cloudflare 或 Google DoH)。
  • 允许为 DNS 查询单独绑定 detour(决定解析请求自身所经由的出站通道),从而有效防止域名解析过程遭到干扰或篡改。

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、Mihomo (Clash) 与 Xray 核心对比

对比项sing-boxMihomoXray-core
实现语言Go(轻量精简实现)Go(衍生自 Clash)Go(衍生自 V2Ray)
内存占用15 MB ~ 40 MB (较低)40 MB ~ 120 MB30 MB ~ 80 MB
配置形式强结构化 JSON兼容 YAML / JSON分段式 JSON
分流规则形式.srs 二进制基数树文件GeoIP.datGeoIP.dat
协议覆盖情况 含 Reality98%(覆盖较广)90%(TUIC
常见图形客户端Karing、Hiddify、sing-box GUIClash Verge Rev, Nyanpasu, Flclashv2rayNG、v2rayU、v2rayN
透明代理与网关支持原生内建 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")。
  • 确认在初始化阶段至少存在一个无需拉取远程 rule-set 即可联网的基础 direct 或 proxy 出站。

6. 总结

sing-box 依靠清晰的层次划分、丰富的协议支持以及高效的二进制规则集,在现代化代理核心中表现突出。理顺 Inbound、Outbound、DNS 和 Route 四个组件之间的协作后,便能构建出稳定且低延迟的网络路由体系。

相关标签: