update zerotier module to support rootless app
This commit is contained in:
+52
-82
@@ -2,96 +2,66 @@
|
||||
|
||||
MODDIR=${0%/*}
|
||||
|
||||
ZTROOT=/data/adb/zerotier
|
||||
ZTRUNTIME=$ZTROOT/run
|
||||
APPROOT=/data/data/com.eventlowop.zerotier_magisk_app/files
|
||||
# load variables
|
||||
. $MODDIR/vars.sh
|
||||
|
||||
pipe=$ZTRUNTIME/pipe
|
||||
ZTLOG=$ZTRUNTIME/zerotier.log
|
||||
daemon_log=$ZTRUNTIME/daemon.log
|
||||
# wait_until_login from yc9559/uperf at uperf/magisk/script/libcommon.sh
|
||||
wait_until_login() {
|
||||
# in case of /data encryption is disabled
|
||||
while [ "$(getprop sys.boot_completed)" != "1" ]; do
|
||||
sleep 1
|
||||
done
|
||||
|
||||
cli_output=$ZTRUNTIME/cli.out
|
||||
cli_pid=$ZTRUNTIME/cli.pid
|
||||
|
||||
log() {
|
||||
t=`date +"%m-%d %H:%M:%S.%3N"`
|
||||
echo -e "[$t][$$][L] $1" >> $daemon_log
|
||||
echo -e "$1" >> $cli_output
|
||||
}
|
||||
log_cli() {
|
||||
echo -e "$1" >> $cli_output
|
||||
}
|
||||
_stop() {
|
||||
pid=`pidof zerotier-one`
|
||||
if [[ $? -ne 0 ]]; then
|
||||
log "zerotier-one not running"
|
||||
return
|
||||
fi
|
||||
|
||||
kill -9 $pid
|
||||
if [[ $? -ne 0 ]]; then
|
||||
log "kill zerotier-one failed"
|
||||
return
|
||||
fi
|
||||
|
||||
wait
|
||||
log "stopped zerotier-one"
|
||||
}
|
||||
__start() {
|
||||
$ZTROOT/zerotier-one -d >> $ZTLOG 2>&1 &
|
||||
}
|
||||
_start() {
|
||||
if pid=`pidof zerotier-one`; then
|
||||
log "zerotier-one already running pid $pid"
|
||||
else
|
||||
__start
|
||||
pid=`pidof zerotier-one`
|
||||
log "started zerotier-one pid $pid"
|
||||
fi
|
||||
}
|
||||
_status() {
|
||||
# fake systemd lol
|
||||
if pid=`pidof zerotier-one`; then
|
||||
log_cli "\033[32m●\033[0m zerotier-one.service - ZeroTier One - Global Area Networking"
|
||||
log_cli " Active: \033[32mactive (running)\033[0m"
|
||||
log_cli " Main PID: $pid (zerotier-one)"
|
||||
else
|
||||
pid_=`cat $zerotier_root/home/zerotier-one.pid`
|
||||
log_cli "○ zerotier-one.service - ZeroTier One - Global Area Networking"
|
||||
log_cli " Active: inactive (dead)"
|
||||
log_cli " Main PID: $pid_ (code=exited)"
|
||||
fi
|
||||
# we doesn't have the permission to rw "/sdcard" before the user unlocks the screen
|
||||
local test_file="/sdcard/Android/.PERMISSION_TEST"
|
||||
true >"$test_file"
|
||||
while [ ! -f "$test_file" ]; do
|
||||
true >"$test_file"
|
||||
sleep 1
|
||||
done
|
||||
rm "$test_file"
|
||||
}
|
||||
|
||||
cd $ZTROOT
|
||||
rm -f $ZTRUNTIME
|
||||
mkfifo $pipe
|
||||
# ----------------------------------------------
|
||||
# clean before start
|
||||
# ----------------------------------------------
|
||||
|
||||
chmod 666 $pipe $cli_output $cli_pid
|
||||
rm -rf $ZTROOT/run
|
||||
mkdir -p $ZTROOT/run
|
||||
|
||||
# ----------------------------------------------
|
||||
# start zerotier
|
||||
# ----------------------------------------------
|
||||
|
||||
# add main route table to lookup rules
|
||||
ip rule add from all lookup main pref 1
|
||||
export LD_LIBRARY_PATH=/system/lib64:/data/adb/zerotier/lib
|
||||
ip -6 rule add from all lookup main pref 1
|
||||
|
||||
if [[ -e /data/data/com.eventlowop.zerotier_magisk_app/files ]]; then
|
||||
ln -sf $pipe $APPROOT/pipe
|
||||
ln -sf $cli_output $APPROOT/cli.out
|
||||
ln -sf $cli_pid $APPROOT/cli.pid
|
||||
fi
|
||||
# LD_LIBRARY_PATH for NDK
|
||||
export LD_LIBRARY_PATH=$ZTROOT/lib
|
||||
|
||||
__start
|
||||
# start zerotier
|
||||
$ZTROOT/zerotier-one -d >> $ZT_LOG 2>&1 &
|
||||
|
||||
while true
|
||||
do
|
||||
if read cmd < $pipe; then
|
||||
case "$cmd" in
|
||||
"start") _start;;
|
||||
"stop") _stop;;
|
||||
"restart") _stop; _start;;
|
||||
"status") _status;;
|
||||
*) log "unknown command $cmd";;
|
||||
esac
|
||||
# ----------------------------------------------
|
||||
# CLI before login
|
||||
# ----------------------------------------------
|
||||
|
||||
cpid=`cat $cli_pid`
|
||||
kill -SIGUSR1 $cpid;
|
||||
fi
|
||||
done
|
||||
touch $PIPE_CLI
|
||||
inotifyd $MODDIR/handle.sh $PIPE_CLI:w &>/dev/null & # toybox inotifyd bug: needs an extra character after colon
|
||||
|
||||
# ----------------------------------------------
|
||||
# APP after login
|
||||
# ----------------------------------------------
|
||||
|
||||
wait_until_login
|
||||
|
||||
if [[ -d "$APPROOT" ]]; then
|
||||
rm -rf $APPROOT/run
|
||||
mkdir -p $APPROOT/run
|
||||
|
||||
cp $ZTROOT/home/authtoken.secret $APPROOT/run/authtoken
|
||||
touch $PIPE_APP
|
||||
chmod 666 $APPROOT/run/authtoken $PIPE_APP
|
||||
inotifyd $MODDIR/handle.sh $PIPE_APP:w &>/dev/null &
|
||||
fi
|
||||
Reference in New Issue
Block a user