2023-02-17 07:02:21 +01:00
|
|
|
from twisted.web.http import Request
|
|
|
|
from twisted.web import resource, server
|
|
|
|
from twisted.internet import reactor, endpoints
|
|
|
|
import yaml
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
import inflection
|
|
|
|
import logging, coloredlogs
|
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
2023-03-05 03:58:51 +01:00
|
|
|
from typing import Dict, Tuple
|
|
|
|
from os import path
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
from core.config import CoreConfig
|
|
|
|
from titles.cxb.config import CxbConfig
|
|
|
|
from titles.cxb.const import CxbConstants
|
|
|
|
from titles.cxb.rev import CxbRev
|
|
|
|
from titles.cxb.rss1 import CxbRevSunriseS1
|
|
|
|
from titles.cxb.rss2 import CxbRevSunriseS2
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
class CxbServlet(resource.Resource):
|
|
|
|
def __init__(self, core_cfg: CoreConfig, cfg_dir: str) -> None:
|
|
|
|
self.isLeaf = True
|
|
|
|
self.cfg_dir = cfg_dir
|
|
|
|
self.core_cfg = core_cfg
|
|
|
|
self.game_cfg = CxbConfig()
|
2023-03-05 03:58:51 +01:00
|
|
|
if path.exists(f"{cfg_dir}/{CxbConstants.CONFIG_NAME}"):
|
2023-03-09 17:38:58 +01:00
|
|
|
self.game_cfg.update(
|
|
|
|
yaml.safe_load(open(f"{cfg_dir}/{CxbConstants.CONFIG_NAME}"))
|
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
self.logger = logging.getLogger("cxb")
|
2023-02-17 07:37:59 +01:00
|
|
|
if not hasattr(self.logger, "inited"):
|
|
|
|
log_fmt_str = "[%(asctime)s] CXB | %(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, "cxb"),
|
|
|
|
encoding="utf8",
|
|
|
|
when="d",
|
|
|
|
backupCount=10,
|
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-02-17 07:37:59 +01:00
|
|
|
fileHandler.setFormatter(log_fmt)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:37:59 +01:00
|
|
|
consoleHandler = logging.StreamHandler()
|
|
|
|
consoleHandler.setFormatter(log_fmt)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-02-17 07:37:59 +01:00
|
|
|
self.logger.addHandler(fileHandler)
|
|
|
|
self.logger.addHandler(consoleHandler)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:37:59 +01:00
|
|
|
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-02-17 07:37:59 +01:00
|
|
|
self.logger.inited = True
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
self.versions = [
|
|
|
|
CxbRev(core_cfg, self.game_cfg),
|
|
|
|
CxbRevSunriseS1(core_cfg, self.game_cfg),
|
|
|
|
CxbRevSunriseS2(core_cfg, self.game_cfg),
|
|
|
|
]
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-03-05 03:58:51 +01:00
|
|
|
@classmethod
|
2023-03-09 17:38:58 +01:00
|
|
|
def get_allnet_info(
|
|
|
|
cls, game_code: str, core_cfg: CoreConfig, cfg_dir: str
|
|
|
|
) -> Tuple[bool, str, str]:
|
2023-03-05 03:58:51 +01:00
|
|
|
game_cfg = CxbConfig()
|
|
|
|
if path.exists(f"{cfg_dir}/{CxbConstants.CONFIG_NAME}"):
|
2023-03-09 17:38:58 +01:00
|
|
|
game_cfg.update(
|
|
|
|
yaml.safe_load(open(f"{cfg_dir}/{CxbConstants.CONFIG_NAME}"))
|
|
|
|
)
|
2023-03-05 03:58:51 +01:00
|
|
|
|
|
|
|
if not game_cfg.server.enable:
|
|
|
|
return (False, "", "")
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-03-05 03:58:51 +01:00
|
|
|
if core_cfg.server.is_develop:
|
2023-03-09 17:38:58 +01:00
|
|
|
return (
|
|
|
|
True,
|
|
|
|
f"http://{core_cfg.title.hostname}:{core_cfg.title.port}/{game_code}/$v/",
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
|
2023-03-05 03:58:51 +01:00
|
|
|
return (True, f"http://{core_cfg.title.hostname}/{game_code}/$v/", "")
|
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
def setup(self):
|
|
|
|
if self.game_cfg.server.enable:
|
2023-03-09 17:38:58 +01:00
|
|
|
endpoints.serverFromString(
|
|
|
|
reactor,
|
|
|
|
f"tcp:{self.game_cfg.server.port}:interface={self.core_cfg.server.listen_address}",
|
|
|
|
).listen(server.Site(CxbServlet(self.core_cfg, self.cfg_dir)))
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
if self.core_cfg.server.is_develop and self.game_cfg.server.ssl_enable:
|
|
|
|
endpoints.serverFromString(
|
|
|
|
reactor,
|
|
|
|
f"ssl:{self.game_cfg.server.port_secure}"
|
|
|
|
f":interface={self.core_cfg.server.listen_address}:privateKey={self.game_cfg.server.ssl_key}:"
|
|
|
|
f"certKey={self.game_cfg.server.ssl_cert}",
|
|
|
|
).listen(server.Site(CxbServlet(self.core_cfg, self.cfg_dir)))
|
|
|
|
|
|
|
|
self.logger.info(
|
2023-03-12 07:59:12 +01:00
|
|
|
f"Ready on ports {self.game_cfg.server.port} & {self.game_cfg.server.port_secure}"
|
2023-03-09 17:38:58 +01:00
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
else:
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger.info(
|
2023-03-12 07:59:12 +01:00
|
|
|
f"Ready on port {self.game_cfg.server.port}"
|
2023-03-09 17:38:58 +01:00
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
def render_POST(self, request: Request):
|
|
|
|
version = 0
|
|
|
|
internal_ver = 0
|
|
|
|
func_to_find = ""
|
|
|
|
cmd = ""
|
|
|
|
subcmd = ""
|
|
|
|
req_url = request.uri.decode()
|
|
|
|
url_split = req_url.split("/")
|
|
|
|
req_bytes = request.content.getvalue()
|
|
|
|
|
|
|
|
try:
|
|
|
|
req_json: Dict = json.loads(req_bytes)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
try:
|
2023-03-09 17:38:58 +01:00
|
|
|
req_json: Dict = json.loads(
|
|
|
|
req_bytes.decode().replace('"', '\\"').replace("'", '"')
|
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
except Exception as f:
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger.warn(
|
|
|
|
f"Error decoding json: {e} / {f} - {req_url} - {req_bytes}"
|
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
return b""
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
if req_json == {}:
|
|
|
|
self.logger.warn(f"Empty json request to {req_url}")
|
|
|
|
return b""
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
cmd = url_split[len(url_split) - 1]
|
|
|
|
subcmd = list(req_json.keys())[0]
|
|
|
|
|
|
|
|
if subcmd == "dldate":
|
2023-03-09 17:38:58 +01:00
|
|
|
if (
|
|
|
|
not type(req_json["dldate"]) is dict
|
|
|
|
or "filetype" not in req_json["dldate"]
|
|
|
|
):
|
2023-02-17 07:02:21 +01:00
|
|
|
self.logger.warn(f"Malformed dldate request: {req_url} {req_json}")
|
|
|
|
return b""
|
|
|
|
|
|
|
|
filetype = req_json["dldate"]["filetype"]
|
|
|
|
filetype_split = filetype.split("/")
|
|
|
|
version = int(filetype_split[0])
|
|
|
|
filetype_inflect_split = inflection.underscore(filetype).split("/")
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
match = re.match(
|
|
|
|
"^([A-Za-z]*)(\d\d\d\d)$", filetype_split[len(filetype_split) - 1]
|
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
if match:
|
|
|
|
subcmd = f"{inflection.underscore(match.group(1))}xxxx"
|
|
|
|
else:
|
|
|
|
subcmd = f"{filetype_inflect_split[len(filetype_inflect_split) - 1]}"
|
|
|
|
else:
|
|
|
|
filetype = subcmd
|
|
|
|
|
|
|
|
func_to_find = f"handle_{cmd}_{subcmd}_request"
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
if version <= 10102:
|
|
|
|
version_string = "Rev"
|
|
|
|
internal_ver = CxbConstants.VER_CROSSBEATS_REV
|
|
|
|
|
|
|
|
elif version == 10113 or version == 10103:
|
|
|
|
version_string = "Rev SunriseS1"
|
|
|
|
internal_ver = CxbConstants.VER_CROSSBEATS_REV_SUNRISE_S1
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
elif version >= 10114 or version == 10104:
|
|
|
|
version_string = "Rev SunriseS2"
|
|
|
|
internal_ver = CxbConstants.VER_CROSSBEATS_REV_SUNRISE_S2
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
else:
|
|
|
|
version_string = "Base"
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
self.logger.info(f"{version_string} Request {req_url} -> {filetype}")
|
|
|
|
self.logger.debug(req_json)
|
|
|
|
|
|
|
|
try:
|
|
|
|
handler = getattr(self.versions[internal_ver], func_to_find)
|
|
|
|
resp = handler(req_json)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
|
|
|
except AttributeError as e:
|
2023-02-17 07:02:21 +01:00
|
|
|
self.logger.warning(f"Unhandled {version_string} request {req_url} - {e}")
|
|
|
|
resp = {}
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.error(f"Error handling {version_string} method {req_url} - {e}")
|
|
|
|
raise
|
2023-03-09 17:38:58 +01:00
|
|
|
|
|
|
|
self.logger.debug(f"{version_string} Response {resp}")
|
2023-02-17 07:02:21 +01:00
|
|
|
return json.dumps(resp, ensure_ascii=False).encode("utf-8")
|