2023-02-24 19:13:31 +00:00
|
|
|
from typing import Dict, Any
|
2023-02-16 22:13:41 +00:00
|
|
|
from types import ModuleType
|
2023-03-12 06:00:51 +00:00
|
|
|
from twisted.web.http import Request
|
2023-02-24 19:13:31 +00:00
|
|
|
import logging
|
2023-02-16 22:13:41 +00:00
|
|
|
import importlib
|
|
|
|
from os import walk
|
|
|
|
|
2023-11-09 02:17:48 +00:00
|
|
|
from .config import CoreConfig
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-16 22:13:41 +00:00
|
|
|
class Utils:
|
2023-11-09 02:17:48 +00:00
|
|
|
real_title_port = None
|
|
|
|
real_title_port_ssl = None
|
2023-02-16 22:13:41 +00:00
|
|
|
@classmethod
|
|
|
|
def get_all_titles(cls) -> Dict[str, ModuleType]:
|
|
|
|
ret: Dict[str, Any] = {}
|
|
|
|
|
|
|
|
for root, dirs, files in walk("titles"):
|
2023-03-09 16:38:58 +00:00
|
|
|
for dir in dirs:
|
2023-02-16 22:13:41 +00:00
|
|
|
if not dir.startswith("__"):
|
|
|
|
try:
|
|
|
|
mod = importlib.import_module(f"titles.{dir}")
|
2023-04-24 01:04:52 +00:00
|
|
|
if hasattr(mod, "game_codes") and hasattr(
|
|
|
|
mod, "index"
|
|
|
|
): # Minimum required to function
|
2023-04-15 04:12:45 +00:00
|
|
|
ret[dir] = mod
|
2023-02-16 22:13:41 +00:00
|
|
|
|
|
|
|
except ImportError as e:
|
2023-02-24 19:13:31 +00:00
|
|
|
logging.getLogger("core").error(f"get_all_titles: {dir} - {e}")
|
2023-02-17 06:02:21 +00:00
|
|
|
raise
|
2023-02-16 22:13:41 +00:00
|
|
|
return ret
|
2023-04-24 01:04:52 +00:00
|
|
|
|
2023-03-12 06:00:51 +00:00
|
|
|
@classmethod
|
|
|
|
def get_ip_addr(cls, req: Request) -> str:
|
2023-04-24 01:04:52 +00:00
|
|
|
return (
|
|
|
|
req.getAllHeaders()[b"x-forwarded-for"].decode()
|
|
|
|
if b"x-forwarded-for" in req.getAllHeaders()
|
|
|
|
else req.getClientAddress().host
|
|
|
|
)
|
2023-11-09 02:17:48 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_title_port(cls, cfg: CoreConfig):
|
|
|
|
if cls.real_title_port is not None: return cls.real_title_port
|
|
|
|
|
|
|
|
if cfg.title.port == 0:
|
|
|
|
cls.real_title_port = cfg.allnet.port
|
|
|
|
|
|
|
|
else:
|
|
|
|
cls.real_title_port = cfg.title.port
|
|
|
|
|
|
|
|
return cls.real_title_port
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_title_port_ssl(cls, cfg: CoreConfig):
|
|
|
|
if cls.real_title_port_ssl is not None: return cls.real_title_port_ssl
|
|
|
|
|
|
|
|
if cfg.title.port_ssl == 0:
|
|
|
|
cls.real_title_port_ssl = 443
|
|
|
|
|
|
|
|
else:
|
|
|
|
cls.real_title_port_ssl = cfg.title.port_ssl
|
|
|
|
|
|
|
|
return cls.real_title_port_ssl
|