使用 Arch 搭建软路由
Doveccl

折腾软路由也有几年了,系统从 Debian 换到 Arch,运营商从电信换到广电又换到移动,透明代理工具也是在 mihomo (Clash.Meta) 和 sing-box 之间来回切换过不少次。这么长时间也积累了不少经验,于是打算在这一篇文章里一并记录下

运营商的选择

由于各地运营商的情况不尽相同,这里只说广州的情况:

  • 电信:贵,千兆档位是给开动态公网 v4 的,有动态公网 v6 封常用端口,稳定性最好
  • 广电(aka 珠江宽频):不给 v6,公网 v4 原来免费现在收 50 元/月,拉完了
  • 移动:性价比高,新装不给光猫改桥接了,无公网 v4,有动态公网 v6 且不封 80, 443 端口
  • 联通:价格应该跟广电差不多,没用过

综合看下来广州移动算是一个不错的选择,晚高峰时期延迟(国内网站 / CN2 线路)也不错

软路由系统的选择

  • 其实 Debian 用来做软路由非常不错,而且官方源的软件版本一般都比较稳定,如果不想折腾的话选择 Debian 完全 OK,可直接参考之前的文章,更推荐这种组合:Debian + systemd-networkd + sing-box
  • 当然,都开始玩软路由了哪有不折腾的,Arch 的优势在于内核足够新且软件源更新比较勤,个人使用体验下来没那么容易滚挂,所以也是非常推荐的
  • 至于 OpenWRT 和爱快(好像已经支持硬件直通了?)这些之前文章也有一些说明,如果技术能力不是很强或者不想折腾也是个不错的选择,就是可玩性没那么高罢了

Arch 安装

安装直接看这个就行 https://wiki.archlinuxcn.org/wiki/安装指南 注意事项如下:

  1. 最好避免直接使用 archinstall 来安装系统
  2. 最好先通过 DHCP 配置好网络,如果运营商不给公网 v4 光猫改桥接意义不大
  3. 运行 pacstrap 时最好一并带上 nftables 包(如果需要路由器拨号再追加一个 ppp 包)
  4. 优先考虑 systemd-boot 作为引导,如果电脑不支持 EFI 再考虑 grub2

安装完成后,强烈推荐同时配置 archlinuxcn,另外也别忘了把源都换成国内镜像

路由器基础配置

sysctl

现行版本 Arch 已经不用 /etc/sysctl.conf 这个文件来进行配置了,直接创建并编辑 /etc/sysctl.d/99-forward.conf

1
net.ipv6.conf.all.forwarding = 1

只需要配置 IPv6 转发并不是因为不需要设置 IPv4 转发,而是如果使用 systemd-networkd 来配置网络会自动帮我们开启 IPv4 转发,无需额外的显式配置(其实理论上 IPv6 也无需显式指定,但是感觉内核有一点 bug,必须开了 all 才生效)

systemd-networkd

先用 ip a 列一下物理网卡,决定好哪个用作 WAN 口,哪些需要组局域网桥(本文不讨论单网口旁路由,双网口机器不需要组网桥),这里仍然假设有四个网口:eno1 用作 WAN,eno2 ~ eno4 用作 LAN

先配置 /etc/systemd/network/10-wan.network WAN 口联网(除非有公网 v4 否则不推荐软路由拨号)

1
2
3
4
5
6
7
8
9
10
11
[Match]
Name=eno1 # 软路由拨号,网口名改成 ppp0

[Network]
DHCP=yes # 软路由拨号,yes 改成 ipv6
IPv6AcceptRA=yes
# 软路由拨号则取消下一行注释,让 networkd 不要覆盖已存在的 v4 地址
# KeepConfiguration=yes

[DHCPv6]
WithoutRA=solicit

接着配置 /etc/systemd/network/20-br0.netdev 创建网桥

1
2
3
[NetDev]
Name=br0
Kind=bridge

然后配置 /etc/systemd/network/30-lan.network 绑定几个物理网口到网桥

1
2
3
4
5
[Match]
Name=eno2 eno3 eno4 # 注意改成自己的网口名

[Network]
Bridge=br0

最后配置 /etc/systemd/network/40-br0.network 完善 LAN 信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[Match]
Name=br0

[Network]
Address=192.168.1.1/24 # 注意子网分配不要和光猫冲突
IPMasquerade=ipv4 # 会同时开启 IPv4 转发和 NAT
DHCPServer=yes
IPv6SendRA=yes
DHCPPrefixDelegation=yes # 自动尝试从 WAN 口拿 IPv6 子网前缀,开启此项需要较新版本的 systemd

# 限制地址池,不限制则不写
[DHCPServer]
PoolOffset=10 # 从 192.168.1.10 开始分配地址
PoolSize=200 # 至多分配 200 个地址

# 这个配置块可重复多次,用于固定特定设备的内网 IPv4
[DHCPServerStaticLease]
# 在一些苹果移动设备和高版本安卓设备上,可能需要关掉随机 MAC 地址
MACAddress=a1:b2:c3:d4:e5:f6
Address=192.168.1.2

注意:

  • 上述配置只会通告 SLAAC(无状态 IPv6 地址),而不开启 DHCPv6 Server(也没必要开)
  • networkd 本身已经可以实现几乎全部 ifupdown + dnsmasq + dhcp6c 和部分 nftables 功能
  • 通过 systemctl enable --now systemd-networkd 开机自启动网络服务
  • 通过 systemctl restart systemd-networkd 重启服务刷新配置

nftables

虽然 networkd 已经接管了 NAT 部分的配置,但是仍然需要防火墙等的额外配置,直接编辑文件 /etc/nftables.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/nft -f

destroy table inet mangle
table inet mangle {
chain forward {
type filter hook forward priority mangle
# 此配置对拨号上网非常重要,因为 Arch 的 ppp 包默认不会对 PPPoE 的 MTU 做修改
oifname ppp* tcp flags syn tcp option maxseg size set rt mtu
}
}

destroy table inet filter
table inet filter {
chain input {
type filter hook input priority filter
ct state established,related accept
# 在下面的第二个括号里填入希望开放给外网的端口即可
iifname { ppp*, en* } tcp dport != { 22, 80, 443 } counter drop
}
}

同样的,需要仿照 systemd-networkd 的命令对 nftables 设置开机自启和重启

透明代理配置

这个时候 Arch 的优势就体现出来了,不管是 mihomo 还是 sing-box 都可以直接通过 archlinuxcn 仓库安装,而且更新非常及时。这里更推荐 sing-box 因为就连 mihomo 的 TUN 功能(透明代理的核心)底层实现也是 sing-tun (fork)

这里直接给出一个 /etc/sing-box/config.json 配置以供参考

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
{
"log": {
"level": "info"
},
"dns": {
"reverse_mapping": true, // 如果需要 ssh github 这个配置会很有用
"servers": [
{ "tag": "l", "type": "dhcp" }, // 如果是软路由拨号,这里手动改成运营商 DNS(不推荐公共 DNS 服务)
{ "tag": "r", "type": "h3", "server": "v.recipes", "domain_resolver": "l" }
],
"rules": [
{ "rule_set": "ad", "action": "reject" },
{ "rule_set": "cn", "server": "l" },
{ "rule_set": "gfw", "server": "r" },
{ "rule_set": "ip:cn", "server": "l" }
]
},
"inbounds": [
{
"tag": "mix",
"type": "mixed",
"listen": "::",
"listen_port": 7890
},
{
"type": "tun",
"auto_route": true,
"auto_redirect": true,
"address": ["172.18.0.1/30", "fd00:172:18::1/126"],
"route_exclude_address_set": ["ip:cn"] // 中国 IP 出站不走 TUN
}
],
"outbounds": [
{ "tag": "direct", "type": "direct" },
{
"tag": "proxy"
// 这里填写自己的节点信息
}
],
"route": {
"auto_detect_interface": true,
"default_domain_resolver": "l",
"rules": [
{ "action": "sniff" },
{ "protocol": "dns", "action": "hijack-dns" },
{ "ip_is_private": true, "outbound": "direct" },
{ "protocol": ["bittorrent", "stun", "ntp"], "outbound": "direct" },
{ "rule_set": ["gfw", "google", "ip:telegram"], "outbound": "proxy" },
{ "inbound": "mix", "outbound": "proxy" }
],
"rule_set": [
{
"tag": "cn",
"type": "remote",
"format": "binary",
"url": "https://testingcf.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@sing/geo/geosite/cn.srs"
},
{
"tag": "gfw",
"type": "remote",
"format": "binary",
"url": "https://testingcf.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@sing/geo/geosite/gfw.srs"
},
{
"tag": "google",
"type": "remote",
"format": "binary",
"url": "https://testingcf.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@sing/geo/geosite/google.srs"
},
{
"tag": "ip:cn",
"type": "remote",
"format": "binary",
"url": "https://testingcf.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@sing/geo/geoip/cn.srs"
},
{
"tag": "ip:telegram",
"type": "remote",
"format": "binary",
"url": "https://testingcf.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@sing/geo/geoip/telegram.srs"
},
{
"tag": "ad",
"type": "remote",
"format": "binary",
"url": "https://anti-ad.net/anti-ad-sing-box.srs"
}
]
}
}

当然,如果是光猫拨号的话,也可以考虑 dae 这种性能更好的方案,这个就纯看个人喜好了

All in Boom

其实对于性能过剩的机器,还是很推荐装个 docker 跑额外服务的,挂个大容量盘做 NAS 也是个不错的选择,这里简单列一下我自己在用的一些服务

  • qBittorrent: 用来挂 PT 站还是很不错的,而且像 byr 这种其实只是禁止了国内运营商地址访问网页,P2P 协议其实仍然可以直连,这个不需要 docker 直接 pacman -S qbittorrent-nox 就行
  • Home Assistant: 如果有很多米家的智能家居设备想要用 Apple Home 控制,如果想要写出高度自定义的自动化任务,这个还是非常推荐的
  • immich: 轻松实现多设备照片自动备份,如果对云盘的隐私协议存疑,可以尝试使用
  • Jellyfin: 配合 PT 站 DIY 自己的影视资源库

另外,飞牛 似乎也是当下比较流行的一个选择,待我有空了折腾下试试

 评论
评论插件加载失败
正在加载评论插件
由 Hexo 驱动 & 主题 Keep
总字数 27k 访客数 访问量