feat: sync-toggle, журнал сервера, systemd runtime

- ClientConfig.syncServerSwitches: разделить клиентские флаги VLESS/bond/WRAP от серверного процесса. При выкл сервер не рестартится при тогле.
- ServerManagementScreen: журнал через journalctl/server.log, refresh + clear, баннер sync-off с AnimatedVisibility.
- vk-turn-control.sh: dual runtime systemd/nohup, авто-генерация unit + launcher, миграция legacy nohup при takeover, journalctl для логов.
- MD3: cardElevation 4dp -> 1dp в SshSetupScreen, magic 14dp -> shapes.medium в EasterEgg.
This commit is contained in:
samosvalishe committed 2026-05-09 11:46:31 +03:00
1 parent 46e72217a6
commit 4615edee66
12 files changed
+502 -90

No files matched your search

+285 -47
View File
@@ -2,6 +2,11 @@
# vk-turn-proxy server control script.
# Streamed via SSH stdin from Android (bash -s -- <subcmd> <args>).
# Output protocol: KEY=VALUE lines, "LOG: ..." for free text, final RESULT=ok|err.
#
# Runtime modes:
# systemd — unit /etc/systemd/system/vk-turn-proxy.service + launch.sh + run.args
# nohup — legacy; nohup + PIDFILE (для машин без systemd)
# Маркер режима — $PREFIX/runtime (одна строка: systemd|nohup).
set -eu
@@ -10,11 +15,16 @@ PIDFILE="$PREFIX/proxy.pid"
WRAPFILE="$PREFIX/wrap.key"
LOGFILE="$PREFIX/server.log"
VERFILE="$PREFIX/version"
RUNTIMEFILE="$PREFIX/runtime"
ARGSFILE="$PREFIX/run.args"
LAUNCHER="$PREFIX/launch.sh"
UNIT_PATH="/etc/systemd/system/vk-turn-proxy.service"
UNIT_NAME="vk-turn-proxy.service"
BASE_URL="https://github.com/Moroka8/vk-turn-proxy/releases/latest/download"
log() { echo "LOG: $*"; }
emit() { echo "$1=$2"; }
die() { echo "ERR=$*"; echo "RESULT=err"; exit 1; }
die() { echo "ERR=$*"; echo "RESULT=err"; trap - EXIT; exit 1; }
trap 'rc=$?; if [ $rc -ne 0 ]; then echo "ERR=script exit $rc"; echo "RESULT=err"; fi' EXIT
@@ -30,7 +40,24 @@ detect_arch() {
binpath() { echo "$PREFIX/$(detect_arch)"; }
is_running() {
# Доступен ли systemd на этой машине.
has_systemd() {
command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ]
}
current_runtime() {
if [ -f "$RUNTIMEFILE" ]; then
cat "$RUNTIMEFILE"
elif has_systemd; then
echo "systemd"
else
echo "nohup"
fi
}
# --- состояние ---
is_running_nohup() {
if [ -f "$PIDFILE" ]; then
local pid
pid=$(cat "$PIDFILE" 2>/dev/null || echo "")
@@ -45,6 +72,33 @@ is_running() {
return 1
}
is_running_systemd() {
systemctl is-active --quiet "$UNIT_NAME"
}
# echo текущий PID процесса (или пусто).
current_pid() {
case "$(current_runtime)" in
systemd)
local p
p=$(systemctl show -p MainPID --value "$UNIT_NAME" 2>/dev/null || echo 0)
[ "$p" != "0" ] && [ -n "$p" ] && echo "$p"
;;
nohup)
[ -f "$PIDFILE" ] && cat "$PIDFILE" 2>/dev/null
;;
esac
}
# Возвращает cmdline текущего процесса (или пусто).
current_cmdline() {
local p
p=$(current_pid)
if [ -n "$p" ] && [ -r "/proc/$p/cmdline" ]; then
tr '\0' ' ' < "/proc/$p/cmdline"
fi
}
cmd_probe() {
local bin
bin=$(binpath)
@@ -59,10 +113,18 @@ cmd_probe() {
else
emit INSTALLED no
fi
if is_running; then
emit RUNTIME "$(current_runtime)"
local running=no
case "$(current_runtime)" in
systemd) is_running_systemd && running=yes ;;
nohup) is_running_nohup && running=yes ;;
esac
if [ "$running" = "yes" ]; then
emit RUNNING yes
local cmdline
cmdline=$(pgrep -af "^$PREFIX/server-linux-" | head -n1 || true)
cmdline=$(current_cmdline)
if echo "$cmdline" | grep -q -- "-vless"; then emit VLESS yes; else emit VLESS no; fi
if echo "$cmdline" | grep -q -- "-vless-bond"; then emit VLESS_BOND yes; else emit VLESS_BOND no; fi
if echo "$cmdline" | grep -q -- "-wrap"; then emit WRAP yes; else emit WRAP no; fi
@@ -96,6 +158,80 @@ _resolve_version() {
echo "$loc" | sed -nE 's#.*/releases/download/([^/]+)/.*#\1#p'
}
# Записать systemd unit и launcher. Идемпотентно — сравниваем содержимое,
# чтобы не дёргать daemon-reload зря.
_install_systemd_unit() {
local need_reload=0
local launcher_content
launcher_content=$(cat <<'LAUNCH_EOF'
#!/bin/bash
# Авто-сгенерирован vk-turn-control.sh — не редактировать вручную.
set -e
PREFIX=/opt/vk-turn
m=$(uname -m)
case "$m" in
x86_64|amd64) arch=server-linux-amd64 ;;
aarch64|arm64) arch=server-linux-arm64 ;;
*) echo "unsupported arch: $m" >&2; exit 1 ;;
esac
[ -f "$PREFIX/run.args" ] || { echo "run.args missing" >&2; exit 1; }
mapfile -t a < "$PREFIX/run.args"
exec "$PREFIX/$arch" "${a[@]}"
LAUNCH_EOF
)
if [ ! -f "$LAUNCHER" ] || [ "$(cat "$LAUNCHER" 2>/dev/null)" != "$launcher_content" ]; then
printf '%s\n' "$launcher_content" > "$LAUNCHER"
chmod 755 "$LAUNCHER"
fi
local unit_content
unit_content=$(cat <<UNIT_EOF
[Unit]
Description=vk-turn-proxy server
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=$LAUNCHER
Restart=on-failure
RestartSec=2
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
UNIT_EOF
)
if [ ! -f "$UNIT_PATH" ] || [ "$(cat "$UNIT_PATH" 2>/dev/null)" != "$unit_content" ]; then
printf '%s\n' "$unit_content" > "$UNIT_PATH"
chmod 644 "$UNIT_PATH"
need_reload=1
fi
if [ "$need_reload" = "1" ]; then
systemctl daemon-reload
fi
systemctl enable "$UNIT_NAME" >/dev/null 2>&1 || true
}
# Завершить процесс старого nohup-режима, если он остался от прошлой версии.
_kill_legacy_nohup() {
if [ -f "$PIDFILE" ]; then
local pid
pid=$(cat "$PIDFILE" 2>/dev/null || echo "")
if [ -n "$pid" ]; then
kill "$pid" 2>/dev/null || true
sleep 1
kill -9 "$pid" 2>/dev/null || true
fi
rm -f "$PIDFILE"
fi
pkill -9 -f "^$PREFIX/server-linux-" 2>/dev/null || true
}
cmd_install() {
mkdir -p "$PREFIX" || die "cannot create $PREFIX"
[ -w "$PREFIX" ] || die "$PREFIX not writable"
@@ -113,39 +249,63 @@ cmd_install() {
curver=""
[ -f "$VERFILE" ] && curver=$(cat "$VERFILE")
local cached=0
if [ -x "$bin" ] && [ "$ver" = "$curver" ]; then
cached=1
fi
if [ "$cached" = "0" ]; then
log "downloading $name @ $ver"
if ! _dl "$asset_url" "$tmp"; then
rm -f "$tmp"
die "binary download failed"
fi
# sanity-check: GitHub отдаёт HTML-404 при отсутствии ассета.
local size
size=$(wc -c < "$tmp" 2>/dev/null || echo 0)
if [ "$size" -lt 100000 ]; then
rm -f "$tmp"
die "downloaded file too small ($size bytes)"
fi
chmod +x "$tmp"
if [ -f "$bin" ]; then
cp -f "$bin" "$bin.bak" 2>/dev/null || true
fi
mv -f "$tmp" "$bin"
echo "$ver" > "$VERFILE"
fi
# Решаем режим запуска и подготавливаем инфраструктуру.
local was_running=0
case "$(current_runtime)" in
systemd) is_running_systemd && was_running=1 ;;
nohup) is_running_nohup && was_running=1 ;;
esac
if has_systemd; then
# Любой остаточный nohup-процесс убиваем перед enable юнита, иначе он
# держит порт и systemctl start не поднимется. Триггеримся на PIDFILE
# ИЛИ висящий бинарь — RUNTIMEFILE на свежей машине ещё не показателен.
if [ -f "$PIDFILE" ] || pgrep -f "^$PREFIX/server-linux-" >/dev/null 2>&1; then
log "killing legacy nohup process before systemd takeover"
_kill_legacy_nohup
fi
_install_systemd_unit
echo "systemd" > "$RUNTIMEFILE"
emit RUNTIME systemd
else
echo "nohup" > "$RUNTIMEFILE"
emit RUNTIME nohup
fi
if [ "$cached" = "1" ]; then
emit STAGE cached
emit BIN "$name"
emit VERSION "$ver"
echo "RESULT=ok"
trap - EXIT
return 0
else
emit STAGE downloaded
fi
log "downloading $name @ $ver"
if ! _dl "$asset_url" "$tmp"; then
rm -f "$tmp"
die "binary download failed"
fi
# sanity-check: GitHub отдаёт HTML-404 при отсутствии ассета.
local size
size=$(wc -c < "$tmp" 2>/dev/null || echo 0)
if [ "$size" -lt 100000 ]; then
rm -f "$tmp"
die "downloaded file too small ($size bytes)"
fi
chmod +x "$tmp"
if [ -f "$bin" ]; then
cp -f "$bin" "$bin.bak" 2>/dev/null || true
fi
mv -f "$tmp" "$bin"
echo "$ver" > "$VERFILE"
emit STAGE downloaded
emit BIN "$name"
emit VERSION "$ver"
if is_running; then
if [ "$was_running" = "1" ] && [ "$cached" = "0" ]; then
emit NEEDS_RESTART yes
fi
echo "RESULT=ok"
@@ -187,17 +347,66 @@ parse_args() {
done
}
cmd_start() {
parse_args "$@"
[ -n "$ARG_LISTEN" ] || die "--listen required"
[ -n "$ARG_CONNECT" ] || die "--connect required"
# Записать аргументы в run.args (по одному на строку, mode 600).
_write_args_file() {
local tmp="$ARGSFILE.tmp"
: > "$tmp"
chmod 600 "$tmp"
{
echo "-listen"
echo "$ARG_LISTEN"
echo "-connect"
echo "$ARG_CONNECT"
[ -n "$ARG_VLESS" ] && echo "-vless"
[ -n "$ARG_VLESS_BOND" ] && echo "-vless-bond"
if [ -n "$ARG_WRAP_KEY" ]; then
echo "-wrap"
echo "-wrap-key"
echo "$ARG_WRAP_KEY"
fi
} >> "$tmp"
mv -f "$tmp" "$ARGSFILE"
chmod 600 "$ARGSFILE"
}
cmd_start_systemd() {
local bin
bin=$(binpath)
[ -x "$bin" ] || die "server binary not installed; run install first"
if is_running; then
_write_args_file
if ! systemctl restart "$UNIT_NAME"; then
die "systemctl restart failed"
fi
sleep 1
if ! is_running_systemd; then
if [ -f "$bin.bak" ]; then
log "process not active; rolling back .bak"
mv -f "$bin.bak" "$bin"
systemctl restart "$UNIT_NAME" || true
sleep 1
is_running_systemd || die "server failed to start (after rollback); see journalctl -u $UNIT_NAME"
else
die "server failed to start; see journalctl -u $UNIT_NAME"
fi
fi
rm -f "$bin.bak"
local pid
pid=$(current_pid)
[ -n "$pid" ] && emit PID "$pid"
echo "RESULT=ok"
trap - EXIT
}
cmd_start_nohup() {
local bin
bin=$(binpath)
[ -x "$bin" ] || die "server binary not installed; run install first"
if is_running_nohup; then
log "already running, stopping first"
cmd_stop_inner
_stop_nohup
fi
cd "$PREFIX"
@@ -223,15 +432,24 @@ cmd_start() {
rm -f "$PIDFILE"
die "server failed to start; see logs"
fi
# Старт удался — резервная копия больше не нужна, чтобы не копились тушки
# после нескольких циклов update.
rm -f "$bin.bak"
emit PID "$pid"
echo "RESULT=ok"
trap - EXIT
}
cmd_stop_inner() {
cmd_start() {
parse_args "$@"
[ -n "$ARG_LISTEN" ] || die "--listen required"
[ -n "$ARG_CONNECT" ] || die "--connect required"
case "$(current_runtime)" in
systemd) cmd_start_systemd ;;
nohup) cmd_start_nohup ;;
*) die "unknown runtime" ;;
esac
}
_stop_nohup() {
if [ -f "$PIDFILE" ]; then
local pid
pid=$(cat "$PIDFILE" 2>/dev/null || echo "")
@@ -246,8 +464,16 @@ cmd_stop_inner() {
rm -f "$WRAPFILE"
}
_stop_systemd() {
# Не disable — autostart должен сохраняться между ручными stop/start.
systemctl stop "$UNIT_NAME" 2>/dev/null || true
}
cmd_stop() {
cmd_stop_inner
case "$(current_runtime)" in
systemd) _stop_systemd ;;
nohup) _stop_nohup ;;
esac
emit STOPPED yes
echo "RESULT=ok"
trap - EXIT
@@ -255,11 +481,23 @@ cmd_stop() {
cmd_logs() {
parse_args "$@"
if [ -f "$LOGFILE" ]; then
tail -n "$ARG_TAIL" "$LOGFILE" | sed 's/^/LOG: /'
else
log "(log empty)"
fi
case "$(current_runtime)" in
systemd)
if command -v journalctl >/dev/null 2>&1; then
journalctl -u "$UNIT_NAME" -n "$ARG_TAIL" --no-pager --output=cat 2>/dev/null \
| sed 's/^/LOG: /' || log "(journalctl read failed)"
else
log "(journalctl unavailable)"
fi
;;
nohup)
if [ -f "$LOGFILE" ]; then
tail -n "$ARG_TAIL" "$LOGFILE" | sed 's/^/LOG: /'
else
log "(log empty)"
fi
;;
esac
echo "RESULT=ok"
trap - EXIT
}