2023-02-17 06:02:21 +00:00
|
|
|
import yaml
|
|
|
|
import logging, coloredlogs
|
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
from hashlib import md5
|
|
|
|
from twisted.web.http import Request
|
2023-03-05 02:34:35 +00:00
|
|
|
from typing import Dict, Tuple
|
|
|
|
from os import path
|
2023-02-17 06:02:21 +00:00
|
|
|
|
2023-03-12 06:00:51 +00:00
|
|
|
from core import CoreConfig, Utils
|
2023-02-17 06:02:21 +00:00
|
|
|
from titles.wacca.config import WaccaConfig
|
|
|
|
from titles.wacca.config import WaccaConfig
|
|
|
|
from titles.wacca.const import WaccaConstants
|
|
|
|
from titles.wacca.reverse import WaccaReverse
|
|
|
|
from titles.wacca.lilyr import WaccaLilyR
|
|
|
|
from titles.wacca.lily import WaccaLily
|
|
|
|
from titles.wacca.s import WaccaS
|
|
|
|
from titles.wacca.base import WaccaBase
|
|
|
|
from titles.wacca.handlers.base import BaseResponse
|
2023-03-02 02:48:43 +00:00
|
|
|
from titles.wacca.handlers.helpers import Version
|
2023-02-17 06:02:21 +00:00
|
|
|
|
2023-03-09 16:38:58 +00:00
|
|
|
|
|
|
|
class WaccaServlet:
|
2023-02-17 06:02:21 +00:00
|
|
|
def __init__(self, core_cfg: CoreConfig, cfg_dir: str) -> None:
|
|
|
|
self.core_cfg = core_cfg
|
|
|
|
self.game_cfg = WaccaConfig()
|
2023-03-05 02:58:51 +00:00
|
|
|
if path.exists(f"{cfg_dir}/{WaccaConstants.CONFIG_NAME}"):
|
2023-03-09 16:38:58 +00:00
|
|
|
self.game_cfg.update(
|
|
|
|
yaml.safe_load(open(f"{cfg_dir}/{WaccaConstants.CONFIG_NAME}"))
|
|
|
|
)
|
2023-02-17 06:02:21 +00:00
|
|
|
|
|
|
|
self.versions = [
|
|
|
|
WaccaBase(core_cfg, self.game_cfg),
|
|
|
|
WaccaS(core_cfg, self.game_cfg),
|
|
|
|
WaccaLily(core_cfg, self.game_cfg),
|
|
|
|
WaccaLilyR(core_cfg, self.game_cfg),
|
|
|
|
WaccaReverse(core_cfg, self.game_cfg),
|
|
|
|
]
|
|
|
|
|
|
|
|
self.logger = logging.getLogger("wacca")
|
|
|
|
log_fmt_str = "[%(asctime)s] Wacca | %(levelname)s | %(message)s"
|
|
|
|
log_fmt = logging.Formatter(log_fmt_str)
|
2023-03-09 16:38:58 +00:00
|
|
|
fileHandler = TimedRotatingFileHandler(
|
|
|
|
"{0}/{1}.log".format(self.core_cfg.server.log_dir, "wacca"),
|
|
|
|
encoding="utf8",
|
|
|
|
when="d",
|
|
|
|
backupCount=10,
|
|
|
|
)
|
2023-02-17 06:02:21 +00:00
|
|
|
|
|
|
|
fileHandler.setFormatter(log_fmt)
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
consoleHandler = logging.StreamHandler()
|
|
|
|
consoleHandler.setFormatter(log_fmt)
|
|
|
|
|
|
|
|
self.logger.addHandler(fileHandler)
|
|
|
|
self.logger.addHandler(consoleHandler)
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
self.logger.setLevel(self.game_cfg.server.loglevel)
|
2023-03-09 16:38:58 +00:00
|
|
|
coloredlogs.install(
|
|
|
|
level=self.game_cfg.server.loglevel, logger=self.logger, fmt=log_fmt_str
|
|
|
|
)
|
|
|
|
|
2023-03-05 02:34:35 +00:00
|
|
|
@classmethod
|
2023-03-09 16:38:58 +00:00
|
|
|
def get_allnet_info(
|
|
|
|
cls, game_code: str, core_cfg: CoreConfig, cfg_dir: str
|
|
|
|
) -> Tuple[bool, str, str]:
|
2023-03-05 02:34:35 +00:00
|
|
|
game_cfg = WaccaConfig()
|
2023-03-05 02:58:51 +00:00
|
|
|
if path.exists(f"{cfg_dir}/{WaccaConstants.CONFIG_NAME}"):
|
2023-03-09 16:38:58 +00:00
|
|
|
game_cfg.update(
|
|
|
|
yaml.safe_load(open(f"{cfg_dir}/{WaccaConstants.CONFIG_NAME}"))
|
|
|
|
)
|
2023-03-05 02:34:35 +00:00
|
|
|
|
|
|
|
if not game_cfg.server.enable:
|
|
|
|
return (False, "", "")
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-03-05 02:34:35 +00:00
|
|
|
if core_cfg.server.is_develop:
|
2023-03-09 16:38:58 +00:00
|
|
|
return (
|
|
|
|
True,
|
|
|
|
f"http://{core_cfg.title.hostname}:{core_cfg.title.port}/{game_code}/$v",
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
|
2023-03-05 02:58:51 +00:00
|
|
|
return (True, f"http://{core_cfg.title.hostname}/{game_code}/$v", "")
|
2023-03-05 02:34:35 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
def render_POST(self, request: Request, version: int, url_path: str) -> bytes:
|
|
|
|
def end(resp: Dict) -> bytes:
|
|
|
|
hash = md5(json.dumps(resp, ensure_ascii=False).encode()).digest()
|
|
|
|
request.responseHeaders.addRawHeader(b"X-Wacca-Hash", hash.hex().encode())
|
|
|
|
return json.dumps(resp).encode()
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-03-12 06:00:51 +00:00
|
|
|
client_ip = Utils.get_ip_addr(request)
|
2023-02-17 06:02:21 +00:00
|
|
|
try:
|
|
|
|
req_json = json.loads(request.content.getvalue())
|
2023-03-02 02:48:43 +00:00
|
|
|
version_full = Version(req_json["appVersion"])
|
2023-02-17 06:02:21 +00:00
|
|
|
except:
|
2023-03-09 16:38:58 +00:00
|
|
|
self.logger.error(
|
2023-03-09 17:02:02 +00:00
|
|
|
f"Failed to parse request to {url_path} -> {request.content.getvalue()}"
|
2023-03-09 16:38:58 +00:00
|
|
|
)
|
2023-02-17 06:02:21 +00:00
|
|
|
resp = BaseResponse()
|
|
|
|
resp.status = 1
|
|
|
|
resp.message = "不正なリクエスト エラーです"
|
|
|
|
return end(resp.make())
|
|
|
|
|
2023-03-18 16:26:57 +00:00
|
|
|
if "api/" in url_path:
|
2023-03-09 17:02:02 +00:00
|
|
|
func_to_find = (
|
|
|
|
"handle_" + url_path.partition("api/")[2].replace("/", "_") + "_request"
|
|
|
|
)
|
2023-02-17 06:02:21 +00:00
|
|
|
|
2023-03-09 17:02:02 +00:00
|
|
|
else:
|
|
|
|
self.logger.error(f"Malformed url {url_path}")
|
|
|
|
resp = BaseResponse()
|
|
|
|
resp.status = 1
|
|
|
|
resp.message = "Bad URL"
|
|
|
|
return end(resp.make())
|
2023-02-17 06:02:21 +00:00
|
|
|
|
2023-03-02 02:48:43 +00:00
|
|
|
ver_search = int(version_full)
|
2023-02-17 06:02:21 +00:00
|
|
|
|
|
|
|
if ver_search < 15000:
|
|
|
|
internal_ver = WaccaConstants.VER_WACCA
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
elif ver_search >= 15000 and ver_search < 20000:
|
|
|
|
internal_ver = WaccaConstants.VER_WACCA_S
|
|
|
|
|
|
|
|
elif ver_search >= 20000 and ver_search < 25000:
|
|
|
|
internal_ver = WaccaConstants.VER_WACCA_LILY
|
|
|
|
|
|
|
|
elif ver_search >= 25000 and ver_search < 30000:
|
|
|
|
internal_ver = WaccaConstants.VER_WACCA_LILY_R
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
elif ver_search >= 30000:
|
|
|
|
internal_ver = WaccaConstants.VER_WACCA_REVERSE
|
|
|
|
|
|
|
|
else:
|
2023-03-09 16:38:58 +00:00
|
|
|
self.logger.warning(
|
|
|
|
f"Unsupported version ({req_json['appVersion']}) request {url_path} - {req_json}"
|
|
|
|
)
|
2023-02-17 06:02:21 +00:00
|
|
|
resp = BaseResponse()
|
|
|
|
resp.status = 1
|
|
|
|
resp.message = "不正なアプリバージョンエラーです"
|
|
|
|
return end(resp.make())
|
2023-03-09 16:38:58 +00:00
|
|
|
|
|
|
|
self.logger.info(
|
2023-03-12 06:00:51 +00:00
|
|
|
f"v{req_json['appVersion']} {url_path} request from {client_ip} with chipId {req_json['chipId']}"
|
2023-03-09 16:38:58 +00:00
|
|
|
)
|
2023-02-17 06:02:21 +00:00
|
|
|
self.logger.debug(req_json)
|
|
|
|
|
2023-03-09 16:29:36 +00:00
|
|
|
if not hasattr(self.versions[internal_ver], func_to_find):
|
2023-03-09 16:38:58 +00:00
|
|
|
self.logger.warn(
|
|
|
|
f"{req_json['appVersion']} has no handler for {func_to_find}"
|
|
|
|
)
|
2023-03-09 16:29:36 +00:00
|
|
|
resp = BaseResponse().make()
|
|
|
|
return end(resp)
|
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
try:
|
|
|
|
handler = getattr(self.versions[internal_ver], func_to_find)
|
2023-03-09 16:29:36 +00:00
|
|
|
resp = handler(req_json)
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
self.logger.debug(f"{req_json['appVersion']} response {resp}")
|
|
|
|
return end(resp)
|
|
|
|
|
|
|
|
except Exception as e:
|
2023-03-09 16:38:58 +00:00
|
|
|
self.logger.error(
|
|
|
|
f"{req_json['appVersion']} Error handling method {url_path} -> {e}"
|
|
|
|
)
|
2023-03-09 16:29:36 +00:00
|
|
|
if self.core_cfg.server.is_develop:
|
2023-02-17 06:02:21 +00:00
|
|
|
raise
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-03-09 16:29:36 +00:00
|
|
|
resp = BaseResponse()
|
|
|
|
resp.status = 1
|
|
|
|
resp.message = "A server error occoured."
|
2023-02-17 06:02:21 +00:00
|
|
|
return end(resp)
|