版本
操作系统: Windows 11
SingBox版本: 1.12.0-alpha.18
前要
最近有空研究了一下SingBox这个工具,总体上来说优缺点都有,本打算全面替换Clash,但目前暂时还没办法实现,所以先记录下来。
以下配置是在Windows上作为客户端使用,不保证软路由器能使用。
配置
SingBox的配置是使用json格式。根据手册上描述,主要是分为以下5个模块:
- DNS
- inbounds
- outbounds
- route
- 其它
DNS
这里主要配置DNS的服务器,DNS解析规则部分我放在了route下。
"dns": {
"servers": [
{
"tag": "tencent-dns",
"type": "https",
"server": "dns.pub",
"domain_resolver": "default-dns"
},
{
"tag": "cf-dns",
"type": "https",
"server": "1.1.1.1"
},
{
"tag": "default-dns",
"type": "udp",
"server": "114.114.114.114"
}
],
"final": "default-dns",
"strategy": "ipv4_only",
"disable_cache": false,
"disable_expire": false,
"independent_cache": true
}
- servers的部分由于版本的更新,配置写法和旧版本有些不同,详细可以看看官方迁移指南。
- domain_resolver这个配置是解析dns域名,如果server是ip地址无需配置此选项
- final选项,在测试的时候查看日志发现,如果inbounds配置了tun模式,是会用到此配置,但若配置为mixed,是走不到此选项。
- DNS规则及规则动作,感觉放到route没问题,放在dns下面也行,有点没搞懂。
inbounds
这是配置进站的,因为我是在Windows下使用,所以我只配置type为mixed。
没什么好说,比较简单,配置一下侦听端口即可。
"inbounds": [
{
"type": "mixed",
"listen": "::",
"listen_port": 7890,
"set_system_proxy": true
}
]
- 如果设置了set_system_proxy这个参数,表示启动时自动设置系统代理,停止时自动清理。 (这个参数不支持移动端)
- 让我感到疑惑的是,根据文档描述,监听字段里面sniff已废弃且将在 sing-box 1.12.0 中被移除。可是我在inbounds里面配置了sniff,但未报错也未提示将要废弃,不过我还是根据迁移指南将其移走了。
outbounds
outbounds是配置出站及如何选择出站,这里只需要配置出站的服务器及选择器。
"outbounds": [
{
"type": "direct",
"tag": "direct-out"
},
{
"type": "selector",
"tag": "normal-out",
"outbounds": [
"香港节点",
"美国节点",
"日本节点"
],
"default": "香港节点"
},
{
"type": "urltest",
"tag": "autoselect",
"outbounds": [
"香港节点",
"美国节点",
"日本节点"
],
"url": "http://www.gstatic.com/generate_204",
"interval": "3m",
"tolerance": 50,
"idle_timeout": "30m",
"interrupt_exist_connections": false
},
{
"type": "selector",
"tag": "special1-out",
"outbounds": [
"美国节点"
]
},
{
"type": "selector",
"tag": "special2-out",
"outbounds": [
"日本节点"
]
},
{
"domain_resolver": "cf-dns",
"type": "shadowsocks",
"tag": "香港节点",
"server": "hknode.com",
"server_port": 9999,
"password": "password",
"method": "aes-128-gcm"
},
{
"type": "trojan",
"tag": "美国节点",
"server": "usanode.com",
"server_port": 443,
"password": "password",
"tls": {
"enabled": true,
"server_name": "usanode.com",
"disable_sni": false,
"insecure": false,
"alpn": [
"http1/1"
]
}
},
{
"type": "vmess",
"tag": "日本节点",
"server": "japannode.com",
"server_port": 9999,
"uuid": "uuid",
"security": "auto",
"alter_id": 0
}
]
- 作为选择器,type一般是direct和selector。urltest是用于测试的出站标签列表。
- 如果节点是域名,可以指定DNS来解析,像香港节点,使用domain_resolver配置cf-dns,表示使用cf-dns,即1.1.1.1来解析这个节点域名。如果没有指定的话,就使用route的default_domain_resolver来解析。
route
整个Singbox的配置中,这是最核心的。主要配置两块,一块配置域名分流规则文件。另一部分是配置路由。
此配置的路由按以下顺序解析处理
- 黑名单,拒绝
- 国内的域名和IP,使用default-dns解析dns,然后直连
- 域名或IP在specialRules里面,使用tencent-dns解析dns,通过outbounds的special-out访问
- 不在上述列表的域名命中final规则,使用tencent-dns解析dns,通过outbounds的normal-out访问
"route": {
"default_domain_resolver": {
"server": "tencent-dns",
"strategy": "prefer_ipv4",
"rewrite_ttl": 60
},
"rules": [
{
"inbound": "in",
"action": "sniff",
"timeout": "1s"
},
{
"rule_set": "blacklist",
"action": "reject"
},
{
"rule_set": [
"cnsiteRules",
"geosite-cn"
],
"action": "resolve",
"server": "default-dns"
},
{
"rule_set": [
"cnsiteRules",
"geosite-cn",
"geoip-cn"
],
"outbound": "direct-out"
},
{
"rule_set": "special1Rules",
"outbound": "special1-out"
}
],
"rule_set": [
{
"type": "local",
"tag": "blacklist",
"format": "source",
"path": "./rules/reject.json"
},
{
"type": "remote",
"tag": "geoip-cn",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-geoip/rule-set/geoip-cn.srs"
},
{
"type": "remote",
"tag": "geosite-cn",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-cn.srs"
},
{
"type": "local",
"tag": "cnsiteRules",
"format": "source",
"path": "./rules/cnsite.json"
},
{
"type": "local",
"tag": "special1Rules",
"format": "source",
"path": "./rules/special1.json"
},
{
"type": "local",
"tag": "special2Rules",
"format": "source",
"path": "./rules/special2.json"
}
],
"final": "normal-out",
"auto_detect_interface": true,
"override_android_vpn": false
}
- 在1.12版本里面,default_domain_resolve若不配置,在启动的时候会被警告。这个配置负责解析DNS的域名和所访问网站的域名。像上述配置中,美国节点和日本节点都会使用tag为tencent-dns,即腾讯的DNS来解析。
- rule_set是配置分流规则文件
其它
这个比较简单,就是配置一些日志之类,可以根据自己需求参照一下官方的文档即可。
"log": {
"disabled": false,
"level": "trace",
"output": "box.log",
"timestamp": true
},
"experimental": {
"cache_file": {
"enabled": true,
"path": "cache.db"
}
}
总结
如果是在Windows下配置的话,相对来说还是比较简单的。
其实仔细思考一下,就是两个问题:
- 域名(包括dns和节点域名)使用哪个DNS来解析?
- 访问网址命中路由哪条规则?
域名如何通过DNS来解析
域名 | 配置 | 默认 |
---|---|---|
DNS域名 | dns.servers.[].domain_resolver | |
节点域名 | outbounds.domain_resolver | route.default_domain_resolver |
访问的网站 | route.rules[].server | route.default_domain_resolver |
说一下缺点吧
- 官方的文档比较晦涩,要看日志才能发现问题,dns路由规则究竟是配置在route还是dns,有什么区别? 类似的问题官方没有答案,要么看issue,要么自己摸索。
- 版本迭代比较频繁。如果移动端被更新了版本,配置还要重新根据迁移指南再修改。
至于为什么不用GUI.for.SingBox,一方面研究这个工具还不如直接研究SingBox,另一方面居然被windows报有Trojan
示例
假定路由使用以下顺序
- 黑名单,拒绝
- 国内的域名和IP,直连
- 域名或IP在specialRules里面,通过outbounds里面special-out访问
- 不在上述列表的域名使用outbounds里面normal-out访问
- 访问baidu.com
- 首先检查是否在黑名单内,不符合
- 检查是否是国内的域名或IP,符合。交由default-dns(114.114.114.114)解析域名
- 域名或IP在cnsiteRules,geosite-cn,geoip-cn名单之内,通过direct-out直连
- 访问google.com(假定google.com在specialRules名单内)
- 在route里面查询,google.com的DNS解析不满足上述条件,使用默认default_domain_resolver进行dns解析
- 解析出来的ip和域名进入route查询,命中了specialRules这条规则,连接使用outbounds里面special-out访问
全文完