Сделаны подписки и переделаны роуты

This commit is contained in:
Disledg
2024-12-28 21:31:07 +03:00
parent 63c0e780b4
commit 3544562b96
21 changed files with 547 additions and 202 deletions

View File

@@ -1,6 +1,6 @@
from .payment_routes import router as payment_router
#from .payment_routes import router as payment_router
from .user_routes import router as user_router
from .subscription_routes import router as subscription_router
# Экспорт всех маршрутов
__all__ = ["payment_router", "user_router", "subscription_router"]
__all__ = [ "user_router", "subscription_router"]

View File

View File

@@ -1,15 +1,17 @@
from fastapi import APIRouter, HTTPException, Depends
from pydantic import BaseModel
from app.services.db_manager import DatabaseManager
from instance.configdb import get_database_manager
from uuid import UUID
import logging
from sqlalchemy.exc import SQLAlchemyError
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
router = APIRouter()
# DatabaseManager должен передаваться через Depends
def get_database_manager():
# Здесь должна быть логика инициализации DatabaseManager
return DatabaseManager()
# Схемы запросов и ответов
class BuySubscriptionRequest(BaseModel):
telegram_id: int
plan_id: str
@@ -38,6 +40,10 @@ async def buy_subscription(
raise HTTPException(status_code=500, detail="Failed to buy subscription")
elif result == "INSUFFICIENT_FUNDS":
raise HTTPException(status_code=400, detail="Insufficient funds")
elif result == "TARIFF_NOT_FOUND":
raise HTTPException(status_code=400, detail="Tariff not found")
elif result == "ACTIVE_SUBSCRIPTION_EXISTS":
raise HTTPException(status_code=400, detail="User already had subscription",)
return {"message": "Subscription purchased successfully"}
except Exception as e:
@@ -45,28 +51,27 @@ async def buy_subscription(
# Эндпоинт для получения последней подписки
@router.get("/subscription/{user_id}/last", response_model=list[SubscriptionResponse])
async def last_subscription(
user_id: int,
database_manager: DatabaseManager = Depends(get_database_manager)
):
"""
Получение последней подписки пользователя.
"""
@router.get("/subscription/{user_id}/last", response_model=SubscriptionResponse)
async def last_subscription(user_id: UUID, database_manager: DatabaseManager = Depends(get_database_manager)):
logger.info(f"Получение последней подписки для пользователя: {user_id}")
try:
subscriptions = await database_manager.last_subscription(user_id)
if subscriptions == "ERROR":
raise HTTPException(status_code=500, detail="Failed to fetch subscriptions")
return [
{
"id": sub.id,
"plan": sub.plan,
"vpn_server_id": sub.vpn_server_id,
"expiry_date": sub.expiry_date.isoformat(),
"created_at": sub.created_at.isoformat(),
"updated_at": sub.updated_at.isoformat(),
} for sub in subscriptions
]
sub = await database_manager.last_subscription(user_id)
if sub is None:
logger.warning(f"Подписки для пользователя {user_id} не найдены")
raise HTTPException(status_code=404, detail="No subscriptions found")
return {
"id": sub.id,
"plan": sub.plan,
"vpn_server_id": sub.vpn_server_id,
"expiry_date": sub.expiry_date.isoformat(),
"created_at": sub.created_at.isoformat(),
"updated_at": sub.updated_at.isoformat(),
}
except SQLAlchemyError as e:
logger.error(f"Ошибка базы данных при получении подписки для пользователя {user_id}: {e}")
raise HTTPException(status_code=500, detail="Database error")
except Exception as e:
logger.error(f"Неожиданная ошибка: {e}")
raise HTTPException(status_code=500, detail=str(e))

View File

@@ -2,6 +2,7 @@ from fastapi import APIRouter, Depends, HTTPException
from app.services.db_manager import DatabaseManager
from instance.configdb import get_database_manager
from pydantic import BaseModel
from uuid import UUID
router = APIRouter()
@@ -66,3 +67,43 @@ async def get_user(
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post("/user/{telegram_id}/balance/{amount}", summary="Обновить баланс")
async def update_balance(
telegram_id: int,
amount: float,
db_manager: DatabaseManager = Depends(get_database_manager)
):
"""
Обновляет баланс пользователя.
"""
try:
result = await db_manager.update_balance(telegram_id, amount)
if result == "ERROR":
raise HTTPException(status_code=500, detail="Failed to update balance")
return {"message": "Balance updated successfully"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/user/{user_id}/transactions", summary="Последние транзакции пользователя")
async def last_transactions(
user_id: UUID,
db_manager: DatabaseManager = Depends(get_database_manager)
):
"""
Возвращает список последних транзакций пользователя.
"""
try:
transactions = await db_manager.last_transaction(user_id)
if transactions == "ERROR":
raise HTTPException(status_code=500, detail="Failed to fetch transactions")
return [
{
"id": tx.id,
"amount": tx.amount,
"created_at": tx.created_at.isoformat(),
"transaction_type": tx.transaction_type,
} for tx in transactions
]
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))

View File

@@ -9,7 +9,7 @@ from .mongo_rep import MongoDBRepository
import random
import string
import logging
import asyncio
from uuid import UUID
class DatabaseManager:
def __init__(self, session_generator):
@@ -85,23 +85,29 @@ class DatabaseManager:
await session.rollback()
return "ERROR"
async def last_subscription(self, user_id: int):
async def last_subscription(self, user_id: str):
"""
Возвращает список подписок пользователя.
Возвращает последнюю подписку пользователя.
"""
async for session in self.session_generator():
try:
result = await session.execute(
select(Subscription)
.where(Subscription.user_id == user_id)
.where(Subscription.user_id == str(user_id))
.order_by(desc(Subscription.created_at))
.limit(1) # Применяем limit правильно
)
return result.scalars().all()
subscription = result.scalar_one_or_none()
if subscription:
return subscription
else:
return None
except SQLAlchemyError as e:
self.logger.error(f"Ошибка при получении последней подписки пользователя {user_id}: {e}")
self.logger.error(f"Ошибка при получении подписки для пользователя {user_id}: {e}")
return "ERROR"
async def last_transaction(self, user_id: int):
async def last_transaction(self, user_id: UUID):
"""
Возвращает список транзакций пользователя.
"""
@@ -109,7 +115,7 @@ class DatabaseManager:
try:
result = await session.execute(
select(Transaction)
.where(Transaction.user_id == user_id)
.where(Transaction.user_id == str(user_id))
.order_by(desc(Transaction.created_at))
)
transactions = result.scalars().all()
@@ -121,73 +127,23 @@ class DatabaseManager:
async def buy_sub(self, telegram_id: str, plan_id: str):
async for session in self.session_generator():
try:
result = await self.create_user(telegram_id)
if not result:
self.logger.error(f"Пользователь с Telegram ID {telegram_id} не найден.")
return "ERROR"
active_subscription = await self._check_active_subscription(telegram_id, session)
if active_subscription:
self.logger.error(f"Пользователь {telegram_id} уже имеет активную подписку.")
return "ACTIVE_SUBSCRIPTION_EXISTS"
result = await self._initialize_user_and_plan(telegram_id, plan_id)
if isinstance(result, str):
return result # Возвращает "ERROR", "TARIFF_NOT_FOUND" или "INSUFFICIENT_FUNDS"
# Получение тарифного плана из MongoDB
plan = await self.mongo_repo.get_subscription_plan(plan_id)
if not plan:
self.logger.error(f"Тарифный план {plan_id} не найден.")
return "ERROR"
user, plan = result
user.balance -= int(plan["price"])
session.add(user)
# Проверка достаточности средств
cost = int(plan["price"])
if result.balance < cost:
self.logger.error(f"Недостаточно средств у пользователя {telegram_id} для покупки плана {plan_id}.")
return "INSUFFICIENT_FUNDS"
# Списываем средства
result.balance -= cost
# Создаем подписку
expiry_date = datetime.utcnow() + relativedelta(months=plan["duration_months"])
server = await self.mongo_repo.get_server_with_least_clients()
self.logger.info(f"Выбран сервер для подписки: {server}")
new_subscription = Subscription(
user_id=result.id,
vpn_server_id=str(server['server']["name"]),
plan=plan_id,
expiry_date=expiry_date
)
session.add(new_subscription)
# Попытка добавить пользователя на сервер
# Получаем информацию о пользователе
user = result # так как result уже содержит пользователя
if not user:
self.logger.error(f"Не удалось найти пользователя для добавления на сервер.")
new_subscription, server = await self._create_subscription_and_add_client(user, plan, session)
if not new_subscription:
await session.rollback()
return "ERROR"
# Получаем сервер из MongoDB
server_data = await self.mongo_repo.get_server(new_subscription.vpn_server_id)
if not server_data:
self.logger.error(f"Не удалось найти сервер с ID {new_subscription.vpn_server_id}.")
await session.rollback()
return "ERROR"
server_info = server_data['server']
url_base = f"https://{server_info['ip']}:{server_info['port']}/{server_info['secretKey']}"
login_data = {
'username': server_info['login'],
'password': server_info['password'],
}
panel = PanelInteraction(url_base, login_data, self.logger,server_info['certificate']['data'])
expiry_date_iso = new_subscription.expiry_date.isoformat()
# Добавляем на сервер
response = await panel.add_client(user.id, expiry_date_iso, user.username)
if response != "OK":
self.logger.error(f"Ошибка при добавлении клиента {telegram_id} на сервер: {response}")
# Если не получилось добавить на сервер, откатываем транзакцию
await session.rollback()
return "ERROR"
# Если мы здесь - значит и подписка, и добавление на сервер успешны
await session.commit()
self.logger.info(f"Подписка успешно оформлена для пользователя {telegram_id} на план {plan_id} и клиент добавлен на сервер.")
return "OK"
@@ -201,6 +157,79 @@ class DatabaseManager:
await session.rollback()
return "ERROR"
async def _initialize_user_and_plan(self, telegram_id, plan_id):
user = await self.create_user(telegram_id)
if not user:
self.logger.error(f"Пользователь с Telegram ID {telegram_id} не найден.")
return "ERROR"
plan = await self.mongo_repo.get_subscription_plan(plan_id)
if not plan:
self.logger.error(f"Тарифный план {plan_id} не найден.")
return "TARIFF_NOT_FOUND"
cost = int(plan["price"])
if user.balance < cost:
self.logger.error(f"Недостаточно средств у пользователя {telegram_id} для покупки плана {plan_id}.")
return "INSUFFICIENT_FUNDS"
return user, plan
async def _create_subscription_and_add_client(self, user, plan, session):
expiry_date = datetime.utcnow() + relativedelta(months=plan["duration_months"])
server = await self.mongo_repo.get_server_with_least_clients()
self.logger.info(f"Выбран сервер для подписки: {server}")
new_subscription = Subscription(
user_id=user.id,
vpn_server_id=str(server['server']["name"]),
plan=plan["name"],
expiry_date=expiry_date
)
session.add(new_subscription)
server_data = await self.mongo_repo.get_server(new_subscription.vpn_server_id)
if not server_data:
self.logger.error(f"Не удалось найти сервер с ID {new_subscription.vpn_server_id}.")
return None, None
server_info = server_data['server']
url_base = f"https://{server_info['ip']}:{server_info['port']}/{server_info['secretKey']}"
login_data = {
'username': server_info['login'],
'password': server_info['password'],
}
panel = PanelInteraction(url_base, login_data, self.logger, server_info['certificate']['data'])
expiry_date_iso = new_subscription.expiry_date.isoformat()
response = await panel.add_client(1, expiry_date_iso, user.username)
if response != "OK":
self.logger.error(f"Ошибка при добавлении клиента {user.telegram_id} на сервер: {response}")
return None, None
return new_subscription, server
async def _check_active_subscription(self, telegram_id, session):
"""
Проверяет наличие активной подписки у пользователя.
:param telegram_id: Telegram ID пользователя.
:param session: Текущая сессия базы данных.
:return: Объект подписки или None.
"""
try:
result = await session.execute(
select(Subscription)
.join(User, Subscription.user_id == User.id)
.where(User.telegram_id == telegram_id, Subscription.expiry_date > datetime.utcnow())
)
return result.scalars().first()
except Exception as e:
self.logger.error(f"Ошибка проверки активной подписки для пользователя {telegram_id}: {e}")
return None
@staticmethod
def generate_string(length):

View File

@@ -1,5 +1,6 @@
import aiohttp
import uuid
import json
import base64
import ssl
@@ -9,79 +10,51 @@ def generate_uuid():
class PanelInteraction:
def __init__(self, base_url, login_data, logger, certificate=None, is_encoded=True):
"""
Initialize the PanelInteraction class.
:param base_url: Base URL for the panel.
:param login_data: Login data (username/password or token).
:param logger: Logger for debugging.
:param certificate: Certificate content (Base64-encoded or raw string).
:param is_encoded: Indicates whether the certificate is Base64-encoded.
"""
self.base_url = base_url
self.login_data = login_data
self.logger = logger
self.cert_content = self._decode_certificate(certificate, is_encoded)
self.session_id = None # Session ID will be initialized lazily
self.ssl_context = self._create_ssl_context(certificate, is_encoded)
self.session_id = None
self.headers = None
def _decode_certificate(self, certificate, is_encoded):
"""
Decode the provided certificate content.
:param certificate: Certificate content (Base64-encoded or raw string).
:param is_encoded: Indicates whether the certificate is Base64-encoded.
:return: Decoded certificate content as bytes.
"""
def _create_ssl_context(self, certificate, is_encoded):
if not certificate:
self.logger.error("No certificate provided.")
raise ValueError("Certificate is required.")
try:
# Создаем SSLContext
ssl_context = ssl.create_default_context()
# Декодируем, если нужно
if is_encoded:
certificate = base64.b64decode(certificate).decode()
# Загружаем сертификат в SSLContext
ssl_context.load_verify_locations(cadata=certificate)
return ssl_context
except Exception as e:
self.logger.error(f"Error while decoding certificate: {e}")
raise ValueError("Invalid certificate format or content.") from e
self.logger.error(f"Error creating SSL context: {e}")
raise ValueError("Invalid certificate format.") from e
async def _ensure_logged_in(self):
"""
Ensure the session ID is available for authenticated requests.
"""
if not self.session_id:
self.session_id = await self.login()
if self.session_id:
self.headers = {
'Accept': 'application/json',
'Cookie': f'3x-ui={self.session_id}',
'Content-Type': 'application/json'
}
else:
raise ValueError("Unable to log in and retrieve session ID.")
try:
self.session_id = await self.login()
if self.session_id:
self.headers = {
'Accept': 'application/json',
'Cookie': f'3x-ui={self.session_id}',
'Content-Type': 'application/json'
}
else:
self.logger.error("Login failed: Unable to retrieve session ID.")
raise ValueError("Login failed: No session ID.")
except Exception as e:
self.logger.exception("Unexpected error during login.")
raise
async def login(self):
"""
Perform login to the panel.
:return: Session ID or None.
"""
login_url = f"{self.base_url}/login"
self.logger.info(f"Attempting to login at: {login_url}")
async with aiohttp.ClientSession() as session:
try:
async with session.post(
login_url, data=self.login_data, ssl=self.cert_content, timeout=10
login_url, data=self.login_data, ssl=self.ssl_context, timeout=10
) as response:
if response.status == 200:
session_id = response.cookies.get("3x-ui")
@@ -89,13 +62,12 @@ class PanelInteraction:
return session_id.value
else:
self.logger.error("Login failed: No session ID received.")
return None
else:
self.logger.error(f"Login failed: {response.status}")
return None
error_details = await response.text()
self.logger.error(f"Login failed with status {response.status}: {error_details}")
except aiohttp.ClientError as e:
self.logger.error(f"Login request failed: {e}")
return None
self.logger.exception(f"Login request failed: {e}")
raise
async def get_inbound_info(self, inbound_id):
"""
@@ -109,7 +81,7 @@ class PanelInteraction:
async with aiohttp.ClientSession() as session:
try:
async with session.get(
url, headers=self.headers, ssl=self.cert_content, timeout=10
url, headers=self.headers, ssl=self.ssl_context, timeout=10
) as response:
if response.status == 200:
return await response.json()
@@ -132,7 +104,7 @@ class PanelInteraction:
async with aiohttp.ClientSession() as session:
try:
async with session.get(
url, headers=self.headers, ssl=self.cert_content, timeout=10
url, headers=self.headers, ssl=self.ssl_context, timeout=10
) as response:
if response.status == 200:
return await response.json()
@@ -176,7 +148,7 @@ class PanelInteraction:
async with aiohttp.ClientSession() as session:
try:
async with session.post(
url, headers=self.headers, json=update_data, ssl=self.cert_content
url, headers=self.headers, json=update_data, ssl=self.ssl_context
) as response:
if response.status == 200:
self.logger.info("Client expiry updated successfully.")
@@ -197,36 +169,39 @@ class PanelInteraction:
await self._ensure_logged_in()
url = f"{self.base_url}/panel/api/inbounds/addClient"
client_info = {
"clients": [
{
"id": generate_uuid(),
"alterId": 0,
"email": email,
"limitIp": 2,
"totalGB": 0,
"flow": "xtls-rprx-vision",
"expiryTime": expiry_date,
"enable": True,
"tgId": "",
"subId": ""
}
]
"id": generate_uuid(),
"flow": "xtls-rprx-vision",
"email": email,
"limitIp": 2,
"totalGB": 0,
"expiryTime": expiry_date,
"enable": True,
"tgId": "",
"subId": "",
"reset": 0
}
settings = json.dumps({"clients": [client_info]}) # Преобразуем объект в JSON-строку
payload = {
"id": inbound_id,
"settings": client_info
"id": int(inbound_id), # Преобразуем inbound_id в число
"settings": settings # Передаем settings как JSON-строку
}
async with aiohttp.ClientSession() as session:
try:
async with session.post(
url, headers=self.headers, json=payload, ssl=self.cert_content
url, headers=self.headers, json=payload, ssl=self.ssl_context
) as response:
if response.status == 200:
return await response.status
response_json = await response.json()
if response.status == 200 and response_json.get('success'):
self.logger.info(f"Клиент успешно добавлен: {response_json}")
return "OK"
else:
self.logger.error(f"Failed to add client: {response.status}")
error_msg = response_json.get('msg', 'Причина не указана')
self.logger.error(f"Не удалось добавить клиента: {error_msg}")
return None
except aiohttp.ClientError as e:
self.logger.error(f"Add client request failed: {e}")
return None