Skip to content
银河录像局gpt
山水云 机场

Sing-box 进阶配置:JSON 路由规则、DNS 防泄漏与多协议出站实战

快速上手只能让你连上代理。要实现「ChatGPT 走美国、B 站直连、DNS 不泄漏」的精细化控制,就需要理解 Sing-box 的 JSON 配置体系。

本文是 Sing-box 专题中最硬核的一篇,但我会尽量用通俗的方式讲清楚。

专题导航

首页下载安装快速上手进阶配置故障排除


一、Sing-box 配置文件全景

Sing-box 使用 JSON 格式的配置文件(config.json),整个配置分为 7 个核心模块:

config.json
├── log          → 日志配置
├── dns          → DNS 服务器与规则
├── inbounds     → 入站配置(TUN / HTTP / Mixed)
├── outbounds    → 出站配置(代理节点 / 直连 / 阻断)
├── route        → 路由规则(核心!决定流量怎么走)
├── experimental → 实验性功能(Clash API 等)
└── endpoint     → 端点配置(新版本特性)

你不需要手写全部内容——机场订阅会自动生成大部分配置。进阶配置主要是在订阅生成的配置基础上,修改 route 和 dns 规则


二、Log 日志配置

json
{
  "log": {
    "level": "info",
    "timestamp": true
  }
}
级别说明适用场景
trace极其详细深度调试
debug调试信息排错时临时使用
info常规信息⭐ 日常推荐
warn仅警告生产环境
error仅错误最安静
fatal仅致命错误几乎不输出

排错技巧

遇到连接问题时,先将 level 改为 debug,查看详细日志定位问题。排错完成后改回 info


三、Inbounds 入站配置

TUN 模式(推荐)

json
{
  "inbounds": [
    {
      "type": "tun",
      "tag": "tun-in",
      "interface_name": "sing-box-tun",
      "inet4_address": "172.19.0.1/30",
      "inet6_address": "fdfe:dcba:9876::1/126",
      "mtu": 1500,
      "auto_route": true,
      "strict_route": true,
      "stack": "system",
      "sniff": true
    }
  ]
}

关键字段说明:

  • auto_route: true:自动设置路由表,接管系统全局流量
  • strict_route: true:严格路由模式,防止流量泄漏(推荐开启)
  • stack:协议栈实现,可选 system(系统栈,兼容性好)或 gvisor(用户态栈,性能略低但更安全)
  • sniff: true:嗅探流量,从 TLS SNI / HTTP Host 中获取真实域名

Mixed 代理模式

如果你不想用 TUN,可以只开 HTTP/SOCKS 代理:

json
{
  "type": "mixed",
  "tag": "mixed-in",
  "listen": "127.0.0.1",
  "listen_port": 2080
}

然后在浏览器或系统代理中设置 127.0.0.1:2080


四、Outbounds 出站配置

出站定义了流量的出口。一个典型配置包含:代理节点、直连、阻断和选择器。

1. 选择器(Selector)——手动切换

json
{
  "type": "selector",
  "tag": "proxy",
  "outbounds": ["auto", "香港-01", "日本-01", "美国-01"],
  "default": "auto"
}

2. URLTest——自动测速

json
{
  "type": "urltest",
  "tag": "auto",
  "outbounds": ["香港-01", "日本-01", "美国-01"],
  "url": "https://www.gstatic.com/generate_204",
  "interval": "5m",
  "tolerance": 50
}
  • interval:测速间隔(5m = 5分钟)
  • tolerance:容差(ms),低于当前节点延迟 + 容差值时不切换,避免频繁跳动

3. 直连与阻断

json
{
  "type": "direct",
  "tag": "direct"
},
{
  "type": "block",
  "tag": "block"
},
{
  "type": "dns",
  "tag": "dns-out"
}

4. 代理节点示例(各协议)

Hysteria2 节点:

json
{
  "type": "hysteria2",
  "tag": "香港-Hy2",
  "server": "hk1.example.com",
  "server_port": 443,
  "password": "your-password",
  "tls": {
    "enabled": true,
    "server_name": "hk1.example.com"
  }
}

VLESS Reality 节点:

json
{
  "type": "vless",
  "tag": "美国-Reality",
  "server": "us1.example.com",
  "server_port": 443,
  "uuid": "your-uuid",
  "flow": "xtls-rprx-vision",
  "tls": {
    "enabled": true,
    "server_name": "www.apple.com",
    "reality": {
      "enabled": true,
      "public_key": "your-public-key",
      "short_id": "your-short-id"
    }
  },
  "transport": {
    "type": "tcp"
  }
}

Shadowsocks 节点:

json
{
  "type": "shadowsocks",
  "tag": "日本-SS",
  "server": "jp1.example.com",
  "server_port": 8388,
  "method": "aes-256-gcm",
  "password": "your-password"
}

五、Route 路由规则(核心)

路由规则决定了「什么流量走代理、什么流量直连、什么流量阻断」。这是进阶配置最重要的部分。

规则结构

json
{
  "route": {
    "rules": [
      {
        "协议": "条件",
        "outbound": "出口"
      }
    ],
    "final": "proxy",
    "auto_detect_interface": true
  }
}

常用匹配条件

条件说明示例
domain精确域名匹配"openai.com"
domain_suffix域名后缀匹配"google.com" 匹配所有 google 子域
domain_keyword域名关键词"youtube"
domain_regex正则匹配"^.*\\.google\\..*$"
geositeGeoSite 规则集"cn", "google", "openai"
geoipGeoIP 规则集"cn", "private"
ip_cidrIP CIDR 匹配"192.168.0.0/16"
port端口匹配80, 443
protocol协议匹配"http", "tls", "quic"
process_name进程名匹配"Telegram.exe"
package_nameAndroid 包名"com.twitter.android"

实战路由规则配置

以下是一个生产级路由规则示例:

json
{
  "route": {
    "rules": [
      {
        "protocol": "dns",
        "outbound": "dns-out"
      },
      {
        "ip_is_private": true,
        "outbound": "direct"
      },
      {
        "clash_mode": "direct",
        "outbound": "direct"
      },
      {
        "clash_mode": "global",
        "outbound": "proxy"
      },
      {
        "domain_suffix": ["openai.com", "chatgpt.com", "claude.ai", "anthropic.com"],
        "outbound": "美国"
      },
      {
        "domain_suffix": ["netflix.com", "nflxvideo.net"],
        "outbound": "美国"
      },
      {
        "domain_suffix": ["disneyplus.com", "disney-plus.net"],
        "outbound": "美国"
      },
      {
        "domain_suffix": ["youtube.com", "googlevideo.com", "ytimg.com"],
        "outbound": "proxy"
      },
      {
        "domain_suffix": ["bilibili.com", "bilibili.tv"],
        "outbound": "direct"
      },
      {
        "geosite": "category-ads-all",
        "outbound": "block"
      },
      {
        "geosite": "cn",
        "outbound": "direct"
      },
      {
        "geoip": "cn",
        "outbound": "direct"
      },
      {
        "geosite": "geolocation-!cn",
        "outbound": "proxy"
      }
    ],
    "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"
      }
    ],
    "final": "proxy",
    "auto_detect_interface": true
  }
}

规则执行顺序

Sing-box 的路由规则是从上到下依次匹配的,第一条匹配成功的规则即生效。因此:

  1. DNS 流量最先处理(避免 DNS 泄漏)
  2. 私有 IP 直连(局域网设备正常通信)
  3. 特定服务指定出口(如 ChatGPT 走美国)
  4. 广告域名阻断
  5. 国内域名/IP 直连
  6. 其余流量走代理(final

GeoSite 与 Rule Set

Sing-box 新版使用 Rule Set(规则集)替代旧版的 GeoSite/GeoIP:

json
"rule_set": [
  {
    "tag": "geosite-openai",
    "type": "remote",
    "format": "binary",
    "url": "https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-openai.srs"
  }
]

规则集会在首次运行时自动下载,后续缓存本地。使用时在规则中引用:

json
{
  "rule_set": "geosite-openai",
  "outbound": "美国"
}

规则集来源

官方规则集仓库:

  • GeoSite:https://github.com/SagerNet/sing-geosite
  • GeoIP:https://github.com/SagerNet/sing-geoip

六、DNS 配置与防泄漏

DNS 泄漏是科学上网中最常见的安全隐患——即使代理连接了,DNS 查询仍可能走国内运营商 DNS,导致域名被污染或记录。

推荐 DNS 配置

json
{
  "dns": {
    "servers": [
      {
        "tag": "remote-dns",
        "address": "https://1.1.1.1/dns-query",
        "address_resolver": "local-dns",
        "detour": "proxy"
      },
      {
        "tag": "local-dns",
        "address": "https://223.5.5.5/dns-query",
        "detour": "direct"
      },
      {
        "tag": "block-dns",
        "address": "rcode://success"
      }
    ],
    "rules": [
      {
        "outbound": "any",
        "server": "local-dns"
      },
      {
        "clash_mode": "direct",
        "server": "local-dns"
      },
      {
        "clash_mode": "global",
        "server": "remote-dns"
      },
      {
        "rule_set": "geosite-cn",
        "server": "local-dns"
      },
      {
        "rule_set": "geosite-geolocation-!cn",
        "server": "remote-dns"
      }
    ],
    "final": "remote-dns",
    "strategy": "ipv4_only",
    "independent_cache": true
  }
}

DNS 配置要点

配置说明
remote-dns国外 DNS(1.1.1.1),走代理,用于国外域名
local-dns国内 DNS(223.5.5.5),走直连,用于国内域名
strategy: ipv4_only仅查询 IPv4,避免 IPv6 导致的 DNS 泄漏
independent_cache: true独立缓存,防止缓存投毒
detour指定 DNS 查询走哪个出站(关键!防止泄漏)

Fake-ip 模式(可选)

json
{
  "dns": {
    "servers": [
      {
        "tag": "fakeip-dns",
        "address": "fakeip"
      }
    ],
    "fakeip": {
      "enabled": true,
      "inet4_range": "198.18.0.0/15",
      "inet6_range": "fc00::/18"
    },
    "independent_cache": true
  }
}

Fake-ip 模式下,DNS 返回一个虚假 IP,实际连接时通过域名路由。优点是速度更快、无 DNS 泄漏;缺点是部分 App 不兼容(如局域网设备发现)。

Fake-ip 注意事项

以下域名需要排除 Fake-ip,否则可能异常:

  • *.local*.lan(局域网)
  • *.msftconnecttest.com(Windows 网络检测)
  • *.push.apple.com(Apple 推送)
  • stun.*(WebRTC/NAT 检测)

七、策略组与多出站

按地区分组

json
{
  "outbounds": [
    {
      "type": "selector",
      "tag": "香港",
      "outbounds": ["香港-01", "香港-02", "香港-03"]
    },
    {
      "type": "selector",
      "tag": "日本",
      "outbounds": ["日本-01", "日本-02", "日本-03"]
    },
    {
      "type": "selector",
      "tag": "美国",
      "outbounds": ["美国-01", "美国-02", "美国-03"]
    },
    {
      "type": "urltest",
      "tag": "auto",
      "outbounds": ["香港-01", "日本-01", "美国-01"],
      "url": "https://www.gstatic.com/generate_204",
      "interval": "5m"
    },
    {
      "type": "selector",
      "tag": "proxy",
      "outbounds": ["auto", "香港", "日本", "美国"],
      "default": "auto"
    }
  ]
}

按用途分组

json
{
  "type": "selector",
  "tag": "流媒体",
  "outbounds": ["美国", "日本", "香港"]
},
{
  "type": "selector",
  "tag": "AI工具",
  "outbounds": ["美国", "日本"]
},
{
  "type": "selector",
  "tag": "下载",
  "outbounds": ["香港", "日本"]
}

然后在路由规则中引用:

json
{
  "domain_suffix": ["netflix.com"],
  "outbound": "流媒体"
},
{
  "domain_suffix": ["openai.com", "claude.ai"],
  "outbound": "AI工具"
}

八、Clash API(可视化面板)

Sing-box 兼容 Clash API,可以配合 YacdMetaCubeXD 面板使用:

json
{
  "experimental": {
    "clash_api": {
      "external_controller": "127.0.0.1:9090",
      "external_ui": "ui",
      "external_ui_download_url": "https://github.com/MetaCubeX/metacubexd/archive/refs/heads/gh-pages.zip",
      "external_ui_download_detour": "proxy",
      "default_mode": "rule"
    },
    "cache_file": {
      "enabled": true,
      "path": "cache.db"
    }
  }
}

配置后访问 http://127.0.0.1:9090/ui 即可看到可视化面板,支持:

  • 实时流量监控
  • 节点延迟测试
  • 切换出站节点
  • 查看连接日志
  • 模式切换(Rule / Global / Direct)

九、完整配置模板

以下是一个可直接使用的生产级配置模板(你需要替换实际节点信息):

完整 config.json 模板
json
{
  "log": {
    "level": "info",
    "timestamp": true
  },
  "dns": {
    "servers": [
      {
        "tag": "remote-dns",
        "address": "https://1.1.1.1/dns-query",
        "address_resolver": "local-dns",
        "detour": "proxy"
      },
      {
        "tag": "local-dns",
        "address": "https://223.5.5.5/dns-query",
        "detour": "direct"
      }
    ],
    "rules": [
      { "outbound": "any", "server": "local-dns" },
      { "clash_mode": "direct", "server": "local-dns" },
      { "clash_mode": "global", "server": "remote-dns" },
      { "rule_set": "geosite-cn", "server": "local-dns" }
    ],
    "final": "remote-dns",
    "strategy": "ipv4_only",
    "independent_cache": true
  },
  "inbounds": [
    {
      "type": "tun",
      "tag": "tun-in",
      "inet4_address": "172.19.0.1/30",
      "inet6_address": "fdfe:dcba:9876::1/126",
      "auto_route": true,
      "strict_route": true,
      "stack": "system",
      "sniff": true
    }
  ],
  "outbounds": [
    {
      "type": "selector",
      "tag": "proxy",
      "outbounds": ["auto"],
      "default": "auto"
    },
    {
      "type": "urltest",
      "tag": "auto",
      "outbounds": ["节点1", "节点2"],
      "url": "https://www.gstatic.com/generate_204",
      "interval": "5m",
      "tolerance": 50
    },
    { "type": "direct", "tag": "direct" },
    { "type": "block", "tag": "block" },
    { "type": "dns", "tag": "dns-out" }
  ],
  "route": {
    "rules": [
      { "protocol": "dns", "outbound": "dns-out" },
      { "ip_is_private": true, "outbound": "direct" },
      { "clash_mode": "direct", "outbound": "direct" },
      { "clash_mode": "global", "outbound": "proxy" },
      { "rule_set": "geosite-category-ads-all", "outbound": "block" },
      { "rule_set": "geosite-cn", "outbound": "direct" },
      { "rule_set": "geoip-cn", "outbound": "direct" }
    ],
    "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"
      }
    ],
    "final": "proxy",
    "auto_detect_interface": true
  },
  "experimental": {
    "clash_api": {
      "external_controller": "127.0.0.1:9090",
      "default_mode": "rule"
    },
    "cache_file": {
      "enabled": true,
      "path": "cache.db"
    }
  }
}

注意

以上模板中的 "节点1""节点2" 需要替换为你实际的节点 tag。如果你使用机场订阅,这些节点信息会自动生成——你只需要修改 route 和 dns 部分。


十、配置技巧与最佳实践

1. 善用 Clash Mode 快捷切换

在 Clash API 面板中可以快速切换三种模式:

  • Rule 模式:按路由规则分流(日常使用)
  • Global 模式:全部走代理(出国旅行时用)
  • Direct 模式:全部直连(临时关闭代理)

2. 分应用代理(Android)

json
{
  "route": {
    "rules": [
      {
        "package_name": ["com.android.chrome", "com.google.android.youtube"],
        "outbound": "proxy"
      },
      {
        "package_name": ["com.taobao.taobao", "com.jingdong.app.mall"],
        "outbound": "direct"
      }
    ]
  }
}

3. 进程匹配(Windows/macOS)

json
{
  "process_name": ["Telegram.exe", "Discord.exe"],
  "outbound": "proxy"
}

4. 避免 DNS 泄漏的黄金法则

  • 国外域名 → 远程 DNS(走代理)
  • 国内域名 → 本地 DNS(走直连)
  • 永远不要用运营商默认 DNS
  • 开启 independent_cache 防止缓存污染
  • 使用 ipv4_only 策略(除非你有 IPv6 代理)

下一步

配置完成后,如果遇到任何问题,请查看 第四课:故障排除

如果你觉得手动编辑 JSON 太复杂,也可以考虑使用基于 Sing-box 内核但界面更友好的客户端:


国家利益和民族团结高于一切

使用网络时请遵守国家法律法规,科学上网,不谈政治,不谈宗教,不碰黄赌毒。