388 lines
15 KiB
Bash
388 lines
15 KiB
Bash
#!/bin/sh
|
||
|
||
KEY_FILE="${KEY_FILE:-/etc/owrt-remote/web.key}"
|
||
AGENT="${AGENT:-/usr/sbin/owrt-remote}"
|
||
PARAMS_FILE="${TMPDIR:-/tmp}/owrt-remote-cgi.$$"
|
||
RESULT_TITLE=""
|
||
RESULT_BODY=""
|
||
RESULT_KIND="info"
|
||
|
||
cleanup() {
|
||
rm -f "$PARAMS_FILE"
|
||
}
|
||
|
||
trap cleanup EXIT INT TERM
|
||
|
||
html_escape() {
|
||
sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g'
|
||
}
|
||
|
||
h() {
|
||
printf '%s' "$1" | html_escape
|
||
}
|
||
|
||
url_decode() {
|
||
local data
|
||
data="$(printf '%s' "$1" | sed 's/+/ /g; s/%/\\x/g')"
|
||
printf '%b' "$data"
|
||
}
|
||
|
||
parse_params() {
|
||
local data old_ifs pair key value
|
||
data="$1"
|
||
old_ifs="$IFS"
|
||
IFS='&'
|
||
for pair in $data; do
|
||
key="${pair%%=*}"
|
||
value="${pair#*=}"
|
||
[ "$pair" = "$key" ] && value=""
|
||
key="$(url_decode "$key" | tr -d '\r\n\t')"
|
||
value="$(url_decode "$value" | tr '\r\n\t' ' ')"
|
||
[ -n "$key" ] && printf '%s\t%s\n' "$key" "$value" >>"$PARAMS_FILE"
|
||
done
|
||
IFS="$old_ifs"
|
||
}
|
||
|
||
param() {
|
||
awk -F ' ' -v k="$1" '$1 == k { print $2; exit }' "$PARAMS_FILE"
|
||
}
|
||
|
||
has_param() {
|
||
awk -F ' ' -v k="$1" '$1 == k { found = 1 } END { exit(found ? 0 : 1) }' "$PARAMS_FILE"
|
||
}
|
||
|
||
read_request() {
|
||
local post_data
|
||
: >"$PARAMS_FILE"
|
||
parse_params "${QUERY_STRING:-}"
|
||
case "${CONTENT_TYPE:-}" in
|
||
multipart/form-data*) return ;;
|
||
esac
|
||
if [ "${REQUEST_METHOD:-GET}" = "POST" ] && [ -n "${CONTENT_LENGTH:-}" ]; then
|
||
post_data="$(dd bs=1 count="$CONTENT_LENGTH" 2>/dev/null)"
|
||
parse_params "$post_data"
|
||
fi
|
||
}
|
||
|
||
http_html() {
|
||
printf 'Content-Type: text/html; charset=utf-8\r\n'
|
||
printf 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0\r\n'
|
||
printf 'Pragma: no-cache\r\n'
|
||
printf 'Expires: 0\r\n\r\n'
|
||
}
|
||
|
||
http_json() {
|
||
printf 'Content-Type: application/json; charset=utf-8\r\n'
|
||
printf 'Cache-Control: no-store\r\n\r\n'
|
||
}
|
||
|
||
web_key() {
|
||
cat "$KEY_FILE" 2>/dev/null
|
||
}
|
||
|
||
authorized() {
|
||
local expected supplied
|
||
expected="$(web_key)"
|
||
supplied="$(param key)"
|
||
[ -n "$expected" ] && [ "$supplied" = "$expected" ]
|
||
}
|
||
|
||
uci_get() {
|
||
local key default value
|
||
key="$1"
|
||
default="$2"
|
||
value="$(uci -q get "owrtremote.main.$key" 2>/dev/null || true)"
|
||
[ -n "$value" ] && {
|
||
printf '%s' "$value"
|
||
return
|
||
}
|
||
printf '%s' "$default"
|
||
}
|
||
|
||
uci_set() {
|
||
local key value
|
||
key="$1"
|
||
value="$2"
|
||
uci -q get owrtremote.main >/dev/null 2>&1 || uci set owrtremote.main=remote
|
||
uci set "owrtremote.main.$key=$value"
|
||
}
|
||
|
||
service_enabled() {
|
||
/etc/init.d/owrt-remote enabled >/dev/null 2>&1
|
||
}
|
||
|
||
xray_running() {
|
||
"$AGENT" status 2>/dev/null | grep -q '^xray_state: running'
|
||
}
|
||
|
||
set_result() {
|
||
RESULT_TITLE="$1"
|
||
RESULT_BODY="$2"
|
||
RESULT_KIND="${3:-info}"
|
||
}
|
||
|
||
save_config() {
|
||
local enabled field
|
||
if has_param enabled; then
|
||
enabled="1"
|
||
else
|
||
enabled="0"
|
||
fi
|
||
uci_set enabled "$enabled"
|
||
|
||
for field in router_id router_name role hub_url hub_token heartbeat_interval xray_bin xray_config vps_host vps_port vless_uuid vless_encryption vless_flow reverse_tag admin_host admin_port public_url; do
|
||
uci_set "$field" "$(param "$field")"
|
||
done
|
||
uci commit owrtremote
|
||
set_result "Настройки сохранены" "Если менял Xray или VPS-поля, перезапусти сервис." "ok"
|
||
}
|
||
|
||
run_action() {
|
||
local action output rc
|
||
action="$1"
|
||
output="${TMPDIR:-/tmp}/owrt-remote-action.$$"
|
||
rc=0
|
||
case "$action" in
|
||
start)
|
||
/etc/init.d/owrt-remote enable >"$output" 2>&1 || rc=$?
|
||
/etc/init.d/owrt-remote restart >>"$output" 2>&1 || rc=$?
|
||
set_result "Сервис запущен" "$(cat "$output")" "ok"
|
||
;;
|
||
stop)
|
||
/etc/init.d/owrt-remote stop >"$output" 2>&1 || rc=$?
|
||
/etc/init.d/owrt-remote disable >>"$output" 2>&1 || rc=$?
|
||
set_result "Сервис остановлен" "$(cat "$output")" "warn"
|
||
;;
|
||
restart)
|
||
/etc/init.d/owrt-remote restart >"$output" 2>&1 || rc=$?
|
||
set_result "Сервис перезапущен" "$(cat "$output")" "ok"
|
||
;;
|
||
heartbeat)
|
||
"$AGENT" heartbeat >"$output" 2>&1 || rc=$?
|
||
set_result "Heartbeat" "$(cat "$output")" "ok"
|
||
;;
|
||
doctor)
|
||
"$AGENT" doctor >"$output" 2>&1 || rc=$?
|
||
set_result "Проверка" "$(cat "$output")" "ok"
|
||
;;
|
||
esac
|
||
if [ "$rc" != "0" ]; then
|
||
set_result "$RESULT_TITLE" "$(cat "$output")" "bad"
|
||
fi
|
||
rm -f "$output"
|
||
}
|
||
|
||
field() {
|
||
local name label value type help
|
||
name="$1"
|
||
label="$2"
|
||
type="${3:-text}"
|
||
help="${4:-}"
|
||
value="$(uci_get "$name" '')"
|
||
printf '<label class="field"><span>%s</span>' "$(h "$label")"
|
||
printf '<input name="%s" type="%s" value="%s" autocomplete="off">' "$(h "$name")" "$(h "$type")" "$(h "$value")"
|
||
if [ -n "$help" ]; then
|
||
printf '<small>%s</small>' "$(h "$help")"
|
||
fi
|
||
printf '</label>'
|
||
}
|
||
|
||
metric() {
|
||
local label value tone
|
||
label="$1"
|
||
value="$2"
|
||
tone="${3:-}"
|
||
printf '<div class="metric %s"><span>%s</span><strong>%s</strong></div>' "$(h "$tone")" "$(h "$label")" "$(h "$value")"
|
||
}
|
||
|
||
render_unauthorized() {
|
||
http_html
|
||
cat <<'EOF'
|
||
<!doctype html><html><head><meta charset="utf-8"><title>OpenWrt Remote</title></head>
|
||
<body><h1>Доступ запрещен</h1><p>Открой панель из LuCI или по ссылке, которую показал install.sh.</p></body></html>
|
||
EOF
|
||
}
|
||
|
||
render_page() {
|
||
local key router_id router_name hub_url vps_host service_state service_tone xray_state xray_tone enabled_checked status_text
|
||
key="$(param key)"
|
||
router_id="$(uci_get router_id main)"
|
||
router_name="$(uci_get router_name "$router_id")"
|
||
hub_url="$(uci_get hub_url '')"
|
||
vps_host="$(uci_get vps_host '')"
|
||
|
||
if service_enabled; then
|
||
service_state="enabled"
|
||
service_tone="good"
|
||
else
|
||
service_state="disabled"
|
||
service_tone="warn"
|
||
fi
|
||
if xray_running; then
|
||
xray_state="running"
|
||
xray_tone="good"
|
||
else
|
||
xray_state="stopped"
|
||
xray_tone="bad"
|
||
fi
|
||
if [ "$(uci_get enabled 0)" = "1" ]; then
|
||
enabled_checked="checked"
|
||
status_text="Агент включен"
|
||
else
|
||
enabled_checked=""
|
||
status_text="Агент выключен"
|
||
fi
|
||
|
||
http_html
|
||
cat <<EOF
|
||
<!doctype html>
|
||
<html lang="ru">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>OpenWrt Remote</title>
|
||
<style>
|
||
:root{color-scheme:dark;--bg:#07040f;--panel:rgba(19,14,32,.88);--text:#f7f2ff;--muted:#b9adc9;--line:rgba(169,126,255,.24);--blue:#7c3aed;--green:#22c55e;--red:#fb7185;--amber:#f59e0b;--teal:#a855f7;--grid:rgba(168,85,247,.16)}
|
||
*{box-sizing:border-box}body{min-height:100vh;margin:0;background-color:var(--bg);background-image:radial-gradient(circle at 12% 8%,rgba(168,85,247,.34),transparent 30%),radial-gradient(circle at 82% 12%,rgba(79,70,229,.25),transparent 28%),radial-gradient(circle at 50% 105%,rgba(236,72,153,.16),transparent 34%),linear-gradient(145deg,#07040f,#120a24 48%,#05030a),repeating-linear-gradient(0deg,transparent 0 30px,var(--grid) 31px),repeating-linear-gradient(90deg,transparent 0 30px,var(--grid) 31px);background-attachment:fixed;color:var(--text);font:14px/1.45 system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}
|
||
.wrap{max-width:1180px;margin:0 auto;padding:22px}.hero{display:flex;gap:18px;align-items:center;justify-content:space-between;padding:20px 0 18px;border-bottom:1px solid var(--line)}
|
||
.brand{display:flex;gap:14px;align-items:center}.titleRow{display:flex;align-items:center;gap:12px;flex-wrap:wrap}
|
||
h1{margin:0;font-size:28px;letter-spacing:0}.sub{margin:4px 0 0;color:var(--muted)}.links{display:flex;gap:8px;flex-wrap:wrap;margin-top:8px}.links a{border:1px solid var(--line);border-radius:999px;background:rgba(255,255,255,.08);color:#f3e8ff;padding:5px 10px;text-decoration:none;font-weight:750}.topActions{display:flex;align-items:center;justify-content:flex-end;gap:10px;flex-wrap:wrap}.returnCard{border:1px solid rgba(245,158,11,.42);border-radius:8px;background:linear-gradient(135deg,rgba(245,158,11,.18),rgba(168,85,247,.08));padding:5px;box-shadow:0 14px 30px rgba(0,0,0,.24)}.backBtn{border:0;border-radius:7px;background:#f59e0b;color:#160f06;padding:9px 12px;font-weight:900;text-decoration:none;box-shadow:0 10px 22px rgba(245,158,11,.22)}
|
||
.pill{display:inline-flex;align-items:center;gap:8px;border:1px solid var(--line);background:var(--panel);border-radius:999px;padding:8px 12px;color:var(--muted);white-space:nowrap}.dot{width:9px;height:9px;border-radius:50%;background:var(--amber)}.dot.good{background:var(--green)}.dot.bad{background:var(--red)}
|
||
.grid{display:grid;grid-template-columns:1.05fr .95fr;gap:16px;margin-top:18px}.panel{background:var(--panel);border:1px solid var(--line);border-radius:8px;padding:16px;box-shadow:0 18px 46px rgba(0,0,0,.28);backdrop-filter:blur(10px)}
|
||
.panel h2{font-size:18px;margin:0 0 12px}.metrics{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:10px}.metric{border:1px solid var(--line);border-radius:8px;padding:12px;background:rgba(255,255,255,.06)}.metric span{display:block;color:var(--muted);font-size:12px}.metric strong{display:block;margin-top:4px;font-size:18px;word-break:break-word}.metric.good strong{color:var(--green)}.metric.bad strong{color:var(--red)}.metric.warn strong{color:var(--amber)}
|
||
form{margin:0}.formgrid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px}.field{display:block}.field span{display:block;font-weight:650;margin-bottom:6px}.field input{width:100%;border:1px solid var(--line);border-radius:8px;background:rgba(8,5,18,.72);padding:10px 11px;color:var(--text)}.field small{display:block;margin-top:5px;color:var(--muted)}
|
||
.check{display:flex;gap:10px;align-items:center;margin-bottom:12px;font-weight:700}.check input{width:18px;height:18px}.actions{display:flex;flex-wrap:wrap;gap:10px;margin-top:14px}.btn{border:1px solid rgba(255,255,255,.08);border-radius:8px;padding:10px 13px;font-weight:750;cursor:pointer;text-decoration:none;display:inline-flex;align-items:center;justify-content:center;background:rgba(255,255,255,.10);color:#f7f2ff}.btn.primary{background:var(--blue);color:#fff}.btn.good{background:var(--green);color:#07120a}.btn.bad{background:rgba(251,113,133,.18);color:#fecdd3}.btn.warn{background:rgba(245,158,11,.16);color:#fde68a;border:1px solid rgba(245,158,11,.34)}
|
||
.notice{margin-top:16px;border-radius:8px;padding:12px;border:1px solid var(--line);background:rgba(255,255,255,.08);white-space:pre-wrap}.notice.ok{border-color:rgba(34,197,94,.46);background:rgba(34,197,94,.12)}.notice.bad{border-color:rgba(251,113,133,.46);background:rgba(251,113,133,.12)}.notice.warn{border-color:rgba(245,158,11,.46);background:rgba(245,158,11,.12)}
|
||
.code{background:#101827;color:#d7e2f2;border-radius:8px;padding:12px;overflow:auto;min-height:120px;font:12px/1.5 ui-monospace,SFMono-Regular,Consolas,monospace}
|
||
@media(max-width:820px){.grid,.formgrid,.metrics{grid-template-columns:1fr}.hero{align-items:flex-start;flex-direction:column}.topActions{justify-content:flex-start}.wrap{padding:14px}h1{font-size:24px}}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<main class="wrap">
|
||
<section class="hero">
|
||
<div class="brand">
|
||
<div>
|
||
<div class="titleRow">
|
||
<h1>OpenWrt Remote</h1>
|
||
<div class="returnCard">
|
||
<a class="backBtn" href="/cgi-bin/luci/admin/services">Вернуться в службы OpenWrt</a>
|
||
</div>
|
||
</div>
|
||
<p class="sub">$(h "$router_name") · $(h "$router_id") · $(h "$status_text")</p>
|
||
<div class="links">
|
||
<a href="https://t.me/kzolotarev95" target="_blank" rel="noopener noreferrer">Telegram</a>
|
||
<a href="https://github.com/kzolotarev95" target="_blank" rel="noopener noreferrer">GitHub</a>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="topActions">
|
||
<div class="pill"><span class="dot $(h "$xray_tone")"></span>Xray: $(h "$xray_state")</div>
|
||
</div>
|
||
</section>
|
||
|
||
<section class="grid">
|
||
<div class="panel">
|
||
<h2>Состояние</h2>
|
||
<div class="metrics">
|
||
EOF
|
||
metric "procd service" "$service_state" "$service_tone"
|
||
metric "xray tunnel" "$xray_state" "$xray_tone"
|
||
metric "VPS host" "${vps_host:-не задан}" ""
|
||
metric "Hub URL" "${hub_url:-не задан}" ""
|
||
cat <<EOF
|
||
</div>
|
||
<form method="post" class="actions">
|
||
<input type="hidden" name="key" value="$(h "$key")">
|
||
<button class="btn good" name="action" value="start">Запустить</button>
|
||
<button class="btn warn" name="action" value="restart">Перезапустить</button>
|
||
<button class="btn bad" name="action" value="stop">Остановить</button>
|
||
<button class="btn" name="action" value="heartbeat">Heartbeat</button>
|
||
<button class="btn" name="action" value="doctor">Проверка</button>
|
||
<a class="btn" href="?key=$(h "$key")&action=download_config">Скачать Xray config</a>
|
||
</form>
|
||
EOF
|
||
if [ -n "$RESULT_TITLE" ]; then
|
||
printf '<div class="notice %s"><strong>%s</strong>\n%s</div>' "$(h "$RESULT_KIND")" "$(h "$RESULT_TITLE")" "$(h "$RESULT_BODY")"
|
||
fi
|
||
cat <<EOF
|
||
</div>
|
||
|
||
<div class="panel">
|
||
<h2>Шпаргалка</h2>
|
||
<div class="code">/etc/init.d/owrt-remote enable
|
||
/etc/init.d/owrt-remote restart
|
||
owrt-remote heartbeat
|
||
owrt-remote status</div>
|
||
</div>
|
||
</section>
|
||
|
||
<section class="panel" style="margin-top:16px">
|
||
<h2>Настройки роутера</h2>
|
||
<form method="post">
|
||
<input type="hidden" name="key" value="$(h "$key")">
|
||
<label class="check"><input type="checkbox" name="enabled" value="1" $enabled_checked> Включить агент и автозапуск</label>
|
||
<div class="formgrid">
|
||
EOF
|
||
field router_id "Router ID" text "Например main или office-1"
|
||
field router_name "Название" text "Будет видно на VPS-карточке"
|
||
field role "Роль" text "main для главного, node для остальных"
|
||
field public_url "Public URL" text "Можно оставить пустым, если используешь proxy через hub"
|
||
field hub_url "Hub URL" text "Например https://vps.example.com:8088"
|
||
field hub_token "Hub token" password "AGENT_TOKEN с VPS"
|
||
field heartbeat_interval "Heartbeat, секунд" number "Минимум 10"
|
||
field xray_bin "Xray binary" text "/usr/bin/xray"
|
||
field xray_config "Xray config" text "/etc/xray/owrt-remote-client.json"
|
||
field vps_host "VPS host" text "IP или домен VPS для Xray"
|
||
field vps_port "VPS Xray port" number "Обычно 8443"
|
||
field vless_uuid "VLESS UUID" text "Из карточки роутера на VPS"
|
||
field vless_encryption "VLESS encryption" text "По умолчанию none"
|
||
field vless_flow "VLESS flow" text "Обычно пусто"
|
||
field reverse_tag "Reverse tag" text "Обычно reverse-in"
|
||
field admin_host "Admin host" text "Обычно 127.0.0.1"
|
||
field admin_port "Admin port" number "LuCI/uhttpd обычно 80"
|
||
cat <<EOF
|
||
</div>
|
||
<div class="actions">
|
||
<button class="btn primary" name="action" value="save">Сохранить</button>
|
||
<button class="btn good" name="action" value="save_start">Сохранить и запустить</button>
|
||
</div>
|
||
</form>
|
||
</section>
|
||
</main>
|
||
</body>
|
||
</html>
|
||
EOF
|
||
}
|
||
|
||
read_request
|
||
|
||
if ! authorized; then
|
||
render_unauthorized
|
||
exit 0
|
||
fi
|
||
|
||
action="$(param action)"
|
||
|
||
if [ "$action" = "download_config" ]; then
|
||
http_json
|
||
"$AGENT" render-client --stdout 2>&1
|
||
exit 0
|
||
fi
|
||
|
||
if [ "${REQUEST_METHOD:-GET}" = "POST" ]; then
|
||
case "$action" in
|
||
save)
|
||
save_config
|
||
;;
|
||
save_start)
|
||
save_config
|
||
run_action start
|
||
;;
|
||
start|stop|restart|heartbeat|doctor)
|
||
run_action "$action"
|
||
;;
|
||
esac
|
||
fi
|
||
|
||
render_page
|