2023-03-03 23:46:29 +01:00
|
|
|
import json
|
|
|
|
import inflection
|
|
|
|
import yaml
|
|
|
|
import string
|
|
|
|
import logging
|
|
|
|
import coloredlogs
|
|
|
|
import zlib
|
2024-01-09 09:07:04 +01:00
|
|
|
from starlette.routing import Route
|
|
|
|
from starlette.responses import Response
|
|
|
|
from starlette.requests import Request
|
2023-03-06 16:06:17 +01:00
|
|
|
from os import path
|
2024-01-09 09:07:04 +01:00
|
|
|
from typing import List
|
2023-03-03 23:46:29 +01:00
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
|
|
|
|
|
|
|
from core.config import CoreConfig
|
2023-04-13 22:22:28 +02:00
|
|
|
from core.utils import Utils
|
2023-11-09 03:17:48 +01:00
|
|
|
from core.title import BaseServlet
|
|
|
|
from .config import CardMakerConfig
|
|
|
|
from .const import CardMakerConstants
|
|
|
|
from .base import CardMakerBase
|
|
|
|
from .cm135 import CardMaker135
|
2023-03-03 23:46:29 +01:00
|
|
|
|
2023-11-09 03:17:48 +01:00
|
|
|
class CardMakerServlet(BaseServlet):
|
2023-03-03 23:46:29 +01:00
|
|
|
def __init__(self, core_cfg: CoreConfig, cfg_dir: str) -> None:
|
2023-11-09 03:17:48 +01:00
|
|
|
super().__init__(core_cfg, cfg_dir)
|
2023-03-03 23:46:29 +01:00
|
|
|
self.game_cfg = CardMakerConfig()
|
2023-03-06 16:06:17 +01:00
|
|
|
if path.exists(f"{cfg_dir}/{CardMakerConstants.CONFIG_NAME}"):
|
2023-03-09 17:38:58 +01:00
|
|
|
self.game_cfg.update(
|
|
|
|
yaml.safe_load(open(f"{cfg_dir}/{CardMakerConstants.CONFIG_NAME}"))
|
|
|
|
)
|
2023-03-03 23:46:29 +01:00
|
|
|
|
|
|
|
self.versions = [
|
2023-03-05 23:54:13 +01:00
|
|
|
CardMakerBase(core_cfg, self.game_cfg),
|
2023-05-30 12:14:18 +02:00
|
|
|
CardMaker135(core_cfg, self.game_cfg)
|
2023-03-03 23:46:29 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
self.logger = logging.getLogger("cardmaker")
|
|
|
|
log_fmt_str = "[%(asctime)s] Card Maker | %(levelname)s | %(message)s"
|
|
|
|
log_fmt = logging.Formatter(log_fmt_str)
|
2023-03-09 17:38:58 +01:00
|
|
|
fileHandler = TimedRotatingFileHandler(
|
|
|
|
"{0}/{1}.log".format(self.core_cfg.server.log_dir, "cardmaker"),
|
|
|
|
encoding="utf8",
|
|
|
|
when="d",
|
|
|
|
backupCount=10,
|
|
|
|
)
|
2023-03-03 23:46:29 +01:00
|
|
|
|
|
|
|
fileHandler.setFormatter(log_fmt)
|
|
|
|
|
|
|
|
consoleHandler = logging.StreamHandler()
|
|
|
|
consoleHandler.setFormatter(log_fmt)
|
|
|
|
|
|
|
|
self.logger.addHandler(fileHandler)
|
|
|
|
self.logger.addHandler(consoleHandler)
|
|
|
|
|
|
|
|
self.logger.setLevel(self.game_cfg.server.loglevel)
|
2023-03-09 17:38:58 +01:00
|
|
|
coloredlogs.install(
|
|
|
|
level=self.game_cfg.server.loglevel, logger=self.logger, fmt=log_fmt_str
|
|
|
|
)
|
2023-11-09 03:17:48 +01:00
|
|
|
|
2023-03-06 16:06:17 +01:00
|
|
|
@classmethod
|
2023-11-09 03:17:48 +01:00
|
|
|
def is_game_enabled(
|
2023-03-09 17:38:58 +01:00
|
|
|
cls, game_code: str, core_cfg: CoreConfig, cfg_dir: str
|
2023-11-09 03:17:48 +01:00
|
|
|
) -> bool:
|
2023-03-06 16:06:17 +01:00
|
|
|
game_cfg = CardMakerConfig()
|
|
|
|
if path.exists(f"{cfg_dir}/{CardMakerConstants.CONFIG_NAME}"):
|
2023-03-09 17:38:58 +01:00
|
|
|
game_cfg.update(
|
|
|
|
yaml.safe_load(open(f"{cfg_dir}/{CardMakerConstants.CONFIG_NAME}"))
|
|
|
|
)
|
2023-03-06 16:06:17 +01:00
|
|
|
|
|
|
|
if not game_cfg.server.enable:
|
2023-11-09 03:17:48 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
def get_routes(self) -> List[Route]:
|
|
|
|
return [
|
|
|
|
Route("/SDED/{version:int}/{endpoint:str}", self.render_POST)
|
|
|
|
]
|
|
|
|
|
|
|
|
async def render_POST(self, request: Request) -> bytes:
|
|
|
|
version: int = request.path_params.get('version')
|
|
|
|
endpoint: str = request.path_params.get('endpoint')
|
|
|
|
req_raw = await request.body()
|
2023-03-03 23:46:29 +01:00
|
|
|
internal_ver = 0
|
2023-04-13 22:22:28 +02:00
|
|
|
client_ip = Utils.get_ip_addr(request)
|
2023-03-03 23:46:29 +01:00
|
|
|
|
|
|
|
if version >= 130 and version < 135: # Card Maker
|
|
|
|
internal_ver = CardMakerConstants.VER_CARD_MAKER
|
2023-05-30 12:14:18 +02:00
|
|
|
elif version >= 135 and version < 140: # Card Maker 1.35
|
2023-03-15 21:03:22 +01:00
|
|
|
internal_ver = CardMakerConstants.VER_CARD_MAKER_135
|
2023-03-03 23:46:29 +01:00
|
|
|
|
|
|
|
if all(c in string.hexdigits for c in endpoint) and len(endpoint) == 32:
|
2023-03-05 23:54:13 +01:00
|
|
|
# If we get a 32 character long hex string, it's a hash and we're
|
|
|
|
# doing encrypted. The likelyhood of false positives is low but
|
2023-03-03 23:46:29 +01:00
|
|
|
# technically not 0
|
|
|
|
self.logger.error("Encryption not supported at this time")
|
|
|
|
|
2023-03-05 23:54:13 +01:00
|
|
|
try:
|
2023-03-03 23:46:29 +01:00
|
|
|
unzip = zlib.decompress(req_raw)
|
|
|
|
|
|
|
|
except zlib.error as e:
|
2023-03-05 23:54:13 +01:00
|
|
|
self.logger.error(
|
2023-03-09 17:38:58 +01:00
|
|
|
f"Failed to decompress v{version} {endpoint} request -> {e}"
|
|
|
|
)
|
2024-01-09 09:07:04 +01:00
|
|
|
return Response(zlib.compress(b'{"stat": "0"}'))
|
2023-03-03 23:46:29 +01:00
|
|
|
|
|
|
|
req_data = json.loads(unzip)
|
|
|
|
|
2023-04-24 03:04:52 +02:00
|
|
|
self.logger.info(f"v{version} {endpoint} request from {client_ip}")
|
2023-04-13 22:22:28 +02:00
|
|
|
self.logger.debug(req_data)
|
2023-03-03 23:46:29 +01:00
|
|
|
|
|
|
|
func_to_find = "handle_" + inflection.underscore(endpoint) + "_request"
|
|
|
|
|
2023-03-09 17:29:36 +01:00
|
|
|
if not hasattr(self.versions[internal_ver], func_to_find):
|
|
|
|
self.logger.warning(f"Unhandled v{version} request {endpoint}")
|
2024-01-09 09:07:04 +01:00
|
|
|
return Response(zlib.compress(b'{"returnCode": 1}'))
|
2023-03-09 17:29:36 +01:00
|
|
|
|
2023-03-03 23:46:29 +01:00
|
|
|
try:
|
|
|
|
handler = getattr(self.versions[internal_ver], func_to_find)
|
|
|
|
resp = handler(req_data)
|
|
|
|
|
|
|
|
except Exception as e:
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger.error(f"Error handling v{version} method {endpoint} - {e}")
|
2023-08-19 07:35:15 +02:00
|
|
|
raise
|
2024-01-09 09:07:04 +01:00
|
|
|
return Response(zlib.compress(b'{"stat": "0"}'))
|
2023-03-03 23:46:29 +01:00
|
|
|
|
|
|
|
if resp is None:
|
2023-03-09 17:38:58 +01:00
|
|
|
resp = {"returnCode": 1}
|
2023-03-03 23:46:29 +01:00
|
|
|
|
2023-07-15 22:51:54 +02:00
|
|
|
self.logger.debug(f"Response {resp}")
|
2023-03-03 23:46:29 +01:00
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
return Response(zlib.compress(json.dumps(resp, ensure_ascii=False).encode("utf-8")))
|