1
0
mirror of synced 2024-11-24 14:30:09 +01:00
artemis/core/frontend.py

260 lines
8.9 KiB
Python
Raw Normal View History

2023-02-19 21:40:25 +01:00
import logging, coloredlogs
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
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
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
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
@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")
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
)
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"):
2023-02-19 21:40:25 +01:00
try:
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-04-11 17:40:05 +02:00
except Exception as e:
self.logger.error(
f"Failed to import frontend from {game_dir} because {e}"
)
2023-03-09 17:38:58 +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()}")
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
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
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
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()
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()
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:
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)
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
self.logger.info(f"Successful login of user {uid} at {ip}")
2023-03-09 17:38:58 +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: bytes = request.args[b"passwd"][0]
2023-02-19 21:40:25 +01:00
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
if not self.data.user.check_password(uid, passwd):
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):
template = self.environment.get_template("core/frontend/user/index.jinja")
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)
cards = self.data.card.get_user_cards(usr_sesh.userId)
user = self.data.user.get_user(usr_sesh.userId)
card_data = []
for c in cards:
if c['is_locked']:
status = 'Locked'
elif c['is_banned']:
status = 'Banned'
else:
status = 'Active'
card_data.append({'access_code': c['access_code'], 'status': status})
2023-03-09 17:38:58 +01:00
return template.render(
title=f"{self.core_config.server.name} | Account", sesh=vars(usr_sesh), cards=card_data, username=user['username']
2023-03-09 17:38:58 +01:00
).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)