2023-02-17 07:02:21 +01:00
|
|
|
from twisted.web.http import Request
|
2023-05-13 04:05:05 +02:00
|
|
|
from twisted.web.server import NOT_DONE_YET
|
2023-02-17 07:02:21 +01:00
|
|
|
import json
|
|
|
|
import inflection
|
|
|
|
import yaml
|
|
|
|
import string
|
|
|
|
import logging, coloredlogs
|
|
|
|
import zlib
|
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
2023-07-16 22:06:34 +02:00
|
|
|
from os import path, mkdir
|
2023-11-09 03:17:48 +01:00
|
|
|
from typing import Tuple, List, Dict
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
from core.config import CoreConfig
|
2023-04-10 18:58:19 +02:00
|
|
|
from core.utils import Utils
|
2023-11-09 03:17:48 +01:00
|
|
|
from core.title import BaseServlet
|
|
|
|
from .config import Mai2Config
|
|
|
|
from .const import Mai2Constants
|
|
|
|
from .base import Mai2Base
|
|
|
|
from .finale import Mai2Finale
|
|
|
|
from .dx import Mai2DX
|
|
|
|
from .dxplus import Mai2DXPlus
|
|
|
|
from .splash import Mai2Splash
|
|
|
|
from .splashplus import Mai2SplashPlus
|
|
|
|
from .universe import Mai2Universe
|
|
|
|
from .universeplus import Mai2UniversePlus
|
|
|
|
from .festival import Mai2Festival
|
|
|
|
from .festivalplus import Mai2FestivalPlus
|
|
|
|
|
|
|
|
|
|
|
|
class Mai2Servlet(BaseServlet):
|
2023-02-17 07:02:21 +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-02-17 07:02:21 +01:00
|
|
|
self.game_cfg = Mai2Config()
|
2023-03-05 03:58:51 +01:00
|
|
|
if path.exists(f"{cfg_dir}/{Mai2Constants.CONFIG_NAME}"):
|
2023-03-09 17:38:58 +01:00
|
|
|
self.game_cfg.update(
|
|
|
|
yaml.safe_load(open(f"{cfg_dir}/{Mai2Constants.CONFIG_NAME}"))
|
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
self.versions = [
|
2023-05-01 04:19:31 +02:00
|
|
|
Mai2Base,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
2023-10-15 19:04:15 +02:00
|
|
|
Mai2Finale,
|
2023-05-07 01:04:10 +02:00
|
|
|
Mai2DX,
|
|
|
|
Mai2DXPlus,
|
|
|
|
Mai2Splash,
|
|
|
|
Mai2SplashPlus,
|
|
|
|
Mai2Universe,
|
|
|
|
Mai2UniversePlus,
|
|
|
|
Mai2Festival,
|
2023-10-15 19:04:15 +02:00
|
|
|
Mai2FestivalPlus,
|
2023-05-01 04:19:31 +02:00
|
|
|
]
|
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
self.logger = logging.getLogger("mai2")
|
2023-05-03 09:25:29 +02:00
|
|
|
if not hasattr(self.logger, "initted"):
|
|
|
|
log_fmt_str = "[%(asctime)s] Mai2 | %(levelname)s | %(message)s"
|
|
|
|
log_fmt = logging.Formatter(log_fmt_str)
|
|
|
|
fileHandler = TimedRotatingFileHandler(
|
|
|
|
"{0}/{1}.log".format(self.core_cfg.server.log_dir, "mai2"),
|
|
|
|
encoding="utf8",
|
|
|
|
when="d",
|
|
|
|
backupCount=10,
|
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-05-03 09:25:29 +02:00
|
|
|
fileHandler.setFormatter(log_fmt)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-05-03 09:25:29 +02:00
|
|
|
consoleHandler = logging.StreamHandler()
|
|
|
|
consoleHandler.setFormatter(log_fmt)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-05-03 09:25:29 +02:00
|
|
|
self.logger.addHandler(fileHandler)
|
|
|
|
self.logger.addHandler(consoleHandler)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-05-03 09:25:29 +02:00
|
|
|
self.logger.setLevel(self.game_cfg.server.loglevel)
|
|
|
|
coloredlogs.install(
|
|
|
|
level=self.game_cfg.server.loglevel, logger=self.logger, fmt=log_fmt_str
|
|
|
|
)
|
|
|
|
self.logger.initted = True
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-03-05 03:58:51 +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-05 03:58:51 +01:00
|
|
|
game_cfg = Mai2Config()
|
|
|
|
|
|
|
|
if path.exists(f"{cfg_dir}/{Mai2Constants.CONFIG_NAME}"):
|
2023-03-09 17:38:58 +01:00
|
|
|
game_cfg.update(
|
|
|
|
yaml.safe_load(open(f"{cfg_dir}/{Mai2Constants.CONFIG_NAME}"))
|
|
|
|
)
|
2023-03-05 03:58:51 +01:00
|
|
|
|
|
|
|
if not game_cfg.server.enable:
|
2023-11-09 03:17:48 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_endpoint_matchers(self) -> Tuple[List[Tuple[str, str, Dict]], List[Tuple[str, str, Dict]]]:
|
|
|
|
return (
|
|
|
|
[
|
|
|
|
("handle_movie", "/{version}/MaimaiServlet/api/movie/{endpoint:..?}", {}),
|
|
|
|
("handle_old_srv", "/{version}/MaimaiServlet/old/{endpoint:..?}", {}),
|
|
|
|
("handle_usbdl", "/{version}/MaimaiServlet/usbdl/{endpoint:..?}", {}),
|
|
|
|
("handle_deliver", "/{version}/MaimaiServlet/deliver/{endpoint:..?}", {}),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
("handle_movie", "/{version}/MaimaiServlet/api/movie/{endpoint:..?}", {}),
|
|
|
|
("handle_mai", "/{version}/MaimaiServlet/{endpoint}", {}),
|
|
|
|
("handle_mai2", "/{version}/Maimai2Servlet/{endpoint}", {}),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_allnet_info(self, game_code: str, game_ver: int, keychip: str) -> Tuple[str, str]:
|
|
|
|
servlet_name = "" if game_code == Mai2Constants.GAME_CODE_DX else "MaimaiServlet/"
|
|
|
|
|
|
|
|
if not self.core_cfg.server.is_using_proxy and Utils.get_title_port(self.core_cfg) != 80:
|
2023-03-09 17:38:58 +01:00
|
|
|
return (
|
2023-11-09 03:17:48 +01:00
|
|
|
f"http://{self.core_cfg.title.hostname}:{Utils.get_title_port(self.core_cfg)}/{game_ver}/{servlet_name}",
|
|
|
|
f"{self.core_cfg.title.hostname}",
|
2023-03-09 17:38:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
2023-11-09 03:17:48 +01:00
|
|
|
f"http://{self.core_cfg.title.hostname}/{game_code}/{game_ver}/{servlet_name}",
|
|
|
|
f"{self.core_cfg.title.hostname}",
|
2023-03-09 17:38:58 +01:00
|
|
|
)
|
2023-03-05 03:58:51 +01:00
|
|
|
|
2023-07-16 22:06:34 +02:00
|
|
|
def setup(self):
|
2023-10-15 19:04:15 +02:00
|
|
|
if (
|
|
|
|
self.game_cfg.uploads.photos
|
|
|
|
and self.game_cfg.uploads.photos_dir
|
|
|
|
and not path.exists(self.game_cfg.uploads.photos_dir)
|
|
|
|
):
|
2023-07-16 22:06:34 +02:00
|
|
|
try:
|
|
|
|
mkdir(self.game_cfg.uploads.photos_dir)
|
2023-07-16 22:58:34 +02:00
|
|
|
except Exception:
|
2023-10-15 19:04:15 +02:00
|
|
|
self.logger.error(
|
|
|
|
f"Failed to make photo upload directory at {self.game_cfg.uploads.photos_dir}"
|
|
|
|
)
|
|
|
|
|
|
|
|
if (
|
|
|
|
self.game_cfg.uploads.movies
|
|
|
|
and self.game_cfg.uploads.movies_dir
|
|
|
|
and not path.exists(self.game_cfg.uploads.movies_dir)
|
|
|
|
):
|
2023-07-16 22:06:34 +02:00
|
|
|
try:
|
|
|
|
mkdir(self.game_cfg.uploads.movies_dir)
|
2023-07-16 22:58:34 +02:00
|
|
|
except Exception:
|
2023-10-15 19:04:15 +02:00
|
|
|
self.logger.error(
|
|
|
|
f"Failed to make movie upload directory at {self.game_cfg.uploads.movies_dir}"
|
|
|
|
)
|
2023-07-16 22:06:34 +02:00
|
|
|
|
2023-11-09 03:17:48 +01:00
|
|
|
def handle_mai2(self, request: Request, game_code: str, matchers: Dict) -> bytes:
|
|
|
|
endpoint = matchers['endpoint']
|
|
|
|
version = int(matchers['version'])
|
|
|
|
if endpoint.lower() == "ping":
|
2023-03-09 17:29:36 +01:00
|
|
|
return zlib.compress(b'{"returnCode": "1"}')
|
2023-10-15 19:04:15 +02:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
req_raw = request.content.getvalue()
|
|
|
|
internal_ver = 0
|
2023-04-10 18:58:19 +02:00
|
|
|
client_ip = Utils.get_ip_addr(request)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-11-09 03:17:48 +01:00
|
|
|
if game_code == "SDEZ":
|
2023-05-03 09:25:29 +02:00
|
|
|
if version < 105: # 1.0
|
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_DX
|
2023-10-15 19:04:15 +02:00
|
|
|
elif version >= 105 and version < 110: # PLUS
|
2023-05-03 09:25:29 +02:00
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_DX_PLUS
|
|
|
|
elif version >= 110 and version < 115: # Splash
|
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_DX_SPLASH
|
2023-10-15 19:04:15 +02:00
|
|
|
elif version >= 115 and version < 120: # Splash PLUS
|
2023-05-03 09:25:29 +02:00
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_DX_SPLASH_PLUS
|
2023-10-15 19:04:15 +02:00
|
|
|
elif version >= 120 and version < 125: # UNiVERSE
|
2023-05-03 09:25:29 +02:00
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_DX_UNIVERSE
|
2023-10-15 19:04:15 +02:00
|
|
|
elif version >= 125 and version < 130: # UNiVERSE PLUS
|
2023-05-03 09:25:29 +02:00
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_DX_UNIVERSE_PLUS
|
2023-10-15 19:04:15 +02:00
|
|
|
elif version >= 130 and version < 135: # FESTiVAL
|
2023-05-03 09:25:29 +02:00
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_DX_FESTIVAL
|
2023-10-15 19:04:15 +02:00
|
|
|
elif version >= 135: # FESTiVAL PLUS
|
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_DX_FESTIVAL_PLUS
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-05-03 09:25:29 +02:00
|
|
|
else:
|
|
|
|
if version < 110: # 1.0
|
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI
|
|
|
|
elif version >= 110 and version < 120: # Plus
|
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_PLUS
|
|
|
|
elif version >= 120 and version < 130: # Green
|
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_GREEN
|
2023-05-05 02:12:31 +02:00
|
|
|
elif version >= 130 and version < 140: # Green Plus
|
2023-05-03 09:25:29 +02:00
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_GREEN_PLUS
|
|
|
|
elif version >= 140 and version < 150: # Orange
|
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_ORANGE
|
2023-05-05 02:12:31 +02:00
|
|
|
elif version >= 150 and version < 160: # Orange Plus
|
2023-05-03 09:25:29 +02:00
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_ORANGE_PLUS
|
|
|
|
elif version >= 160 and version < 170: # Pink
|
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_PINK
|
2023-05-05 02:12:31 +02:00
|
|
|
elif version >= 170 and version < 180: # Pink Plus
|
2023-05-03 09:25:29 +02:00
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_PINK_PLUS
|
|
|
|
elif version >= 180 and version < 185: # Murasaki
|
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_MURASAKI
|
2023-05-05 02:12:31 +02:00
|
|
|
elif version >= 185 and version < 190: # Murasaki Plus
|
2023-05-03 09:25:29 +02:00
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_MURASAKI_PLUS
|
|
|
|
elif version >= 190 and version < 195: # Milk
|
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_MILK
|
2023-05-05 02:12:31 +02:00
|
|
|
elif version >= 195 and version < 197: # Milk Plus
|
2023-05-03 09:25:29 +02:00
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_MILK_PLUS
|
|
|
|
elif version >= 197: # Finale
|
|
|
|
internal_ver = Mai2Constants.VER_MAIMAI_FINALE
|
2023-10-15 19:04:15 +02:00
|
|
|
|
|
|
|
if (
|
|
|
|
request.getHeader("Mai-Encoding") is not None
|
|
|
|
or request.getHeader("X-Mai-Encoding") is not None
|
|
|
|
):
|
2023-08-01 08:34:40 +02:00
|
|
|
# The has is some flavor of MD5 of the endpoint with a constant bolted onto the end of it.
|
|
|
|
# See cake.dll's Obfuscator function for details. Hopefully most DLL edits will remove
|
|
|
|
# these two(?) headers to not cause issues, but given the general quality of SEGA data...
|
2023-10-15 19:04:15 +02:00
|
|
|
enc_ver = request.getHeader("Mai-Encoding")
|
2023-08-01 08:34:40 +02:00
|
|
|
if enc_ver is None:
|
2023-10-15 19:04:15 +02:00
|
|
|
enc_ver = request.getHeader("X-Mai-Encoding")
|
|
|
|
self.logger.debug(
|
|
|
|
f"Encryption v{enc_ver} - User-Agent: {request.getHeader('User-Agent')}"
|
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
try:
|
2023-02-17 07:02:21 +01:00
|
|
|
unzip = zlib.decompress(req_raw)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
except zlib.error as e:
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger.error(
|
|
|
|
f"Failed to decompress v{version} {endpoint} request -> {e}"
|
|
|
|
)
|
2023-03-09 17:29:36 +01:00
|
|
|
return zlib.compress(b'{"stat": "0"}')
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
req_data = json.loads(unzip)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-04-24 03:04:52 +02:00
|
|
|
self.logger.info(f"v{version} {endpoint} request from {client_ip}")
|
2023-04-10 18:58:19 +02:00
|
|
|
self.logger.debug(req_data)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
func_to_find = "handle_" + inflection.underscore(endpoint) + "_request"
|
2023-05-07 01:04:10 +02:00
|
|
|
handler_cls = self.versions[internal_ver](self.core_cfg, self.game_cfg)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-04-10 18:58:19 +02:00
|
|
|
if not hasattr(handler_cls, func_to_find):
|
2023-03-09 17:29:36 +01:00
|
|
|
self.logger.warning(f"Unhandled v{version} request {endpoint}")
|
2023-04-10 18:58:19 +02:00
|
|
|
resp = {"returnCode": 1}
|
2023-03-09 17:29:36 +01:00
|
|
|
|
2023-04-10 18:58:19 +02:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
handler = getattr(handler_cls, func_to_find)
|
|
|
|
resp = handler(req_data)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-04-10 18:58:19 +02:00
|
|
|
except Exception as e:
|
|
|
|
self.logger.error(f"Error handling v{version} method {endpoint} - {e}")
|
|
|
|
return zlib.compress(b'{"stat": "0"}')
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
if resp == None:
|
2023-03-09 17:38:58 +01:00
|
|
|
resp = {"returnCode": 1}
|
|
|
|
|
2023-04-10 18:58:19 +02:00
|
|
|
self.logger.debug(f"Response {resp}")
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
return zlib.compress(json.dumps(resp, ensure_ascii=False).encode("utf-8"))
|
2023-05-07 01:04:10 +02:00
|
|
|
|
|
|
|
def render_GET(self, request: Request, version: int, url_path: str) -> bytes:
|
2023-08-31 07:24:41 +02:00
|
|
|
self.logger.debug(f"v{version} GET {url_path}")
|
2023-05-10 08:31:30 +02:00
|
|
|
url_split = url_path.split("/")
|
|
|
|
|
2023-10-15 19:04:15 +02:00
|
|
|
if (url_split[0] == "api" and url_split[1] == "movie") or url_split[
|
|
|
|
0
|
|
|
|
] == "movie":
|
2023-06-26 01:10:34 +02:00
|
|
|
if url_split[2] == "moviestart":
|
2023-10-15 19:04:15 +02:00
|
|
|
return json.dumps({"moviestart": {"status": "OK"}}).encode()
|
|
|
|
|
2023-08-27 17:53:29 +02:00
|
|
|
else:
|
|
|
|
request.setResponseCode(404)
|
|
|
|
return b""
|
2023-06-26 00:43:00 +02:00
|
|
|
|
2023-05-10 08:31:30 +02:00
|
|
|
if url_split[0] == "old":
|
|
|
|
if url_split[1] == "ping":
|
|
|
|
self.logger.info(f"v{version} old server ping")
|
|
|
|
return zlib.compress(b"ok")
|
|
|
|
|
|
|
|
elif url_split[1].startswith("userdata"):
|
|
|
|
self.logger.info(f"v{version} old server userdata inquire")
|
|
|
|
return zlib.compress(b"{}")
|
|
|
|
|
|
|
|
elif url_split[1].startswith("friend"):
|
|
|
|
self.logger.info(f"v{version} old server friend inquire")
|
|
|
|
return zlib.compress(b"{}")
|
2023-10-15 19:04:15 +02:00
|
|
|
|
2023-08-27 17:53:29 +02:00
|
|
|
else:
|
|
|
|
request.setResponseCode(404)
|
|
|
|
return b""
|
2023-10-15 19:04:15 +02:00
|
|
|
|
2023-05-10 08:31:30 +02:00
|
|
|
elif url_split[0] == "usbdl":
|
|
|
|
if url_split[1] == "CONNECTIONTEST":
|
|
|
|
self.logger.info(f"v{version} usbdl server test")
|
2023-08-27 17:53:29 +02:00
|
|
|
return b""
|
2023-10-15 19:04:15 +02:00
|
|
|
|
|
|
|
elif self.game_cfg.deliver.udbdl_enable and path.exists(
|
|
|
|
f"{self.game_cfg.deliver.content_folder}/usb/{url_split[-1]}"
|
|
|
|
):
|
|
|
|
with open(
|
|
|
|
f"{self.game_cfg.deliver.content_folder}/usb/{url_split[-1]}", "rb"
|
|
|
|
) as f:
|
2023-08-27 17:53:29 +02:00
|
|
|
return f.read()
|
2023-10-15 19:04:15 +02:00
|
|
|
|
2023-08-27 17:53:29 +02:00
|
|
|
else:
|
|
|
|
request.setResponseCode(404)
|
|
|
|
return b""
|
2023-05-10 08:31:30 +02:00
|
|
|
|
|
|
|
elif url_split[0] == "deliver":
|
2023-05-13 04:05:05 +02:00
|
|
|
file = url_split[len(url_split) - 1]
|
|
|
|
self.logger.info(f"v{version} {file} deliver inquire")
|
2023-10-15 19:04:15 +02:00
|
|
|
self.logger.debug(
|
|
|
|
f"{self.game_cfg.deliver.content_folder}/net_deliver/{file}"
|
|
|
|
)
|
|
|
|
|
|
|
|
if self.game_cfg.deliver.enable and path.exists(
|
|
|
|
f"{self.game_cfg.deliver.content_folder}/net_deliver/{file}"
|
|
|
|
):
|
|
|
|
with open(
|
|
|
|
f"{self.game_cfg.deliver.content_folder}/net_deliver/{file}", "rb"
|
|
|
|
) as f:
|
2023-08-27 17:53:29 +02:00
|
|
|
return f.read()
|
2023-10-15 19:04:15 +02:00
|
|
|
|
2023-08-27 17:53:29 +02:00
|
|
|
else:
|
|
|
|
request.setResponseCode(404)
|
|
|
|
return b""
|
|
|
|
|
2023-05-07 01:04:10 +02:00
|
|
|
else:
|
2023-05-10 08:31:30 +02:00
|
|
|
return zlib.compress(b"{}")
|