2023-02-16 06:06:42 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import argparse
|
2023-02-24 20:07:54 +01:00
|
|
|
import logging, coloredlogs
|
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
2023-02-19 05:12:40 +01:00
|
|
|
from typing import Dict
|
2023-02-16 06:06:42 +01:00
|
|
|
import yaml
|
|
|
|
from os import path, mkdir, access, W_OK
|
|
|
|
from core import *
|
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
from twisted.web import server, resource
|
2023-02-16 06:06:42 +01:00
|
|
|
from twisted.internet import reactor, endpoints
|
2023-02-17 07:02:21 +01:00
|
|
|
from twisted.web.http import Request
|
|
|
|
from routes import Mapper
|
2023-07-08 03:50:24 +02:00
|
|
|
from threading import Thread
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
class HttpDispatcher(resource.Resource):
|
|
|
|
def __init__(self, cfg: CoreConfig, config_dir: str):
|
|
|
|
super().__init__()
|
|
|
|
self.config = cfg
|
|
|
|
self.isLeaf = True
|
|
|
|
self.map_get = Mapper()
|
|
|
|
self.map_post = Mapper()
|
2023-02-24 20:07:54 +01:00
|
|
|
self.logger = logging.getLogger("core")
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
self.allnet = AllnetServlet(cfg, config_dir)
|
|
|
|
self.title = TitleServlet(cfg, config_dir)
|
2023-03-05 04:46:26 +01:00
|
|
|
self.mucha = MuchaServlet(cfg, config_dir)
|
2023-04-24 03:04:52 +02:00
|
|
|
|
2023-04-24 01:00:30 +02:00
|
|
|
self.map_get.connect(
|
|
|
|
"allnet_downloadorder_ini",
|
|
|
|
"/dl/ini/{file}",
|
|
|
|
controller="allnet",
|
|
|
|
action="handle_dlorder_ini",
|
|
|
|
conditions=dict(method=["GET"]),
|
|
|
|
)
|
|
|
|
|
|
|
|
self.map_post.connect(
|
|
|
|
"allnet_downloadorder_report",
|
2023-07-23 18:47:10 +02:00
|
|
|
"/report-api/Report",
|
2023-04-24 01:00:30 +02:00
|
|
|
controller="allnet",
|
|
|
|
action="handle_dlorder_report",
|
|
|
|
conditions=dict(method=["POST"]),
|
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-05-03 05:55:57 +02:00
|
|
|
self.map_get.connect(
|
2023-03-09 17:38:58 +01:00
|
|
|
"allnet_ping",
|
|
|
|
"/naomitest.html",
|
|
|
|
controller="allnet",
|
|
|
|
action="handle_naomitest",
|
|
|
|
conditions=dict(method=["GET"]),
|
|
|
|
)
|
|
|
|
self.map_post.connect(
|
|
|
|
"allnet_poweron",
|
|
|
|
"/sys/servlet/PowerOn",
|
|
|
|
controller="allnet",
|
|
|
|
action="handle_poweron",
|
|
|
|
conditions=dict(method=["POST"]),
|
|
|
|
)
|
|
|
|
self.map_post.connect(
|
|
|
|
"allnet_downloadorder",
|
|
|
|
"/sys/servlet/DownloadOrder",
|
|
|
|
controller="allnet",
|
|
|
|
action="handle_dlorder",
|
|
|
|
conditions=dict(method=["POST"]),
|
|
|
|
)
|
2023-06-30 06:26:07 +02:00
|
|
|
self.map_post.connect(
|
|
|
|
"allnet_loaderstaterecorder",
|
|
|
|
"/sys/servlet/LoaderStateRecorder",
|
|
|
|
controller="allnet",
|
|
|
|
action="handle_loaderstaterecorder",
|
|
|
|
conditions=dict(method=["POST"]),
|
|
|
|
)
|
2023-06-30 06:32:52 +02:00
|
|
|
self.map_post.connect(
|
|
|
|
"allnet_alive",
|
|
|
|
"/sys/servlet/Alive",
|
|
|
|
controller="allnet",
|
|
|
|
action="handle_alive",
|
|
|
|
conditions=dict(method=["POST"]),
|
|
|
|
)
|
|
|
|
self.map_get.connect(
|
|
|
|
"allnet_alive",
|
|
|
|
"/sys/servlet/Alive",
|
|
|
|
controller="allnet",
|
|
|
|
action="handle_alive",
|
|
|
|
conditions=dict(method=["GET"]),
|
|
|
|
)
|
2023-03-09 17:38:58 +01:00
|
|
|
self.map_post.connect(
|
|
|
|
"allnet_billing",
|
|
|
|
"/request",
|
|
|
|
controller="allnet",
|
|
|
|
action="handle_billing_request",
|
|
|
|
conditions=dict(method=["POST"]),
|
|
|
|
)
|
|
|
|
self.map_post.connect(
|
|
|
|
"allnet_billing",
|
|
|
|
"/request/",
|
|
|
|
controller="allnet",
|
|
|
|
action="handle_billing_request",
|
|
|
|
conditions=dict(method=["POST"]),
|
|
|
|
)
|
|
|
|
|
2023-07-25 15:18:45 +02:00
|
|
|
# Maintain compatability
|
2023-03-09 17:38:58 +01:00
|
|
|
self.map_post.connect(
|
|
|
|
"mucha_boardauth",
|
|
|
|
"/mucha/boardauth.do",
|
|
|
|
controller="mucha",
|
|
|
|
action="handle_boardauth",
|
|
|
|
conditions=dict(method=["POST"]),
|
|
|
|
)
|
|
|
|
self.map_post.connect(
|
|
|
|
"mucha_updatacheck",
|
|
|
|
"/mucha/updatacheck.do",
|
|
|
|
controller="mucha",
|
2023-03-11 02:31:29 +01:00
|
|
|
action="handle_updatecheck",
|
2023-03-09 17:38:58 +01:00
|
|
|
conditions=dict(method=["POST"]),
|
|
|
|
)
|
2023-07-12 06:41:53 +02:00
|
|
|
self.map_post.connect(
|
|
|
|
"mucha_dlstate",
|
|
|
|
"/mucha/downloadstate.do",
|
|
|
|
controller="mucha",
|
|
|
|
action="handle_dlstate",
|
|
|
|
conditions=dict(method=["POST"]),
|
|
|
|
)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-07-25 15:18:45 +02:00
|
|
|
self.map_post.connect(
|
|
|
|
"mucha_boardauth",
|
|
|
|
"/mucha_front/boardauth.do",
|
|
|
|
controller="mucha",
|
|
|
|
action="handle_boardauth",
|
|
|
|
conditions=dict(method=["POST"]),
|
|
|
|
)
|
|
|
|
self.map_post.connect(
|
|
|
|
"mucha_updatacheck",
|
|
|
|
"/mucha_front/updatacheck.do",
|
|
|
|
controller="mucha",
|
|
|
|
action="handle_updatecheck",
|
|
|
|
conditions=dict(method=["POST"]),
|
|
|
|
)
|
|
|
|
self.map_post.connect(
|
|
|
|
"mucha_dlstate",
|
|
|
|
"/mucha_front/downloadstate.do",
|
|
|
|
controller="mucha",
|
|
|
|
action="handle_dlstate",
|
|
|
|
conditions=dict(method=["POST"]),
|
|
|
|
)
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
self.map_get.connect(
|
|
|
|
"title_get",
|
|
|
|
"/{game}/{version}/{endpoint:.*?}",
|
|
|
|
controller="title",
|
|
|
|
action="render_GET",
|
|
|
|
conditions=dict(method=["GET"]),
|
|
|
|
requirements=dict(game=R"S..."),
|
|
|
|
)
|
|
|
|
self.map_post.connect(
|
|
|
|
"title_post",
|
|
|
|
"/{game}/{version}/{endpoint:.*?}",
|
|
|
|
controller="title",
|
|
|
|
action="render_POST",
|
|
|
|
conditions=dict(method=["POST"]),
|
|
|
|
requirements=dict(game=R"S..."),
|
|
|
|
)
|
|
|
|
|
|
|
|
def render_GET(self, request: Request) -> bytes:
|
2023-02-17 07:02:21 +01:00
|
|
|
test = self.map_get.match(request.uri.decode())
|
2023-03-12 07:00:51 +01:00
|
|
|
client_ip = Utils.get_ip_addr(request)
|
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
if test is None:
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger.debug(
|
2023-03-12 07:00:51 +01:00
|
|
|
f"Unknown GET endpoint {request.uri.decode()} from {client_ip} to port {request.getHost().port}"
|
2023-03-09 17:38:58 +01:00
|
|
|
)
|
2023-02-24 20:07:54 +01:00
|
|
|
request.setResponseCode(404)
|
|
|
|
return b"Endpoint not found."
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-02-19 05:12:40 +01:00
|
|
|
return self.dispatch(test, request)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
def render_POST(self, request: Request) -> bytes:
|
2023-02-17 07:02:21 +01:00
|
|
|
test = self.map_post.match(request.uri.decode())
|
2023-03-12 07:00:51 +01:00
|
|
|
client_ip = Utils.get_ip_addr(request)
|
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
if test is None:
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger.debug(
|
2023-03-12 07:00:51 +01:00
|
|
|
f"Unknown POST endpoint {request.uri.decode()} from {client_ip} to port {request.getHost().port}"
|
2023-03-09 17:38:58 +01:00
|
|
|
)
|
2023-02-24 20:07:54 +01:00
|
|
|
request.setResponseCode(404)
|
|
|
|
return b"Endpoint not found."
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-19 05:12:40 +01:00
|
|
|
return self.dispatch(test, request)
|
2023-02-17 07:02:21 +01:00
|
|
|
|
2023-02-19 05:12:40 +01:00
|
|
|
def dispatch(self, matcher: Dict, request: Request) -> bytes:
|
|
|
|
controller = getattr(self, matcher["controller"], None)
|
2023-02-17 07:02:21 +01:00
|
|
|
if controller is None:
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger.error(
|
|
|
|
f"Controller {matcher['controller']} not found via endpoint {request.uri.decode()}"
|
|
|
|
)
|
2023-02-24 20:07:54 +01:00
|
|
|
request.setResponseCode(404)
|
|
|
|
return b"Endpoint not found."
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-19 05:12:40 +01:00
|
|
|
handler = getattr(controller, matcher["action"], None)
|
2023-02-17 07:02:21 +01:00
|
|
|
if handler is None:
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger.error(
|
|
|
|
f"Action {matcher['action']} not found in controller {matcher['controller']} via endpoint {request.uri.decode()}"
|
|
|
|
)
|
2023-02-24 20:07:54 +01:00
|
|
|
request.setResponseCode(404)
|
|
|
|
return b"Endpoint not found."
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-19 05:12:40 +01:00
|
|
|
url_vars = matcher
|
2023-02-17 07:02:21 +01:00
|
|
|
url_vars.pop("controller")
|
2023-02-19 05:12:40 +01:00
|
|
|
url_vars.pop("action")
|
2023-02-17 07:02:21 +01:00
|
|
|
ret = handler(request, url_vars)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
if type(ret) == str:
|
|
|
|
return ret.encode()
|
2023-05-13 04:12:03 +02:00
|
|
|
|
|
|
|
elif type(ret) == bytes or type(ret) == tuple: # allow for bytes or tuple (data, response code) responses
|
2023-02-17 07:02:21 +01:00
|
|
|
return ret
|
2023-05-13 04:12:03 +02:00
|
|
|
|
|
|
|
elif ret is None:
|
2023-08-08 16:17:56 +02:00
|
|
|
self.logger.warning(f"None returned by controller for {request.uri.decode()} endpoint")
|
2023-05-13 04:12:03 +02:00
|
|
|
return b""
|
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
else:
|
2023-08-08 16:17:56 +02:00
|
|
|
self.logger.warning(f"Unknown data type returned by controller for {request.uri.decode()} endpoint")
|
2023-02-17 07:02:21 +01:00
|
|
|
return b""
|
2023-03-09 17:38:58 +01:00
|
|
|
|
|
|
|
|
2023-02-16 06:06:42 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(description="ARTEMiS main entry point")
|
2023-03-09 17:38:58 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--config", "-c", type=str, default="config", help="Configuration folder"
|
|
|
|
)
|
2023-02-16 06:06:42 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if not path.exists(f"{args.config}/core.yaml"):
|
2023-03-09 17:38:58 +01:00
|
|
|
print(
|
|
|
|
f"The config folder you specified ({args.config}) does not exist or does not contain core.yaml.\nDid you copy the example folder?"
|
|
|
|
)
|
2023-02-16 06:06:42 +01:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
cfg: CoreConfig = CoreConfig()
|
2023-03-05 03:58:51 +01:00
|
|
|
if path.exists(f"{args.config}/core.yaml"):
|
|
|
|
cfg.update(yaml.safe_load(open(f"{args.config}/core.yaml")))
|
2023-02-16 06:06:42 +01:00
|
|
|
|
2023-03-10 01:03:30 +01:00
|
|
|
if not path.exists(cfg.server.log_dir):
|
|
|
|
mkdir(cfg.server.log_dir)
|
|
|
|
|
2023-03-17 02:36:42 +01:00
|
|
|
if not access(cfg.server.log_dir, W_OK):
|
|
|
|
print(
|
|
|
|
f"Log directory {cfg.server.log_dir} NOT writable, please check permissions"
|
|
|
|
)
|
|
|
|
exit(1)
|
|
|
|
|
2023-02-24 20:07:54 +01:00
|
|
|
logger = logging.getLogger("core")
|
2023-02-24 20:14:18 +01:00
|
|
|
log_fmt_str = "[%(asctime)s] Core | %(levelname)s | %(message)s"
|
2023-03-09 17:38:58 +01:00
|
|
|
log_fmt = logging.Formatter(log_fmt_str)
|
2023-02-24 20:07:54 +01:00
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
fileHandler = TimedRotatingFileHandler(
|
|
|
|
"{0}/{1}.log".format(cfg.server.log_dir, "core"), when="d", backupCount=10
|
|
|
|
)
|
2023-02-24 20:14:18 +01:00
|
|
|
fileHandler.setFormatter(log_fmt)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-24 20:14:18 +01:00
|
|
|
consoleHandler = logging.StreamHandler()
|
|
|
|
consoleHandler.setFormatter(log_fmt)
|
2023-02-24 20:07:54 +01:00
|
|
|
|
2023-02-24 20:14:18 +01:00
|
|
|
logger.addHandler(fileHandler)
|
|
|
|
logger.addHandler(consoleHandler)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-24 20:14:18 +01:00
|
|
|
log_lv = logging.DEBUG if cfg.server.is_develop else logging.INFO
|
|
|
|
logger.setLevel(log_lv)
|
|
|
|
coloredlogs.install(level=log_lv, logger=logger, fmt=log_fmt_str)
|
2023-02-24 20:07:54 +01:00
|
|
|
|
2023-02-16 23:13:41 +01:00
|
|
|
if not cfg.aimedb.key:
|
2023-02-24 20:07:54 +01:00
|
|
|
logger.error("!!AIMEDB KEY BLANK, SET KEY IN CORE.YAML!!")
|
2023-02-16 06:06:42 +01:00
|
|
|
exit(1)
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
logger.info(
|
|
|
|
f"ARTEMiS starting in {'develop' if cfg.server.is_develop else 'production'} mode"
|
|
|
|
)
|
|
|
|
|
|
|
|
allnet_server_str = f"tcp:{cfg.allnet.port}:interface={cfg.server.listen_address}"
|
2023-02-16 23:13:41 +01:00
|
|
|
title_server_str = f"tcp:{cfg.title.port}:interface={cfg.server.listen_address}"
|
2023-02-16 06:06:42 +01:00
|
|
|
adb_server_str = f"tcp:{cfg.aimedb.port}:interface={cfg.server.listen_address}"
|
2023-03-09 17:38:58 +01:00
|
|
|
frontend_server_str = (
|
|
|
|
f"tcp:{cfg.frontend.port}:interface={cfg.server.listen_address}"
|
|
|
|
)
|
2023-02-16 06:06:42 +01:00
|
|
|
|
|
|
|
billing_server_str = f"tcp:{cfg.billing.port}:interface={cfg.server.listen_address}"
|
|
|
|
if cfg.server.is_develop:
|
2023-03-09 17:38:58 +01:00
|
|
|
billing_server_str = (
|
|
|
|
f"ssl:{cfg.billing.port}:interface={cfg.server.listen_address}"
|
2023-02-16 06:06:42 +01:00
|
|
|
f":privateKey={cfg.billing.ssl_key}:certKey={cfg.billing.ssl_cert}"
|
2023-03-09 17:38:58 +01:00
|
|
|
)
|
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
dispatcher = HttpDispatcher(cfg, args.config)
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
endpoints.serverFromString(reactor, allnet_server_str).listen(
|
|
|
|
server.Site(dispatcher)
|
|
|
|
)
|
2023-02-16 06:06:42 +01:00
|
|
|
endpoints.serverFromString(reactor, adb_server_str).listen(AimedbFactory(cfg))
|
|
|
|
|
2023-02-19 21:40:25 +01:00
|
|
|
if cfg.frontend.enable:
|
2023-03-09 17:38:58 +01:00
|
|
|
endpoints.serverFromString(reactor, frontend_server_str).listen(
|
|
|
|
server.Site(FrontendServlet(cfg, args.config))
|
|
|
|
)
|
2023-02-19 21:40:25 +01:00
|
|
|
|
2023-02-16 06:06:42 +01:00
|
|
|
if cfg.billing.port > 0:
|
2023-03-09 17:38:58 +01:00
|
|
|
endpoints.serverFromString(reactor, billing_server_str).listen(
|
|
|
|
server.Site(dispatcher)
|
|
|
|
)
|
|
|
|
|
|
|
|
if cfg.title.port > 0:
|
|
|
|
endpoints.serverFromString(reactor, title_server_str).listen(
|
|
|
|
server.Site(dispatcher)
|
|
|
|
)
|
|
|
|
|
2023-07-08 06:34:55 +02:00
|
|
|
if cfg.server.threading:
|
|
|
|
Thread(target=reactor.run, args=(False,)).start()
|
|
|
|
else:
|
|
|
|
reactor.run()
|