mirror of
https://github.com/andrey271192/domen_hydra.git
synced 2026-09-20 14:42:00 +00:00
Add HydraRoute Manager web server — standalone domain management UI
Made-with: Cursor
This commit is contained in:
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
.venv/
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
server/data/
|
||||||
|
server/.env
|
||||||
|
*.log
|
||||||
96
README.md
Normal file
96
README.md
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
# 🌐 HydraRoute Manager
|
||||||
|
|
||||||
|
Веб-интерфейс для централизованного управления конфигурацией **HydraRoute Neo** на роутерах Keenetic.
|
||||||
|
|
||||||
|
- Управление группами доменов и IP через браузер
|
||||||
|
- Включение / отключение групп одним переключателем
|
||||||
|
- Импорт `domain.conf` и `ip.list` с роутера
|
||||||
|
- Отправка конфига на все роутеры одной кнопкой
|
||||||
|
- Авторизация по паролю
|
||||||
|
- Скачивание файлов для роутера по HTTP
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Установка сервера (Ubuntu 22/24)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://github.com/andrey271192/domen_hydra.git /opt/domen-hydra
|
||||||
|
cd /opt/domen-hydra
|
||||||
|
bash server/install.sh
|
||||||
|
nano server/.env # задать ADMIN_PASSWORD
|
||||||
|
systemctl restart hydra-manager
|
||||||
|
```
|
||||||
|
|
||||||
|
Интерфейс откроется на `http://IP:8000`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Установка на роутер (Keenetic + Entware)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
export SERVER_URL="http://IP_СЕРВЕРА:8000" \
|
||||||
|
&& curl -fsSL https://raw.githubusercontent.com/andrey271192/domen_hydra/main/install_router.sh | sh
|
||||||
|
```
|
||||||
|
|
||||||
|
Роутер будет каждый день в 02:00 скачивать актуальный конфиг с сервера.
|
||||||
|
|
||||||
|
Ручное обновление:
|
||||||
|
```bash
|
||||||
|
sh /opt/bin/hydra_update.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Как использовать
|
||||||
|
|
||||||
|
1. Открой `http://IP:8000` → введи пароль
|
||||||
|
2. **Импорт**: вставь `domain.conf` и `ip.list` с роутера → "Сохранить на сервер"
|
||||||
|
3. Редактируй группы доменов и IP прямо в браузере
|
||||||
|
4. Нажми **"📡 Обновить все роутеры"** — конфиг применится через SSH
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Формат файлов
|
||||||
|
|
||||||
|
**domain.conf:**
|
||||||
|
```
|
||||||
|
##youtube
|
||||||
|
youtube.com,youtu.be,googlevideo.com/HydraRoute
|
||||||
|
|
||||||
|
##avito
|
||||||
|
avito.ru,ozon.ru/RU
|
||||||
|
```
|
||||||
|
|
||||||
|
**ip.list:**
|
||||||
|
```
|
||||||
|
##geoip:ru
|
||||||
|
/RU
|
||||||
|
geoip:ru
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Структура проекта
|
||||||
|
|
||||||
|
```
|
||||||
|
server/
|
||||||
|
main.py — FastAPI приложение
|
||||||
|
config.py — настройки из .env
|
||||||
|
hydra_manager.py — парсинг/генерация domain.conf + ip.list
|
||||||
|
models.py — модели данных
|
||||||
|
database.py — работа с JSON
|
||||||
|
install.sh — установка на Ubuntu
|
||||||
|
.env.example — шаблон конфигурации
|
||||||
|
templates/
|
||||||
|
index.html — веб-интерфейс
|
||||||
|
|
||||||
|
install_router.sh — установка на роутер
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Обновление
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd /opt/domen-hydra && git pull && systemctl restart hydra-manager
|
||||||
|
```
|
||||||
13
hydra_update.sh
Normal file
13
hydra_update.sh
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
SERVER=$(cat /opt/etc/hydra_server_url 2>/dev/null)
|
||||||
|
[ -z "$SERVER" ] && echo "❌ /opt/etc/hydra_server_url не задан" && exit 1
|
||||||
|
|
||||||
|
HYDRA_DIR="/opt/etc/HydraRoute"
|
||||||
|
[ ! -d "$HYDRA_DIR" ] && HYDRA_DIR="/opt/etc/hydra"
|
||||||
|
[ ! -d "$HYDRA_DIR" ] && mkdir -p /opt/etc/HydraRoute && HYDRA_DIR="/opt/etc/HydraRoute"
|
||||||
|
|
||||||
|
echo "$(date) Обновляю конфиг с $SERVER..."
|
||||||
|
curl -sf "$SERVER/hydra/domain.conf" -o "$HYDRA_DIR/domain.conf" && echo "✅ domain.conf" || echo "❌ domain.conf"
|
||||||
|
curl -sf "$SERVER/hydra/ip.list" -o "$HYDRA_DIR/ip.list" && echo "✅ ip.list" || echo "❌ ip.list"
|
||||||
|
|
||||||
|
neo restart 2>/dev/null && echo "✅ neo restarted" || echo "⚠️ neo restart failed"
|
||||||
24
install_router.sh
Normal file
24
install_router.sh
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
echo "🌐 HydraRoute Manager — установка на роутер"
|
||||||
|
[ -z "$SERVER_URL" ] && printf "URL сервера (http://IP:8000): " && read SERVER_URL
|
||||||
|
[ -z "$SERVER_URL" ] && echo "❌ SERVER_URL обязателен" && exit 1
|
||||||
|
|
||||||
|
mkdir -p /opt/bin /opt/var/log /opt/var/run
|
||||||
|
|
||||||
|
echo "$SERVER_URL" > /opt/etc/hydra_server_url
|
||||||
|
|
||||||
|
REPO="https://raw.githubusercontent.com/andrey271192/domen_hydra/main"
|
||||||
|
curl -fsSL "$REPO/hydra_update.sh" -o /opt/bin/hydra_update.sh
|
||||||
|
chmod +x /opt/bin/hydra_update.sh
|
||||||
|
|
||||||
|
# Add cron: update domains daily at 02:00
|
||||||
|
CT="/tmp/cron_hydra"
|
||||||
|
crontab -l 2>/dev/null | grep -v hydra_update > "$CT" || true
|
||||||
|
echo "0 2 * * * /opt/bin/hydra_update.sh >> /opt/var/log/hydra_update.log 2>&1" >> "$CT"
|
||||||
|
crontab "$CT" && rm -f "$CT"
|
||||||
|
|
||||||
|
# Run immediately
|
||||||
|
sh /opt/bin/hydra_update.sh
|
||||||
|
|
||||||
|
echo "✅ Установлено. Обновление каждый день в 02:00"
|
||||||
7
server/.env.example
Normal file
7
server/.env.example
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
HOST=0.0.0.0
|
||||||
|
PORT=8000
|
||||||
|
ADMIN_PASSWORD=admin
|
||||||
|
|
||||||
|
# SSH defaults for all routers (can be overridden per-router)
|
||||||
|
SSH_USER=root
|
||||||
|
SSH_PASS=keenetic
|
||||||
0
server/__init__.py
Normal file
0
server/__init__.py
Normal file
30
server/config.py
Normal file
30
server/config.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import os, json, logging
|
||||||
|
from pathlib import Path
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
logger = logging.getLogger("hydra")
|
||||||
|
|
||||||
|
BASE_DIR = Path(__file__).resolve().parent
|
||||||
|
DATA_DIR = BASE_DIR / "data"
|
||||||
|
|
||||||
|
HOST = os.getenv("HOST", "0.0.0.0")
|
||||||
|
PORT = int(os.getenv("PORT", "8000"))
|
||||||
|
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "admin")
|
||||||
|
SSH_USER = os.getenv("SSH_USER", "root")
|
||||||
|
SSH_PASS = os.getenv("SSH_PASS", "keenetic")
|
||||||
|
|
||||||
|
HYDRA_FILE = DATA_DIR / "hydra_config.json"
|
||||||
|
ROUTERS_FILE = DATA_DIR / "routers.json"
|
||||||
|
|
||||||
|
def ensure_data():
|
||||||
|
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
|
defaults = {
|
||||||
|
HYDRA_FILE: {"version":"1.0","domain_groups":[],"ip_groups":[]},
|
||||||
|
ROUTERS_FILE: {},
|
||||||
|
}
|
||||||
|
for fp, default in defaults.items():
|
||||||
|
if not fp.exists():
|
||||||
|
fp.write_text(json.dumps(default, ensure_ascii=False, indent=2))
|
||||||
|
|
||||||
|
ensure_data()
|
||||||
22
server/database.py
Normal file
22
server/database.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import json, logging
|
||||||
|
from pathlib import Path
|
||||||
|
logger = logging.getLogger("hydra")
|
||||||
|
|
||||||
|
def load_json(path: Path, default=None):
|
||||||
|
if default is None: default = {}
|
||||||
|
if not isinstance(path, Path): path = Path(path)
|
||||||
|
try:
|
||||||
|
if path.exists():
|
||||||
|
text = path.read_text(encoding="utf-8")
|
||||||
|
if text.strip(): return json.loads(text)
|
||||||
|
return default
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Corrupt {path.name}: {e}"); return default
|
||||||
|
|
||||||
|
def save_json(path: Path, data):
|
||||||
|
if not isinstance(path, Path): path = Path(path)
|
||||||
|
try:
|
||||||
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
path.write_text(json.dumps(data, ensure_ascii=False, indent=2, default=str), encoding="utf-8")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Save fail {path}: {e}")
|
||||||
60
server/hydra_manager.py
Normal file
60
server/hydra_manager.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import hashlib
|
||||||
|
from .models import HydraConfig, DomainGroup, IpGroup
|
||||||
|
from . import config
|
||||||
|
from .database import load_json, save_json
|
||||||
|
|
||||||
|
def load_hydra_config():
|
||||||
|
d = load_json(config.HYDRA_FILE, {"version":"1.0","domain_groups":[],"ip_groups":[]})
|
||||||
|
return HydraConfig(**d)
|
||||||
|
|
||||||
|
def save_hydra_config(cfg):
|
||||||
|
save_json(config.HYDRA_FILE, cfg.model_dump())
|
||||||
|
|
||||||
|
def generate_domain_conf(cfg):
|
||||||
|
lines = []
|
||||||
|
for g in cfg.domain_groups:
|
||||||
|
lines.append(f"##{g.name}")
|
||||||
|
e = ",".join(g.entries)
|
||||||
|
lines.append(f"{e}/{g.policy}" if g.enabled else f"{e}#/{g.policy}")
|
||||||
|
return "\n".join(lines)+"\n" if lines else ""
|
||||||
|
|
||||||
|
def generate_ip_list(cfg):
|
||||||
|
lines = []
|
||||||
|
for g in cfg.ip_groups:
|
||||||
|
lines.append(f"##{g.name}")
|
||||||
|
lines.append(f"/{g.policy}" if g.enabled else f"#/{g.policy}")
|
||||||
|
for e in g.entries: lines.append(e)
|
||||||
|
return "\n".join(lines)+"\n" if lines else ""
|
||||||
|
|
||||||
|
def get_config_version(cfg):
|
||||||
|
return hashlib.sha256((generate_domain_conf(cfg)+generate_ip_list(cfg)).encode()).hexdigest()[:12]
|
||||||
|
|
||||||
|
def parse_domain_conf(text):
|
||||||
|
groups=[]; cn=None
|
||||||
|
for line in text.strip().split("\n"):
|
||||||
|
line=line.strip()
|
||||||
|
if not line: continue
|
||||||
|
if line.startswith("##"): cn=line[2:].strip(); continue
|
||||||
|
if "/" in line and cn is not None:
|
||||||
|
en=True
|
||||||
|
if "#/" in line: p=line.split("#/",1); es=p[0]; pol=p[1]; en=False
|
||||||
|
else: p=line.rsplit("/",1); es=p[0]; pol=p[1] if len(p)>1 else "HydraRoute"
|
||||||
|
entries=[e.strip() for e in es.split(",") if e.strip()]
|
||||||
|
et="geosite" if any(e.startswith("geosite:") for e in entries) else "domain"
|
||||||
|
groups.append(DomainGroup(name=cn,entries=entries,policy=pol,entry_type=et,enabled=en)); cn=None
|
||||||
|
return groups
|
||||||
|
|
||||||
|
def parse_ip_list(text):
|
||||||
|
groups=[]; cn=None; cp=None; ce=[]; en=True
|
||||||
|
def flush():
|
||||||
|
if cn and cp:
|
||||||
|
et="geoip" if any(e.startswith("geoip:") for e in ce) else "ip"
|
||||||
|
groups.append(IpGroup(name=cn,entries=list(ce),policy=cp,entry_type=et,enabled=en))
|
||||||
|
for line in text.strip().split("\n"):
|
||||||
|
line=line.strip()
|
||||||
|
if not line: continue
|
||||||
|
if line.startswith("##"): flush(); cn=line[2:].strip(); cp=None; ce=[]; en=True; continue
|
||||||
|
if line.startswith("#/"): en=False; cp=line[2:].strip(); continue
|
||||||
|
if line.startswith("/"): cp=line[1:].strip(); continue
|
||||||
|
if cp: ce.append(line)
|
||||||
|
flush(); return groups
|
||||||
32
server/install.sh
Normal file
32
server/install.sh
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
echo "🌐 HydraRoute Manager — установка"
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
|
||||||
|
apt-get update -qq && apt-get install -y -qq python3 python3-pip python3-venv sshpass
|
||||||
|
|
||||||
|
python3 -m venv .venv
|
||||||
|
.venv/bin/pip install -q -r server/requirements.txt
|
||||||
|
|
||||||
|
[ ! -f server/.env ] && cp server/.env.example server/.env && echo "⚠️ Настрой server/.env и перезапусти"
|
||||||
|
|
||||||
|
cat > /etc/systemd/system/hydra-manager.service <<EOF
|
||||||
|
[Unit]
|
||||||
|
Description=HydraRoute Manager
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
WorkingDirectory=$(pwd)
|
||||||
|
ExecStart=$(pwd)/.venv/bin/uvicorn server.main:app --host 0.0.0.0 --port 8000
|
||||||
|
Restart=always
|
||||||
|
RestartSec=5
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
EOF
|
||||||
|
|
||||||
|
systemctl daemon-reload
|
||||||
|
systemctl enable hydra-manager
|
||||||
|
systemctl start hydra-manager
|
||||||
|
|
||||||
|
echo "✅ Запущен на http://$(curl -sf ifconfig.me 2>/dev/null || echo IP):8000"
|
||||||
159
server/main.py
Normal file
159
server/main.py
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
"""HydraRoute Domain Manager — standalone web server."""
|
||||||
|
import logging
|
||||||
|
from fastapi import FastAPI, Request, Header, HTTPException
|
||||||
|
from fastapi.responses import HTMLResponse, Response
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from . import config
|
||||||
|
from .database import load_json, save_json
|
||||||
|
from .hydra_manager import (load_hydra_config, save_hydra_config,
|
||||||
|
generate_domain_conf, generate_ip_list, get_config_version,
|
||||||
|
parse_domain_conf, parse_ip_list)
|
||||||
|
from .models import DomainGroup, IpGroup, HydraConfig
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
app = FastAPI(title="HydraRoute Domain Manager")
|
||||||
|
|
||||||
|
def _chk(pwd: str):
|
||||||
|
if config.ADMIN_PASSWORD and pwd != config.ADMIN_PASSWORD:
|
||||||
|
raise HTTPException(401, "Неверный пароль")
|
||||||
|
|
||||||
|
# ── Pages ────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
@app.get("/", response_class=HTMLResponse)
|
||||||
|
async def index():
|
||||||
|
with open(config.BASE_DIR / "templates" / "index.html", encoding="utf-8") as f:
|
||||||
|
return f.read()
|
||||||
|
|
||||||
|
# ── Auth ─────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
@app.get("/api/auth")
|
||||||
|
async def auth(x_admin_password: str = Header("")):
|
||||||
|
_chk(x_admin_password); return {"ok": True}
|
||||||
|
|
||||||
|
@app.post("/api/set_password")
|
||||||
|
async def set_password(body: dict, x_admin_password: str = Header("")):
|
||||||
|
_chk(x_admin_password)
|
||||||
|
new_pwd = (body.get("password") or "").strip()
|
||||||
|
if len(new_pwd) < 4: raise HTTPException(400, "Минимум 4 символа")
|
||||||
|
import re
|
||||||
|
env_path = config.BASE_DIR / ".env"
|
||||||
|
if env_path.exists():
|
||||||
|
txt = env_path.read_text()
|
||||||
|
txt = re.sub(r"ADMIN_PASSWORD=.*", f"ADMIN_PASSWORD={new_pwd}", txt) if "ADMIN_PASSWORD" in txt else txt + f"\nADMIN_PASSWORD={new_pwd}\n"
|
||||||
|
env_path.write_text(txt)
|
||||||
|
config.ADMIN_PASSWORD = new_pwd
|
||||||
|
return {"ok": True}
|
||||||
|
|
||||||
|
# ── HydraRoute files (served to routers) ─────────────────────────────────────
|
||||||
|
|
||||||
|
@app.get("/hydra/domain.conf")
|
||||||
|
async def domain_conf():
|
||||||
|
return Response(content=generate_domain_conf(load_hydra_config()), media_type="text/plain")
|
||||||
|
|
||||||
|
@app.get("/hydra/ip.list")
|
||||||
|
async def ip_list():
|
||||||
|
return Response(content=generate_ip_list(load_hydra_config()), media_type="text/plain")
|
||||||
|
|
||||||
|
@app.get("/hydra/version")
|
||||||
|
async def version():
|
||||||
|
return Response(content=get_config_version(load_hydra_config()), media_type="text/plain")
|
||||||
|
|
||||||
|
@app.get("/hydra/config")
|
||||||
|
async def hydra_config():
|
||||||
|
return load_hydra_config().model_dump()
|
||||||
|
|
||||||
|
# ── Domain groups ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
@app.post("/api/domain-group")
|
||||||
|
async def upsert_domain_group(g: DomainGroup, x_admin_password: str = Header("")):
|
||||||
|
_chk(x_admin_password)
|
||||||
|
cfg = load_hydra_config()
|
||||||
|
cfg.domain_groups = [x for x in cfg.domain_groups if x.name != g.name]
|
||||||
|
cfg.domain_groups.append(g)
|
||||||
|
save_hydra_config(cfg); return {"ok": True}
|
||||||
|
|
||||||
|
@app.delete("/api/domain-group/{name}")
|
||||||
|
async def delete_domain_group(name: str, x_admin_password: str = Header("")):
|
||||||
|
_chk(x_admin_password)
|
||||||
|
cfg = load_hydra_config()
|
||||||
|
cfg.domain_groups = [x for x in cfg.domain_groups if x.name != name]
|
||||||
|
save_hydra_config(cfg); return {"ok": True}
|
||||||
|
|
||||||
|
# ── IP groups ─────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
@app.post("/api/ip-group")
|
||||||
|
async def upsert_ip_group(g: IpGroup, x_admin_password: str = Header("")):
|
||||||
|
_chk(x_admin_password)
|
||||||
|
cfg = load_hydra_config()
|
||||||
|
cfg.ip_groups = [x for x in cfg.ip_groups if x.name != g.name]
|
||||||
|
cfg.ip_groups.append(g)
|
||||||
|
save_hydra_config(cfg); return {"ok": True}
|
||||||
|
|
||||||
|
@app.delete("/api/ip-group/{name}")
|
||||||
|
async def delete_ip_group(name: str, x_admin_password: str = Header("")):
|
||||||
|
_chk(x_admin_password)
|
||||||
|
cfg = load_hydra_config()
|
||||||
|
cfg.ip_groups = [x for x in cfg.ip_groups if x.name != name]
|
||||||
|
save_hydra_config(cfg); return {"ok": True}
|
||||||
|
|
||||||
|
# ── Import ────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
class ImportBody(BaseModel):
|
||||||
|
domain_conf: str = ""; ip_list: str = ""
|
||||||
|
|
||||||
|
@app.post("/api/import")
|
||||||
|
async def import_config(body: ImportBody, x_admin_password: str = Header("")):
|
||||||
|
_chk(x_admin_password)
|
||||||
|
cfg = load_hydra_config()
|
||||||
|
if body.domain_conf.strip():
|
||||||
|
cfg.domain_groups = parse_domain_conf(body.domain_conf)
|
||||||
|
if body.ip_list.strip():
|
||||||
|
cfg.ip_groups = parse_ip_list(body.ip_list)
|
||||||
|
save_hydra_config(cfg); return {"ok": True, "domains": len(cfg.domain_groups), "ips": len(cfg.ip_groups)}
|
||||||
|
|
||||||
|
# ── Push to all routers via SSH ───────────────────────────────────────────────
|
||||||
|
|
||||||
|
@app.post("/api/push_all")
|
||||||
|
async def push_all(request: Request, x_admin_password: str = Header("")):
|
||||||
|
_chk(x_admin_password)
|
||||||
|
import asyncio, subprocess
|
||||||
|
routers = load_json(config.ROUTERS_FILE, {})
|
||||||
|
server_url = str(request.base_url).rstrip("/")
|
||||||
|
results = []
|
||||||
|
for name, rcfg in routers.items():
|
||||||
|
ip = rcfg.get("ip","")
|
||||||
|
if not ip: results.append({"router":name,"ok":False,"msg":"нет IP"}); continue
|
||||||
|
user = rcfg.get("user") or config.SSH_USER
|
||||||
|
pwd = rcfg.get("password") or config.SSH_PASS
|
||||||
|
cmd = (
|
||||||
|
f"curl -sf '{server_url}/hydra/domain.conf' -o /opt/etc/HydraRoute/domain.conf && "
|
||||||
|
f"curl -sf '{server_url}/hydra/ip.list' -o /opt/etc/HydraRoute/ip.list && "
|
||||||
|
f"neo restart"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
r = await asyncio.to_thread(subprocess.run,
|
||||||
|
["sshpass","-p",pwd,"ssh","-o","StrictHostKeyChecking=no","-o","ConnectTimeout=10",
|
||||||
|
f"{user}@{ip}", cmd], capture_output=True, text=True, timeout=60)
|
||||||
|
results.append({"router":name,"ok":r.returncode==0,"msg":r.stdout[:200]})
|
||||||
|
except Exception as e:
|
||||||
|
results.append({"router":name,"ok":False,"msg":str(e)})
|
||||||
|
return {"results": results, "ok": sum(1 for r in results if r["ok"]), "failed": sum(1 for r in results if not r["ok"])}
|
||||||
|
|
||||||
|
# ── Routers CRUD ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
@app.get("/api/routers")
|
||||||
|
async def get_routers():
|
||||||
|
return load_json(config.ROUTERS_FILE, {})
|
||||||
|
|
||||||
|
@app.post("/api/routers/{name}")
|
||||||
|
async def upsert_router(name: str, body: dict, x_admin_password: str = Header("")):
|
||||||
|
_chk(x_admin_password)
|
||||||
|
R = load_json(config.ROUTERS_FILE, {})
|
||||||
|
R[name.strip().lower()] = body
|
||||||
|
save_json(config.ROUTERS_FILE, R); return {"ok": True}
|
||||||
|
|
||||||
|
@app.delete("/api/routers/{name}")
|
||||||
|
async def delete_router(name: str, x_admin_password: str = Header("")):
|
||||||
|
_chk(x_admin_password)
|
||||||
|
R = load_json(config.ROUTERS_FILE, {})
|
||||||
|
R.pop(name, None); save_json(config.ROUTERS_FILE, R); return {"ok": True}
|
||||||
15
server/models.py
Normal file
15
server/models.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
|
class DomainGroup(BaseModel):
|
||||||
|
name: str; entries: list[str]; policy: str
|
||||||
|
entry_type: Literal["domain","geosite"] = "domain"; enabled: bool = True
|
||||||
|
|
||||||
|
class IpGroup(BaseModel):
|
||||||
|
name: str; entries: list[str]; policy: str
|
||||||
|
entry_type: Literal["ip","geoip"] = "ip"; enabled: bool = True
|
||||||
|
|
||||||
|
class HydraConfig(BaseModel):
|
||||||
|
version: str = "1.0"
|
||||||
|
domain_groups: list[DomainGroup] = []
|
||||||
|
ip_groups: list[IpGroup] = []
|
||||||
4
server/requirements.txt
Normal file
4
server/requirements.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
fastapi>=0.115.0
|
||||||
|
uvicorn>=0.30.0
|
||||||
|
python-dotenv>=1.0.1
|
||||||
|
pydantic>=2.11.0
|
||||||
365
server/templates/index.html
Normal file
365
server/templates/index.html
Normal file
@@ -0,0 +1,365 @@
|
|||||||
|
<!DOCTYPE html><html lang="ru"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>HydraRoute Manager</title>
|
||||||
|
<style>
|
||||||
|
*{margin:0;padding:0;box-sizing:border-box}
|
||||||
|
:root{--bg:#000;--card:#1c1c1e;--card2:#2c2c2e;--card3:#3a3a3c;--border:#38383a;--text:#f5f5f7;--muted:#86868b;--accent:#0a84ff;--green:#30d158;--red:#ff453a;--yellow:#ffd60a;--orange:#ff9f0a}
|
||||||
|
body{font-family:-apple-system,BlinkMacSystemFont,'SF Pro Display',sans-serif;background:var(--bg);color:var(--text);padding:20px 24px;max-width:1200px;margin:0 auto}
|
||||||
|
h1{font-size:22px;font-weight:700;margin-bottom:4px}
|
||||||
|
.sub{color:var(--muted);font-size:13px;margin-bottom:20px}
|
||||||
|
.topbar{display:flex;justify-content:space-between;align-items:flex-start;flex-wrap:wrap;gap:12px;margin-bottom:20px;margin-top:12px}
|
||||||
|
.topbar-btns{display:flex;gap:8px;flex-wrap:wrap}
|
||||||
|
.tabs{display:flex;gap:8px;margin-bottom:20px}
|
||||||
|
.tab{padding:8px 18px;border-radius:10px;border:none;font-size:13px;font-weight:600;cursor:pointer;font-family:inherit;background:var(--card);color:var(--muted)}
|
||||||
|
.tab.active{background:var(--accent);color:#fff}
|
||||||
|
.section{background:var(--card);border-radius:16px;padding:18px 20px;margin-bottom:16px}
|
||||||
|
.section-head{display:flex;justify-content:space-between;align-items:center;margin-bottom:14px}
|
||||||
|
.section-head h2{font-size:14px;font-weight:700;display:flex;align-items:center;gap:8px}
|
||||||
|
.groups-grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(320px,1fr));gap:10px}
|
||||||
|
.group{background:var(--card2);border-radius:12px;padding:0;overflow:hidden;transition:box-shadow .2s}
|
||||||
|
.group.editing{box-shadow:0 0 0 2px var(--accent)}
|
||||||
|
.group-head{display:flex;justify-content:space-between;align-items:center;padding:12px 14px 8px}
|
||||||
|
.group-name{font-weight:700;font-size:13px}
|
||||||
|
.group-actions{display:flex;gap:4px}
|
||||||
|
.icon-btn{background:none;border:none;cursor:pointer;font-size:14px;padding:2px 6px;border-radius:6px;color:var(--muted)}
|
||||||
|
.icon-btn:hover{background:var(--card3);color:var(--text)}
|
||||||
|
.badge{font-size:11px;padding:2px 8px;border-radius:6px;font-weight:600;white-space:nowrap}
|
||||||
|
.p-HydraRoute,.p-amnezia{background:rgba(10,132,255,.15);color:var(--accent)}
|
||||||
|
.p-RU{background:rgba(255,214,10,.15);color:var(--yellow)}
|
||||||
|
.p-Google{background:rgba(48,209,88,.15);color:var(--green)}
|
||||||
|
.p-default{background:rgba(255,159,10,.15);color:var(--orange)}
|
||||||
|
.group-view{padding:0 14px 12px}
|
||||||
|
.entries-list{font-size:11px;color:var(--muted);line-height:1.7;max-height:90px;overflow-y:auto;font-family:monospace}
|
||||||
|
.group-meta{font-size:11px;color:var(--border);margin-top:6px}
|
||||||
|
.group-edit{padding:10px 14px 14px;display:none;border-top:1px solid var(--border)}
|
||||||
|
.group-edit.open{display:block}
|
||||||
|
.edit-label{font-size:11px;color:var(--muted);margin-bottom:4px;font-weight:600}
|
||||||
|
textarea.entries-ta{width:100%;min-height:120px;background:var(--card3);border:1px solid var(--border);border-radius:8px;padding:10px;color:var(--text);font-size:11px;font-family:monospace;outline:none;resize:vertical}
|
||||||
|
textarea.entries-ta:focus{border-color:var(--accent)}
|
||||||
|
.add-quick{display:flex;gap:6px;margin-top:8px}
|
||||||
|
input.quick-input{background:var(--card3);border:1px solid var(--border);border-radius:8px;padding:7px 10px;color:var(--text);font-size:12px;font-family:monospace;outline:none;flex:1}
|
||||||
|
input.quick-input:focus{border-color:var(--accent)}
|
||||||
|
input.quick-input::placeholder{color:var(--muted)}
|
||||||
|
.edit-footer{display:flex;justify-content:space-between;align-items:center;margin-top:10px;gap:8px}
|
||||||
|
.edit-footer-left{display:flex;gap:6px}
|
||||||
|
.btn{padding:8px 16px;border-radius:8px;border:none;font-size:12px;font-weight:700;cursor:pointer;font-family:inherit;white-space:nowrap}
|
||||||
|
.btn-b{background:var(--accent);color:#fff}.btn-r{background:var(--red);color:#fff}.btn-g{background:var(--green);color:#000}.btn-o{background:var(--orange);color:#000}
|
||||||
|
.btn-ghost{background:var(--card2);color:var(--muted)}.btn-ghost:hover{background:var(--card3)}
|
||||||
|
.btn:disabled{opacity:.4;cursor:not-allowed}
|
||||||
|
.new-group-form{background:var(--card2);border-radius:12px;padding:14px;border:1px dashed var(--border);display:none}
|
||||||
|
.new-group-form.open{display:block}
|
||||||
|
.form-row{display:flex;gap:8px;flex-wrap:wrap;margin-bottom:10px}
|
||||||
|
input.f-inp,select.f-sel{background:var(--card3);border:1px solid var(--border);border-radius:8px;padding:8px 12px;color:var(--text);font-size:12px;font-family:inherit;outline:none}
|
||||||
|
input.f-inp:focus,select.f-sel:focus{border-color:var(--accent)}
|
||||||
|
.msg{padding:7px 12px;border-radius:8px;font-size:12px;font-weight:600;margin-top:6px;display:none}
|
||||||
|
.msg.ok{display:block;background:rgba(48,209,88,.1);color:var(--green)}
|
||||||
|
.msg.err{display:block;background:rgba(255,69,58,.1);color:var(--red)}
|
||||||
|
.push-log{background:#111;border-radius:10px;padding:12px;font-size:11px;font-family:monospace;color:var(--muted);max-height:120px;overflow-y:auto;margin-top:10px;display:none}
|
||||||
|
.pending-banner{background:rgba(255,159,10,.12);border:1px solid rgba(255,159,10,.4);border-radius:12px;padding:12px 16px;display:none;justify-content:space-between;align-items:center;gap:12px;margin-bottom:14px}
|
||||||
|
.pending-banner span{font-size:13px;color:var(--orange);font-weight:600}
|
||||||
|
.hdr{padding:0 24px;height:54px;background:rgba(28,28,30,.95);backdrop-filter:blur(20px);border-bottom:1px solid rgba(255,255,255,.08);position:sticky;top:0;z-index:100;display:flex;align-items:center;justify-content:space-between;gap:16px;margin:-20px -24px 20px}
|
||||||
|
.hdr-logo{font-size:17px;font-weight:800;white-space:nowrap}.hdr-logo em{font-style:normal;color:var(--accent)}
|
||||||
|
.logout-btn{background:transparent;border:1px solid var(--border);color:var(--muted);border-radius:20px;padding:5px 14px;font-size:12px;font-weight:600;cursor:pointer;font-family:inherit;flex-shrink:0}
|
||||||
|
.imp-grid{display:grid;grid-template-columns:1fr 1fr;gap:12px}
|
||||||
|
@media(max-width:600px){.imp-grid{grid-template-columns:1fr}.groups-grid{grid-template-columns:1fr}}
|
||||||
|
textarea.imp-ta{width:100%;min-height:200px;background:var(--card2);border:1px solid var(--border);border-radius:10px;padding:12px;color:var(--text);font-size:11px;font-family:monospace;outline:none;resize:vertical}
|
||||||
|
textarea.imp-ta:focus{border-color:var(--accent)}
|
||||||
|
.spinner{display:inline-block;width:12px;height:12px;border:2px solid rgba(255,255,255,.3);border-top-color:#fff;border-radius:50%;animation:spin .6s linear infinite;vertical-align:middle;margin-right:5px}
|
||||||
|
@keyframes spin{to{transform:rotate(360deg)}}
|
||||||
|
.version{font-size:11px;color:var(--muted);font-family:monospace}
|
||||||
|
.toggle{width:34px;height:20px;background:var(--border);border-radius:10px;position:relative;cursor:pointer;border:none;transition:.2s}
|
||||||
|
.toggle.on{background:var(--green)}.toggle::after{content:'';position:absolute;width:16px;height:16px;background:#fff;border-radius:50%;top:2px;left:2px;transition:.2s}
|
||||||
|
.toggle.on::after{left:16px}
|
||||||
|
input[type=text],input[type=password]{background:var(--card2);border:1px solid var(--border);border-radius:10px;padding:9px 14px;color:var(--text);font-size:13px;font-family:inherit;outline:none}
|
||||||
|
input[type=text]:focus,input[type=password]:focus{border-color:var(--accent)}
|
||||||
|
.ri{padding:10px 0;border-bottom:1px solid rgba(255,255,255,.05);font-size:13px;display:flex;justify-content:space-between;align-items:center;font-weight:500}
|
||||||
|
.ri:last-child{border:none}.ri .n{font-weight:700;color:var(--accent)}
|
||||||
|
</style></head><body>
|
||||||
|
|
||||||
|
<!-- AUTH OVERLAY -->
|
||||||
|
<div id="auth-overlay" style="display:none;position:fixed;inset:0;background:rgba(0,0,0,.97);z-index:9999;align-items:center;justify-content:center">
|
||||||
|
<div style="background:#1c1c1e;border-radius:24px;padding:40px 36px;width:min(360px,90vw);text-align:center;box-shadow:0 20px 60px rgba(0,0,0,.8)">
|
||||||
|
<div style="font-size:44px;margin-bottom:14px">🌐</div>
|
||||||
|
<h2 style="font-size:20px;font-weight:700;color:#f5f5f7;margin-bottom:6px">HydraRoute Manager</h2>
|
||||||
|
<p style="font-size:13px;color:#86868b;margin-bottom:22px">Введите пароль для доступа</p>
|
||||||
|
<input id="auth-pwd" type="password" placeholder="Пароль" autocomplete="current-password"
|
||||||
|
style="width:100%;background:#2c2c2e;border:1px solid #38383a;border-radius:12px;padding:12px 16px;color:#f5f5f7;font-size:15px;outline:none;font-family:inherit;margin-bottom:8px;box-sizing:border-box"
|
||||||
|
onkeydown="if(event.key==='Enter')_doLogin()">
|
||||||
|
<div id="auth-err" style="color:#ff453a;font-size:12px;font-weight:600;margin-bottom:8px;display:none">❌ Неверный пароль</div>
|
||||||
|
<button onclick="_doLogin()" id="auth-btn" style="width:100%;background:#0a84ff;color:#fff;border:none;border-radius:12px;padding:13px;font-size:15px;font-weight:700;cursor:pointer;font-family:inherit">Войти</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- NAV -->
|
||||||
|
<div class="hdr">
|
||||||
|
<span class="hdr-logo">🌐 <em>HydraRoute</em> Manager</span>
|
||||||
|
<button class="logout-btn" onclick="logout()">Выйти</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="topbar">
|
||||||
|
<div>
|
||||||
|
<h1>🌐 Управление доменами</h1>
|
||||||
|
<div class="sub">HydraRoute Neo · <span id="ver" class="version">...</span></div>
|
||||||
|
</div>
|
||||||
|
<div class="topbar-btns">
|
||||||
|
<button class="btn btn-b" onclick="pushAll()" id="push-btn">📡 Обновить все роутеры</button>
|
||||||
|
<a href="/hydra/domain.conf" target="_blank" class="btn btn-ghost" style="text-decoration:none">↓ domain.conf</a>
|
||||||
|
<a href="/hydra/ip.list" target="_blank" class="btn btn-ghost" style="text-decoration:none">↓ ip.list</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div id="pending-banner" class="pending-banner">
|
||||||
|
<span>⚠️ Есть несохранённые изменения — роутеры ещё не обновлены</span>
|
||||||
|
<button class="btn btn-o" onclick="pushAll()">📡 Отправить сейчас</button>
|
||||||
|
</div>
|
||||||
|
<div id="push-log" class="push-log"></div>
|
||||||
|
|
||||||
|
<div class="tabs">
|
||||||
|
<button class="tab active" onclick="showTab('view')">📋 Конфигурация</button>
|
||||||
|
<button class="tab" onclick="showTab('import')">⬆ Импорт файлов</button>
|
||||||
|
<button class="tab" onclick="showTab('routers')">🔧 Роутеры</button>
|
||||||
|
<button class="tab" onclick="showTab('settings')">⚙ Настройки</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- CONFIGURATION TAB -->
|
||||||
|
<div id="tab-view">
|
||||||
|
<div class="section">
|
||||||
|
<div class="section-head">
|
||||||
|
<h2>📄 Группы доменов <span id="dg-count" class="badge p-HydraRoute">0</span></h2>
|
||||||
|
<button class="btn btn-b" onclick="toggleNewGroup('domain')" style="font-size:12px">+ Новая группа</button>
|
||||||
|
</div>
|
||||||
|
<div id="new-domain-form" class="new-group-form" style="margin-bottom:12px">
|
||||||
|
<div class="form-row">
|
||||||
|
<input class="f-inp" id="nd-name" placeholder="Имя группы" style="width:140px">
|
||||||
|
<select class="f-sel" id="nd-policy"><option>HydraRoute</option><option>RU</option><option>Google</option><option>amnezia</option></select>
|
||||||
|
<select class="f-sel" id="nd-type"><option value="domain">domain</option><option value="geosite">geosite</option></select>
|
||||||
|
</div>
|
||||||
|
<textarea class="entries-ta" id="nd-entries" placeholder="Одна запись на строку: youtube.com netflix.com" style="min-height:80px"></textarea>
|
||||||
|
<div style="margin-top:8px;display:flex;gap:6px">
|
||||||
|
<button class="btn btn-g" onclick="saveNewGroup('domain')">✅ Создать</button>
|
||||||
|
<button class="btn btn-ghost" onclick="toggleNewGroup('domain')">Отмена</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div id="domain-groups" class="groups-grid"></div>
|
||||||
|
</div>
|
||||||
|
<div class="section">
|
||||||
|
<div class="section-head">
|
||||||
|
<h2>🔢 Группы IP <span id="ig-count" class="badge p-RU">0</span></h2>
|
||||||
|
<button class="btn btn-b" onclick="toggleNewGroup('ip')" style="font-size:12px">+ Новая группа</button>
|
||||||
|
</div>
|
||||||
|
<div id="new-ip-form" class="new-group-form" style="margin-bottom:12px">
|
||||||
|
<div class="form-row">
|
||||||
|
<input class="f-inp" id="ni-name" placeholder="Имя группы" style="width:140px">
|
||||||
|
<select class="f-sel" id="ni-policy"><option>HydraRoute</option><option>RU</option><option>Google</option><option>amnezia</option></select>
|
||||||
|
<select class="f-sel" id="ni-type"><option value="ip">ip</option><option value="geoip">geoip</option></select>
|
||||||
|
</div>
|
||||||
|
<textarea class="entries-ta" id="ni-entries" placeholder="1.2.3.0/24 geoip:ru" style="min-height:80px"></textarea>
|
||||||
|
<div style="margin-top:8px;display:flex;gap:6px">
|
||||||
|
<button class="btn btn-g" onclick="saveNewGroup('ip')">✅ Создать</button>
|
||||||
|
<button class="btn btn-ghost" onclick="toggleNewGroup('ip')">Отмена</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div id="ip-groups" class="groups-grid"></div>
|
||||||
|
</div>
|
||||||
|
<div id="view-msg" class="msg"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- IMPORT TAB -->
|
||||||
|
<div id="tab-import" style="display:none">
|
||||||
|
<div class="section">
|
||||||
|
<div class="section-head"><h2>⬆ Импорт domain.conf + ip.list</h2></div>
|
||||||
|
<p style="font-size:12px;color:var(--muted);margin-bottom:12px">Вставь содержимое файлов с роутера — формат сохраняется 1:1</p>
|
||||||
|
<div class="imp-grid">
|
||||||
|
<div>
|
||||||
|
<div style="font-size:12px;font-weight:700;margin-bottom:6px;color:var(--accent)">domain.conf</div>
|
||||||
|
<textarea class="imp-ta" id="dc-text" placeholder="##groupname domain1.com,domain2.com/PolicyName"></textarea>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div style="font-size:12px;font-weight:700;margin-bottom:6px;color:var(--green)">ip.list</div>
|
||||||
|
<textarea class="imp-ta" id="il-text" placeholder="##groupname /PolicyName 1.2.3.0/24"></textarea>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div style="margin-top:12px;display:flex;gap:8px;flex-wrap:wrap">
|
||||||
|
<button class="btn btn-b" onclick="importFiles()">💾 Сохранить на сервер</button>
|
||||||
|
<button class="btn btn-g" onclick="importAndPush()">💾📡 Сохранить + обновить все роутеры</button>
|
||||||
|
</div>
|
||||||
|
<div id="im" class="msg"></div>
|
||||||
|
</div>
|
||||||
|
<div class="section">
|
||||||
|
<div class="section-head"><h2>📤 Текущая конфигурация (для копирования)</h2></div>
|
||||||
|
<div class="imp-grid">
|
||||||
|
<div>
|
||||||
|
<div style="font-size:12px;font-weight:700;margin-bottom:6px;color:var(--accent)">domain.conf</div>
|
||||||
|
<textarea class="imp-ta" id="dc-export" readonly></textarea>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<div style="font-size:12px;font-weight:700;margin-bottom:6px;color:var(--green)">ip.list</div>
|
||||||
|
<textarea class="imp-ta" id="il-export" readonly></textarea>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- ROUTERS TAB -->
|
||||||
|
<div id="tab-routers" style="display:none">
|
||||||
|
<div class="section">
|
||||||
|
<h2 style="font-size:14px;font-weight:700;margin-bottom:14px">➕ Добавить роутер</h2>
|
||||||
|
<div style="display:flex;gap:8px;flex-wrap:wrap;margin-bottom:8px">
|
||||||
|
<input type="text" id="r-name" placeholder="Имя (andrey)" style="width:130px">
|
||||||
|
<input type="text" id="r-ip" placeholder="IP (192.168.88.1)" style="width:160px">
|
||||||
|
<input type="text" id="r-user" value="root" style="width:80px">
|
||||||
|
<input type="password" id="r-pass" placeholder="SSH пароль" style="width:130px">
|
||||||
|
<button class="btn btn-b" onclick="addRouter()">+ Добавить</button>
|
||||||
|
</div>
|
||||||
|
<div id="rm" class="msg"></div>
|
||||||
|
</div>
|
||||||
|
<div class="section">
|
||||||
|
<h2 style="font-size:14px;font-weight:700;margin-bottom:14px">📋 Роутеры</h2>
|
||||||
|
<div id="routers-list"><span style="color:var(--muted)">Загрузка...</span></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- SETTINGS TAB -->
|
||||||
|
<div id="tab-settings" style="display:none">
|
||||||
|
<div class="section">
|
||||||
|
<h2 style="font-size:14px;font-weight:700;margin-bottom:14px">🔑 Смена пароля</h2>
|
||||||
|
<div style="display:flex;gap:8px;flex-wrap:wrap">
|
||||||
|
<input type="password" id="new-pwd" placeholder="Новый пароль" style="width:200px">
|
||||||
|
<input type="password" id="new-pwd2" placeholder="Повторить" style="width:200px">
|
||||||
|
<button class="btn btn-b" onclick="changePwd()">💾 Сохранить</button>
|
||||||
|
</div>
|
||||||
|
<div id="pwd-msg" class="msg"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// ── AUTH ──────────────────────────────────────────────────────────────────
|
||||||
|
function getPass(){ return sessionStorage.getItem('hm_pass')||''; }
|
||||||
|
function authHdr(){ return {'Content-Type':'application/json','X-Admin-Password':getPass()}; }
|
||||||
|
function _showLogin(){ const o=document.getElementById('auth-overlay'); o.style.display='flex'; setTimeout(()=>document.getElementById('auth-pwd').focus(),100); }
|
||||||
|
async function _checkAuth(){
|
||||||
|
const p=sessionStorage.getItem('hm_pass'); if(!p){ _showLogin(); return false; }
|
||||||
|
const r=await fetch('/api/auth',{headers:{'X-Admin-Password':p}});
|
||||||
|
if(!r.ok){ sessionStorage.removeItem('hm_pass'); _showLogin(); return false; }
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
async function _doLogin(){
|
||||||
|
const p=document.getElementById('auth-pwd').value; if(!p) return;
|
||||||
|
const err=document.getElementById('auth-err'),btn=document.getElementById('auth-btn');
|
||||||
|
err.style.display='none'; btn.textContent='Проверка...'; btn.disabled=true;
|
||||||
|
const r=await fetch('/api/auth',{headers:{'X-Admin-Password':p}});
|
||||||
|
if(r.ok){ sessionStorage.setItem('hm_pass',p); document.getElementById('auth-overlay').style.display='none'; _afterLogin(); }
|
||||||
|
else { err.style.display='block'; }
|
||||||
|
btn.textContent='Войти'; btn.disabled=false;
|
||||||
|
}
|
||||||
|
function logout(){ sessionStorage.removeItem('hm_pass'); _showLogin(); }
|
||||||
|
function _afterLogin(){ loadConfig(); }
|
||||||
|
|
||||||
|
// ── TABS ──────────────────────────────────────────────────────────────────
|
||||||
|
const TABS = ['view','import','routers','settings'];
|
||||||
|
function showTab(t){
|
||||||
|
TABS.forEach(id=>{ document.getElementById('tab-'+id).style.display=id===t?'':'none'; });
|
||||||
|
document.querySelectorAll('.tab').forEach((el,i)=>el.classList.toggle('active',i===TABS.indexOf(t)));
|
||||||
|
if(t==='import') loadExport();
|
||||||
|
if(t==='routers') loadRouters();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── CONFIG ────────────────────────────────────────────────────────────────
|
||||||
|
let CFG={domain_groups:[],ip_groups:[]};
|
||||||
|
let pendingChanges=false;
|
||||||
|
function markPending(){ pendingChanges=true; document.getElementById('pending-banner').style.display='flex'; }
|
||||||
|
function clearPending(){ pendingChanges=false; document.getElementById('pending-banner').style.display='none'; }
|
||||||
|
function pc(p){ const m={'HydraRoute':'p-HydraRoute','RU':'p-RU','Google':'p-Google','amnezia':'p-HydraRoute'}; return m[p]||'p-default'; }
|
||||||
|
|
||||||
|
async function loadConfig(){
|
||||||
|
CFG=await(await fetch('/hydra/config')).json();
|
||||||
|
const ver=await(await fetch('/hydra/version')).text();
|
||||||
|
document.getElementById('ver').textContent='v: '+ver.trim();
|
||||||
|
renderGroups();
|
||||||
|
}
|
||||||
|
function renderGroups(){
|
||||||
|
document.getElementById('dg-count').textContent=CFG.domain_groups.length;
|
||||||
|
document.getElementById('ig-count').textContent=CFG.ip_groups.length;
|
||||||
|
document.getElementById('domain-groups').innerHTML=CFG.domain_groups.map((g,i)=>groupCard(g,i,'domain')).join('');
|
||||||
|
document.getElementById('ip-groups').innerHTML=CFG.ip_groups.map((g,i)=>groupCard(g,i,'ip')).join('');
|
||||||
|
}
|
||||||
|
function groupCard(g,idx,type){
|
||||||
|
const key=`${type}-${idx}`;
|
||||||
|
const entries=g.entries.join('\n');
|
||||||
|
const preview=g.entries.slice(0,6).join('<br>')+(g.entries.length>6?`<br><span style="color:var(--border)">+${g.entries.length-6} ещё...</span>`:'');
|
||||||
|
return `<div class="group" id="card-${key}">
|
||||||
|
<div class="group-head">
|
||||||
|
<div style="display:flex;align-items:center;gap:8px">
|
||||||
|
<button class="toggle ${g.enabled?'on':''}" onclick="toggleGroup('${type}',${idx})"></button>
|
||||||
|
<span class="group-name">${g.name}</span>
|
||||||
|
</div>
|
||||||
|
<div style="display:flex;align-items:center;gap:6px">
|
||||||
|
<span class="badge ${pc(g.policy)}">${g.policy}</span>
|
||||||
|
<div class="group-actions">
|
||||||
|
<button class="icon-btn" onclick="openEdit('${type}',${idx})">✏️</button>
|
||||||
|
<button class="icon-btn" onclick="deleteGroup('${type}','${g.name}')">🗑</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="group-view" id="view-${key}">
|
||||||
|
<div class="entries-list">${preview}</div>
|
||||||
|
<div class="group-meta">${g.entries.length} записей · ${g.entry_type}</div>
|
||||||
|
</div>
|
||||||
|
<div class="group-edit" id="edit-${key}">
|
||||||
|
<div class="edit-label">Записи (по одной на строку):</div>
|
||||||
|
<textarea class="entries-ta" id="ta-${key}">${entries}</textarea>
|
||||||
|
<div class="add-quick">
|
||||||
|
<input class="quick-input" id="qi-${key}" placeholder="Быстро добавить домен или IP" onkeydown="if(event.key==='Enter')quickAdd('${type}',${idx})">
|
||||||
|
<button class="btn btn-b" onclick="quickAdd('${type}',${idx})">+</button>
|
||||||
|
</div>
|
||||||
|
<div class="edit-footer">
|
||||||
|
<div class="edit-footer-left">
|
||||||
|
<button class="btn btn-g" onclick="saveGroup('${type}',${idx})">💾 Сохранить</button>
|
||||||
|
<button class="btn btn-ghost" onclick="closeEdit('${type}',${idx})">Отмена</button>
|
||||||
|
</div>
|
||||||
|
<span style="font-size:11px;color:var(--muted)" id="cnt-${key}">${g.entries.length} записей</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>`;
|
||||||
|
}
|
||||||
|
function openEdit(type,idx){ const key=`${type}-${idx}`; document.getElementById(`edit-${key}`).classList.add('open'); document.getElementById(`card-${key}`).classList.add('editing'); const ta=document.getElementById(`ta-${key}`); ta.addEventListener('input',()=>{ document.getElementById(`cnt-${key}`).textContent=ta.value.split('\n').filter(l=>l.trim()).length+' записей'; }); }
|
||||||
|
function closeEdit(type,idx){ const key=`${type}-${idx}`; document.getElementById(`edit-${key}`).classList.remove('open'); document.getElementById(`card-${key}`).classList.remove('editing'); }
|
||||||
|
function quickAdd(type,idx){ const key=`${type}-${idx}`; const qi=document.getElementById(`qi-${key}`); const val=qi.value.trim(); if(!val)return; const ta=document.getElementById(`ta-${key}`); ta.value=ta.value.trimEnd()+(ta.value.trim()?'\n':'')+val; qi.value=''; document.getElementById(`cnt-${key}`).textContent=ta.value.split('\n').filter(l=>l.trim()).length+' записей'; }
|
||||||
|
async function saveGroup(type,idx){ const key=`${type}-${idx}`; const g=type==='domain'?CFG.domain_groups[idx]:CFG.ip_groups[idx]; const entries=document.getElementById(`ta-${key}`).value.split('\n').map(l=>l.trim()).filter(l=>l); const url=type==='domain'?'/api/domain-group':'/api/ip-group'; try{ await fetch(url,{method:'POST',headers:authHdr(),body:JSON.stringify({...g,entries})}); sm('view-msg','ok',`✅ ${g.name} сохранён`); markPending(); await loadConfig(); }catch(e){sm('view-msg','err','❌ '+e)} }
|
||||||
|
async function deleteGroup(type,name){ if(!confirm(`Удалить "${name}"?`))return; const url=type==='domain'?`/api/domain-group/${name}`:`/api/ip-group/${name}`; await fetch(url,{method:'DELETE',headers:{'X-Admin-Password':getPass()}}); sm('view-msg','ok',`🗑 ${name} удалён`); markPending(); await loadConfig(); }
|
||||||
|
async function toggleGroup(type,idx){ const g=type==='domain'?CFG.domain_groups[idx]:CFG.ip_groups[idx]; const url=type==='domain'?'/api/domain-group':'/api/ip-group'; await fetch(url,{method:'POST',headers:authHdr(),body:JSON.stringify({...g,enabled:!g.enabled})}); markPending(); await loadConfig(); }
|
||||||
|
function toggleNewGroup(type){ document.getElementById(type==='domain'?'new-domain-form':'new-ip-form').classList.toggle('open'); }
|
||||||
|
async function saveNewGroup(type){ const pfx=type==='domain'?'nd':'ni'; const name=document.getElementById(`${pfx}-name`).value.trim(); const policy=document.getElementById(`${pfx}-policy`).value; const entry_type=document.getElementById(`${pfx}-type`).value; const raw=document.getElementById(`${pfx}-entries`).value; if(!name){alert('Введи имя группы');return;} const entries=raw.split(/[\n,]/).map(l=>l.trim()).filter(l=>l); const url=type==='domain'?'/api/domain-group':'/api/ip-group'; await fetch(url,{method:'POST',headers:authHdr(),body:JSON.stringify({name,entries,policy,entry_type,enabled:true})}); sm('view-msg','ok',`✅ Группа "${name}" создана`); document.getElementById(`${pfx}-name`).value=''; document.getElementById(`${pfx}-entries`).value=''; toggleNewGroup(type); markPending(); await loadConfig(); }
|
||||||
|
|
||||||
|
// ── PUSH ──────────────────────────────────────────────────────────────────
|
||||||
|
async function pushAll(){
|
||||||
|
const btn=document.getElementById('push-btn'); const log=document.getElementById('push-log');
|
||||||
|
btn.disabled=true; btn.innerHTML='<span class="spinner"></span>Обновляю...'; log.style.display='block'; log.textContent='Запуск...\n';
|
||||||
|
try{
|
||||||
|
const j=await(await fetch('/api/push_all',{method:'POST',headers:{'X-Admin-Password':getPass()}})).json();
|
||||||
|
log.textContent='';
|
||||||
|
for(const r of j.results) log.textContent+=`${r.ok?'✅':'❌'} ${r.router}: ${r.msg}\n`;
|
||||||
|
log.textContent+=`\nИтого: ${j.ok} успешно, ${j.failed} ошибок`;
|
||||||
|
if(j.failed===0) clearPending();
|
||||||
|
}catch(e){log.textContent='❌ '+e}
|
||||||
|
btn.disabled=false; btn.innerHTML='📡 Обновить все роутеры';
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── IMPORT ────────────────────────────────────────────────────────────────
|
||||||
|
async function importFiles(){ const dc=document.getElementById('dc-text').value.trim(); const il=document.getElementById('il-text').value.trim(); if(!dc&&!il){sm('im','err','❌ Поля пусты');return;} const b={}; if(dc)b.domain_conf=dc; if(il)b.ip_list=il; try{ const j=await(await fetch('/api/import',{method:'POST',headers:authHdr(),body:JSON.stringify(b)})).json(); sm('im','ok',`✅ ${j.domains} доменных групп, ${j.ips} IP групп`); markPending(); await loadConfig(); }catch(e){sm('im','err','❌ '+e)} }
|
||||||
|
async function importAndPush(){ await importFiles(); setTimeout(pushAll,300); }
|
||||||
|
async function loadExport(){ document.getElementById('dc-export').value=await(await fetch('/hydra/domain.conf')).text(); document.getElementById('il-export').value=await(await fetch('/hydra/ip.list')).text(); }
|
||||||
|
|
||||||
|
// ── ROUTERS ───────────────────────────────────────────────────────────────
|
||||||
|
async function loadRouters(){
|
||||||
|
const R=await(await fetch('/api/routers')).json();
|
||||||
|
const el=document.getElementById('routers-list');
|
||||||
|
if(!Object.keys(R).length){ el.innerHTML='<span style="color:var(--muted)">Роутеры не добавлены</span>'; return; }
|
||||||
|
el.innerHTML=Object.entries(R).map(([n,c])=>`<div class="ri"><span><span class="n">${n}</span> — ${c.ip}</span><button class="btn btn-r" style="font-size:11px;padding:5px 10px" onclick="delRouter('${n}')">✗</button></div>`).join('');
|
||||||
|
}
|
||||||
|
async function addRouter(){ const n=document.getElementById('r-name').value.trim(); if(!n)return; const b={ip:document.getElementById('r-ip').value.trim(),user:document.getElementById('r-user').value||'root',password:document.getElementById('r-pass').value}; try{ await fetch(`/api/routers/${n}`,{method:'POST',headers:authHdr(),body:JSON.stringify(b)}); sm('rm','ok','✅ '+n); loadRouters(); }catch(e){sm('rm','err','❌ '+e)} }
|
||||||
|
async function delRouter(n){ if(!confirm(`Удалить ${n}?`))return; await fetch(`/api/routers/${n}`,{method:'DELETE',headers:{'X-Admin-Password':getPass()}}); loadRouters(); }
|
||||||
|
|
||||||
|
// ── SETTINGS ──────────────────────────────────────────────────────────────
|
||||||
|
async function changePwd(){ const p1=document.getElementById('new-pwd').value; const p2=document.getElementById('new-pwd2').value; if(p1!==p2){sm('pwd-msg','err','❌ Пароли не совпадают');return;} if(p1.length<4){sm('pwd-msg','err','❌ Минимум 4 символа');return;} try{ const r=await fetch('/api/set_password',{method:'POST',headers:authHdr(),body:JSON.stringify({password:p1})}); if(r.ok){ sessionStorage.setItem('hm_pass',p1); sm('pwd-msg','ok','✅ Пароль сохранён'); document.getElementById('new-pwd').value=''; document.getElementById('new-pwd2').value=''; } }catch(e){sm('pwd-msg','err','❌ '+e)} }
|
||||||
|
|
||||||
|
function sm(id,c,t){const e=document.getElementById(id);e.className='msg '+c;e.textContent=t;e.style.display='block';setTimeout(()=>e.style.display='none',5000)}
|
||||||
|
_checkAuth().then(ok=>{ if(ok) _afterLogin(); });
|
||||||
|
</script></body></html>
|
||||||
Reference in New Issue
Block a user