1
0
mirror of synced 2024-12-12 05:51:04 +01:00
artemis/titles/pokken/index.py

155 lines
5.5 KiB
Python
Raw Normal View History

from typing import Tuple
2023-02-17 08:09:26 +01:00
from twisted.web.http import Request
from twisted.web import resource, server
from twisted.internet import reactor, endpoints
import yaml
import logging, coloredlogs
from logging.handlers import TimedRotatingFileHandler
from titles.pokken.proto import jackal_pb2
2023-02-27 22:51:17 +01:00
from os import path
2023-02-28 06:41:32 +01:00
from google.protobuf.message import DecodeError
2023-02-17 08:09:26 +01:00
from core.config import CoreConfig
from titles.pokken.config import PokkenConfig
from titles.pokken.base import PokkenBase
from titles.pokken.const import PokkenConstants
2023-02-17 08:09:26 +01:00
2023-03-09 17:38:58 +01:00
2023-02-17 08:09:26 +01:00
class PokkenServlet(resource.Resource):
def __init__(self, core_cfg: CoreConfig, cfg_dir: str) -> None:
self.isLeaf = True
self.core_cfg = core_cfg
self.config_dir = cfg_dir
self.game_cfg = PokkenConfig()
if path.exists(f"{cfg_dir}/pokken.yaml"):
self.game_cfg.update(yaml.safe_load(open(f"{cfg_dir}/pokken.yaml")))
2023-02-17 08:09:26 +01:00
self.logger = logging.getLogger("pokken")
if not hasattr(self.logger, "inited"):
log_fmt_str = "[%(asctime)s] Pokken | %(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, "pokken"),
encoding="utf8",
when="d",
backupCount=10,
)
2023-02-17 08:09:26 +01:00
fileHandler.setFormatter(log_fmt)
2023-03-09 17:38:58 +01:00
2023-02-17 08:09:26 +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 08:09:26 +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 08:09:26 +01:00
self.logger.inited = True
self.base = PokkenBase(core_cfg, self.game_cfg)
@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]:
game_cfg = PokkenConfig()
2023-03-09 17:38:58 +01:00
if path.exists(f"{cfg_dir}/{PokkenConstants.CONFIG_NAME}"):
2023-03-09 17:38:58 +01:00
game_cfg.update(
yaml.safe_load(open(f"{cfg_dir}/{PokkenConstants.CONFIG_NAME}"))
)
if not game_cfg.server.enable:
return (False, "", "")
2023-03-09 17:38:58 +01:00
if core_cfg.server.is_develop:
2023-03-09 17:38:58 +01:00
return (
True,
f"https://{game_cfg.server.hostname}:{game_cfg.server.port}/{game_code}/$v/",
f"{game_cfg.server.hostname}:{game_cfg.server.port}/",
)
return (
True,
f"https://{game_cfg.server.hostname}/{game_code}/$v/",
f"{game_cfg.server.hostname}/",
)
2023-03-05 04:46:26 +01:00
@classmethod
2023-03-09 17:38:58 +01:00
def get_mucha_info(
cls, core_cfg: CoreConfig, cfg_dir: str
) -> Tuple[bool, str, str]:
2023-03-05 04:46:26 +01:00
game_cfg = PokkenConfig()
2023-03-09 17:38:58 +01:00
2023-03-05 04:46:26 +01:00
if path.exists(f"{cfg_dir}/{PokkenConstants.CONFIG_NAME}"):
2023-03-09 17:38:58 +01:00
game_cfg.update(
yaml.safe_load(open(f"{cfg_dir}/{PokkenConstants.CONFIG_NAME}"))
)
2023-03-05 04:46:26 +01:00
if not game_cfg.server.enable:
return (False, "")
2023-03-09 17:38:58 +01:00
2023-03-05 04:46:26 +01:00
return (True, "PKFN")
2023-03-09 17:38:58 +01:00
2023-02-17 08:09:26 +01:00
def setup(self):
2023-02-28 06:41:32 +01:00
"""
There's currently no point in having this server on because Twisted
2023-03-09 17:38:58 +01:00
won't play ball with both the fact that it's TLSv1.1, and because the
2023-02-28 06:41:32 +01:00
types of certs that pokken will accept are too flimsy for Twisted
so it will throw a fit. Currently leaving this here in case a bypass
is discovered in the future, but it's unlikly. For now, just use NGINX.
"""
2023-03-09 17:38:58 +01:00
if self.game_cfg.server.enable and self.core_cfg.server.is_develop:
2023-02-27 22:51:17 +01:00
key_exists = path.exists(self.game_cfg.server.ssl_key)
cert_exists = path.exists(self.game_cfg.server.ssl_cert)
2023-03-09 17:38:58 +01:00
2023-02-28 06:41:32 +01:00
if key_exists and cert_exists:
2023-03-09 17:38:58 +01:00
endpoints.serverFromString(
reactor,
f"ssl:{self.game_cfg.server.port}"
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(self))
self.logger.info(
f"Pokken title server ready on port {self.game_cfg.server.port}"
)
2023-02-17 08:09:26 +01:00
else:
2023-03-09 17:38:58 +01:00
self.logger.error(
f"Could not find cert at {self.game_cfg.server.ssl_key} or key at {self.game_cfg.server.ssl_cert}, Pokken not running."
)
2023-02-17 08:09:26 +01:00
2023-03-09 17:38:58 +01:00
def render_POST(
self, request: Request, version: int = 0, endpoints: str = ""
) -> bytes:
2023-02-28 06:41:32 +01:00
if endpoints == "":
endpoints = request.uri.decode()
if endpoints.startswith("/matching"):
2023-02-17 08:09:26 +01:00
self.logger.info("Matching request")
2023-03-09 17:38:58 +01:00
2023-02-28 06:41:32 +01:00
content = request.content.getvalue()
if content == b"":
self.logger.info("Empty request")
return b""
2023-02-17 08:09:26 +01:00
pokken_request = jackal_pb2.Request()
2023-02-28 06:41:32 +01:00
try:
pokken_request.ParseFromString(content)
except DecodeError as e:
self.logger.warn(f"{e} {content}")
return b""
2023-02-17 08:09:26 +01:00
endpoint = jackal_pb2.MessageType(pokken_request.type).name.lower()
2023-03-09 17:38:58 +01:00
2023-02-17 08:09:26 +01:00
self.logger.info(f"{endpoint} request")
handler = getattr(self.base, f"handle_{endpoint}", None)
if handler is None:
self.logger.warn(f"No handler found for message type {endpoint}")
return self.base.handle_noop(pokken_request)
return handler(pokken_request)