forked from CHIZI-0618/box4magisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.service
executable file
·232 lines (213 loc) · 7.82 KB
/
box.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/system/bin/sh
export PATH="/data/adb/magisk:/data/adb/ksu/bin:$PATH:/data/data/com.termux/files/usr/bin"
scripts=$(realpath $0)
scripts_dir=$(dirname ${scripts})
source ${scripts_dir}/box.config
mkdir -p ${run_path}
mkdir -p ${box_path}/${bin_name}
# ${box_path}/bin/yq -i ".tproxy-port=${tproxy_port}" ${box_path}/clash/config.yaml
# ${box_path}/bin/yq -i ".dns.listen=\"${clash_dns_listen}\"" ${box_path}/clash/config.yaml
# ${box_path}/bin/yq -i ".dns.fake-ip-range=\"${clash_fake_ip_range}\"" ${box_path}/clash/config.yaml
# ${box_path}/bin/yq -o=json -i "(.inbounds[] | select(.type == \"tproxy\") | .listen_port) = ${tproxy_port}" ${box_path}/sing-box/config.json
find ${box_path} -mtime +3 -type f -name "*.log" | xargs rm -f
log() {
export TZ=Asia/Shanghai
now=$(date +"[%Y-%m-%d %H:%M:%S %Z]")
case $1 in
Info)
[ -t 1 ] && echo -e "\033[1;32m${now} [Info]: $2\033[0m" || echo "${now} [Info]: $2"
;;
Warn)
[ -t 1 ] && echo -e "\033[1;33m${now} [Warn]: $2\033[0m" || echo "${now} [Warn]: $2"
;;
Error)
[ -t 1 ] && echo -e "\033[1;31m${now} [Error]: $2\033[0m" || echo "${now} [Error]: $2"
;;
*)
[ -t 1 ] && echo -e "\033[1;30m${now} [$1]: $2\033[0m" || echo "${now} [$1]: $2"
;;
esac
}
create_tun_link() {
mkdir -p /dev/net
[ ! -L /dev/net/tun ] && ln -s /dev/tun /dev/net/tun
}
probe_tun_device() {
ifconfig | grep -q ${tun_device} || return 1
}
forward() {
iptables -w 100 $1 FORWARD -o ${tun_device} -j ACCEPT
iptables -w 100 $1 FORWARD -i ${tun_device} -j ACCEPT
ip6tables -w 100 $1 FORWARD -o ${tun_device} -j ACCEPT
ip6tables -w 100 $1 FORWARD -i ${tun_device} -j ACCEPT
}
check_permission() {
if which ${bin_name} | grep -q "/system/bin/" ; then
box_user=$(echo ${box_user_group} | awk -F ':' '{print $1}')
box_group=$(echo ${box_user_group} | awk -F ':' '{print $2}')
box_user_id=$(id -u ${box_user})
box_group_id=$(id -g ${box_group})
[ ${box_user_id} ] && [ ${box_group_id} ] || \
(box_user_group="root:net_admin" && log Error "${box_user_group} error, use root:net_admin instead.")
bin_path=$(which ${bin_name})
chown ${box_user_group} ${bin_path}
chmod 0700 ${bin_path}
if [ "${box_user_id}" != "0" ] || [ "${box_group_id}" != "3005" ] ; then
# setcap has been deprecated as it does not support binary outside of the /system/bin directory
setcap 'cap_net_admin,cap_net_raw,cap_net_bind_service+ep' ${bin_path} || \
(box_user_group="root:net_admin" && log Error "setcap authorization failed, you may need libcap package.")
fi
chown -R ${box_user_group} ${box_path}
return 0
elif [ -f ${bin_path} ] ; then
box_user_group="root:net_admin"
chown ${box_user_group} ${bin_path}
chmod 0700 ${bin_path}
chown -R ${box_user_group} ${box_path}
return 0
else
return 1
fi
}
start_bin() {
ulimit -SHn 1000000
case "${bin_name}" in
sing-box)
if ${bin_path} check -D ${box_path}/${bin_name} > ${run_path}/check.log 2>&1 ; then
log Info "starting ${bin_name} service."
nohup busybox setuidgid ${box_user_group} ${bin_path} run -D ${box_path}/${bin_name} > /dev/null 2> ${run_path}/error_${bin_name}.log &
echo -n $! > ${pid_file}
return 0
else
log Error "configuration check failed, please check the ${run_path}/check.log file."
return 1
fi
;;
clash)
if ${bin_path} -t -d ${box_path}/${bin_name} > ${run_path}/check.log 2>&1 ; then
log Info "starting ${bin_name} service."
nohup busybox setuidgid ${box_user_group} ${bin_path} -d ${box_path}/${bin_name} > ${box_path}/${bin_name}/${bin_name}_$(date +%Y%m%d%H%M).log 2> ${run_path}/error_${bin_name}.log &
echo -n $! > ${pid_file}
return 0
else
log Error "configuration check failed, please check the ${run_path}/check.log file."
return 1
fi
;;
xray)
export XRAY_LOCATION_ASSET="${box_path}/${bin_name}"
if ${bin_path} -test -confdir ${box_path}/${bin_name} > ${run_path}/check.log 2>&1 ; then
log Info "starting ${bin_name} service."
nohup busybox setuidgid ${box_user_group} ${bin_path} -confdir ${box_path}/${bin_name} > /dev/null 2> ${run_path}/error_${bin_name}.log &
echo -n $! > ${pid_file}
return 0
else
log Error "configuration check failed, please check the ${run_path}/check.log file."
return 1
fi
;;
v2ray)
export V2RAY_LOCATION_ASSET="${box_path}/${bin_name}"
if ${bin_path} test -d ${box_path}/${bin_name} > ${run_path}/check.log 2>&1 ; then
log Info "starting ${bin_name} service."
nohup busybox setuidgid ${box_user_group} ${bin_path} run -d ${box_path}/${bin_name} > /dev/null 2> ${run_path}/error_${bin_name}.log &
echo -n $! > ${pid_file}
return 0
else
log Error "configuration check failed, please check the ${run_path}/check.log file."
return 1
fi
;;
*)
log Error "$1 core error, it must be one of ${bin_name_list[*]}"
return 2
;;
esac
}
find_netstat_path() {
[ -f /system/bin/netstat ] && alias netstat="/system/bin/netstat" && return 0
[ -f /system/xbin/netstat ] && alias netstat="/system/xbin/netstat" && return 0
return 1
}
wait_bin_listen() {
wait_count=0
bin_pid=$(busybox pidof ${bin_name})
find_netstat_path && \
check_bin_cmd="netstat -tnulp | grep -q ${bin_name}" || \
check_bin_cmd="ls -lh /proc/${bin_pid}/fd | grep -q socket"
while [ ${bin_pid} ] && ! eval "${check_bin_cmd}" && [ ${wait_count} -lt 100 ] ; do
sleep 1 ; wait_count=$((${wait_count} + 1))
done
if [ ${bin_pid} ] && eval "${check_bin_cmd}" ; then
return 0
else
return 1
fi
}
display_bin_status() {
if bin_pid=$(busybox pidof ${bin_name}) ; then
log Info "${bin_name} has started with the $(stat -c %U:%G /proc/${bin_pid}) user group."
log Info "${bin_name} service is running. ( PID: ${bin_pid} )"
log Info "${bin_name} memory usage: $(cat /proc/${bin_pid}/status | grep -w VmRSS | awk '{print $2$3}')"
log Info "${bin_name} cpu usage: $((/system/bin/ps -eo %CPU,NAME | grep ${bin_name} | awk '{print $1"%"}') 2> /dev/null || dumpsys cpuinfo | grep ${bin_name} | awk '{print $1}')"
log Info "${bin_name} running time: $(busybox ps -o comm,etime | grep ${bin_name} | awk '{print $2}')"
echo -n ${bin_pid} > ${pid_file}
return 0
else
log Warn "${bin_name} service is stopped."
return 1
fi
}
start_service() {
if check_permission ; then
log Info "${bin_name} will be started with the ${box_user_group} user group."
[ "${proxy_method}" != "TPROXY" ] && create_tun_link
if start_bin && wait_bin_listen ; then
log Info "${bin_name} service is running. ( PID: $(cat ${pid_file}) )"
probe_tun_device && forward -I
return 0
else
if bin_pid=$(pidof ${bin_name}) ; then
log Warn "${bin_name} service is running but may not listening. ( PID: ${bin_pid} )"
probe_tun_device && forward -I
return 0
else
log Error "start ${bin_name} service failed, please check the ${run_path}/error_${bin_name}.log file."
rm -f ${pid_file} >> /dev/null 2>&1
return 1
fi
fi
else
log Error "missing ${bin_name} core, please download and place it in the ${box_path}/bin/ directory"
return 2
fi
}
stop_service() {
if display_bin_status ; then
log Warn "stopping ${bin_name} service."
kill $(cat ${pid_file}) || killall ${bin_name}
forward -D >> /dev/null 2>&1
sleep 1
display_bin_status
fi
rm -f ${pid_file} >> /dev/null 2>&1
}
case "$1" in
start)
display_bin_status || start_service
;;
stop)
stop_service
;;
restart)
stop_service
sleep 2
start_service
;;
status)
display_bin_status
;;
*)
log Error "$0 $1 usage: $0 {start|stop|restart|status}"
;;
esac