рабочий вариант выдачи ключей через подтверждение
This commit is contained in:
@@ -7,8 +7,8 @@ from aiogram.fsm.state import State, StatesGroup
|
||||
from auth import login_with_credentials, link
|
||||
from client import add_client
|
||||
from datetime import datetime, timedelta
|
||||
from config import API_TOKEN, ADMIN_PASSWORD, ADMIN_USERNAME, ADMIN_CHAT_ID
|
||||
from database import add_connection, has_active_key, get_active_key_email, DATABASE_PATH
|
||||
from config import API_TOKEN, ADMIN_PASSWORD, ADMIN_USERNAME, ADMIN_CHAT_ID, DATABASE_PATH
|
||||
from database import add_connection, has_active_key, get_active_key_email, get_balance, store_key
|
||||
import uuid
|
||||
import aiosqlite
|
||||
import re
|
||||
@@ -18,6 +18,7 @@ class Form(StatesGroup):
|
||||
waiting_for_admin_confirmation = State()
|
||||
waiting_for_statistics = State()
|
||||
waiting_for_expiry_date = State()
|
||||
viewing_profile = State()
|
||||
|
||||
bot = Bot(token=API_TOKEN)
|
||||
storage = MemoryStorage()
|
||||
@@ -29,15 +30,88 @@ session = login_with_credentials(ADMIN_USERNAME, ADMIN_PASSWORD)
|
||||
|
||||
@dp.message(Command("start"))
|
||||
async def start_command(message: Message):
|
||||
welcome_text = "Добро пожаловать! Вы можете создать ключ для подключения VPN, просмотреть статистику использования или узнать дату окончания ключа."
|
||||
welcome_text = "Добро пожаловать! Вы можете создать ключ для подключения VPN, просмотреть статистику использования, узнать дату окончания ключа или просмотреть ваш профиль."
|
||||
|
||||
button_create_key = InlineKeyboardButton(text='Создать ключ', callback_data='create_key')
|
||||
button_view_stats = InlineKeyboardButton(text='Посмотреть статистику', callback_data='view_stats')
|
||||
button_view_expiry = InlineKeyboardButton(text='Дата окончания ключа', callback_data='view_expiry')
|
||||
keyboard = InlineKeyboardMarkup(inline_keyboard=[[button_create_key], [button_view_stats], [button_view_expiry]])
|
||||
button_view_profile = InlineKeyboardButton(text='Мой профиль', callback_data='view_profile')
|
||||
keyboard = InlineKeyboardMarkup(inline_keyboard=[
|
||||
[button_create_key],
|
||||
[button_view_stats],
|
||||
[button_view_expiry],
|
||||
[button_view_profile]
|
||||
])
|
||||
|
||||
await message.reply(welcome_text, reply_markup=keyboard)
|
||||
|
||||
@dp.callback_query(F.data == 'view_profile')
|
||||
async def process_callback_view_profile(callback_query: types.CallbackQuery, state: FSMContext):
|
||||
tg_id = callback_query.from_user.id
|
||||
username = callback_query.from_user.username # Получаем никнейм пользователя
|
||||
|
||||
try:
|
||||
email = await get_active_key_email(tg_id)
|
||||
if email:
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
async with db.execute("SELECT expiry_time FROM connections WHERE tg_id = ? AND expiry_time > ?",
|
||||
(tg_id, int(datetime.utcnow().timestamp() * 1000))) as cursor:
|
||||
record = await cursor.fetchone()
|
||||
if record:
|
||||
expiry_time = record[0]
|
||||
expiry_date = datetime.utcfromtimestamp(expiry_time / 1000).strftime("%Y-%m-%d %H:%M:%S")
|
||||
else:
|
||||
expiry_date = "Неизвестно"
|
||||
|
||||
# Получите баланс клиента (замените пример на вашу реальную логику получения баланса)
|
||||
balance = await get_balance(tg_id) # Предполагается, что у вас есть функция для получения баланса
|
||||
|
||||
profile_message = (
|
||||
f"Профиль клиента:\n"
|
||||
f"Никнейм: @{username}\n" # Добавляем никнейм
|
||||
f"Email: {email}\n"
|
||||
f"Дата окончания ключа: {expiry_date}\n"
|
||||
f"Баланс: {balance}\n"
|
||||
)
|
||||
|
||||
# Кнопка для просмотра ключей
|
||||
button_view_keys = InlineKeyboardButton(text='Мои ключи', callback_data='view_keys')
|
||||
keyboard = InlineKeyboardMarkup(inline_keyboard=[
|
||||
[button_view_keys]
|
||||
])
|
||||
|
||||
profile_message += "Вы можете просмотреть ваши ключи ниже:"
|
||||
else:
|
||||
profile_message = "У вас нет активных ключей."
|
||||
keyboard = None
|
||||
|
||||
except Exception as e:
|
||||
profile_message = f"Ошибка при получении данных профиля: {e}"
|
||||
keyboard = None
|
||||
|
||||
await callback_query.message.reply(profile_message, reply_markup=keyboard)
|
||||
await callback_query.answer()
|
||||
|
||||
@dp.callback_query(F.data == 'view_keys')
|
||||
async def process_callback_view_keys(callback_query: types.CallbackQuery):
|
||||
tg_id = callback_query.from_user.id
|
||||
|
||||
try:
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
async with db.execute("SELECT email, expiry_time FROM connections WHERE tg_id = ?", (tg_id,)) as cursor:
|
||||
records = await cursor.fetchall()
|
||||
if records:
|
||||
keys_info = "\n".join([f"Email: {email}, Дата окончания: {datetime.utcfromtimestamp(expiry_time / 1000).strftime('%Y-%m-%d %H:%M:%S')}" for email, expiry_time in records])
|
||||
else:
|
||||
keys_info = "У вас нет ключей."
|
||||
|
||||
except Exception as e:
|
||||
keys_info = f"Ошибка при получении ключей: {e}"
|
||||
|
||||
await callback_query.message.reply(f"Ваши ключи:\n{keys_info}")
|
||||
await callback_query.answer()
|
||||
|
||||
|
||||
@dp.callback_query(F.data == 'create_key')
|
||||
async def process_callback_create_key(callback_query: types.CallbackQuery, state: FSMContext):
|
||||
await callback_query.message.reply("Введите имя вашего профиля VPN:")
|
||||
@@ -51,6 +125,7 @@ async def handle_text(message: types.Message, state: FSMContext):
|
||||
if current_state == Form.waiting_for_key_name.state:
|
||||
tg_id = message.from_user.id
|
||||
key_name = message.text
|
||||
tg_name = message.from_user.username
|
||||
|
||||
if not key_name:
|
||||
await message.reply("Имя клиента не указано.")
|
||||
@@ -68,7 +143,9 @@ async def handle_text(message: types.Message, state: FSMContext):
|
||||
# Отправляем запрос на подтверждение админу
|
||||
admin_message = (
|
||||
f"Новый запрос на создание ключа:\n"
|
||||
f"Имя клиента: {key_name}\n"
|
||||
f"Телеграм-ID: {tg_id}\n"
|
||||
f"Имя ключа: {key_name}\n"
|
||||
f"Имя пользователя Telegram: @{tg_name}\n"
|
||||
f"Введите 'yes' для подтверждения или 'no' для отклонения."
|
||||
)
|
||||
keyboard = InlineKeyboardMarkup(inline_keyboard=[
|
||||
@@ -99,13 +176,14 @@ async def handle_admin_confirmation(callback_query: CallbackQuery, state: FSMCon
|
||||
expiry_time = int((current_time + timedelta(days=30)).timestamp() * 1000)
|
||||
enable = True
|
||||
flow = "xtls-rprx-vision"
|
||||
balance = 0
|
||||
|
||||
add_client(session, client_id, email, tg_id, limit_ip, total_gb, expiry_time, enable, flow)
|
||||
|
||||
connection_link = link(session, client_id, email)
|
||||
|
||||
await add_connection(tg_id, client_id, email, expiry_time)
|
||||
|
||||
await add_connection(tg_id, client_id, email, expiry_time, balance)
|
||||
await store_key(client_id, email, connection_link)
|
||||
await bot.send_message(tg_id, f"Ключ создан:\n<pre>{connection_link}</pre>", parse_mode="HTML")
|
||||
await callback_query.message.reply("Ключ создан и отправлен клиенту.")
|
||||
except Exception as e:
|
||||
@@ -124,22 +202,32 @@ async def handle_rejection(callback_query: CallbackQuery, state: FSMContext):
|
||||
await callback_query.answer()
|
||||
|
||||
@dp.callback_query(F.data == 'view_stats')
|
||||
async def process_callback_view_stats(callback_query: types.CallbackQuery):
|
||||
async def process_callback_view_stats(callback_query: types.CallbackQuery, state: FSMContext):
|
||||
tg_id = callback_query.from_user.id
|
||||
|
||||
try:
|
||||
email = await get_active_key_email(tg_id)
|
||||
if email:
|
||||
connection_link = link(session, email)
|
||||
|
||||
# Извлечение данных о загрузке и выгрузке из ссылки
|
||||
up_match = re.search(r'up=(\d+)', connection_link)
|
||||
down_match = re.search(r'down=(\d+)', connection_link)
|
||||
|
||||
up = up_match.group(1) if up_match else "Неизвестно"
|
||||
down = down_match.group(1) if down_match else "Неизвестно"
|
||||
|
||||
statistics = f"Статистика вашего ключа:\nЗагрузка: {up} MB\nВыгрузка: {down} MB"
|
||||
# Получаем client_id из базы данных
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
async with db.execute("SELECT client_id FROM connections WHERE tg_id = ? AND expiry_time > ?",
|
||||
(tg_id, int(datetime.utcnow().timestamp() * 1000))) as cursor:
|
||||
record = await cursor.fetchone()
|
||||
if record:
|
||||
client_id = record[0]
|
||||
# Передаем оба параметра в функцию link
|
||||
connection_link = link(session, client_id, email)
|
||||
|
||||
# Извлечение данных о загрузке и выгрузке из ссылки
|
||||
up_match = re.search(r'up=(\d+)', connection_link)
|
||||
down_match = re.search(r'down=(\d+)', connection_link)
|
||||
|
||||
up = up_match.group(1) if up_match else "Неизвестно"
|
||||
down = down_match.group(1) if down_match else "Неизвестно"
|
||||
|
||||
statistics = f"Статистика вашего ключа:\nЗагрузка: {up} MB\nВыгрузка: {down} MB"
|
||||
else:
|
||||
statistics = "У вас нет активных ключей."
|
||||
else:
|
||||
statistics = "У вас нет активных ключей."
|
||||
|
||||
@@ -149,6 +237,7 @@ async def process_callback_view_stats(callback_query: types.CallbackQuery):
|
||||
await callback_query.message.reply(f"Ваша статистика:\n{statistics}")
|
||||
await callback_query.answer()
|
||||
|
||||
|
||||
@dp.callback_query(F.data == 'view_expiry')
|
||||
async def process_callback_view_expiry(callback_query: types.CallbackQuery):
|
||||
tg_id = callback_query.from_user.id
|
||||
@@ -174,3 +263,21 @@ async def process_callback_view_expiry(callback_query: types.CallbackQuery):
|
||||
|
||||
await callback_query.message.reply(message_text)
|
||||
await callback_query.answer()
|
||||
|
||||
@dp.callback_query(F.data == 'view_balance')
|
||||
async def process_callback_view_balance(callback_query: types.CallbackQuery):
|
||||
tg_id = callback_query.from_user.id
|
||||
|
||||
try:
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
async with db.execute("SELECT balance FROM connections WHERE tg_id = ? AND expiry_time > ?",
|
||||
(tg_id, int(datetime.utcnow().timestamp() * 1000))) as cursor:
|
||||
record = await cursor.fetchone()
|
||||
balance = record[0]
|
||||
message_text = f"Ваш баланс: {balance} RUB"
|
||||
|
||||
except Exception as e:
|
||||
message_text = f"Ошибка при получении баланса: {e}"
|
||||
|
||||
await callback_query.message.reply(message_text)
|
||||
await callback_query.answer()
|
||||
|
||||
@@ -3,12 +3,6 @@ import json
|
||||
from config import ADMIN_USERNAME, ADMIN_PASSWORD, GET_INBOUNDS_URL
|
||||
import uuid
|
||||
|
||||
import requests
|
||||
from config import ADMIN_USERNAME, ADMIN_PASSWORD
|
||||
|
||||
import requests
|
||||
from config import ADMIN_USERNAME, ADMIN_PASSWORD
|
||||
|
||||
def add_client(session, client_id: str, email: str, tg_id: str, limit_ip: int, total_gb: int, expiry_time: int, enable: bool, flow: str):
|
||||
url = 'https://solonet.pocomacho.ru:62553/solonet/panel/api/inbounds/addClient'
|
||||
|
||||
|
||||
+39
-5
@@ -11,19 +11,47 @@ async def init_db():
|
||||
client_id TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
expiry_time INTEGER NOT NULL,
|
||||
balance REAL NOT NULL DEFAULT 0.0, -- Добавлено поле для баланса
|
||||
PRIMARY KEY (tg_id, client_id)
|
||||
)
|
||||
''')
|
||||
await db.execute('''
|
||||
CREATE TABLE IF NOT EXISTS keys (
|
||||
client_id TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL,
|
||||
key TEXT NOT NULL,
|
||||
PRIMARY KEY (client_id)
|
||||
)
|
||||
''')
|
||||
await db.commit()
|
||||
|
||||
async def add_connection(tg_id: int, client_id: str, email: str, expiry_time: int):
|
||||
async def add_connection(tg_id: int, client_id: str, email: str, expiry_time: int, balance: float = 0.0):
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
await db.execute('''
|
||||
INSERT INTO connections (tg_id, client_id, email, expiry_time)
|
||||
VALUES (?, ?, ?, ?)
|
||||
''', (tg_id, client_id, email, expiry_time))
|
||||
INSERT INTO connections (tg_id, client_id, email, expiry_time, balance)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
''', (tg_id, client_id, email, expiry_time, balance))
|
||||
await db.commit()
|
||||
|
||||
async def store_key(client_id: str, email: str, key: str):
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
await db.execute('''
|
||||
INSERT INTO keys (client_id, email, created_at, key)
|
||||
VALUES (?, ?, ?, ?)
|
||||
''', (client_id, email, int(datetime.utcnow().timestamp() * 1000), key))
|
||||
await db.commit()
|
||||
|
||||
async def get_keys(tg_id: int):
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
async with db.execute('''
|
||||
SELECT client_id, email, created_at, key
|
||||
FROM keys
|
||||
WHERE tg_id = ?
|
||||
''', (tg_id,)) as cursor:
|
||||
return await cursor.fetchall()
|
||||
|
||||
|
||||
async def has_active_key(tg_id: int) -> bool:
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
async with db.execute("SELECT COUNT(*) FROM connections WHERE tg_id = ? AND expiry_time > ?",
|
||||
@@ -46,4 +74,10 @@ async def get_key_expiry_time(tg_id: int) -> datetime:
|
||||
if record:
|
||||
expiry_time = record[0]
|
||||
return datetime.utcfromtimestamp(expiry_time / 1000)
|
||||
return None
|
||||
return None
|
||||
|
||||
async def get_balance(tg_id: int) -> str:
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
async with db.execute("SELECT balance FROM connections WHERE tg_id = ?", (tg_id,)) as cursor:
|
||||
record = await cursor.fetchone()
|
||||
return record[0] if record else "Неизвестно"
|
||||
|
||||
Reference in New Issue
Block a user