2024-01-09 09:07:04 +01:00
|
|
|
from starlette.routing import Route
|
2023-02-17 07:02:21 +01:00
|
|
|
import yaml
|
|
|
|
import logging, coloredlogs
|
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
from hashlib import md5
|
2024-01-09 09:07:04 +01:00
|
|
|
from starlette.requests import Request
|
|
|
|
from starlette.responses import JSONResponse, Response
|
2023-11-09 03:17:48 +01:00
|
|
|
from typing import Dict, Tuple, List
|
2023-03-05 03:34:35 +01:00
|
|
|
from os import path
|
2023-11-17 16:57:12 +01:00
|
|
|
import traceback
|
2023-11-19 07:05:34 +01:00
|
|
|
import sys
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-03-12 07:00:51 +01:00
|
|
|
from core import CoreConfig, Utils
|
2024-04-08 23:07:18 +02:00
|
|
|
from core.data import Data
|
2024-01-09 09:07:04 +01:00
|
|
|
from core.title import BaseServlet
|
2023-11-09 03:17:48 +01:00
|
|
|
from .config import WaccaConfig
|
|
|
|
from .config import WaccaConfig
|
|
|
|
from .const import WaccaConstants
|
|
|
|
from .reverse import WaccaReverse
|
|
|
|
from .lilyr import WaccaLilyR
|
|
|
|
from .lily import WaccaLily
|
|
|
|
from .s import WaccaS
|
|
|
|
from .base import WaccaBase
|
2023-11-17 17:37:11 +01:00
|
|
|
from .handlers.base import BaseResponse, BaseRequest
|
2023-11-09 03:17:48 +01:00
|
|
|
from .handlers.helpers import Version
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
class WaccaServlet(BaseServlet):
|
2023-02-17 07:02:21 +01:00
|
|
|
def __init__(self, core_cfg: CoreConfig, cfg_dir: str) -> None:
|
|
|
|
self.core_cfg = core_cfg
|
|
|
|
self.game_cfg = WaccaConfig()
|
2023-03-05 03:58:51 +01:00
|
|
|
if path.exists(f"{cfg_dir}/{WaccaConstants.CONFIG_NAME}"):
|
2023-03-09 17:38:58 +01:00
|
|
|
self.game_cfg.update(
|
|
|
|
yaml.safe_load(open(f"{cfg_dir}/{WaccaConstants.CONFIG_NAME}"))
|
|
|
|
)
|
2024-04-08 23:07:18 +02:00
|
|
|
self.data = Data(core_cfg)
|
2023-02-17 07:02:21 +01: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 17:38:58 +01:00
|
|
|
fileHandler = TimedRotatingFileHandler(
|
|
|
|
"{0}/{1}.log".format(self.core_cfg.server.log_dir, "wacca"),
|
|
|
|
encoding="utf8",
|
|
|
|
when="d",
|
|
|
|
backupCount=10,
|
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
fileHandler.setFormatter(log_fmt)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
consoleHandler = logging.StreamHandler()
|
|
|
|
consoleHandler.setFormatter(log_fmt)
|
|
|
|
|
|
|
|
self.logger.addHandler(fileHandler)
|
|
|
|
self.logger.addHandler(consoleHandler)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +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
|
|
|
|
)
|
2024-01-09 09:07:04 +01:00
|
|
|
|
|
|
|
def get_routes(self) -> List[Route]:
|
|
|
|
return [
|
|
|
|
Route("/WaccaServlet/api/{api:str}/{endpoint:str}", self.render_POST, methods=['POST']),
|
|
|
|
Route("/WaccaServlet/api/{api:str}/{branch:str}/{endpoint:str}", self.render_POST, methods=['POST']),
|
|
|
|
]
|
2023-11-09 03:17:48 +01:00
|
|
|
|
2023-03-05 03:34:35 +01:00
|
|
|
@classmethod
|
2023-11-09 03:17:48 +01:00
|
|
|
def is_game_enabled(cls, game_code: str, core_cfg: CoreConfig, cfg_dir: str) -> bool:
|
2023-03-05 03:34:35 +01:00
|
|
|
game_cfg = WaccaConfig()
|
2023-03-05 03:58:51 +01:00
|
|
|
if path.exists(f"{cfg_dir}/{WaccaConstants.CONFIG_NAME}"):
|
2023-03-09 17:38:58 +01:00
|
|
|
game_cfg.update(
|
|
|
|
yaml.safe_load(open(f"{cfg_dir}/{WaccaConstants.CONFIG_NAME}"))
|
|
|
|
)
|
2023-03-05 03:34:35 +01:00
|
|
|
|
|
|
|
if not game_cfg.server.enable:
|
2023-11-09 03:17:48 +01:00
|
|
|
return False
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-11-09 03:17:48 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
def get_allnet_info(self, game_code: str, game_ver: int, keychip: str) -> Tuple[str, str]:
|
|
|
|
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 (
|
2024-01-09 09:07:04 +01:00
|
|
|
f"http://{self.core_cfg.server.hostname}:{Utils.get_title_port(self.core_cfg)}/WaccaServlet",
|
|
|
|
self.core_cfg.server.hostname,
|
2023-03-09 17:38:58 +01:00
|
|
|
)
|
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
return (f"http://{self.core_cfg.server.hostname}/WaccaServlet", self.core_cfg.server.hostname)
|
2023-11-09 03:17:48 +01:00
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
async def render_POST(self, request: Request) -> bytes:
|
2023-02-17 07:02:21 +01:00
|
|
|
def end(resp: Dict) -> bytes:
|
|
|
|
hash = md5(json.dumps(resp, ensure_ascii=False).encode()).digest()
|
2024-01-10 01:10:24 +01:00
|
|
|
j_Resp = Response(json.dumps(resp, ensure_ascii=False))
|
|
|
|
j_Resp.raw_headers.append((b"X-Wacca-Hash", hash.hex().encode()))
|
|
|
|
return j_Resp
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
api = request.path_params.get('api', '')
|
|
|
|
branch = request.path_params.get('branch', '')
|
|
|
|
endpoint = request.path_params.get('endpoint', '')
|
2023-03-12 07:00:51 +01:00
|
|
|
client_ip = Utils.get_ip_addr(request)
|
2024-01-09 09:07:04 +01:00
|
|
|
bod = await request.body()
|
2023-11-09 03:17:48 +01:00
|
|
|
|
|
|
|
if branch:
|
|
|
|
url_path = f"{api}/{branch}/{endpoint}"
|
|
|
|
func_to_find = f"handle_{api}_{branch}_{endpoint}_request"
|
|
|
|
|
|
|
|
else:
|
|
|
|
url_path = f"{api}/{endpoint}"
|
|
|
|
func_to_find = f"handle_{api}_{endpoint}_request"
|
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
try:
|
2024-01-09 09:07:04 +01:00
|
|
|
req_json = json.loads(bod)
|
2023-03-02 03:48:43 +01:00
|
|
|
version_full = Version(req_json["appVersion"])
|
2023-11-17 17:37:11 +01:00
|
|
|
req = BaseRequest(req_json)
|
2023-11-09 03:17:48 +01:00
|
|
|
|
2023-11-17 17:37:11 +01:00
|
|
|
except KeyError as e:
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger.error(
|
2024-01-09 09:07:04 +01:00
|
|
|
f"Failed to parse request to {bod} -> Missing required value {e}"
|
2023-11-17 17:37:11 +01:00
|
|
|
)
|
|
|
|
resp = BaseResponse()
|
|
|
|
resp.status = 1
|
|
|
|
resp.message = "不正なリクエスト エラーです"
|
|
|
|
return end(resp.make())
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.error(
|
2024-01-09 09:07:04 +01:00
|
|
|
f"Failed to parse request to {url_path} -> {bod} -> {e}"
|
2023-03-09 17:38:58 +01:00
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
resp = BaseResponse()
|
|
|
|
resp.status = 1
|
|
|
|
resp.message = "不正なリクエスト エラーです"
|
|
|
|
return end(resp.make())
|
2024-04-08 23:07:18 +02:00
|
|
|
|
|
|
|
if not self.core_cfg.server.allow_unregistered_serials:
|
|
|
|
mech = await self.data.arcade.get_machine(req.chipId)
|
|
|
|
if not mech:
|
|
|
|
self.logger.error(f"Blocked request from unregistered serial {req.chipId} to {url_path}")
|
|
|
|
resp = BaseResponse()
|
|
|
|
resp.status = 1
|
|
|
|
resp.message = "機械エラーです"
|
|
|
|
return end(resp.make())
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-03-02 03:48:43 +01:00
|
|
|
ver_search = int(version_full)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
|
|
|
if ver_search < 15000:
|
|
|
|
internal_ver = WaccaConstants.VER_WACCA
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01: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 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
elif ver_search >= 30000:
|
|
|
|
internal_ver = WaccaConstants.VER_WACCA_REVERSE
|
|
|
|
|
|
|
|
else:
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger.warning(
|
2023-11-17 17:37:11 +01:00
|
|
|
f"Unsupported version ({req.appVersion}) request {url_path} - {req_json}"
|
2023-03-09 17:38:58 +01:00
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
resp = BaseResponse()
|
|
|
|
resp.status = 1
|
|
|
|
resp.message = "不正なアプリバージョンエラーです"
|
|
|
|
return end(resp.make())
|
2023-03-09 17:38:58 +01:00
|
|
|
|
|
|
|
self.logger.info(
|
2023-11-17 17:37:11 +01:00
|
|
|
f"v{req.appVersion} {url_path} request from {client_ip} with chipId {req.chipId}"
|
2023-03-09 17:38:58 +01:00
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
self.logger.debug(req_json)
|
|
|
|
|
2023-03-09 17:29:36 +01:00
|
|
|
if not hasattr(self.versions[internal_ver], func_to_find):
|
2023-08-08 16:17:56 +02:00
|
|
|
self.logger.warning(
|
2023-03-09 17:38:58 +01:00
|
|
|
f"{req_json['appVersion']} has no handler for {func_to_find}"
|
|
|
|
)
|
2023-03-09 17:29:36 +01:00
|
|
|
resp = BaseResponse().make()
|
|
|
|
return end(resp)
|
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
try:
|
|
|
|
handler = getattr(self.versions[internal_ver], func_to_find)
|
2024-01-09 09:07:04 +01:00
|
|
|
resp = await handler(req_json)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-11-17 17:37:11 +01:00
|
|
|
self.logger.debug(f"{req.appVersion} response {resp}")
|
2023-02-17 07:02:21 +01:00
|
|
|
return end(resp)
|
|
|
|
|
|
|
|
except Exception as e:
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger.error(
|
2023-11-17 17:37:11 +01:00
|
|
|
f"{req.appVersion} Error handling method {url_path} -> {e}"
|
2023-03-09 17:38:58 +01:00
|
|
|
)
|
2023-11-17 16:57:12 +01:00
|
|
|
if self.logger.level == logging.DEBUG:
|
2023-11-19 07:05:34 +01:00
|
|
|
tp, val, tb = sys.exc_info()
|
2023-11-19 18:15:57 +01:00
|
|
|
traceback.print_exception(tp, val, tb, limit=3)
|
2023-11-17 16:57:12 +01:00
|
|
|
with open("{0}/{1}.log".format(self.core_cfg.server.log_dir, "wacca"), "a") as f:
|
2023-11-19 18:15:57 +01:00
|
|
|
traceback.print_exception(tp, val, tb, limit=3, file=f)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-03-09 17:29:36 +01:00
|
|
|
resp = BaseResponse()
|
|
|
|
resp.status = 1
|
|
|
|
resp.message = "A server error occoured."
|
2023-11-17 16:45:18 +01:00
|
|
|
return end(resp.make())
|