update zerotier module to support rootless app

This commit is contained in:
eventlOwOp
2024-07-02 12:25:20 +08:00
parent 7394eee0f0
commit fa2d60e1b8
11 changed files with 271 additions and 104 deletions
+1
View File
@@ -25,6 +25,7 @@ ln -sf $ZTPATH/zerotier-idtool $MODPATH$SYSBIN/zerotier-idtool
ui_print "- set file permission"
set_perm_recursive $MODPATH 0 0 0755 0644
set_perm_recursive $ZTPATH 0 0 0755 0644
set_perm $MODPATH/handle.sh 0 0 0755
set_perm $ZTPATH/zerotier.sh 0 0 0755
set_perm $ZTPATH/zerotier-one 0 0 0755
+83
View File
@@ -0,0 +1,83 @@
#!/system/bin/sh
MODDIR=${0%/*}
# load variables
. $MODDIR/vars.sh
# LD_LIBRARY_PATH for NDK
export LD_LIBRARY_PATH=/system/lib64:/data/adb/zerotier/lib
log_cli() {
echo -e "$1" >> $ZTROOT/run/cli.out
}
log() {
t=`date +"%m-%d %H:%M:%S.%3N"`
echo -e "[$t][$$][L] $@" >> $DAEMON_LOG
log_cli "$@"
}
_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
sleep 1 # sometimes it fails without sleeping
log "stopped zerotier-one"
}
_start() {
if pid=`pidof zerotier-one`; then
log "zerotier-one already running pid $pid"
else
$ZTROOT/zerotier-one -d >> $ZT_LOG 2>&1 &
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
read pid_ < $ZTROOT/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
}
# ----------------------------------------------
# call from inotifyd
# ----------------------------------------------
if [[ $# == 2 && "$1" == "w" ]]; then
read cmd < $2
rm -f $ZTROOT/run/cli.out
case "$cmd" in
"start") _start;;
"stop") _stop;;
"restart") _stop; _start;;
"status") _status;;
*) log "unknown command $cmd";;
esac
if [[ -f "$ZTROOT/run/cli.pid" ]]; then
read cpid < $ZTROOT/run/cli.pid
rm -f $ZTROOT/run/cli.pid
kill -SIGUSR1 $cpid
fi
exit 0
fi
+52 -82
View File
@@ -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
+12
View File
@@ -0,0 +1,12 @@
# variables
ZTROOT=/data/adb/zerotier
APPROOT=/data/data/com.eventlowop.zerotier_magisk_app/app_flutter
ZT_LOG=$ZTROOT/run/zerotier.log
DAEMON_LOG=$ZTROOT/run/daemon.log
PIPE_CLI=$ZTROOT/run/pipe
PIPE_APP=$APPROOT/run/pipe
BB=/data/adb/magisk/busybox
+8 -10
View File
@@ -1,26 +1,24 @@
#!/system/bin/sh
pipe=/data/adb/zerotier/run/pipe
cli_output=/data/adb/zerotier/run/cli.out
cli_pid=/data/adb/zerotier/run/cli.pid
ZTROOT=/data/adb/zerotier
PIPE=$ZTROOT/run/pipe
help_text="Usage: zerotier.sh {start|stop|restart|status}"
HELP="Usage: zerotier.sh {start|stop|restart|status}"
if [[ ! -p $pipe ]]; then
if [[ ! -f $PIPE ]]; then
echo "daemon not running"
exit 1
fi
rm -f $cli_output
echo $$ > $cli_pid
echo $$ > $ZTROOT/run/cli.pid
on_receive() {
kill -9 %%
cat $cli_output
cat $ZTROOT/run/cli.out
exit 0
}
run() {
echo $cmd > $pipe
echo $cmd > $PIPE
}
trap 'on_receive' SIGUSR1
@@ -35,7 +33,7 @@ if [[ $# -eq 1 ]]; then
*) echo "unknown command $cmd";;
esac
else
echo $help_text
echo $HELP
exit 1
fi