@@ -244,12 +244,14 @@ add_firewall_rule() {
244244 $iptables_nat -A PSW $( dst $IPSET_VPSIPLIST ) -j RETURN
245245 $iptables_nat -A PSW $( dst $IPSET_WHITELIST ) -j RETURN
246246 $iptables_nat -N PSW_ACL
247+ $iptables_nat -N PSW_OUTPUT
247248
248249 $iptables_mangle -N PSW
249250 $iptables_mangle -A PSW $( dst $IPSET_LANIPLIST ) -j RETURN
250251 $iptables_mangle -A PSW $( dst $IPSET_VPSIPLIST ) -j RETURN
251252 $iptables_mangle -A PSW $( dst $IPSET_WHITELIST ) -j RETURN
252253 $iptables_mangle -N PSW_ACL
254+ $iptables_mangle -N PSW_OUTPUT
253255
254256 if [[ " $TCP_NODE_NUM " -ge 1 ]] || [[ " $UDP_NODE_NUM " -ge 1 ]]; then
255257 local max_num=1
@@ -322,7 +324,7 @@ add_firewall_rule() {
322324
323325 # 用于本机流量转发,默认只走router
324326 $iptables_mangle -A PSW -s $lan_ip -p tcp $( dst $IPSET_ROUTER ) -j TPROXY --tproxy-mark 0x1/0x1 --on-port $local_port
325- $iptables_mangle -A OUTPUT -p tcp -m multiport --dport $TCP_REDIR_PORTS $( dst $IPSET_ROUTER ) $iptables_comment -j MARK --set-mark 1
327+ $iptables_mangle -A PSW_OUTPUT -p tcp -m multiport --dport $TCP_REDIR_PORTS $( dst $IPSET_ROUTER ) -j MARK --set-mark 1
326328 else
327329 # 全局模式
328330 $iptables_nat -A PSW_GLO$k -p tcp -j REDIRECT --to-ports $local_port
@@ -382,27 +384,27 @@ add_firewall_rule() {
382384 fi
383385 $iptables_nat -I PREROUTING $PR_INDEX -j PSW
384386 fi
385- # 用于本机流量转发,默认只走router
386- # $iptables_nat -I OUTPUT -j PSW
387- $iptables_nat -A OUTPUT $( dst $IPSET_LANIPLIST ) $iptables_comment -j RETURN
387+ # 用于本机流量转发
388+ $iptables_nat -A OUTPUT -j PSW_OUTPUT
389+ $iptables_nat -A PSW_OUTPUT $( dst $IPSET_LANIPLIST ) -j RETURN
388390 [ " $use_tcp_node_resolve_dns " == 1 -a -n " $DNS_FORWARD " ] && {
389391 for dns in $DNS_FORWARD
390392 do
391393 local dns_ip=$( echo $dns | awk -F " #" ' {print $1}' )
392394 local dns_port=$( echo $dns | awk -F " #" ' {print $2}' )
393395 [ -z " $dns_port " ] && dns_port=53
394- $iptables_nat -A OUTPUT -p tcp -d $dns_ip --dport $dns_port $iptables_comment -j REDIRECT --to-ports $TCP_REDIR_PORT1
396+ $iptables_nat -A PSW_OUTPUT -p tcp -d $dns_ip --dport $dns_port -j REDIRECT --to-ports $TCP_REDIR_PORT1
395397 done
396398 }
397- $iptables_nat -A OUTPUT $( dst $IPSET_VPSIPLIST ) $iptables_comment -j RETURN
398- $iptables_nat -A OUTPUT $( dst $IPSET_WHITELIST ) $iptables_comment -j RETURN
399- $iptables_nat -A OUTPUT -p tcp -m multiport --dport $TCP_REDIR_PORTS $( dst $IPSET_ROUTER ) $iptables_comment -j REDIRECT --to-ports $TCP_REDIR_PORT1
400- $iptables_nat -A OUTPUT -p tcp -m multiport --dport $TCP_REDIR_PORTS $( dst $IPSET_BLACKLIST ) $iptables_comment -j REDIRECT --to-ports $TCP_REDIR_PORT1
399+ $iptables_nat -A PSW_OUTPUT $( dst $IPSET_VPSIPLIST ) -j RETURN
400+ $iptables_nat -A PSW_OUTPUT $( dst $IPSET_WHITELIST ) -j RETURN
401+ $iptables_nat -A PSW_OUTPUT -p tcp -m multiport --dport $TCP_REDIR_PORTS $( dst $IPSET_ROUTER ) -j REDIRECT --to-ports $TCP_REDIR_PORT1
402+ $iptables_nat -A PSW_OUTPUT -p tcp -m multiport --dport $TCP_REDIR_PORTS $( dst $IPSET_BLACKLIST ) -j REDIRECT --to-ports $TCP_REDIR_PORT1
401403
402- [ " $LOCALHOST_PROXY_MODE " == " global" ] && $iptables_nat -A OUTPUT -p tcp -m multiport --dport $TCP_REDIR_PORTS $iptables_comment -j REDIRECT --to-ports $TCP_REDIR_PORT1
403- [ " $LOCALHOST_PROXY_MODE " == " gfwlist" ] && $iptables_nat -A OUTPUT -p tcp -m multiport --dport $TCP_REDIR_PORTS $( dst $IPSET_GFW ) $iptables_comment -j REDIRECT --to-ports $TCP_REDIR_PORT1
404+ [ " $LOCALHOST_PROXY_MODE " == " global" ] && $iptables_nat -A PSW_OUTPUT -p tcp -m multiport --dport $TCP_REDIR_PORTS -j REDIRECT --to-ports $TCP_REDIR_PORT1
405+ [ " $LOCALHOST_PROXY_MODE " == " gfwlist" ] && $iptables_nat -A PSW_OUTPUT -p tcp -m multiport --dport $TCP_REDIR_PORTS $( dst $IPSET_GFW ) -j REDIRECT --to-ports $TCP_REDIR_PORT1
404406 [ " $LOCALHOST_PROXY_MODE " == " chnroute" ] && {
405- $iptables_nat -A OUTPUT -p tcp -m multiport --dport $TCP_REDIR_PORTS -m set ! --match-set $IPSET_CHN dst $iptables_comment -j REDIRECT --to-ports $TCP_REDIR_PORT1
407+ $iptables_nat -A PSW_OUTPUT -p tcp -m multiport --dport $TCP_REDIR_PORTS -m set ! --match-set $IPSET_CHN dst -j REDIRECT --to-ports $TCP_REDIR_PORT1
406408 }
407409 }
408410 # 重定所有流量到透明代理端口
@@ -467,27 +469,28 @@ add_firewall_rule() {
467469 $iptables_mangle -A PSW_GAME$k -p udp $( dst $IPSET_CHN ) -j RETURN
468470 $iptables_mangle -A PSW_GAME$k -p udp -j TPROXY --tproxy-mark 0x1/0x1 --on-port $local_port
469471
470- # 用于本机流量转发,默认只走router
471- $iptables_mangle -A OUTPUT -p udp $( dst $IPSET_LANIPLIST ) $iptables_comment -j RETURN
472+ # 用于本机流量转发
473+ $iptables_mangle -A OUTPUT -j PSW_OUTPUT
474+ $iptables_mangle -A PSW_OUTPUT -p udp $( dst $IPSET_LANIPLIST ) -j RETURN
472475 [ " $use_udp_node_resolve_dns " == 1 -a -n " $DNS_FORWARD " ] && {
473476 for dns in $DNS_FORWARD
474477 do
475478 local dns_ip=$( echo $dns | awk -F " #" ' {print $1}' )
476479 local dns_port=$( echo $dns | awk -F " #" ' {print $2}' )
477480 [ -z " $dns_port " ] && dns_port=53
478- $iptables_mangle -A OUTPUT -p udp -d $dns_ip --dport $dns_port $iptables_comment -j MARK --set-mark 1
479- $iptables_mangle -I PSW 2 -p udp -d $dns_ip --dport $dns_port $iptables_comment -j TPROXY --tproxy-mark 0x1/0x1 --on-port $local_port
481+ $iptables_mangle -A PSW_OUTPUT -p udp -d $dns_ip --dport $dns_port -j MARK --set-mark 1
482+ $iptables_mangle -I PSW 2 -p udp -d $dns_ip --dport $dns_port -j TPROXY --tproxy-mark 0x1/0x1 --on-port $local_port
480483 done
481484 }
482- $iptables_mangle -A OUTPUT -p udp $( dst $IPSET_VPSIPLIST ) $iptables_comment -j RETURN
483- $iptables_mangle -A OUTPUT -p udp $( dst $IPSET_WHITELIST ) $iptables_comment -j RETURN
484- $iptables_mangle -A OUTPUT -p udp -m multiport --dport $UDP_REDIR_PORTS $( dst $IPSET_ROUTER ) $iptables_comment -j MARK --set-mark 1
485- $iptables_mangle -A OUTPUT -p udp -m multiport --dport $UDP_REDIR_PORTS $( dst $IPSET_BLACKLIST ) $iptables_comment -j MARK --set-mark 1
485+ $iptables_mangle -A PSW_OUTPUT -p udp $( dst $IPSET_VPSIPLIST ) -j RETURN
486+ $iptables_mangle -A PSW_OUTPUT -p udp $( dst $IPSET_WHITELIST ) -j RETURN
487+ $iptables_mangle -A PSW_OUTPUT -p udp -m multiport --dport $UDP_REDIR_PORTS $( dst $IPSET_ROUTER ) -j MARK --set-mark 1
488+ $iptables_mangle -A PSW_OUTPUT -p udp -m multiport --dport $UDP_REDIR_PORTS $( dst $IPSET_BLACKLIST ) -j MARK --set-mark 1
486489
487- [ " $LOCALHOST_PROXY_MODE " == " global" ] && $iptables_mangle -A OUTPUT -p udp -m multiport --dport $UDP_REDIR_PORTS $iptables_comment -j MARK --set-mark 1
488- [ " $LOCALHOST_PROXY_MODE " == " gfwlist" ] && $iptables_mangle -A OUTPUT -p udp -m multiport --dport $UDP_REDIR_PORTS $( dst $IPSET_GFW ) $iptables_comment -j MARK --set-mark 1
490+ [ " $LOCALHOST_PROXY_MODE " == " global" ] && $iptables_mangle -A PSW_OUTPUT -p udp -m multiport --dport $UDP_REDIR_PORTS -j MARK --set-mark 1
491+ [ " $LOCALHOST_PROXY_MODE " == " gfwlist" ] && $iptables_mangle -A PSW_OUTPUT -p udp -m multiport --dport $UDP_REDIR_PORTS $( dst $IPSET_GFW ) -j MARK --set-mark 1
489492 [ " $LOCALHOST_PROXY_MODE " == " chnroute" ] && {
490- $iptables_mangle -A OUTPUT -p udp -m multiport --dport $UDP_REDIR_PORTS -m set ! --match-set $IPSET_CHN dst $iptables_comment -j MARK --set-mark 1
493+ $iptables_mangle -A PSW_OUTPUT -p udp -m multiport --dport $UDP_REDIR_PORTS -m set ! --match-set $IPSET_CHN dst -j MARK --set-mark 1
491494 }
492495
493496 echolog " IPv4 防火墙UDP转发规则加载完成!"
@@ -505,9 +508,9 @@ add_firewall_rule() {
505508 local ip=$( echo $balancing_node | awk -F " :" ' {print $1}' )
506509 local port=$( echo $balancing_node | awk -F " :" ' {print $2}' )
507510 $iptables_nat -I PSW 2 -p tcp -d $ip --dport $port -j RETURN
508- $iptables_nat -I OUTPUT 2 -p tcp -d $ip --dport $port $iptables_comment -j RETURN
511+ $iptables_nat -I PSW_OUTPUT 2 -p tcp -d $ip --dport $port -j RETURN
509512 $iptables_mangle -I PSW 2 -p udp -d $ip --dport $port -j RETURN
510- $iptables_mangle -I OUTPUT 2 -p udp -d $ip --dport $port $iptables_comment -j RETURN
513+ $iptables_mangle -I PSW_OUTPUT 2 -p udp -d $ip --dport $port -j RETURN
511514 done
512515 fi
513516
@@ -534,30 +537,6 @@ add_firewall_rule() {
534537
535538del_firewall_rule () {
536539 echolog " 删除所有防火墙规则..."
537- ipv4_output_exist=$( $iptables_nat -L OUTPUT 2> /dev/null | grep -c -E " PassWall" )
538- [ -n " $ipv4_output_exist " ] && {
539- until [ " $ipv4_output_exist " = 0 ]; do
540- rules=$( $iptables_nat -L OUTPUT --line-numbers | grep -E " PassWall" | awk ' {print $1}' )
541- for rule in $rules ; do
542- $iptables_nat -D OUTPUT $rule 2> /dev/null
543- break
544- done
545- ipv4_output_exist=$( expr $ipv4_output_exist - 1)
546- done
547- }
548-
549- ipv4_output_exist=$( $iptables_mangle -L OUTPUT 2> /dev/null | grep -c -E " PassWall" )
550- [ -n " $ipv4_output_exist " ] && {
551- until [ " $ipv4_output_exist " = 0 ]; do
552- rules=$( $iptables_mangle -L OUTPUT --line-numbers | grep -E " PassWall" | awk ' {print $1}' )
553- for rule in $rules ; do
554- $iptables_mangle -D OUTPUT $rule 2> /dev/null
555- break
556- done
557- ipv4_output_exist=$( expr $ipv4_output_exist - 1)
558- done
559- }
560-
561540 ipv6_output_ss_exist=$( $ip6tables_nat -L OUTPUT 2> /dev/null | grep -c " PSW" )
562541 [ -n " $ipv6_output_ss_exist " ] && {
563542 until [ " $ipv6_output_ss_exist " = 0 ]; do
@@ -571,16 +550,22 @@ del_firewall_rule() {
571550 }
572551
573552 $iptables_nat -D PREROUTING -j PSW 2> /dev/null
553+ $iptables_nat -D OUTPUT -j PSW_OUTPUT 2> /dev/null
574554 $iptables_nat -F PSW 2> /dev/null && $iptables_nat -X PSW 2> /dev/null
575555 $iptables_nat -F PSW_ACL 2> /dev/null && $iptables_nat -X PSW_ACL 2> /dev/null
556+ $iptables_nat -F PSW_OUTPUT 2> /dev/null && $iptables_nat -X PSW_OUTPUT 2> /dev/null
576557
577- $iptables_mangle -D PREROUTING -j PSW$k 2> /dev/null
558+ $iptables_mangle -D PREROUTING -j PSW 2> /dev/null
559+ $iptables_mangle -D OUTPUT -j PSW_OUTPUT 2> /dev/null
578560 $iptables_mangle -F PSW 2> /dev/null && $iptables_mangle -X PSW 2> /dev/null
579561 $iptables_mangle -F PSW_ACL 2> /dev/null && $iptables_mangle -X PSW_ACL 2> /dev/null
562+ $iptables_mangle -F PSW_OUTPUT 2> /dev/null && $iptables_mangle -X PSW_OUTPUT 2> /dev/null
580563
581564 $ip6tables_nat -D PREROUTING -j PSW 2> /dev/null
565+ $ip6tables_nat -D OUTPUT -j PSW_OUTPUT 2> /dev/null
582566 $ip6tables_nat -F PSW 2> /dev/null && $ip6tables_nat -X PSW 2> /dev/null
583567 $ip6tables_nat -F PSW_ACL 2> /dev/null && $ip6tables_nat -X PSW_ACL 2> /dev/null
568+ $ip6tables_nat -F PSW_OUTPUT 2> /dev/null && $ip6tables_nat -X PSW_OUTPUT 2> /dev/null
584569
585570 local max_num=5
586571 if [ " $max_num " -ge 1 ]; then
0 commit comments