Add VPS Xray reload action
This commit is contained in:
1 parent
f02161f683
commit
d21f279f89
2 files changed
+62
No files matched your search
@@ -8,6 +8,7 @@ import json
|
||||
import os
|
||||
import secrets
|
||||
import sqlite3
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import urllib.parse
|
||||
@@ -481,6 +482,33 @@ def make_router_xray_config(row):
|
||||
}
|
||||
|
||||
|
||||
def reload_vps_xray(db_path=DB_PATH):
|
||||
out = Path(os.environ.get("OWRT_REMOTE_XRAY_CONFIG", "/etc/xray/owrt-remote.json"))
|
||||
service = os.environ.get("OWRT_REMOTE_XRAY_SERVICE", "owrt-remote-xray")
|
||||
with connect(db_path) as conn:
|
||||
init_db(conn)
|
||||
rows = list_router_rows(conn)
|
||||
config = make_server_xray_config(rows)
|
||||
out.parent.mkdir(parents=True, exist_ok=True)
|
||||
out.write_text(json.dumps(config, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
try:
|
||||
os.chmod(out, 0o600)
|
||||
except OSError:
|
||||
pass
|
||||
result = subprocess.run(
|
||||
["systemctl", "restart", service],
|
||||
text=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
timeout=20,
|
||||
check=False,
|
||||
)
|
||||
if result.returncode != 0:
|
||||
detail = (result.stderr or result.stdout or "").strip()
|
||||
raise RuntimeError(f"systemctl restart {service} failed: {detail}")
|
||||
return {"config": str(out), "service": service, "routers": len(rows)}
|
||||
|
||||
|
||||
def make_openwrt_config(row, hub_url):
|
||||
lines = [
|
||||
"uci -q delete owrtremote.main",
|
||||
@@ -581,6 +609,7 @@ input,select{{min-width:0;border:1px solid var(--line);border-radius:8px;padding
|
||||
</div>
|
||||
<div class="headerActions">
|
||||
<div class="badge"><span class="dot on"></span>Hub online</div>
|
||||
<button class="badge" id="xrayReload" type="button">Обновить Xray VPS</button>
|
||||
<button class="badge authToggle" id="authToggle" type="button">login: {safe_username}</button>
|
||||
<a class="btn" href="/logout">Выйти</a>
|
||||
<div class="authMenu" id="authMenu" hidden>
|
||||
@@ -760,6 +789,22 @@ routerForm.name.addEventListener('input', () => {{
|
||||
routerForm.name.dataset.touched = '1';
|
||||
}});
|
||||
|
||||
document.getElementById('xrayReload').addEventListener('click', async () => {{
|
||||
showRouterMsg('Обновляю Xray на VPS...');
|
||||
const res = await fetch('/api/xray/reload', {{method: 'POST'}});
|
||||
const text = await res.text();
|
||||
if (res.ok) {{
|
||||
let message = 'Xray VPS обновлён. Теперь кнопка Админка должна идти в свежие порты.';
|
||||
try {{
|
||||
const data = JSON.parse(text);
|
||||
message = `Xray VPS обновлён: ${{data.config}}, роутеров в конфиге: ${{data.routers}}.`;
|
||||
}} catch (e) {{}}
|
||||
showRouterMsg(message);
|
||||
}} else {{
|
||||
showRouterMsg(text || 'Не удалось обновить Xray VPS', true);
|
||||
}}
|
||||
}});
|
||||
|
||||
async function loadRouters() {{
|
||||
const res = await fetch('/api/routers', {{cache: 'no-store'}});
|
||||
if (res.ok) {{
|
||||
@@ -1066,6 +1111,13 @@ class Handler(BaseHTTPRequestHandler):
|
||||
if path == "/api/auth":
|
||||
self.update_auth()
|
||||
return
|
||||
if path == "/api/xray/reload":
|
||||
try:
|
||||
result = reload_vps_xray(self.app.db_path)
|
||||
self.send_json(200, {"ok": True, **result})
|
||||
except Exception as exc:
|
||||
self.send_text(500, str(exc))
|
||||
return
|
||||
if path == "/api/router":
|
||||
try:
|
||||
payload = self.read_payload()
|
||||
|
||||
Reference in new issue
Block a user