Skip to content

Commit df26747

Browse files
committed
feat(tproxy): 默认 bypass 网段支持静态 ipset 优化,并保留不支持环境下的逐条规则回退逻辑
1 parent c5896a4 commit df26747

1 file changed

Lines changed: 84 additions & 11 deletions

File tree

src/module/scripts/network/tproxy.sh

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,60 @@ download_file() {
695695
fi
696696
}
697697

698+
setup_static_bypass_ipset() {
699+
if [ "$HAS_IPSET" -eq 0 ] || [ "$HAS_XT_SET" -eq 0 ]; then
700+
log Debug "Kernel does not support ipset, static bypass ipset skipped"
701+
return 0
702+
fi
703+
704+
if ! command -v ipset > /dev/null 2>&1; then
705+
log Warn "ipset command not found, skipping static bypass optimization"
706+
return 1
707+
fi
708+
709+
log Info "Setting up static bypass ipsets for default ranges"
710+
711+
if [ "$DRY_RUN" -eq 1 ]; then
712+
log Debug "[EXEC] ipset destroy bypass4_static (skipped, dry-run)"
713+
log Debug "[EXEC] ipset restore -! (skipped, dry-run)"
714+
return 0
715+
fi
716+
717+
# Rebuild sets to ensure content matches current configuration
718+
ipset destroy bypass4_static 2> /dev/null || true
719+
ipset destroy bypass6_static 2> /dev/null || true
720+
721+
{
722+
echo "create bypass4_static hash:net family inet hashsize 64 maxelem 256"
723+
for ip in $BYPASS_IPv4_LIST; do
724+
echo "add bypass4_static $ip"
725+
done
726+
727+
if [ "$PROXY_IPV6" -eq 1 ]; then
728+
echo "create bypass6_static hash:net family inet6 hashsize 64 maxelem 256"
729+
for ip in $BYPASS_IPv6_LIST; do
730+
echo "add bypass6_static $ip"
731+
done
732+
fi
733+
} | ipset restore -!
734+
735+
if [ $? -eq 0 ]; then
736+
return 0
737+
else
738+
log Error "Failed to populate static bypass ipsets"
739+
return 1
740+
fi
741+
}
742+
743+
cleanup_static_bypass_ipset() {
744+
if [ "$DRY_RUN" -eq 1 ]; then
745+
log Debug "[EXEC] ipset destroy bypass[46]_static (skipped, dry-run)"
746+
return 0
747+
fi
748+
ipset destroy bypass4_static 2> /dev/null || true
749+
ipset destroy bypass6_static 2> /dev/null || true
750+
}
751+
698752
download_cn_ip_list() {
699753
if [ "$BYPASS_CN_IP" -eq 0 ]; then
700754
log Debug "CN IP bypass is disabled, download skipped"
@@ -993,18 +1047,35 @@ setup_proxy_chain() {
9931047
log Info "Added local address type bypass"
9941048
fi
9951049

996-
if [ "$family" = "6" ]; then
997-
for subnet6 in $BYPASS_IPv6_LIST; do
998-
$cmd -t "$table" -A "BYPASS_IP$suffix" -d "$subnet6" -p udp ! --dport 53 -j ACCEPT
999-
$cmd -t "$table" -A "BYPASS_IP$suffix" -d "$subnet6" ! -p udp -j ACCEPT
1000-
done
1001-
log Info "Added bypass rules for BYPASS IPv6 ranges"
1050+
local use_ipset=0
1051+
local ipset_name="bypass4_static"
1052+
[ "$family" = "6" ] && ipset_name="bypass6_static"
1053+
1054+
if [ "$HAS_IPSET" -eq 1 ] && [ "$HAS_XT_SET" -eq 1 ]; then
1055+
if ipset list "$ipset_name" > /dev/null 2>&1; then
1056+
use_ipset=1
1057+
fi
1058+
fi
1059+
1060+
if [ "$use_ipset" -eq 1 ]; then
1061+
$cmd -t "$table" -A "BYPASS_IP$suffix" -m set --match-set "$ipset_name" dst -p udp ! --dport 53 -j ACCEPT
1062+
$cmd -t "$table" -A "BYPASS_IP$suffix" -m set --match-set "$ipset_name" dst ! -p udp -j ACCEPT
1063+
log Info "Added ipset-based ($ipset_name) bypass rules"
10021064
else
1003-
for subnet4 in $BYPASS_IPv4_LIST; do
1004-
$cmd -t "$table" -A "BYPASS_IP$suffix" -d "$subnet4" -p udp ! --dport 53 -j ACCEPT
1005-
$cmd -t "$table" -A "BYPASS_IP$suffix" -d "$subnet4" ! -p udp -j ACCEPT
1006-
done
1007-
log Info "Added bypass rules for BYPASS IPv4 ranges"
1065+
log Info "Using per-rule fallback for bypass ranges (ipset not available)"
1066+
if [ "$family" = "6" ]; then
1067+
for subnet6 in $BYPASS_IPv6_LIST; do
1068+
$cmd -t "$table" -A "BYPASS_IP$suffix" -d "$subnet6" -p udp ! --dport 53 -j ACCEPT
1069+
$cmd -t "$table" -A "BYPASS_IP$suffix" -d "$subnet6" ! -p udp -j ACCEPT
1070+
done
1071+
log Info "Added bypass rules for BYPASS IPv6 ranges"
1072+
else
1073+
for subnet4 in $BYPASS_IPv4_LIST; do
1074+
$cmd -t "$table" -A "BYPASS_IP$suffix" -d "$subnet4" -p udp ! --dport 53 -j ACCEPT
1075+
$cmd -t "$table" -A "BYPASS_IP$suffix" -d "$subnet4" ! -p udp -j ACCEPT
1076+
done
1077+
log Info "Added bypass rules for BYPASS IPv4 ranges"
1078+
fi
10081079
fi
10091080

10101081
if [ "$BYPASS_CN_IP" -eq 1 ]; then
@@ -1515,6 +1586,7 @@ detect_proxy_mode() {
15151586

15161587
start_proxy() {
15171588
log Info "Starting proxy setup..."
1589+
setup_static_bypass_ipset
15181590
if [ "$BYPASS_CN_IP" -eq 1 ]; then
15191591
if [ "$HAS_IPSET" -eq 0 ] || [ "$HAS_XT_SET" -eq 0 ]; then
15201592
log Error "Kernel does not support ipset (CONFIG_IP_SET, CONFIG_NETFILTER_XT_SET). Cannot bypass CN IPs"
@@ -1572,6 +1644,7 @@ stop_proxy() {
15721644
cleanup_redirect_chain6
15731645
fi
15741646
fi
1647+
cleanup_static_bypass_ipset
15751648
cleanup_ipset
15761649
log Info "Proxy stopped"
15771650
block_loopback_traffic disable

0 commit comments

Comments
 (0)