idac: add echo server

This commit is contained in:
Hay1tsme 2024-01-09 12:41:47 -05:00
parent 7b49a8ab58
commit 1c22c6bec0
4 changed files with 61 additions and 39 deletions

View File

@ -10,19 +10,14 @@ from titles.idac.config import IDACConfig
from titles.idac.database import IDACData from titles.idac.database import IDACData
class IDACEchoUDP(DatagramProtocol): class IDACEchoUDP:
def __init__(self, cfg: CoreConfig, game_cfg: IDACConfig, port: int) -> None: def connection_made(self, transport):
super().__init__() self.transport = transport
self.port = port
self.core_config = cfg def datagram_received(self, data, addr):
self.game_config = game_cfg logging.getLogger('idz').debug(f'Received echo from {addr}')
self.logger = logging.getLogger("idac") 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): class IDACEchoTCP(BaseRequestHandler):

View File

@ -9,10 +9,10 @@ import coloredlogs
from os import path from os import path
from typing import Dict, List, Tuple from typing import Dict, List, Tuple
from logging.handlers import TimedRotatingFileHandler from logging.handlers import TimedRotatingFileHandler
import asyncio
from core.config import CoreConfig from core.config import CoreConfig
from core.title import BaseServlet from core.title import BaseServlet, JSONResponseNoASCII
from core.utils import Utils from core.utils import Utils
from titles.idac.base import IDACBase from titles.idac.base import IDACBase
from titles.idac.season2 import IDACSeason2 from titles.idac.season2 import IDACSeason2
@ -75,7 +75,8 @@ class IDACServlet(BaseServlet):
def get_routes(self) -> List[Route]: def get_routes(self) -> List[Route]:
return [ 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( def get_allnet_info(
@ -87,7 +88,7 @@ class IDACServlet(BaseServlet):
return ( return (
f"", f"",
# requires http or else it defaults to https # 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: async def render_POST(self, request: Request) -> bytes:
@ -134,8 +135,43 @@ class IDACServlet(BaseServlet):
resp = {"status_code": "0"} resp = {"status_code": "0"}
self.logger.debug(f"Response {resp}") 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: def decode_header(self, app: str) -> Dict:
ret = {} ret = {}
@ -147,22 +183,14 @@ class IDACServlet(BaseServlet):
return ret return ret
def setup(self): def setup(self):
return
"""
if self.game_cfg.server.enable: if self.game_cfg.server.enable:
endpoints.serverFromString( loop = asyncio.get_running_loop()
reactor, asyncio.create_task(
f"tcp:{self.game_cfg.server.matching}:interface={self.core_cfg.server.listen_address}", loop.create_datagram_endpoint(
).listen(server.Site(IDACMatching(self.core_cfg, self.game_cfg))) lambda: IDACEchoUDP(),
local_addr=(self.core_cfg.server.listen_address, self.game_cfg.server.echo1)
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),
) )
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}")
"""

View File

@ -109,9 +109,9 @@ class IDACSeason2(IDACBase):
ver_str = version.replace(".", "")[:3] ver_str = version.replace(".", "")[:3]
if self.core_cfg.server.is_develop: 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: 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 { return {
"status_code": "0", "status_code": "0",
@ -136,9 +136,9 @@ class IDACSeason2(IDACBase):
"server_maintenance_end_hour": 0, "server_maintenance_end_hour": 0,
"server_maintenance_end_minutes": 0, "server_maintenance_end_minutes": 0,
"domain_api_game": domain_api_game, "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_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}", "domain_ping": f"{self.core_cfg.server.hostname}",
"battle_gift_event_master": [], "battle_gift_event_master": [],
"round_event": [ "round_event": [

View File

@ -9,6 +9,5 @@ class IDZEcho:
self.transport = transport self.transport = transport
def datagram_received(self, data, addr): def datagram_received(self, data, addr):
message = data.decode() logging.getLogger('idz').debug(f'Received echo from {addr}')
self.logger.debug(f'Received echo from {addr}')
self.transport.sendto(data, addr) self.transport.sendto(data, addr)