Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

外网端口转发重新拨号后导致防火墙规则乱序 #35

Closed
koho opened this issue Oct 30, 2019 · 8 comments
Closed

外网端口转发重新拨号后导致防火墙规则乱序 #35

koho opened this issue Oct 30, 2019 · 8 comments

Comments

@koho
Copy link

koho commented Oct 30, 2019

问题描述:
先看正常时的防火墙规则,V2RAY链是插在nat表的PREROUTING链的最后:

Chain PREROUTING (policy ACCEPT 148 packets, 15985 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1    94723 7030K prerouting_rule  all  --  *      *       0.0.0.0/0            0.0.0.0/0            /* !fw3: Custom prerouting rule chain */
2    48415 4437K zone_lan_prerouting  all  --  br-lan *       0.0.0.0/0            0.0.0.0/0            /* !fw3 */
3    46308 2594K zone_wan_prerouting  all  --  pppoe-wan *       0.0.0.0/0            0.0.0.0/0            /* !fw3 */
4        0     0 zone_wan_prerouting  all  --  eth1.2 *       0.0.0.0/0            0.0.0.0/0            /* !fw3 */
5      343 18023 V2RAY      tcp  --  *      *       0.0.0.0/0            0.0.0.0/0

zone_wan_prerouting链包含了系统设定的一些端口转发规则。因此当存在匹配的端口转发时,zone_wan_prerouting链能比V2RAY链优先DNAT掉,然后就是正常的直接通信。

但是当本机外网IP租期到重新拨号时,系统重新设定了防火墙规则,导致V2RAY链移动到PREROUTING链的顶端:

Chain PREROUTING (policy ACCEPT 148 packets, 15985 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1      343 18023 V2RAY      tcp  --  *      *       0.0.0.0/0            0.0.0.0/0
2    94723 7030K prerouting_rule  all  --  *      *       0.0.0.0/0            0.0.0.0/0            /* !fw3: Custom prerouting rule chain */
3    48415 4437K zone_lan_prerouting  all  --  br-lan *       0.0.0.0/0            0.0.0.0/0            /* !fw3 */
4    46308 2594K zone_wan_prerouting  all  --  pppoe-wan *       0.0.0.0/0            0.0.0.0/0            /* !fw3 */
5        0     0 zone_wan_prerouting  all  --  eth1.2 *       0.0.0.0/0            0.0.0.0/0            /* !fw3 */

V2RAY链没有设定本机外网IP网段直接返回规则:

Chain V2RAY (2 references)
num   pkts bytes target     prot opt in     out     source               destination
1    32245 1935K RETURN     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            mark match 0xff
2        0     0 RETURN     tcp  --  *      *       0.0.0.0/0            0.0.0.0/8
3        0     0 RETURN     tcp  --  *      *       0.0.0.0/0            10.0.0.0/8
4        0     0 RETURN     tcp  --  *      *       0.0.0.0/0            100.64.0.0/10
5        0     0 RETURN     tcp  --  *      *       0.0.0.0/0            127.0.0.0/8
6        0     0 RETURN     tcp  --  *      *       0.0.0.0/0            169.254.0.0/16
7        0     0 RETURN     tcp  --  *      *       0.0.0.0/0            172.16.0.0/12
8        0     0 RETURN     tcp  --  *      *       0.0.0.0/0            192.0.0.0/24
9        0     0 RETURN     tcp  --  *      *       0.0.0.0/0            192.0.2.0/24
10       0     0 RETURN     tcp  --  *      *       0.0.0.0/0            192.88.99.0/24
11   45895 2752K RETURN     tcp  --  *      *       0.0.0.0/0            192.168.0.0/16
12       0     0 RETURN     tcp  --  *      *       0.0.0.0/0            198.18.0.0/15
13       0     0 RETURN     tcp  --  *      *       0.0.0.0/0            198.51.100.0/24
14       0     0 RETURN     tcp  --  *      *       0.0.0.0/0            203.0.113.0/24
15       0     0 RETURN     tcp  --  *      *       0.0.0.0/0            224.0.0.0/4
16       0     0 RETURN     tcp  --  *      *       0.0.0.0/0            240.0.0.0/4
17       0     0 RETURN     tcp  --  *      *       0.0.0.0/0            255.255.255.255
18   42172 2211K REDIRECT   tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            redir ports 1081

所以任何访问本机外网IP的tcp连接都被REDIRECT到本机1081端口的V2RAY。例如本机外网IP的3000端口转发到某内网IP的2000端口。当V2RAY路由到直接访问时,尝试访问本机外网IP的源端口3000时,由于请求已经不会过nat表了,所以不会找到对应的目标端口2000转发,直接访问了路由器的3000端口。而如果路由器该端口没开服务,那么V2RAY就返回客户端结束tcp连接了。

解决方法:
目前想到3个解决方法:

  1. V2RAY链最后一条REDIRECT规则前添加一条本机外网IP网段直接返回的规则。
  2. 在系统重新拨号后重新载入V2RAY的防火墙规则。
  3. 让系统插PREROUTING链时从前面开始插
kuoruan added a commit that referenced this issue Oct 31, 2019
@kuoruan
Copy link
Owner

kuoruan commented Oct 31, 2019

写了个 hotplug 在 ifup 的时候重启服务,试试看行不行
我没有测试环境

@koho
Copy link
Author

koho commented Oct 31, 2019

修改了16行的引号问题后,测试了一下已经没问题了。

未使用脚本前:

未使用脚本

使用脚本后:

使用脚本后

日志:

Thu Oct 31 16:21:17 2019 daemon.notice netifd: Network device 'pppoe-wan' link is up
Thu Oct 31 16:21:17 2019 daemon.notice netifd: Interface 'wan' is now up
Thu Oct 31 16:21:17 2019 daemon.info dnsmasq[9213]: reading /tmp/resolv.conf.auto
Thu Oct 31 16:21:17 2019 daemon.info dnsmasq[9213]: using local addresses only for domain test
Thu Oct 31 16:21:17 2019 daemon.info dnsmasq[9213]: using local addresses only for domain onion
Thu Oct 31 16:21:17 2019 daemon.info dnsmasq[9213]: using local addresses only for domain localhost
Thu Oct 31 16:21:17 2019 daemon.info dnsmasq[9213]: using local addresses only for domain local
Thu Oct 31 16:21:17 2019 daemon.info dnsmasq[9213]: using local addresses only for domain invalid
Thu Oct 31 16:21:17 2019 daemon.info dnsmasq[9213]: using local addresses only for domain bind
Thu Oct 31 16:21:17 2019 daemon.info dnsmasq[9213]: using local addresses only for domain lan
Thu Oct 31 16:21:17 2019 daemon.info dnsmasq[9213]: using nameserver 114.114.114.114#53
Thu Oct 31 16:21:17 2019 user.notice firewall: Reloading firewall due to ifup of wan (pppoe-wan)
Thu Oct 31 16:21:17 2019 daemon.info v2ray: Policy disabled: main_policy
Thu Oct 31 16:21:17 2019 daemon.info v2ray: Reverse disabled: main_reverse
Thu Oct 31 16:21:17 2019 daemon.info v2ray[30725]: V2Ray 4.21.0 (V2Fly, a community-driven edition of V2Ray.) OpenWrt - Release 1
Thu Oct 31 16:21:17 2019 daemon.info v2ray[30725]: A unified platform for anti-censorship.

一切正常。

@koho
Copy link
Author

koho commented Nov 1, 2019

刚刚发现如果修改或新增端口转发规则也会触发类似上面的系统重设防火墙操作

@kuoruan
Copy link
Owner

kuoruan commented Nov 1, 2019

会手动修改 iptables 的人,一般都会查看这些规则,所以可以忽略这种

kuoruan added a commit that referenced this issue Nov 1, 2019
@kuoruan
Copy link
Owner

kuoruan commented Nov 1, 2019

发现了一种新方法,你可以试试
原来的 hotplug 可以删了

kuoruan added a commit that referenced this issue Nov 1, 2019
@koho
Copy link
Author

koho commented Nov 4, 2019

刚测试了一下,一开始没触发。发现定义了两个service_triggers,合并后确实触发了重载。发现v2ray的重载却在firewall的重载前面:

Mon Nov  4 10:19:29 2019 daemon.info v2ray: Policy disabled: main_policy
Mon Nov  4 10:19:29 2019 daemon.info v2ray: Reverse disabled: main_reverse
Mon Nov  4 10:19:29 2019 user.notice root: Start reloading firewall
Mon Nov  4 10:19:29 2019 user.notice root: End reloading firewall

查了一下其他项目,加了个延时解决
https://github.com/openwrt/packages/blob/d30a8bd61e0c6e3d8fc80ef682d335fd930f6845/net/samba4/files/samba.init#L166-L175

@kuoruan
Copy link
Owner

kuoruan commented Nov 4, 2019

默认的延迟 1000,关键在于这个时间其实是不确定的。
我再换一种方法吧

@csquya
Copy link

csquya commented Nov 6, 2019

修改的文件 /etc/uci-defaults/40_luci-v2ray 在我的路由器里找不到啊, 其他2个文件倒是验证过有的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants