Initial commit: VPN Telegram bot (sanitized defaults)
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
"""Фоновое отключение WG/AWG конфигов при истекшей подписке Remnawave."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from remnawave import RemnawaveSDK
|
||||
|
||||
from services.awg_configs import enforce_awg_configs_subscription
|
||||
from services.db import Database
|
||||
from services.xui_wg import enforce_wg_configs_subscription
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def run_vpn_configs_expiry_cycle(
|
||||
db: Database,
|
||||
sdk: RemnawaveSDK,
|
||||
*,
|
||||
limit: int = 500,
|
||||
) -> int:
|
||||
ids = db.list_telegram_ids_with_enabled_vpn_configs(limit=limit)
|
||||
touched = 0
|
||||
for tid in ids:
|
||||
try:
|
||||
user = db.get_user(tid)
|
||||
login = user.login if user else None
|
||||
wg = await enforce_wg_configs_subscription(
|
||||
db=db, sdk=sdk, telegram_id=tid, login=login
|
||||
)
|
||||
awg = await enforce_awg_configs_subscription(
|
||||
db=db, sdk=sdk, telegram_id=tid, login=login
|
||||
)
|
||||
if (wg.get("disabled") or 0) + (awg.get("disabled") or 0) > 0:
|
||||
touched += 1
|
||||
logger.info(
|
||||
"vpn configs auto-disabled tg=%s wg=%s awg=%s",
|
||||
tid,
|
||||
wg.get("disabled"),
|
||||
awg.get("disabled"),
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.exception("vpn configs expiry enforce failed tg=%s", tid)
|
||||
return touched
|
||||
|
||||
|
||||
async def vpn_configs_expiry_worker(
|
||||
db: Database,
|
||||
sdk: RemnawaveSDK,
|
||||
*,
|
||||
interval_sec: int = 120,
|
||||
) -> None:
|
||||
"""Каждые ~2 минуты проверяет включённые конфиги и гасит их без подписки."""
|
||||
await asyncio.sleep(45)
|
||||
while True:
|
||||
try:
|
||||
n = await run_vpn_configs_expiry_cycle(db, sdk)
|
||||
if n:
|
||||
logger.info("vpn configs expiry cycle: disabled for %s users", n)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.exception("vpn configs expiry cycle error")
|
||||
await asyncio.sleep(interval_sec)
|
||||
Reference in New Issue
Block a user