-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_management.py
137 lines (120 loc) · 6.07 KB
/
server_management.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from aiogram import types, Dispatcher
from aiogram.fsm.context import FSMContext
from aiogram.fsm.state import State, StatesGroup
from aiogram.filters import Command
from models import Server
from db import encrypt_password
import aiosqlite
import ipaddress
import logging
class ServerForm(StatesGroup):
name = State()
ip = State()
port = State()
login = State()
password = State()
class DeleteServerForm(StatesGroup):
server_id = State()
async def cmd_list_servers(message: types.Message):
async with aiosqlite.connect('bot.db') as db:
async with db.execute('SELECT id, name, ip FROM servers') as cursor:
servers = await cursor.fetchall()
if servers:
server_list = "\n".join([f"{i+1}. {row[1]} ({row[2]})" for i, row in enumerate(servers)])
await message.reply(f"Available servers:\n{server_list}")
else:
await message.reply("No servers found.")
async def cmd_delete_server(message: types.Message, state: FSMContext):
async with aiosqlite.connect('bot.db') as db:
async with db.execute('SELECT id, name, ip FROM servers') as cursor:
servers = await cursor.fetchall()
if servers:
server_list = "\n".join([f"{i+1}. {row[1]} ({row[2]})" for i, row in enumerate(servers)])
await message.reply(f"Available servers:\n{server_list}\n\nEnter the number of the server to delete:")
await state.set_state(DeleteServerForm.server_id)
else:
await message.reply("No servers found.")
async def process_delete_server_id(message: types.Message, state: FSMContext):
try:
server_number = int(message.text)
async with aiosqlite.connect('bot.db') as db:
async with db.execute('SELECT id FROM servers') as cursor:
server_ids = await cursor.fetchall()
if 0 < server_number <= len(server_ids):
server_id = server_ids[server_number - 1][0]
await db.execute('DELETE FROM servers WHERE id = ?', (server_id,))
await db.commit()
await message.reply(f"Server #{server_number} deleted successfully.")
else:
await message.reply("Invalid server number.")
except ValueError:
await message.reply("Invalid input. Please enter a number.")
finally:
await state.clear()
async def cmd_add_server(message: types.Message, state: FSMContext):
await state.set_state(ServerForm.name)
await message.reply("Enter server name:")
async def process_name(message: types.Message, state: FSMContext):
await state.update_data(name=message.text)
await message.reply("Server name is done.\nEnter server IPv4 address:")
await message.delete()
await state.set_state(ServerForm.ip)
async def process_ip(message: types.Message, state: FSMContext):
try:
ipaddress.IPv4Address(message.text)
await state.update_data(ip=message.text)
await message.reply("IPv4 address is done.\nEnter server port:")
await message.delete()
await state.set_state(ServerForm.port)
except ipaddress.AddressValueError:
await message.reply("Invalid IPv4 address. Please enter a valid IPv4 address.")
async def process_port(message: types.Message, state: FSMContext):
try:
port = int(message.text)
if 1 <= port <= 65535:
await state.update_data(port=port)
await message.reply("Server port is done.\nEnter login (alphanumeric characters and underscore):")
await message.delete()
await state.set_state(ServerForm.login)
else:
await message.reply("Invalid port number. Please enter a port between 1 and 65535.")
except ValueError:
await message.reply("Invalid port number. Please enter a number.")
async def process_login(message: types.Message, state: FSMContext):
if message.text.isalnum() or "_" in message.text:
await state.update_data(login=message.text)
await message.reply("Login is done.\nEnter password:")
await message.delete()
await state.set_state(ServerForm.password)
else:
await message.reply("Invalid login. Please use only alphanumeric characters and underscore.")
async def process_password(message: types.Message, state: FSMContext):
await state.update_data(password=await encrypt_password(message.text))
user_data = await state.get_data()
try:
async with aiosqlite.connect('bot.db') as db:
await db.execute('''
INSERT INTO servers (name, ip, port, login, password) VALUES (?, ?, ?, ?, ?)
''', (user_data['name'], user_data['ip'], user_data['port'], user_data['login'], user_data['password']))
await db.commit()
await state.clear()
server_info = f"**Server added:**\n" \
f"Name: {user_data['name']}\n" \
f"IP: {user_data['ip']}\n" \
f"Port: {user_data['port']}\n" \
f"Login: {user_data['login']}\n"
await message.reply(server_info, parse_mode="Markdown")
await message.delete()
except Exception as e:
logging.error(f"Failed to add server: {e}")
await message.reply("Failed to add server. Please try again later.")
def register_handlers_server_management(dp: Dispatcher):
dp.message.register(cmd_list_servers, Command(commands='list_servers'))
dp.message.register(cmd_add_server, Command(commands='add_server'))
dp.message.register(cmd_delete_server, Command(commands='delete_server'))
dp.message.register(process_name, ServerForm.name)
dp.message.register(process_ip, ServerForm.ip)
dp.message.register(process_port, ServerForm.port)
dp.message.register(process_login, ServerForm.login)
dp.message.register(process_password, ServerForm.password)
dp.message.register(process_delete_server_id, DeleteServerForm.server_id)