如果你已经在openwrt的路由器中安装了透明代理,如ss,xray,trojan等等,那么应该如何设置防火墙,才能按需分流流量呢?虽然很多app是提供自动的设置,但有些人希望根据自己的需求定制,那么下面就提供一个gfwlist分流的方法,来详细介绍一下原理。
1. 首先安装dnsmasq-full
opkg update opkg install dnsmasq-full
安装完成后,对dnsmasq-full设置。
2. 下载gfwlist.conf文件
mkdir /etc/dnsmasq.d cd dnsmasq.d wget https://cokebar.github.io/gfwlist2dnsmasq/dnsmasq_gfwlist_ipset.conf
修改dnsmasq_gfwlist_ipset.conf中,你需要查询的dns服务器的地址和端口。如果你装了DOH,那么就修改为127.0.01#5053, 如果你是IPv4/IPv6双栈,ipset中设置如下面格式:ipset=/138.com/gfwlist, gfwlist6
3.dnsmasq-full配置
vi /etc/dnsmasq.conf
在dnsmasq.conf文件加入下面几行
no-resolv no-poll conf-dir=/etc/dnsmasq.d cache-size=1000 server=116.116.116.116
4. 设置防火墙
在防火墙的自定义规则中,添加如下配置:
ipset -N gfwlist iphash ipset -N gfwlist6 nethash family inet6 ip route add local 0.0.0.0/0 dev lo table 110 ip -6 route add local ::/0 dev lo table 111 ip rule add fwmark 2 lookup 110 ip rule add fwmark 4 lookup 110 ip -6 rule add fwmark 3 table 111 ip -6 rule add fwmark 5 table 111 iptables -t mangle -I PREROUTING -m mark --mark 2 -j RETURN ip6tables -t mangle -I PREROUTING -m mark --mark 3 -j RETURN iptables -t mangle -A PREROUTING -p tcp -m set --match-set gfwlist dst -j TPROXY --on-port 1234 --tproxy-mark 2 iptables -t mangle -A PREROUTING -p udp -m set --match-set gfwlist dst -j TPROXY --on-port 1234 --tproxy-mark 2 ip6tables -t mangle -A PREROUTING -p tcp -m set --match-set gfwlist6 dst -j TPROXY --on-port 1234 --tproxy-mark 3 ip6tables -t mangle -A PREROUTING -p udp -m set --match-set gfwlist6 dst -j TPROXY --on-port 1234 --tproxy-mark 3 iptables -t mangle -I OUTPUT -m mark --mark 4 -j RETURN ip6tables -t mangle -I OUTPUT -m mark --mark 5 -j RETURN iptables -t mangle -A OUTPUT -p tcp -m set --match-set gfwlist dst -j MARK --set-mark 4 iptables -t mangle -A OUTPUT -p udp -m set --match-set gfwlist dst -j MARK --set-mark 4 ip6tables -t mangle -A OUTPUT -p tcp -m set --match-set gfwlist6 dst -j MARK --set-mark 5 ip6tables -t mangle -A OUTPUT -p udp -m set --match-set gfwlist6 dst -j MARK --set-mark 5
在OUTPUT是不能使用TPROXY,否则防火墙启动出错提示“不能在OUTPUT中使用TPROXY”,所用通过IP rule的方式,将路由器本机出站流量跳转到路由器入站,就可以使用PREROUTING的表进行操作了。
没有评论:
发表评论