add proxy for tg and fix auth

This commit is contained in:
Yuriy Yuriev
2026-05-14 19:18:44 +07:00
parent 328f510151
commit bdf3fef7ec
7 changed files with 79 additions and 90 deletions
+13 -21
View File
@@ -2,8 +2,8 @@
Хранилище учётных данных пользователей.
"""
import json
import asyncio
import json
import logging
from pathlib import Path
from typing import Optional, Dict
@@ -20,57 +20,49 @@ class AuthStorage:
self._lock = asyncio.Lock()
async def register(self, user_id: int, password_hash: str, salt: str) -> bool:
"""Регистрирует нового пользователя. Возвращает True если успешно."""
async with self._lock:
data = self._load()
data = await asyncio.to_thread(self._load_sync)
if str(user_id) in data:
return False
data[str(user_id)] = {
"password_hash": password_hash,
"salt": salt,
}
self._save(data)
data[str(user_id)] = {"password_hash": password_hash, "salt": salt}
await asyncio.to_thread(self._save_sync, data)
logger.info(f"User {user_id} registered")
return True
async def get_user(self, user_id: int) -> Optional[Dict]:
"""Получает данные пользователя по ID."""
async with self._lock:
data = self._load()
data = await asyncio.to_thread(self._load_sync)
return data.get(str(user_id))
async def change_password(self, user_id: int, new_hash: str, new_salt: str) -> bool:
"""Сменить пароль пользователя."""
async with self._lock:
data = self._load()
data = await asyncio.to_thread(self._load_sync)
user_str = str(user_id)
if user_str not in data:
return False
data[user_str]["password_hash"] = new_hash
data[user_str]["salt"] = new_salt
self._save(data)
await asyncio.to_thread(self._save_sync, data)
logger.info(f"User {user_id} changed password")
return True
async def delete_user(self, user_id: int) -> bool:
"""Удалить учётную запись пользователя."""
async with self._lock:
data = self._load()
data = await asyncio.to_thread(self._load_sync)
user_str = str(user_id)
if user_str not in data:
return False
del data[user_str]
self._save(data)
await asyncio.to_thread(self._save_sync, data)
logger.info(f"User {user_id} deleted")
return True
async def user_exists(self, user_id: int) -> bool:
"""Проверяет, существует ли пользователь."""
async with self._lock:
data = self._load()
data = await asyncio.to_thread(self._load_sync)
return str(user_id) in data
def _load(self) -> dict:
def _load_sync(self) -> dict:
if not self.file_path.exists():
return {}
try:
@@ -80,9 +72,9 @@ class AuthStorage:
logger.warning(f"Failed to load auth storage: {e}")
return {}
def _save(self, data: dict):
def _save_sync(self, data: dict) -> None:
try:
with open(self.file_path, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
except IOError as e:
logger.error(f"Failed to save auth storage: {e}")
logger.error(f"Failed to save auth storage: {e}")