接上一篇从零开始 NanoPi R3S,本文将介绍如何在 NanoPi R3S 上配置 sing-box 的 TUN 模式。

配置 sing-box

  1. 选择 sing-box 版本:
    推荐使用风佬的 sing-box 分支,该分支提供了一些实用功能,例如 provider 和 remote rule-set path。

  2. 编译与安装:

    • sing-box 编译为适用于 aarch64_cortex-a53 架构的二进制文件。
    • 参考官方 GitHub Workflow 流程将二进制文件打包成 ipk 包。
    • 将 ipk 包安装到 NanoPi R3S。
  3. 模式选择:
    本文采用 TUN 模式进行配置,TProxy 模式理论上也可行。

  4. DNS 配置:
    为避免冲突和二次缓存,建议禁用 dnsmasq 的 DNS 功能。具体操作是将 dnsmasq 的监听端口改为 0。

与 smartdns 配合

为了获得更优的 DNS 解析体验,可以配合 smartdns 使用。

  1. 安装 smartdns:

    • 访问 smartdns Releases 页面下载适用于 aarch64_cortex-a53 架构的 ipk 包。例如 smartdns.1.*.aarch64-openwrt-all.ipkluci-app-smartdns.1.*.all-luci-all.ipk
    • opkg 安装下载的 ipk 包。
  2. 配置 smartdns:
    由于 smartdns 的 LuCI 界面在多端口配置方面不够便捷,建议采用自定义配置的方式。之后,可以在 sing-box 中将 DNS 请求分流到 smartdns 的不同端口。

    smartdns LuCI 界面的 自定义设置 中填入以下内容:

smartdns.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
speed-check-mode tcp:443,tcp:80
log-level warn
log-file /tmp/smartdns/smartdns.log
log-syslog yes

cache-checkpoint-time 3600

bind [::]:5335 -group proxy_group -no-speed-check
bind [::]:5336 -group direct_group

server-https https://dns.google/dns-query -group proxy_group -exclude-default-group -proxy global_proxy
server-https https://cloudflare-dns.com/dns-query -group proxy_group -exclude-default-group -proxy global_proxy
server-https https://doh.dns.sb/dns-query -group proxy_group -exclude-default-group -proxy global_proxy
server-https https://doh.opendns.com/dns-query -group proxy_group -exclude-default-group -proxy global_proxy
server-https https://common.dot.dns.yandex.net/dns-query -group proxy_group -exclude-default-group -proxy global_proxy
server-https https://1.1.1.1/dns-query -group proxy_group -exclude-default-group -proxy global_proxy
server-tls 1.1.1.1 -group proxy_group -exclude-default-group -proxy global_proxy
server-tls 1.0.0.1 -group proxy_group -exclude-default-group -proxy global_proxy
server-tls 8.8.8.8 -group proxy_group -exclude-default-group -proxy global_proxy
server-tls 8.8.4.4 -group proxy_group -exclude-default-group -proxy global_proxy
server 1.1.1.1 -group proxy_group -exclude-default-group -proxy global_proxy -fallback
server 1.0.0.1 -group proxy_group -exclude-default-group -proxy global_proxy -fallback
server 8.8.8.8 -group proxy_group -exclude-default-group -proxy global_proxy -fallback
server 8.8.4.4 -group proxy_group -exclude-default-group -proxy global_proxy -fallback

server 202.115.39.9 -group direct_group -exclude-default-group -proxy direct_proxy
server 202.115.39.6 -group direct_group -exclude-default-group -proxy direct_proxy
server-https https://dns.alidns.com/dns-query -group direct_group -exclude-default-group -proxy direct_proxy
server-https https://doh.pub/dns-query -group direct_group -exclude-default-group -proxy direct_proxy
server 119.29.29.29 -group direct_group -exclude-default-group -proxy direct_proxy
server 119.28.28.28 -group direct_group -exclude-default-group -proxy direct_proxy
server 223.5.5.5 -group direct_group -exclude-default-group -proxy direct_proxy
server 223.6.6.6 -group direct_group -exclude-default-group -proxy direct_proxy

proxy-server socks5://127.0.0.1:20809 -name global_proxy
proxy-server socks5://127.0.0.1:20811 -name direct_proxy

注意: 请将上述配置中的 202.115.39.9202.115.39.6 替换为您所在地区的 DNS 服务器 IP 地址。

  1. 调整 smartdns 设置:
    • 取消勾选 smartdns LuCI 界面中的 自动设置 Dnsmasq 功能。
    • smartdns LuCI 界面的 本地端口 设置为 53
    • 添加上游服务器,指向 sing-box 的 DNS 端口 5340,以利用 sing-box 提供的分流功能。
  2. 指定 DHCP DNS 服务器:
    • 在 iStoreOS 的 LuCI 界面中,导航至 网络 - 接口 - LAN,在 DHCP 服务器 -> 高级设置 中,将 DHCP 选项 设置为 6,192.168.100.1,以确保局域网内的设备使用路由器作为 DNS 服务器。

注意:

  • sing-box 的 tun 会自动劫持下游设备的 DNS 请求,所以 fake-ip 自动生效,不会与其他 DNS 服务器冲突。
  • smartdns 指向 sing-box 的 DNS 端口 5340,而不是提供 fake-ip 的端口 5339,是因为避免软路由器本身的 DNS 请求被 fake-ip 劫持,导致无法解析某些域名。
  • DHCP 选项 设置为 6,192.168.100.1,是为了保证下属设备的虚拟机使用了 Nat 模式依然可以正常使用,否则虚拟机可能会报错未找到 DNS 服务器。
  • 可以尝试禁用 sing-box 的 dns 缓存功能,避免二次缓存,副作用未知

以下是 sing-box 的配置示例:

sing-box_openwrt_tun

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
{
"log": {
"level": "error",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "dns-remote",
"type": "udp",
"server": "127.0.0.1",
"server_port": 5335
},
{
"tag": "dns-local",
"type": "udp",
"server": "127.0.0.1",
"server_port": 5336
},
{
"tag": "dns-fake",
"type": "fakeip",
"inet4_range": "198.18.0.0/15",
"inet6_range": "fc00::/18"
}
],
"rules": [
{
"clash_mode": "Direct",
"server": "dns-local"
},
{
"clash_mode": "Global",
"server": "dns-remote"
},
{
"query_type": [
64,
65
],
"action": "predefined",
"rcode": "NXDOMAIN"
},
{
"domain": [
"basic-static-server.infinitynewtab.com"
],
"action": "predefined",
"rcode": "NOTIMP"
},
{
"type": "logical",
"mode": "and",
"rules": [
{
"clash_mode": "AllowAds",
"invert": true
},
{
"rule_set": "geosite-category-ads-all"
}
],
"action": "predefined",
"rcode": "NXDOMAIN"
},
{
"type": "logical",
"mode": "and",
"rules": [
{
"query_type": [
"A",
"AAAA"
]
},
{
"inbound": [
"dns-real-in"
],
"invert": true
},
{
"rule_set": [
"geosite-fake-filter"
],
"domain_suffix": [
"xiaomi.com"
],
"invert": true
}
],
"action": "route",
"server": "dns-fake",
"rewrite_ttl": 1
},
{
"domain_suffix": [
"bing.com",
"googleapis.cn",
"gstatic.com"
],
"server": "dns-remote"
},
{
"domain_suffix": [
"senhewenhua.com",
"cnmdm.top",
"akamaized.net",
"moedot.net",
"cycani.org",
"xifanacg.com",
"skland.com",
"aliyun.com",
"online-fix.me"
],
"rule_set": [
"geosite-cn"
],
"server": "dns-local"
}
],
"final": "dns-remote",
"disable_cache": true
},
"inbounds": [
{
"type": "mixed",
"tag": "mixed-in",
"listen": "127.0.0.1",
"listen_port": 20808
},
{
"type": "mixed",
"tag": "mixed-in2",
"listen": "::",
"listen_port": 20809
},
{
"type": "mixed",
"tag": "mixed-in3",
"listen": "::",
"listen_port": 20810
},
{
"type": "mixed",
"tag": "mixed-in4",
"listen": "::",
"listen_port": 20811
},
{
"type": "mixed",
"tag": "mixed-in5",
"listen": "::",
"listen_port": 20828
},
{
"type": "direct",
"tag": "dns-in",
"listen": "::",
"listen_port": 5339,
"override_address": "1.1.1.1",
"override_port": 53
},
{
"type": "direct",
"tag": "dns-real-in",
"listen": "::",
"listen_port": 5340,
"override_address": "1.1.1.1",
"override_port": 53
},
{
"type": "tun",
"tag": "tun-in",
"interface_name": "singbox_tun",
"address": [
"172.64.0.1/30",
"fd6a:7446:b1cd::1/126"
],
"mtu": 65535,
"auto_route": true,
"auto_redirect": true,
"strict_route": true,
"stack": "mixed",
"platform": {
"http_proxy": {
"enabled": true,
"server": "127.0.0.1",
"server_port": 20808
}
}
}
],
"outbounds": [
],
"route": {
"default_domain_resolver": {
"server": "dns-local",
"strategy": "prefer_ipv6"
},
"rules": [
{
"inbound": [
"mixed-in2"
],
"outbound": "bypass-edu-net",
"clash_mode": "BypassEduNet"
},
{
"outbound": "bypass-edu-net",
"inbound": [
"mixed-in4"
],
"clash_mode": "BypassEduNet"
},
{
"inbound": [
"mixed-in2"
],
"outbound": "proxy"
},
{
"inbound": [
"mixed-in4"
],
"outbound": "direct"
},
{
"inbound": [
"mixed-in5"
],
"outbound": "bypass-edu-net"
},
{
"inbound": [
"dns-in",
"dns-real-in"
],
"action": "hijack-dns"
},
{
"source_ip_is_private": false,
"network": [
"udp"
],
"port": [
135,
137,
138,
139,
5353
],
"action": "reject"
},
{
"source_ip_is_private": false,
"ip_cidr": [
"224.0.0.0/3",
"ff00::/8"
],
"action": "reject"
},
{
"type": "logical",
"mode": "and",
"rules": [
{
"rule_set": [
"geosite-cn",
"geoip-cn"
],
"invert": true
},
{
"network": "udp",
"port": 443
}
],
"action": "reject",
"method": "default"
},
{
"source_ip_cidr": [
"224.0.0.0/3",
"ff00::/8"
],
"ip_cidr": [
"224.0.0.0/3",
"ff00::/8"
],
"action": "reject",
"method": "default"
},
{
"type": "logical",
"mode": "and",
"rules": [
{
"clash_mode": "AllowAds",
"invert": true
},
{
"rule_set": "geosite-category-ads-all"
}
],
"action": "reject",
"method": "default"
},
{
"port": [
53
],
"process_name": [
"v2ray",
"v2ray",
"xray",
"mihomo-windows-amd64-compatible",
"mihomo-windows-amd64",
"mihomo-linux-amd64",
"clash",
"mihomo",
"hysteria",
"naive",
"naiveproxy",
"tuic-client",
"tuic",
"juicity-client",
"juicity",
"hysteria-windows-amd64",
"hysteria-linux-amd64",
"hysteria",
"brook_windows_amd64",
"brook_linux_amd64",
"brook",
"overtls-bin",
"overtls"
],
"action": "sniff",
"sniffer": [
"dns"
]
},
{
"protocol": "dns",
"action": "hijack-dns"
},
{
"outbound": "direct",
"process_name": [
"v2ray",
"v2ray",
"xray",
"mihomo-windows-amd64-compatible",
"mihomo-windows-amd64",
"mihomo-linux-amd64",
"clash",
"mihomo",
"hysteria",
"naive",
"naiveproxy",
"tuic-client",
"tuic",
"sing-box-client",
"sing-box",
"juicity-client",
"juicity",
"hysteria-windows-amd64",
"hysteria-linux-amd64",
"hysteria",
"brook_windows_amd64",
"brook_linux_amd64",
"brook",
"overtls-bin",
"overtls",
"utorrent",
"qbittorrent",
"smartdns"
]
},
{
"outbound": "bypass-edu-net",
"clash_mode": "BypassEduNet"
},
{
"rule_set": "geoip-telegram",
"clash_mode": "Direct",
"outbound": "direct"
},
{
"rule_set": "geoip-telegram",
"outbound": "proxy"
},
{
"port": 53,
"action": "sniff",
"sniffer": [
"dns"
]
},
{
"action": "sniff",
"sniffer": [
"http",
"tls",
"bittorrent"
],
"timeout": "500ms"
},
{
"protocol": "dns",
"action": "hijack-dns"
},
{
"type": "logical",
"mode": "or",
"rules": [
{
"protocol": "bittorrent"
}
],
"outbound": "direct"
},
{
"ip_is_private": true,
"outbound": "direct"
},
{
"rule_set": "geosite-private",
"outbound": "direct"
},
{
"outbound": "proxy",
"clash_mode": "Global"
},
{
"outbound": "direct",
"clash_mode": "Direct"
},
{
"type": "logical",
"mode": "and",
"rules": [
{
"rule_set": [
"geosite-cn",
"geoip-cn"
],
"invert": true
},
{
"network": "udp",
"port": 443
}
],
"action": "reject",
"method": "default"
},
{
"source_ip_cidr": [
"224.0.0.0/3",
"ff00::/8"
],
"ip_cidr": [
"224.0.0.0/3",
"ff00::/8"
],
"action": "reject",
"method": "default"
},
{
"type": "logical",
"mode": "and",
"rules": [
{
"clash_mode": "AllowAds",
"invert": true
},
{
"rule_set": "geosite-category-ads-all"
}
],
"action": "reject",
"method": "default"
},
{
"domain_suffix": [
"bing.com",
"googleapis.cn",
"gstatic.com"
],
"outbound": "proxy"
},
{
"clash_mode": "GameDirect",
"rule_set": [
"geosite-category-games"
],
"outbound": "direct"
},
{
"domain_suffix": [
"cycani.org",
"senhewenhua.com",
"cnmdm.top",
"akamaized.net",
"moedot.net",
"xifanacg.com",
"skland.com",
"aliyun.com",
"online-fix.me"
],
"rule_set": "geosite-cn",
"outbound": "direct"
},
{
"rule_set": "geosite-geolocation-!cn",
"outbound": "proxy"
},
{
"action": "resolve",
"server": "dns-remote"
},
{
"type": "logical",
"mode": "or",
"rules": [
{
"rule_set": "geoip-cn"
},
{
"ip_is_private": true
}
],
"invert": true,
"outbound": "proxy"
},
{
"action": "resolve",
"server": "dns-local",
"strategy": "prefer_ipv6"
}
],
"rule_set": [
{
"type": "remote",
"tag": "geosite-category-games",
"format": "binary",
"url": "https://raw.githubusercontent.com/lyc8503/sing-box-rules/rule-set-geosite/geosite-category-games.srs",
"path": "/etc/sing-box/srss/geosite-category-games.srs",
"download_detour": "proxy-init",
"update_interval": "72h0m0s"
},
{
"type": "remote",
"tag": "geosite-category-ads-all",
"format": "binary",
"url": "https://raw.githubusercontent.com/lyc8503/sing-box-rules/rule-set-geosite/geosite-category-ads-all.srs",
"path": "/etc/sing-box/srss/geosite-category-ads-all.srs",
"download_detour": "proxy-init",
"update_interval": "72h0m0s"
},
{
"type": "remote",
"tag": "geosite-private",
"format": "binary",
"url": "https://raw.githubusercontent.com/lyc8503/sing-box-rules/rule-set-geosite/geosite-private.srs",
"path": "/etc/sing-box/srss/geosite-private.srs",
"download_detour": "proxy-init",
"update_interval": "72h0m0s"
},
{
"type": "remote",
"tag": "geosite-cn",
"format": "binary",
"url": "https://raw.githubusercontent.com/lyc8503/sing-box-rules/rule-set-geosite/geosite-cn.srs",
"path": "/etc/sing-box/srss/geosite-cn.srs",
"download_detour": "proxy-init",
"update_interval": "72h0m0s"
},
{
"type": "remote",
"tag": "geoip-cn",
"format": "binary",
"url": "https://raw.githubusercontent.com/lyc8503/sing-box-rules/rule-set-geoip/geoip-cn.srs",
"path": "/etc/sing-box/srss/geoip-cn.srs",
"download_detour": "proxy-init",
"update_interval": "72h0m0s"
},
{
"type": "remote",
"tag": "geoip-telegram",
"format": "binary",
"url": "https://raw.githubusercontent.com/lyc8503/sing-box-rules/rule-set-geoip/geoip-telegram.srs",
"path": "/etc/sing-box/srss/geoip-telegram.srs",
"download_detour": "proxy-init",
"update_interval": "72h0m0s"
},
{
"type": "remote",
"tag": "geosite-geolocation-!cn",
"format": "binary",
"url": "https://raw.githubusercontent.com/lyc8503/sing-box-rules/rule-set-geosite/geosite-geolocation-!cn.srs",
"path": "/etc/sing-box/srss/geosite-geolocation-!cn.srs",
"download_detour": "proxy",
"update_interval": "72h0m0s"
},
{
"type": "remote",
"tag": "geosite-fake-filter",
"format": "binary",
"url": "https://raw.githubusercontent.com/DustinWin/ruleset_geodata/sing-box-ruleset/fakeip-filter.srs",
"path": "/etc/sing-box/srss/geosite-fake-filter.srs",
"download_detour": "proxy",
"update_interval": "72h0m0s"
}
],
"final": "direct",
"auto_detect_interface": true
},
"experimental": {
"cache_file": {
"enabled": true,
"path": "/etc/sing-box/cache.db",
"store_fakeip": true
},
"clash_api": {
"external_controller": "0.0.0.0:9090",
"external_ui": "/etc/sing-box/ui",
"external_ui_download_url": "https://github.com/Zephyruso/zashboard/releases/latest/download/dist.zip",
"external_ui_download_detour": "proxy-init"
}
},
"providers": [
]
}

注意:

  • sing-box 的 TUN 可能导致校园网认证环境下 IPv6 无法使用,以及在 IPv6 Relay 模式下下属设备无法访问 IPv6 网站,需要关闭 sing-box 后,重新认证再重启 sing-box。