From 109068acf4301780220718a47f8a5cee0cfadc74 Mon Sep 17 00:00:00 2001 From: themanyfaceddemon Date: Fri, 18 Oct 2024 07:58:02 +0300 Subject: [PATCH] add ping, get all servers --- hub_dbs/main_db/bans_manager.py | 8 +++++ hub_dbs/main_db/server_manager.py | 52 +++++++++++++++++++++++++++++++ main.py | 4 ++- routes/__init__.py | 2 ++ routes/connect.py | 26 ++++++++++++++++ routes/info.py | 15 +++++++++ routes/server.py | 0 routes/user.py | 1 + 8 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 routes/connect.py create mode 100644 routes/info.py create mode 100644 routes/server.py diff --git a/hub_dbs/main_db/bans_manager.py b/hub_dbs/main_db/bans_manager.py index 2a24583..255e4e2 100644 --- a/hub_dbs/main_db/bans_manager.py +++ b/hub_dbs/main_db/bans_manager.py @@ -67,6 +67,14 @@ async def _add_ban( ), ) + if entity_type == "ip": + await db.execute( + """ + DELETE FROM servers_connect WHERE ip_address = ?; + """, + (entity_name,), + ) + if system: LogsDatabase.log_system_action( server_name=None, diff --git a/hub_dbs/main_db/server_manager.py b/hub_dbs/main_db/server_manager.py index d02d977..9081edd 100644 --- a/hub_dbs/main_db/server_manager.py +++ b/hub_dbs/main_db/server_manager.py @@ -23,6 +23,58 @@ def _hash_token(cls, token: str) -> str: def _verify_token(cls, token: str, token_hash: str) -> bool: return bcrypt.checkpw(token.encode("utf-8"), token_hash.encode("utf-8")) + @classmethod + async def get_all_active_servers(cls) -> list[dict]: + active_servers = [] + + async with Database() as db: + async with db.execute( + """ + SELECT server_name, dns_name, ip_address, port, current_players, max_players + FROM servers_connect + WHERE status = 'online'; + """ + ) as cursor: + servers = await cursor.fetchall() + for server in servers: + active_servers.append({ + "server_name": server[0], + "dns_name": server[1], + "ip_address": server[2], + "port": server[3], + "current_players": server[4], + "max_players": server[5], + }) + + return active_servers + + @classmethod + async def get_server_info(cls, dns_name: str) -> Optional[dict]: + async with Database() as db: + async with db.execute( + """ + SELECT server_name, ip_address, port, status, max_players, current_players + FROM servers_connect + WHERE dns_name = ?; + """, + (dns_name,), + ) as cursor: + server_row = await cursor.fetchone() + + if not server_row: + return None + + server_info = { + "server_name": server_row[0], + "ip_address": server_row[1], + "port": server_row[2], + "status": server_row[3], + "max_players": server_row[4], + "current_players": server_row[5], + } + + return server_info + @classmethod async def add_server( cls, diff --git a/main.py b/main.py index 993e058..c4cbf59 100644 --- a/main.py +++ b/main.py @@ -5,7 +5,7 @@ from fastapi import FastAPI from hub_dbs import Database, LogsDatabase, RoleManager, UserManager -from routes import user_router +from routes import connect_router, info_router, user_router @asynccontextmanager @@ -20,6 +20,8 @@ async def lifespan(app: FastAPI): app = FastAPI(lifespan=lifespan) +app.include_router(connect_router) +app.include_router(info_router) app.include_router(user_router) if __name__ == "__main__": diff --git a/routes/__init__.py b/routes/__init__.py index 63f9960..b7b46e4 100644 --- a/routes/__init__.py +++ b/routes/__init__.py @@ -1 +1,3 @@ +from .connect import connect_router +from .info import info_router from .user import user_router diff --git a/routes/connect.py b/routes/connect.py new file mode 100644 index 0000000..426009a --- /dev/null +++ b/routes/connect.py @@ -0,0 +1,26 @@ +from fastapi import APIRouter, HTTPException + +from hub_dbs.main_db import ClusterManager, ServerManager + +connect_router = APIRouter(prefix="/api_v1/connect") + + +@connect_router.get("/{full_path}") +async def connect(full_path: str): + try: + type_connect, name = full_path.split(".") + + except ValueError: + raise HTTPException(status_code=400, detail="Invalid format. Use 'type.name'.") + + result = None + if type_connect == "cluster": + result = await ClusterManager.get_least_loaded_server(name) + + elif type_connect == "single": + result = await ServerManager.get_server_info(name) + + if result is None: + raise HTTPException(status_code=404, detail=f"{type_connect}.{name} not found") + + return result diff --git a/routes/info.py b/routes/info.py new file mode 100644 index 0000000..fe89589 --- /dev/null +++ b/routes/info.py @@ -0,0 +1,15 @@ +from fastapi import APIRouter + +from hub_dbs.main_db import ServerManager + +info_router = APIRouter(prefix='/api_v1/') + + +@info_router.get("/") +async def get_servers(): + return await ServerManager.get_all_active_servers() + + +@info_router.get("/ping") +async def ping(): + return {"message": "pong!"} diff --git a/routes/server.py b/routes/server.py new file mode 100644 index 0000000..e69de29 diff --git a/routes/user.py b/routes/user.py index 3812f98..93a79ec 100644 --- a/routes/user.py +++ b/routes/user.py @@ -49,6 +49,7 @@ async def logout( try: if full_logout: await UserManager.full_logout(token) + else: await UserManager.logout(token)