Skip to content

Commit a63e6f0

Browse files
committed
fix and improvement
1 parent 5bdd86f commit a63e6f0

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

module.prop

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
id=box_for_magisk
22
name=SingBox for Magisk
3-
version=v1.2.1
4-
versionCode=20250120
3+
version=v1.2.2
4+
versionCode=20250121
55
author=gitduk
66
description=use sing-box for tunnel proxy on android device
77
updateJson=https://github.com/gitduk/box_for_magisk/raw/master/update.json

singbox/scripts/iptables.sh

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ chains=(
88
BOX_IP_V4
99
)
1010

11+
init_chains() {
12+
local table="$1"
13+
local iptables="iptables -w 64"
14+
15+
for chain in "${chains[@]}"; do
16+
${iptables} -t ${table} -N ${chain} 2>/dev/null
17+
${iptables} -t ${table} -F ${chain}
18+
done
19+
}
20+
1121
cleanup_limit() {
1222
local iptables="iptables -w 64"
1323

@@ -49,6 +59,24 @@ setup_intranet_rules() {
4959
done
5060
}
5161

62+
# 处理 dns 流量
63+
setup_dns() {
64+
local table="$1"
65+
local chain="$2"
66+
local iptables="iptables -w 64"
67+
68+
for proto in tcp udp; do
69+
case ${table} in
70+
nat)
71+
${iptables} -t ${table} -A ${chain} -p ${proto} --dport 53 -j REDIRECT --to-ports "${redir_port}"
72+
;;
73+
mangle)
74+
${iptables} -t ${table} -A ${chain} -p ${proto} --dport 53 -j TPROXY --on-port ${tproxy_port} --tproxy-mark ${fwmark}
75+
;;
76+
esac
77+
done
78+
}
79+
5280
# 统一的包过滤函数
5381
handle_packages() {
5482
local table="$1"
@@ -101,13 +129,10 @@ redirect() {
101129

102130
log info "Setting up iptables for redirect mode"
103131

104-
# 创建自定义链
105-
for chain in "${chains[@]}"; do
106-
${iptables} -t nat -N ${chain} 2>/dev/null
107-
${iptables} -t nat -F ${chain}
108-
done
132+
# 初始化自定义链
133+
init_chains nat
109134

110-
# 设置基础绕过规则
135+
# 处理 sing-box 流量
111136
${iptables} -t nat -I BOX_LOCAL -m owner --uid-owner "${box_user}" --gid-owner "${box_group}" -j RETURN
112137

113138
# 处理应用过滤
@@ -119,10 +144,8 @@ redirect() {
119144
setup_intranet_rules nat BOX_LOCAL
120145

121146
# DNS 处理
122-
for proto in tcp udp; do
123-
${iptables} -t nat -A BOX_EXTERNAL -p ${proto} --dport 53 -j REDIRECT --to-ports "${redir_port}"
124-
${iptables} -t nat -A BOX_LOCAL -p ${proto} --dport 53 -j REDIRECT --to-ports "${redir_port}"
125-
done
147+
setup_dns nat BOX_EXTERNAL
148+
setup_dns nat BOX_LOCAL
126149

127150
# 处理特殊接口
128151
${iptables} -t nat -A BOX_EXTERNAL -p tcp -i lo -j REDIRECT --to-ports "${redir_port}"
@@ -159,31 +182,26 @@ tproxy() {
159182

160183
log info "Setting up iptables for tproxy mode"
161184

185+
# 初始化自定义链
186+
init_chains mangle
187+
162188
# 配置策略路由
163189
ip rule add fwmark "${fwmark}" table "${table}" pref "${pref}"
164190
ip route add local default dev lo table "${table}"
165191

166-
# 创建自定义链
167-
for chain in "${chains[@]}"; do
168-
${iptables} -t mangle -N ${chain} 2>/dev/null
169-
${iptables} -t mangle -F ${chain}
170-
done
192+
# 处理 sing-box 流量
193+
${iptables} -t mangle -A BOX_LOCAL -m owner --uid-owner ${box_user} --gid-owner ${box_group} -j RETURN
171194

172-
# DNS 处理
173-
for proto in tcp udp; do
174-
${iptables} -t mangle -A BOX_EXTERNAL -p ${proto} --dport 53 -j TPROXY --on-port ${tproxy_port} --tproxy-mark ${fwmark}
175-
done
195+
# 应用过滤
196+
handle_packages mangle BOX_LOCAL "include"
197+
handle_packages mangle BOX_LOCAL "exclude"
176198

177199
# 内网流量处理
178200
setup_intranet_rules mangle BOX_EXTERNAL
179201
setup_intranet_rules mangle BOX_LOCAL
180202

181-
# 处理本地流量
182-
${iptables} -t mangle -A BOX_LOCAL -m owner --uid-owner ${box_user} --gid-owner ${box_group} -j RETURN
183-
184-
# 应用过滤
185-
handle_packages mangle BOX_LOCAL "include"
186-
handle_packages mangle BOX_LOCAL "exclude"
203+
# DNS 处理
204+
setup_dns mangle BOX_EXTERNAL
187205

188206
# 处理特殊接口
189207
for proto in tcp udp; do
@@ -236,7 +254,7 @@ tun() {
236254
}
237255

238256
# 清理手机产商的网络限制
239-
cleanup_limit
257+
(sleep 10 && cleanup_limit) &
240258

241259
# 主程序入口
242260
case "$1" in

singbox/scripts/service.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ check_system_requirements() {
1818

1919
# 检查并创建 TUN 设备
2020
if [ -n "${tun_device}" ]; then
21-
log debug "Creating TUN device: ${tun_device}"
21+
log info "Creating TUN device: ${tun_device}"
2222
mkdir -p /dev/net
2323
[ ! -L /dev/net/tun ] && ln -s /dev/tun /dev/net/tun
2424

0 commit comments

Comments
 (0)