From 1c22c6bec050f37812a158187c22345cbb7252cc Mon Sep 17 00:00:00 2001 From: Hay1tsme Date: Tue, 9 Jan 2024 12:41:47 -0500 Subject: [PATCH] idac: add echo server --- titles/idac/echo.py | 19 +++++------- titles/idac/index.py | 70 +++++++++++++++++++++++++++++------------- titles/idac/season2.py | 8 ++--- titles/idz/echo.py | 3 +- 4 files changed, 61 insertions(+), 39 deletions(-) diff --git a/titles/idac/echo.py b/titles/idac/echo.py index 08e5526..5520392 100644 --- a/titles/idac/echo.py +++ b/titles/idac/echo.py @@ -10,19 +10,14 @@ from titles.idac.config import IDACConfig from titles.idac.database import IDACData -class IDACEchoUDP(DatagramProtocol): - def __init__(self, cfg: CoreConfig, game_cfg: IDACConfig, port: int) -> None: - super().__init__() - self.port = port - self.core_config = cfg - self.game_config = game_cfg - self.logger = logging.getLogger("idac") +class IDACEchoUDP: + def connection_made(self, transport): + self.transport = transport + + def datagram_received(self, data, addr): + logging.getLogger('idz').debug(f'Received echo from {addr}') + self.transport.sendto(data, addr) - def datagramReceived(self, data, addr): - self.logger.info( - f"UDP Ping from from {addr[0]}:{addr[1]} -> {self.port} - {data.hex()}" - ) - self.transport.write(data, addr) class IDACEchoTCP(BaseRequestHandler): diff --git a/titles/idac/index.py b/titles/idac/index.py index 1aa8050..c1e9d02 100644 --- a/titles/idac/index.py +++ b/titles/idac/index.py @@ -9,10 +9,10 @@ import coloredlogs from os import path from typing import Dict, List, Tuple from logging.handlers import TimedRotatingFileHandler - +import asyncio from core.config import CoreConfig -from core.title import BaseServlet +from core.title import BaseServlet, JSONResponseNoASCII from core.utils import Utils from titles.idac.base import IDACBase from titles.idac.season2 import IDACSeason2 @@ -75,7 +75,8 @@ class IDACServlet(BaseServlet): def get_routes(self) -> List[Route]: return [ - Route("/{version:int}/initiald/{category:str}/{endpoint:str}", self.render_POST, methods=["POST"]) + Route("/{version:int}/initiald/{category:str}/{endpoint:str}", self.render_POST, methods=["POST"]), + Route("/{version:int}/initiald-matching/{endpoint:str}", self.render_POST, methods=["POST"]), ] def get_allnet_info( @@ -87,7 +88,7 @@ class IDACServlet(BaseServlet): return ( f"", # requires http or else it defaults to https - f"http://{self.core_cfg.server.hostname}{t_port}/{game_code}/{game_ver}/", + f"http://{self.core_cfg.server.hostname}{t_port}/{game_ver}/", ) async def render_POST(self, request: Request) -> bytes: @@ -134,8 +135,43 @@ class IDACServlet(BaseServlet): resp = {"status_code": "0"} self.logger.debug(f"Response {resp}") - return JSONResponse(json.dumps(resp, ensure_ascii=False)) + return JSONResponseNoASCII(resp) + async def render_matching(self, request: Request): + url: str = request.path_params.get("endpoint") + ver: int = request.path_params.get("version") + client_ip = Utils.get_ip_addr(request) + req_data = await request.json() + header_application = self.decode_header(request.headers.get('application', '')) + user_id = int(header_application["session"]) + + # self.getMatchingStatus(user_id) + + self.logger.info( + f"IDAC Matching request from {client_ip}: {url} - {req_data}" + ) + + resp = {"status_code": "0"} + if url == "/regist": + self.queue = self.queue + 1 + elif url == "/status": + if req_data.get("cancel_flag"): + self.queue = self.queue - 1 + self.logger.info( + f"IDAC Matching endpoint {client_ip} had quited" + ) + + resp = { + "status_code": "0", + # Only IPv4 is supported + "host": self.game_config.server.matching_host, + "port": self.game_config.server.matching_p2p, + "room_name": "INDTA", + "state": 1, + } + + self.logger.debug(f"Response {resp}") + return JSONResponseNoASCII(resp) def decode_header(self, app: str) -> Dict: ret = {} @@ -147,22 +183,14 @@ class IDACServlet(BaseServlet): return ret def setup(self): - return - """ if self.game_cfg.server.enable: - endpoints.serverFromString( - reactor, - f"tcp:{self.game_cfg.server.matching}:interface={self.core_cfg.server.listen_address}", - ).listen(server.Site(IDACMatching(self.core_cfg, self.game_cfg))) - - reactor.listenUDP( - self.game_cfg.server.echo1, - IDACEchoUDP(self.core_cfg, self.game_cfg, self.game_cfg.server.echo1), - ) - reactor.listenUDP( - self.game_cfg.server.echo2, - IDACEchoUDP(self.core_cfg, self.game_cfg, self.game_cfg.server.echo2), + loop = asyncio.get_running_loop() + asyncio.create_task( + loop.create_datagram_endpoint( + lambda: IDACEchoUDP(), + local_addr=(self.core_cfg.server.listen_address, self.game_cfg.server.echo1) + ) ) - self.logger.info(f"Matching listening on {self.game_cfg.server.matching} with echos on {self.game_cfg.server.echo1} and {self.game_cfg.server.echo2}") - """ + self.logger.info(f"Matching listening on {self.game_cfg.server.matching} with echo on {self.game_cfg.server.echo1}") + diff --git a/titles/idac/season2.py b/titles/idac/season2.py index fabcbcd..2797990 100644 --- a/titles/idac/season2.py +++ b/titles/idac/season2.py @@ -109,9 +109,9 @@ class IDACSeason2(IDACBase): ver_str = version.replace(".", "")[:3] if self.core_cfg.server.is_develop: - domain_api_game = f"http://{self.core_cfg.server.hostname}:{self.core_cfg.server.port}/SDGT/{ver_str}/" + domain_api_game = f"http://{self.core_cfg.server.hostname}:{self.core_cfg.server.port}/{ver_str}/" else: - domain_api_game = f"http://{self.core_cfg.server.hostname}/SDGT/{ver_str}/" + domain_api_game = f"http://{self.core_cfg.server.hostname}/{ver_str}/" return { "status_code": "0", @@ -136,9 +136,9 @@ class IDACSeason2(IDACBase): "server_maintenance_end_hour": 0, "server_maintenance_end_minutes": 0, "domain_api_game": domain_api_game, - "domain_matching": f"http://{self.core_cfg.server.hostname}:{self.game_config.server.matching}", + "domain_matching": f"{domain_api_game}initiald-matching/", "domain_echo1": f"{self.core_cfg.server.hostname}:{self.game_config.server.echo1}", - "domain_echo2": f"{self.core_cfg.server.hostname}:{self.game_config.server.echo2}", + "domain_echo2": f"{self.core_cfg.server.hostname}:{self.game_config.server.echo1}", "domain_ping": f"{self.core_cfg.server.hostname}", "battle_gift_event_master": [], "round_event": [ diff --git a/titles/idz/echo.py b/titles/idz/echo.py index a8ff3a2..82f7003 100644 --- a/titles/idz/echo.py +++ b/titles/idz/echo.py @@ -9,6 +9,5 @@ class IDZEcho: self.transport = transport def datagram_received(self, data, addr): - message = data.decode() - self.logger.debug(f'Received echo from {addr}') + logging.getLogger('idz').debug(f'Received echo from {addr}') self.transport.sendto(data, addr)