所有设备的网关填写透明网关的ip

透明网关作为客户端的sing-box入站配置

1
2
3
4
5
6
7
8
9
10
"inbounds": [
{
"type": "tproxy",
"tag": "tproxy-in",
"listen": "::",
"listen_port": 12345,
"tcp_fast_open": true,
"udp_fragment": true
}
],

iptables(旧版)防火墙配置

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
###################### 路由表设置
ip route add local default dev lo table 100
ip rule add fwmark 1 table 100


###################### 局域网流量设置
iptables -t mangle -N SING_BOX
iptables -t mangle -A SING_BOX -d 100.64.0.0/10 -j RETURN
iptables -t mangle -A SING_BOX -d 127.0.0.0/8 -j RETURN
iptables -t mangle -A SING_BOX -d 169.254.0.0/16 -j RETURN
iptables -t mangle -A SING_BOX -d 172.16.0.0/12 -j RETURN
iptables -t mangle -A SING_BOX -d 192.0.0.0/24 -j RETURN
iptables -t mangle -A SING_BOX -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A SING_BOX -d 240.0.0.0/4 -j RETURN
iptables -t mangle -A SING_BOX -d 255.255.255.255/32 -j RETURN
# 192.168.1.0修改为你的内网网段
iptables -t mangle -A SING_BOX -d 192.168.1.0/24 -p tcp ! --dport 53 -j RETURN
iptables -t mangle -A SING_BOX -d 192.168.1.0/24 -p udp ! --dport 53 -j RETURN
# 12345修改为你的透明代理程序的端口
iptables -t mangle -A SING_BOX -p tcp -j TPROXY --on-port 12345 --tproxy-mark 1
iptables -t mangle -A SING_BOX -p udp -j TPROXY --on-port 12345 --tproxy-mark 1
iptables -t mangle -A PREROUTING -j SING_BOX


###################### 本机流量设置
iptables -t mangle -N SING_BOX_SELF
iptables -t mangle -A SING_BOX_SELF -d 100.64.0.0/10 -j RETURN
iptables -t mangle -A SING_BOX_SELF -d 127.0.0.0/8 -j RETURN
iptables -t mangle -A SING_BOX_SELF -d 169.254.0.0/16 -j RETURN
iptables -t mangle -A SING_BOX_SELF -d 172.16.0.0/12 -j RETURN
iptables -t mangle -A SING_BOX_SELF -d 192.0.0.0/24 -j RETURN
iptables -t mangle -A SING_BOX_SELF -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A SING_BOX_SELF -d 240.0.0.0/4 -j RETURN
iptables -t mangle -A SING_BOX_SELF -d 255.255.255.255/32 -j RETURN
iptables -t mangle -A SING_BOX_SELF -j RETURN -m mark --mark 1234

# 192.168.1.0修改为你的内网网段
iptables -t mangle -A SING_BOX_SELF -d 192.168.1.0/24 -p tcp ! --dport 53 -j RETURN
iptables -t mangle -A SING_BOX_SELF -d 192.168.1.0/24 -p udp ! --dport 53 -j RETURN
iptables -t mangle -A SING_BOX_SELF -p tcp -j MARK --set-mark 1
iptables -t mangle -A SING_BOX_SELF -p udp -j MARK --set-mark 1
iptables -t mangle -A OUTPUT -j SING_BOX_SELF

查看规则

1
2
3
4
iptables -L -t mangle

#清空规则
iptables -F -t mangle

终止所有sing-box进程,释放被占用的端口

1
pkill -f sing-box

启动sing-box

1
sing-box run -c /etc/sing-box/config.json

nftables(新版)防火墙配置

先设置路由表(命名为100)

1
2
ip route add local default dev lo table 100
ip rule add fwmark 1 table 100

ip route add local default dev lo table 100 的作用是:

  • 在路由表 100 中添加一条路由。
  • local default 表示匹配所有目标地址。
  • dev lo 指定使用环回接口 lo。 这条命令通常用于将发往本机的数据包通过环回接口进行处理,例如,用于透明代理,将发往代理服务的数据包路由到代理服务监听的地址。

ip rule add fwmark 1 table 100 的作用是:

  • 创建一条策略路由规则。
  • fwmark 1 匹配所有 fwmark 值为 1 的数据包。fwmark 通常由 iptablesnft 设置。
  • table 100 指定匹配的数据包使用路由表 100 进行路由。

结合起来,这两条命令通常一起使用,用于将带有特定 fwmark 的流量通过环回接口发送到本地进程。

更改nftables默认配置

文件路径:/etc/nftables.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/sbin/nft -f

flush ruleset

table inet filter {
chain input {
type filter hook input priority filter;
}
chain forward {
type filter hook forward priority filter;
}
chain output {
type filter hook output priority filter;
}
}

更改配置如下

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
#!/usr/sbin/nft -f

flush ruleset

define RESERVED_IP = {
100.64.0.0/10,
127.0.0.0/8,
169.254.0.0/16,
172.16.0.0/12,
192.0.0.0/24,
224.0.0.0/4,
240.0.0.0/4,
255.255.255.255/32
}

table ip sing-box {
chain prerouting {
type filter hook prerouting priority mangle; policy accept;
ip daddr $RESERVED_IP return
# 192.168.1.0修改为你的内网网段
ip daddr 192.168.1.0/24 tcp dport != 53 return
ip daddr 192.168.1.0/24 udp dport != 53 return
# 12345修改为你的透明代理程序的端口
ip protocol tcp tproxy to :12345 meta mark set 1
ip protocol udp tproxy to :12345 meta mark set 1
}
chain output {
type route hook output priority mangle; policy accept;
ip daddr $RESERVED_IP return
# 192.168.1.0修改为你的内网网段
ip daddr 192.168.1.0/24 tcp dport != 53 return
ip daddr 192.168.1.0/24 udp dport != 53 return
ip protocol tcp meta mark set 1
ip protocol udp meta mark set 1
}
}

刷新配置

1
nft -f /etc/nftables.conf

持久化路由表(开机自启)

文件路径:/etc/network/interfaces

1
2
3
4
5
6
7
8
9
auto enp1s0             #enp1s0改为自己的网卡
iface enp1s0 inet static
address 192.168.1.8 #静态ip
netmask 255.255.255.0 #子网掩码
gateway 192.168.1.1 #网关
dns-nameservers 192.168.1.1 #dns

post-up ip route add local default dev lo table 100
post-up ip rule add fwmark 1 table 100

重启网络服务

1
systemctl restart networking.service

或者重启系统

验证配置

重启后,使用以下命令验证路由和规则是否已成功添加

1
2
3
ip route show table 100

ip rule show

你应该能看到你配置的路由和规则。