2023-02-19 21:40:25 +01:00
|
|
|
import logging, coloredlogs
|
2023-02-21 03:55:12 +01:00
|
|
|
from typing import Any, Dict, List
|
2023-02-19 21:40:25 +01:00
|
|
|
from twisted.web import resource
|
|
|
|
from twisted.web.util import redirectTo
|
|
|
|
from twisted.web.http import Request
|
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
2023-03-04 03:31:23 +01:00
|
|
|
from twisted.web.server import Session
|
|
|
|
from zope.interface import Interface, Attribute, implementer
|
|
|
|
from twisted.python.components import registerAdapter
|
2023-02-19 21:40:25 +01:00
|
|
|
import jinja2
|
|
|
|
import bcrypt
|
|
|
|
|
2023-03-12 07:00:51 +01:00
|
|
|
from core import CoreConfig, Utils
|
2023-02-19 21:40:25 +01:00
|
|
|
from core.data import Data
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-03-04 03:31:23 +01:00
|
|
|
class IUserSession(Interface):
|
|
|
|
userId = Attribute("User's ID")
|
|
|
|
current_ip = Attribute("User's current ip address")
|
|
|
|
permissions = Attribute("User's permission level")
|
2023-03-09 17:38:58 +01:00
|
|
|
|
|
|
|
|
2023-03-04 03:31:23 +01:00
|
|
|
@implementer(IUserSession)
|
|
|
|
class UserSession(object):
|
|
|
|
def __init__(self, session):
|
|
|
|
self.userId = 0
|
|
|
|
self.current_ip = "0.0.0.0"
|
|
|
|
self.permissions = 0
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-19 21:40:25 +01:00
|
|
|
class FrontendServlet(resource.Resource):
|
|
|
|
def getChild(self, name: bytes, request: Request):
|
2023-03-17 07:11:49 +01:00
|
|
|
self.logger.debug(f"{Utils.get_ip_addr(request)} -> {name.decode()}")
|
2023-03-09 17:38:58 +01:00
|
|
|
if name == b"":
|
2023-02-19 21:40:25 +01:00
|
|
|
return self
|
|
|
|
return resource.Resource.getChild(self, name, request)
|
|
|
|
|
|
|
|
def __init__(self, cfg: CoreConfig, config_dir: str) -> None:
|
|
|
|
self.config = cfg
|
|
|
|
log_fmt_str = "[%(asctime)s] Frontend | %(levelname)s | %(message)s"
|
|
|
|
log_fmt = logging.Formatter(log_fmt_str)
|
|
|
|
self.logger = logging.getLogger("frontend")
|
2023-02-21 03:55:12 +01:00
|
|
|
self.environment = jinja2.Environment(loader=jinja2.FileSystemLoader("."))
|
|
|
|
self.game_list: List[Dict[str, str]] = []
|
|
|
|
self.children: Dict[str, Any] = {}
|
2023-02-19 21:40:25 +01:00
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
fileHandler = TimedRotatingFileHandler(
|
|
|
|
"{0}/{1}.log".format(self.config.server.log_dir, "frontend"),
|
|
|
|
when="d",
|
|
|
|
backupCount=10,
|
|
|
|
)
|
2023-02-19 21:40:25 +01:00
|
|
|
fileHandler.setFormatter(log_fmt)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-19 21:40:25 +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-19 21:40:25 +01:00
|
|
|
self.logger.setLevel(cfg.frontend.loglevel)
|
2023-03-09 17:38:58 +01:00
|
|
|
coloredlogs.install(
|
|
|
|
level=cfg.frontend.loglevel, logger=self.logger, fmt=log_fmt_str
|
|
|
|
)
|
2023-03-04 03:31:23 +01:00
|
|
|
registerAdapter(UserSession, Session, IUserSession)
|
2023-02-19 21:40:25 +01:00
|
|
|
|
|
|
|
fe_game = FE_Game(cfg, self.environment)
|
|
|
|
games = Utils.get_all_titles()
|
|
|
|
for game_dir, game_mod in games.items():
|
|
|
|
if hasattr(game_mod, "frontend"):
|
|
|
|
try:
|
2023-02-21 03:55:12 +01:00
|
|
|
game_fe = game_mod.frontend(cfg, self.environment, config_dir)
|
|
|
|
self.game_list.append({"url": game_dir, "name": game_fe.nav_name})
|
|
|
|
fe_game.putChild(game_dir.encode(), game_fe)
|
2023-02-19 21:40:25 +01:00
|
|
|
except:
|
|
|
|
raise
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-21 03:55:12 +01:00
|
|
|
self.environment.globals["game_list"] = self.game_list
|
2023-02-19 21:40:25 +01:00
|
|
|
self.putChild(b"gate", FE_Gate(cfg, self.environment))
|
|
|
|
self.putChild(b"user", FE_User(cfg, self.environment))
|
|
|
|
self.putChild(b"game", fe_game)
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger.info(
|
|
|
|
f"Ready on port {self.config.frontend.port} serving {len(fe_game.children)} games"
|
|
|
|
)
|
2023-02-19 21:40:25 +01:00
|
|
|
|
|
|
|
def render_GET(self, request):
|
2023-03-17 07:11:49 +01:00
|
|
|
self.logger.debug(f"{Utils.get_ip_addr(request)} -> {request.uri.decode()}")
|
2023-03-04 03:31:23 +01:00
|
|
|
template = self.environment.get_template("core/frontend/index.jinja")
|
2023-03-09 17:38:58 +01:00
|
|
|
return template.render(
|
|
|
|
server_name=self.config.server.name,
|
|
|
|
title=self.config.server.name,
|
|
|
|
game_list=self.game_list,
|
|
|
|
sesh=vars(IUserSession(request.getSession())),
|
|
|
|
).encode("utf-16")
|
|
|
|
|
2023-02-19 21:40:25 +01:00
|
|
|
|
|
|
|
class FE_Base(resource.Resource):
|
|
|
|
"""
|
|
|
|
A Generic skeleton class that all frontend handlers should inherit from
|
|
|
|
Initializes the environment, data, logger, config, and sets isLeaf to true
|
|
|
|
It is expected that game implementations of this class overwrite many of these
|
|
|
|
"""
|
2023-03-09 17:38:58 +01:00
|
|
|
|
|
|
|
isLeaf = True
|
|
|
|
|
2023-02-21 03:55:12 +01:00
|
|
|
def __init__(self, cfg: CoreConfig, environment: jinja2.Environment) -> None:
|
2023-02-19 21:40:25 +01:00
|
|
|
self.core_config = cfg
|
|
|
|
self.data = Data(cfg)
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger = logging.getLogger("frontend")
|
2023-02-19 21:40:25 +01:00
|
|
|
self.environment = environment
|
2023-02-21 03:55:12 +01:00
|
|
|
self.nav_name = "nav_name"
|
2023-02-19 21:40:25 +01:00
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-19 21:40:25 +01:00
|
|
|
class FE_Gate(FE_Base):
|
2023-03-09 17:38:58 +01:00
|
|
|
def render_GET(self, request: Request):
|
2023-03-17 07:11:49 +01:00
|
|
|
self.logger.debug(f"{Utils.get_ip_addr(request)} -> {request.uri.decode()}")
|
2023-02-19 21:40:25 +01:00
|
|
|
uri: str = request.uri.decode()
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-03-04 03:31:23 +01:00
|
|
|
sesh = request.getSession()
|
|
|
|
usr_sesh = IUserSession(sesh)
|
|
|
|
if usr_sesh.userId > 0:
|
|
|
|
return redirectTo(b"/user", request)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-19 21:40:25 +01:00
|
|
|
if uri.startswith("/gate/create"):
|
|
|
|
return self.create_user(request)
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
if b"e" in request.args:
|
2023-02-19 21:40:25 +01:00
|
|
|
try:
|
2023-03-09 17:38:58 +01:00
|
|
|
err = int(request.args[b"e"][0].decode())
|
2023-02-19 21:40:25 +01:00
|
|
|
except:
|
|
|
|
err = 0
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
else:
|
|
|
|
err = 0
|
|
|
|
|
|
|
|
template = self.environment.get_template("core/frontend/gate/gate.jinja")
|
|
|
|
return template.render(
|
|
|
|
title=f"{self.core_config.server.name} | Login Gate",
|
|
|
|
error=err,
|
|
|
|
sesh=vars(usr_sesh),
|
|
|
|
).encode("utf-16")
|
2023-02-19 21:40:25 +01:00
|
|
|
|
|
|
|
def render_POST(self, request: Request):
|
|
|
|
uri = request.uri.decode()
|
2023-03-12 07:00:51 +01:00
|
|
|
ip = Utils.get_ip_addr(request)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
|
|
|
if uri == "/gate/gate.login":
|
2023-02-19 21:40:25 +01:00
|
|
|
access_code: str = request.args[b"access_code"][0].decode()
|
2023-03-04 03:31:23 +01:00
|
|
|
passwd: bytes = request.args[b"passwd"][0]
|
2023-02-19 21:40:25 +01:00
|
|
|
if passwd == b"":
|
|
|
|
passwd = None
|
|
|
|
|
|
|
|
uid = self.data.card.get_user_id_from_card(access_code)
|
|
|
|
if uid is None:
|
|
|
|
return redirectTo(b"/gate?e=1", request)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-19 21:40:25 +01:00
|
|
|
if passwd is None:
|
2023-03-04 03:31:23 +01:00
|
|
|
sesh = self.data.user.check_password(uid)
|
2023-02-19 21:40:25 +01:00
|
|
|
|
|
|
|
if sesh is not None:
|
2023-03-09 17:38:58 +01:00
|
|
|
return redirectTo(
|
|
|
|
f"/gate/create?ac={access_code}".encode(), request
|
|
|
|
)
|
2023-02-19 21:40:25 +01:00
|
|
|
return redirectTo(b"/gate?e=1", request)
|
|
|
|
|
2023-03-04 03:31:23 +01:00
|
|
|
if not self.data.user.check_password(uid, passwd):
|
2023-02-19 21:40:25 +01:00
|
|
|
return redirectTo(b"/gate?e=1", request)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-03-04 03:31:23 +01:00
|
|
|
self.logger.info(f"Successful login of user {uid} at {ip}")
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-03-04 03:31:23 +01:00
|
|
|
sesh = request.getSession()
|
|
|
|
usr_sesh = IUserSession(sesh)
|
|
|
|
usr_sesh.userId = uid
|
|
|
|
usr_sesh.current_ip = ip
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-19 21:40:25 +01:00
|
|
|
return redirectTo(b"/user", request)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-19 21:40:25 +01:00
|
|
|
elif uri == "/gate/gate.create":
|
|
|
|
access_code: str = request.args[b"access_code"][0].decode()
|
|
|
|
username: str = request.args[b"username"][0]
|
|
|
|
email: str = request.args[b"email"][0].decode()
|
|
|
|
passwd: str = request.args[b"passwd"][0]
|
|
|
|
|
|
|
|
uid = self.data.card.get_user_id_from_card(access_code)
|
|
|
|
if uid is None:
|
|
|
|
return redirectTo(b"/gate?e=1", request)
|
|
|
|
|
|
|
|
salt = bcrypt.gensalt()
|
|
|
|
hashed = bcrypt.hashpw(passwd, salt)
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
result = self.data.user.create_user(
|
|
|
|
uid, username, email, hashed.decode(), 1
|
|
|
|
)
|
2023-02-19 21:40:25 +01:00
|
|
|
if result is None:
|
|
|
|
return redirectTo(b"/gate?e=3", request)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-03-04 03:31:23 +01:00
|
|
|
if not self.data.user.check_password(uid, passwd.encode()):
|
2023-02-19 21:40:25 +01:00
|
|
|
return redirectTo(b"/gate", request)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
2023-02-19 21:40:25 +01:00
|
|
|
return redirectTo(b"/user", request)
|
|
|
|
|
|
|
|
else:
|
|
|
|
return b""
|
|
|
|
|
|
|
|
def create_user(self, request: Request):
|
2023-03-09 17:38:58 +01:00
|
|
|
if b"ac" not in request.args or len(request.args[b"ac"][0].decode()) != 20:
|
2023-02-19 21:40:25 +01:00
|
|
|
return redirectTo(b"/gate?e=2", request)
|
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
ac = request.args[b"ac"][0].decode()
|
|
|
|
|
|
|
|
template = self.environment.get_template("core/frontend/gate/create.jinja")
|
|
|
|
return template.render(
|
|
|
|
title=f"{self.core_config.server.name} | Create User",
|
|
|
|
code=ac,
|
|
|
|
sesh={"userId": 0},
|
|
|
|
).encode("utf-16")
|
|
|
|
|
2023-02-19 21:40:25 +01:00
|
|
|
|
|
|
|
class FE_User(FE_Base):
|
|
|
|
def render_GET(self, request: Request):
|
2023-02-21 03:55:12 +01:00
|
|
|
template = self.environment.get_template("core/frontend/user/index.jinja")
|
2023-03-04 03:31:23 +01:00
|
|
|
|
|
|
|
sesh: Session = request.getSession()
|
|
|
|
usr_sesh = IUserSession(sesh)
|
|
|
|
if usr_sesh.userId == 0:
|
2023-02-19 21:40:25 +01:00
|
|
|
return redirectTo(b"/gate", request)
|
2023-03-09 17:38:58 +01:00
|
|
|
|
|
|
|
return template.render(
|
|
|
|
title=f"{self.core_config.server.name} | Account", sesh=vars(usr_sesh)
|
|
|
|
).encode("utf-16")
|
|
|
|
|
2023-02-19 21:40:25 +01:00
|
|
|
|
|
|
|
class FE_Game(FE_Base):
|
|
|
|
isLeaf = False
|
|
|
|
children: Dict[str, Any] = {}
|
|
|
|
|
|
|
|
def getChild(self, name: bytes, request: Request):
|
2023-03-09 17:38:58 +01:00
|
|
|
if name == b"":
|
2023-02-19 21:40:25 +01:00
|
|
|
return self
|
|
|
|
return resource.Resource.getChild(self, name, request)
|
|
|
|
|
|
|
|
def render_GET(self, request: Request) -> bytes:
|
2023-03-09 17:38:58 +01:00
|
|
|
return redirectTo(b"/user", request)
|