2023-03-03 22:46:29 +00:00
|
|
|
import json
|
|
|
|
import inflection
|
|
|
|
import yaml
|
|
|
|
import string
|
|
|
|
import logging
|
|
|
|
import coloredlogs
|
|
|
|
import zlib
|
2023-03-06 15:06:17 +00:00
|
|
|
|
|
|
|
from os import path
|
|
|
|
from typing import Tuple
|
|
|
|
from twisted.web.http import Request
|
2023-03-03 22:46:29 +00:00
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
|
|
|
|
|
|
|
from core.config import CoreConfig
|
|
|
|
from titles.cm.config import CardMakerConfig
|
|
|
|
from titles.cm.const import CardMakerConstants
|
|
|
|
from titles.cm.base import CardMakerBase
|
2023-03-05 22:54:13 +00:00
|
|
|
from titles.cm.cm136 import CardMaker136
|
2023-03-03 22:46:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CardMakerServlet():
|
|
|
|
def __init__(self, core_cfg: CoreConfig, cfg_dir: str) -> None:
|
|
|
|
self.core_cfg = core_cfg
|
|
|
|
self.game_cfg = CardMakerConfig()
|
2023-03-06 15:06:17 +00:00
|
|
|
if path.exists(f"{cfg_dir}/{CardMakerConstants.CONFIG_NAME}"):
|
|
|
|
self.game_cfg.update(yaml.safe_load(open(f"{cfg_dir}/{CardMakerConstants.CONFIG_NAME}")))
|
2023-03-03 22:46:29 +00:00
|
|
|
|
|
|
|
self.versions = [
|
2023-03-05 22:54:13 +00:00
|
|
|
CardMakerBase(core_cfg, self.game_cfg),
|
|
|
|
CardMaker136(core_cfg, self.game_cfg)
|
2023-03-03 22:46:29 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
self.logger = logging.getLogger("cardmaker")
|
|
|
|
log_fmt_str = "[%(asctime)s] Card Maker | %(levelname)s | %(message)s"
|
|
|
|
log_fmt = logging.Formatter(log_fmt_str)
|
|
|
|
fileHandler = TimedRotatingFileHandler("{0}/{1}.log".format(self.core_cfg.server.log_dir, "cardmaker"), encoding='utf8',
|
2023-03-05 22:54:13 +00:00
|
|
|
when="d", backupCount=10)
|
2023-03-03 22:46:29 +00: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-05 22:54:13 +00:00
|
|
|
coloredlogs.install(level=self.game_cfg.server.loglevel,
|
|
|
|
logger=self.logger, fmt=log_fmt_str)
|
2023-03-03 22:46:29 +00:00
|
|
|
|
2023-03-06 15:06:17 +00:00
|
|
|
@classmethod
|
|
|
|
def get_allnet_info(cls, game_code: str, core_cfg: CoreConfig, cfg_dir: str) -> Tuple[bool, str, str]:
|
|
|
|
game_cfg = CardMakerConfig()
|
|
|
|
if path.exists(f"{cfg_dir}/{CardMakerConstants.CONFIG_NAME}"):
|
|
|
|
game_cfg.update(yaml.safe_load(open(f"{cfg_dir}/{CardMakerConstants.CONFIG_NAME}")))
|
|
|
|
|
|
|
|
if not game_cfg.server.enable:
|
|
|
|
return (False, "", "")
|
|
|
|
|
|
|
|
if core_cfg.server.is_develop:
|
|
|
|
return (True, f"http://{core_cfg.title.hostname}:{core_cfg.title.port}/{game_code}/$v/", "")
|
|
|
|
|
|
|
|
return (True, f"http://{core_cfg.title.hostname}/{game_code}/$v/", "")
|
|
|
|
|
2023-03-03 22:46:29 +00:00
|
|
|
def render_POST(self, request: Request, version: int, url_path: str) -> bytes:
|
|
|
|
req_raw = request.content.getvalue()
|
|
|
|
url_split = url_path.split("/")
|
|
|
|
internal_ver = 0
|
|
|
|
endpoint = url_split[len(url_split) - 1]
|
|
|
|
|
|
|
|
print(f"version: {version}")
|
|
|
|
|
|
|
|
if version >= 130 and version < 135: # Card Maker
|
|
|
|
internal_ver = CardMakerConstants.VER_CARD_MAKER
|
2023-03-05 22:54:13 +00:00
|
|
|
elif version >= 135 and version < 140: # Card Maker
|
|
|
|
internal_ver = CardMakerConstants.VER_CARD_MAKER_136
|
2023-03-03 22:46:29 +00:00
|
|
|
|
|
|
|
if all(c in string.hexdigits for c in endpoint) and len(endpoint) == 32:
|
2023-03-05 22:54:13 +00: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 22:46:29 +00:00
|
|
|
# technically not 0
|
|
|
|
self.logger.error("Encryption not supported at this time")
|
|
|
|
|
2023-03-05 22:54:13 +00:00
|
|
|
try:
|
2023-03-03 22:46:29 +00:00
|
|
|
unzip = zlib.decompress(req_raw)
|
|
|
|
|
|
|
|
except zlib.error as e:
|
2023-03-05 22:54:13 +00:00
|
|
|
self.logger.error(
|
|
|
|
f"Failed to decompress v{version} {endpoint} request -> {e}")
|
2023-03-09 16:29:36 +00:00
|
|
|
return zlib.compress(b'{"stat": "0"}')
|
2023-03-03 22:46:29 +00:00
|
|
|
|
|
|
|
req_data = json.loads(unzip)
|
|
|
|
|
|
|
|
self.logger.info(f"v{version} {endpoint} request - {req_data}")
|
|
|
|
|
|
|
|
func_to_find = "handle_" + inflection.underscore(endpoint) + "_request"
|
|
|
|
|
2023-03-09 16:29:36 +00:00
|
|
|
if not hasattr(self.versions[internal_ver], func_to_find):
|
|
|
|
self.logger.warning(f"Unhandled v{version} request {endpoint}")
|
|
|
|
return zlib.compress(b'{"returnCode": 1}')
|
|
|
|
|
2023-03-03 22:46:29 +00:00
|
|
|
try:
|
|
|
|
handler = getattr(self.versions[internal_ver], func_to_find)
|
|
|
|
resp = handler(req_data)
|
|
|
|
|
|
|
|
except Exception as e:
|
2023-03-05 22:54:13 +00:00
|
|
|
self.logger.error(
|
|
|
|
f"Error handling v{version} method {endpoint} - {e}")
|
2023-03-09 16:29:36 +00:00
|
|
|
return zlib.compress(b'{"stat": "0"}')
|
2023-03-03 22:46:29 +00:00
|
|
|
|
|
|
|
if resp is None:
|
|
|
|
resp = {'returnCode': 1}
|
|
|
|
|
|
|
|
self.logger.info(f"Response {resp}")
|
|
|
|
|
|
|
|
return zlib.compress(json.dumps(resp, ensure_ascii=False).encode("utf-8"))
|