#!/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, kit version, # mihomo API port/secret (dashboard link) # 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 # update_check latest kit commit on the git server vs the installed one # config_get settings for the form (secrets: only whether they are set) # config_set save the form (via "mihomo-kit.sh configure") as the job # # 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 # rpcd runs with umask 077; jobs (kit update) install web files that must stay readable umask 022 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 .env envval() { cat "$PROFILES_D/common.env" "$PROFILES_D/$2.env" 2>/dev/null | sed -n "s/^$1=//p" | tail -n 1 | tr -d "'\"" } q() { printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")"; } 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_add_string kit_version "$(cat "$NIKKI_DIR/kit/VERSION" 2>/dev/null)" l=$(uci -q get nikki.mixin.api_listen) port=${l##*:} case "$port" in '' | *[!0-9]*) port=9090 ;; esac json_add_int api_port "$port" json_add_string api_secret "$(uci -q get nikki.mixin.api_secret)" 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 start_job "$@" } # start_job cmd args...: run it in the background, reply at once start_job() { 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_config_get() { [ -f "$PROFILES_D/common.env" ] && . "$PROFILES_D/common.env" [ -f "$NIKKI_DIR/bmstu.env" ] && . "$NIKKI_DIR/bmstu.env" [ -f /etc/openwrt_release ] && . /etc/openwrt_release json_init json_add_boolean configured "$(b test -f "$PROFILES_D/common.env")" link= [ "${BOOT:-0}" = 1 ] && link="vless://$BOOT_UUID@$BOOT_SERVER:$BOOT_PORT?security=reality&sni=$BOOT_SNI&pbk=$BOOT_PBK&sid=$BOOT_SID" json_add_string boot_link "$link" json_add_array subs i=0 # " "; keys SUB__*, else the old SUB_* SUBS=${SUBS:-} mihomo-kit.sh sub-ids > /tmp/mihomo-kit-ids.$$ while read -r id w; do K=$(echo "$id" | tr 'a-z' 'A-Z') eval "p=\${SUB_${K}_PROXY:-\${SUB${i}_PROXY:-}} h=\${SUB_${K}_HWID:-\${SUB${i}_HWID:-0}}" [ -n "$p" ] || { [ "${BOOT:-0}" = 1 ] && p=BOOT || p=DIRECT; } json_add_object "" json_add_string id "$id" json_add_string url "$w" json_add_string via "$p" json_add_boolean hwid "$h" json_close_object i=$((i + 1)) done < /tmp/mihomo-kit-ids.$$ rm -f /tmp/mihomo-kit-ids.$$ json_close_array # HWID defaults: the same as the terminal wizard suggests json_add_string hwid_ua "${HWID_UA:-v2rayN/9.99}" json_add_string hwid_os "${HWID_OS:-OpenWrt}" json_add_string hwid_os_ver "${HWID_OS_VER:-${DISTRIB_RELEASE:-}}" json_add_string hwid_model "${HWID_MODEL:-$(cat /tmp/sysinfo/model 2>/dev/null)}" json_add_string hwid_id "${HWID_ID:-$(md5sum < /sys/class/net/br-lan/address 2>/dev/null | cut -c1-16)}" json_add_boolean torrent "${TORRENT:-1}" json_add_string ru_ips "${RU_IPS:-}" json_add_boolean openvpn "${OPENVPN:-0}" json_add_boolean ovpn_file "$(b test -f "$NIKKI_DIR/bmstu.ovpn")" json_add_string vpn_username "${VPN_USERNAME:-}" json_add_boolean has_vpn_password "$(b test -n "${VPN_PASSWORD:-}")" json_add_boolean has_totp "$(b test -n "${TOTP_SECRET:-}")" json_add_boolean tailscale "${TAILSCALE:-0}" json_add_string ts_hostname "${TS_HOSTNAME:-$(uci -q get 'system.@system[0].hostname' | tr 'A-Z' 'a-z')-mihomo}" json_add_string ts_domains "${TS_DOMAINS:-}" json_add_array hy2 for n in ${HY2:-}; do k=HY2_$(echo "$n" | tr 'a-z-' 'A-Z_') json_add_object "" json_add_string name "$n" for v in server port password obfs_password sni insecure up down; do eval "val=\${${k}_$(echo "$v" | tr 'a-z' 'A-Z'):-}" json_add_string "$v" "$val" done json_close_object done json_close_array json_dump } # {"data": {boot_link, subs: [{url, via, hwid}], hwid_*, torrent, ru_ips, # openvpn, ovpn (file text, optional), vpn_username, vpn_password, totp_secret, # tailscale, ts_hostname, ts_domains, hy2: [{name, server, port, password, # obfs_password, sni, insecure, up, down}]}}; empty OpenVPN secrets keep the saved ones m_config_set() { json_load "$(cat)" || reply_error "bad request" json_select data || reply_error "no data" job_running && reply_error "busy: $(cat "$JOB/cmd")" for v in boot_link hwid_ua hwid_os hwid_os_ver hwid_model hwid_id torrent ru_ips openvpn ovpn \ vpn_username vpn_password totp_secret tailscale ts_hostname ts_domains; do json_get_var "$v" "$v" "" done if [ -n "$ovpn" ]; then umask 077 printf '%s\n' "$ovpn" > "$NIKKI_DIR/bmstu.ovpn.tmp" && mv "$NIKKI_DIR/bmstu.ovpn.tmp" "$NIKKI_DIR/bmstu.ovpn" fi umask 077 env=$(mktemp /tmp/mihomo-kit-form.XXXXXX) || reply_error "mktemp failed" { echo "BOOT_LINK=$(q "$boot_link")" subs= i=0 if json_select subs; then json_get_keys keys for k in $keys; do url= via= hwid= json_select "$k" json_get_vars url via hwid json_select .. url=$(printf '%s' "$url" | tr -d ' \t\r\n') [ -n "$url" ] || continue subs="${subs:+$subs }$url" echo "SUB${i}_PROXY=$(q "$via")" echo "SUB${i}_HWID=$(q "${hwid:-0}")" i=$((i + 1)) done json_select .. fi echo "SUBS=$(q "$subs")" echo "HWID_UA=$(q "$hwid_ua")" echo "HWID_OS=$(q "$hwid_os")" echo "HWID_OS_VER=$(q "$hwid_os_ver")" echo "HWID_MODEL=$(q "$hwid_model")" echo "HWID_ID=$(q "$hwid_id")" echo "TORRENT=$(q "${torrent:-0}")" echo "RU_IPS=$(q "$ru_ips")" echo "OPENVPN=$(q "${openvpn:-0}")" echo "VPN_USERNAME=$(q "$vpn_username")" [ -n "$vpn_password" ] && echo "VPN_PASSWORD=$(q "$vpn_password")" [ -n "$totp_secret" ] && echo "TOTP_SECRET=$(q "$totp_secret")" echo "TAILSCALE=$(q "${tailscale:-0}")" echo "TS_HOSTNAME=$(q "$ts_hostname")" echo "TS_DOMAINS=$(q "$ts_domains")" echo "HY2_SET=1" hy2= if json_select hy2; then json_get_keys keys for k in $keys; do json_select "$k" json_get_var name name "" name=$(printf '%s' "$name" | tr -d ' \t\r\n') if [ -n "$name" ]; then hy2="${hy2:+$hy2 }$name" K=HY2_$(echo "$name" | tr 'a-z-' 'A-Z_') for v in server port password obfs_password sni insecure up down; do json_get_var val "$v" "" echo "${K}_$(echo "$v" | tr 'a-z' 'A-Z')=$(q "$val")" done fi json_select .. done json_select .. fi echo "HY2=$(q "$hy2")" } > "$env" umask 022 start_job mihomo-kit.sh configure "$env" } m_update_check() { out=$(mihomo-kit.sh check-update 2>&1) || reply_error "$(echo "$out" | tail -n 1)" json_init echo "$out" > /tmp/mihomo-kit-upd.$$ while read -r k v; do json_add_string "$k" "$v" done < /tmp/mihomo-kit-upd.$$ rm -f /tmp/mihomo-kit-upd.$$ 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"},"config_get":{},"config_set":{"data":{}},"update_check":{}}' ;; call) case "$2" in status) m_status ;; ovpn_status) m_ovpn_status ;; job) m_job ;; run) m_run ;; config_get) m_config_get ;; config_set) m_config_set ;; update_check) m_update_check ;; *) reply_error "unknown method" ;; esac ;; esac