mihomo-kit/luci/rpcd.sh
2026-10-09 17:02:44 +03:00

127 lines
3.6 KiB
Bash

#!/bin/sh
# rpcd plugin "mihomo-kit" for the LuCI page (installed as /usr/libexec/rpcd/mihomo-kit).
#
# status profile, nikki running, which features are on
# ovpn_status OpenVPN state (up/down/off) + current OTP; slow (a delay test)
# run start a whitelisted command as the background job
# job the job's command, state, exit code and output
#
# rpcd kills calls after ~30 s; use/update/ts-login take longer, hence the job.
. /usr/share/libubox/jshn.sh
export PATH=/usr/sbin:/usr/bin:/sbin:/bin
NIKKI_DIR=/etc/nikki
PROFILES_D=$NIKKI_DIR/profiles.d
JOB=/tmp/mihomo-kit-job
# 1 if the command succeeds, else 0 (for json_add_boolean)
b() { "$@" >/dev/null 2>&1 && echo 1 || echo 0; }
# envval VAR profile -> value from common.env, overridden by <profile>.env
envval() {
cat "$PROFILES_D/common.env" "$PROFILES_D/$2.env" 2>/dev/null |
sed -n "s/^$1=//p" | tail -n 1 | tr -d "'\""
}
job_running() { [ -f "$JOB/pid" ] && kill -0 "$(cat "$JOB/pid")" 2>/dev/null; }
reply_error() {
json_init
json_add_string error "$1"
json_dump
exit 0
}
m_status() {
prof=$(uci -q get nikki.config.profile)
prof=${prof#file:}
prof=${prof%.yaml}
json_init
json_add_string profile "$prof"
json_add_boolean running "$(b pidof mihomo)"
json_add_boolean configured "$(b test -f "$PROFILES_D/common.env")"
json_add_array profiles
for f in "$NIKKI_DIR"/profiles/*.yaml; do
[ -f "$f" ] || continue
n=${f##*/}
json_add_string "" "${n%.yaml}"
done
json_close_array
json_add_boolean openvpn "$(b test "$(envval OPENVPN "$prof")" = 1)"
json_add_boolean tailscale "$(b test "$(envval TAILSCALE "$prof")" = 1)"
json_add_boolean ts_logged_in "$(b grep -q _current-profile "$NIKKI_DIR/run/tailscale/tailscaled.state")"
json_dump
}
m_ovpn_status() {
json_init
if [ -f "$NIKKI_DIR/bmstu.off" ]; then
json_add_string state off
elif CHECK_TIMEOUT=8000 bmstu-otp.sh status 2>/dev/null | grep -q "tunnel is up"; then
json_add_string state up
else
json_add_string state down
fi
json_add_string otp "$(bmstu-otp.sh otp 2>/dev/null)"
json_add_int otp_left "$((30 - $(date +%s) % 30))"
json_dump
}
m_run() {
input=$(cat)
cmd=$(jsonfilter -s "$input" -e '@.cmd' 2>/dev/null)
arg=$(jsonfilter -s "$input" -e '@.arg' 2>/dev/null)
case "$cmd" in
ovpn-on) set -- bmstu-otp.sh on ;;
ovpn-off) set -- bmstu-otp.sh off ;;
ovpn-refresh) set -- bmstu-otp.sh refresh ;;
use)
case "$arg" in '' | *[!A-Za-z0-9_-]*) reply_error "bad profile name" ;; esac
[ -f "$NIKKI_DIR/profiles/$arg.yaml" ] || reply_error "no profile $arg"
set -- mihomo-kit.sh use "$arg"
;;
ts-login) set -- mihomo-kit.sh ts-login ;;
ts-relogin) set -- mihomo-kit.sh ts-login -f ;;
build | update | fix) set -- mihomo-kit.sh "$cmd" ;;
*) reply_error "unknown command: $cmd" ;;
esac
job_running && reply_error "busy: $(cat "$JOB/cmd")"
rm -rf "$JOB"
mkdir -p "$JOB"
echo "$*" > "$JOB/cmd"
# detached from rpcd: all fds redirected, so the call returns at once
(
"$@"
echo $? > "$JOB/rc"
) < /dev/null > "$JOB/log" 2>&1 &
echo $! > "$JOB/pid"
json_init
json_add_string started "$*"
json_dump
}
m_job() {
json_init
json_add_string cmd "$(cat "$JOB/cmd" 2>/dev/null)"
json_add_boolean running "$(b job_running)"
[ -f "$JOB/rc" ] && json_add_int rc "$(cat "$JOB/rc")"
json_add_string log "$(tail -c 20000 "$JOB/log" 2>/dev/null)"
json_dump
}
case "$1" in
list)
echo '{"status":{},"ovpn_status":{},"job":{},"run":{"cmd":"str","arg":"str"}}'
;;
call)
case "$2" in
status) m_status ;;
ovpn_status) m_ovpn_status ;;
job) m_job ;;
run) m_run ;;
*) reply_error "unknown method" ;;
esac
;;
esac