forked from powerAn2020/ZeroTierForKSU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zerotier.sh
210 lines (200 loc) · 5.27 KB
/
zerotier.sh
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
#!/system/bin/sh
###
### ZeroTier for KSU - zerotier.sh
###
### Usage:
### zerotier.sh options
###
### Options:
### -h -- Show this message.
### start -- Start Zerotier Service
### restart -- Retart Zerotier Service
### stop -- Stop Zerotier Service
### status -- Show Node Status
### token -- Show Local Service Token
### apiToken -- Show Remote Service apiToken
### inotifyd -- Start inotifyd Service
### switch [ main,dev ] -- Switch module version
###
### Example:
### help
### sh zerotier.sh -h
### sh zerotier.sh start
### sh zerotier.sh restart
### sh zerotier.sh stop
### sh zerotier.sh status
### sh zerotier.sh token
### sh zerotier.sh apiToken
### sh zerotier.sh inotifyd
### sh zerotier.sh switch dev
###
MODDIR=${0%/*}
if [ -f "/data/adb/ksu/bin/busybox" ]; then
# busybox KSU
busybox="/data/adb/ksu/bin/busybox"
elif [ -f "/data/adb/ap/bin/busybox" ]; then
# busybox APatch
busybox="/data/adb/ap/bin/busybox"
else
# busybox Magisk
busybox="/data/adb/magisk/busybox"
fi
ZTPATH=/data/adb/zerotier
MANUAL=${ZTPATH}/MANUAL
ALLOW_9993=${ZTPATH}/ALLOW_9993
KEEP_ON_UNINSTALL=${ZTPATH}/KEEP_ON_UNINSTALL
ROUTER_RULE_NEW=${ZTPATH}/ROUTER_RULE_NEW
PIDFILE=$ZTPATH/zerotier-one.pid
ZEROTIERD=$MODDIR/zerotier-one
SECRETFILE=$ZTPATH/authtoken.secret
current_time=$(date "%I:%M %P")
start_inotifyd() {
PIDs=$(pgrep -f inotifyd)
for PID in "${PIDs[@]}"; do
if grep -q "zerotier.inotify" "/proc/$PID/cmdline"; then
kill -9 "$PID"
fi
done
echo "inotifyd ${ZTPATH}/state"
sed -Ei "s/^description=(\[.*][[:space:]]*)?/description=[ $current_time | inotifyd is running ] /g" $MODDIR/module.prop
inotifyd "${MODDIR}/zerotier.inotify" "${ZTPATH}/state" >>/dev/null 2>&1 &
}
stop_service() {
zpid=$(pgrep -f "zerotier-one")
if [ -z $zpid ]; then
echo "service is stop"
else
kill $zpid
fi
if [ ! -f "${ALLOW_9993}" ]; then
# set firewall
sh ${MODDIR}/api.sh local firewall D
fi
sed -Ei "s/^description=(\[.*][[:space:]]*)?/description=[ $current_time | ❌ service is stop ] /g" $MODDIR/module.prop
if [ ! -f ${ROUTER_RULE_NEW} ]; then
sh ${MODDIR}/api.sh local router main del
fi
# /data/adb/ksu/bin/ksud module disable ZeroTierForKSU
echo done.
}
status_service() {
zpid=$(pgrep -f "zerotier-one")
uninstallKeep=true
if [ ! -f "${KEEP_ON_UNINSTALL}" ]; then
uninstallKeep=false
fi
autoStart=false
if [ ! -f "${MANUAL}" ]; then
autoStart=true
fi
routerRuleNew=1
if [ -f ${ROUTER_RULE_NEW} ]; then
routerRuleNew=0
fi
firewall=true
if [ ! -f "${ALLOW_9993}" ]; then
firewall=false
fi
cliStatus=$(sh ${MODDIR}/zerotier-cli status)
if [ $? != 0 ]; then
cliStatus=''
fi
apiToken=$(get_apiToken)
if [ $? != 0 ]; then
apiToken=''
fi
branch=$(get_branch)
if [ $? != 0 ]; then
branch=''
fi
data='{ "enable": "'${zpid}'","branch": "'${branch}'", "firewall": '${firewall}', "autoStart": '${autoStart}', "apiToken": "'${apiToken}'", "uninstallKeep": '${uninstallKeep}',"routerRuleNew": '${routerRuleNew}', "cliStatus": "'${cliStatus}'" }'
echo $data
}
start_service() {
zpid=$(pgrep -f "zerotier-one")
if [ -z $zpid ]; then
if [ -f "${ALLOW_9993}" ]; then
# set firewall
sh ${MODDIR}/api.sh local firewall A
fi
# Start ZEROTIERD
echo "starting $ZEROTIERD... \c"
if [ -f ${ZTPATH}/state/disable ]; then
rm ${ZTPATH}/state/disable
fi
$ZEROTIERD -d $ZTPATH 2>&1 >$ZTPATH/error.log
zt_rc=$?
if [ $zt_rc -ne 0 ]; then
echo "$0: Error ${zt_rc} starting ${ZEROTIERD}... bailing."
exit $zt_rc
fi
sed -Ei "s/^description=(\[.*][[:space:]]*)?/description=[ $current_time | ✔︎ service is running ] /g" $MODDIR/module.prop
if [ ! -f ${ROUTER_RULE_NEW} ]; then
sh ${MODDIR}/api.sh local router main add
fi
else
echo "service is running,pid:$zpid"
fi
# /data/adb/ksu/bin/ksud module enable ZeroTierForKSU
echo done.
}
get_token() {
echo "$(cat ${SECRETFILE})"
}
get_apiToken() {
apiToken=$(grep -v '^[[:space:]]*$' $ZTPATH/TOKENAUTH)
if [ -f $ZTPATH/TOKENAUTH -a -n "$apiToken" ]; then
echo $apiToken
else
{
echo "The api token was not found. Use 'api.sh update xxxx' to add it." 1>&2
exit 1
}
fi
}
switch_brach(){
version=$1
arch=$(sed -n '/arch=/s/arch=\(.*\)/\1/p' ${MODDIR}/module.prop)
if [ "$version" = "main" ];then
sed -i -E "s/dev\/update_${arch}_dev/main\/update_${arch}/g" ${MODDIR}/module.prop
else
sed -i -E "s/main\/update_${arch}/dev\/update_${arch}_dev/g" ${MODDIR}/module.prop
fi
}
get_branch(){
sed -n '/updateJson/{s/.*ZeroTierOneForKSU\/\([^/]*\).*/\1/p}' $MODDIR/module.prop
}
help() {
sed -rn 's/^### ?//;T;p;' "$0"
}
case $1 in
start)
start_service
;;
stop)
stop_service
;;
restart)
stop_service
start_service
;;
status)
status_service
;;
token)
get_token
;;
apiToken)
get_apiToken
;;
inotifyd)
start_inotifyd
;;
switch)
shift
switch_brach $1
;;
*)
help
;;
esac