2023-02-19 20:40:25 +00:00
|
|
|
import logging, coloredlogs
|
2024-01-09 20:54:34 +00:00
|
|
|
from typing import Any, Dict, List, Union, Optional
|
2024-01-09 08:07:04 +00:00
|
|
|
from starlette.requests import Request
|
2024-01-09 20:54:34 +00:00
|
|
|
from starlette.routing import Route, Mount
|
|
|
|
from starlette.responses import Response, PlainTextResponse, RedirectResponse
|
2024-01-11 17:08:22 +00:00
|
|
|
from starlette.applications import Starlette
|
2023-02-19 20:40:25 +00:00
|
|
|
from logging.handlers import TimedRotatingFileHandler
|
|
|
|
import jinja2
|
|
|
|
import bcrypt
|
2023-07-24 02:21:49 +00:00
|
|
|
import re
|
2024-01-09 20:54:34 +00:00
|
|
|
import jwt
|
2024-01-11 17:08:22 +00:00
|
|
|
import yaml
|
2024-03-24 00:01:32 +00:00
|
|
|
import secrets
|
|
|
|
import string
|
|
|
|
import random
|
2024-01-09 20:54:34 +00:00
|
|
|
from base64 import b64decode
|
2023-07-24 02:21:49 +00:00
|
|
|
from enum import Enum
|
2024-01-09 20:54:34 +00:00
|
|
|
from datetime import datetime, timezone
|
2024-01-11 17:08:22 +00:00
|
|
|
from os import path, environ, mkdir, W_OK, access
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2023-03-12 06:00:51 +00:00
|
|
|
from core import CoreConfig, Utils
|
2023-02-19 20:40:25 +00:00
|
|
|
from core.data import Data
|
|
|
|
|
2023-07-24 02:21:49 +00:00
|
|
|
class PermissionOffset(Enum):
|
|
|
|
USER = 0 # Regular user
|
|
|
|
USERMOD = 1 # Can moderate other users
|
|
|
|
ACMOD = 2 # Can add arcades and cabs
|
|
|
|
SYSADMIN = 3 # Can change settings
|
|
|
|
# 4 - 6 reserved for future use
|
|
|
|
OWNER = 7 # Can do anything
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
class ShopPermissionOffset(Enum):
|
|
|
|
VIEW = 0 # View info and cabs
|
|
|
|
BOOKKEEP = 1 # View bookeeping info
|
|
|
|
EDITOR = 2 # Can edit name, settings
|
|
|
|
REGISTRAR = 3 # Can add cabs
|
|
|
|
# 4 - 6 reserved for future use
|
|
|
|
OWNER = 7 # Can do anything
|
2023-03-04 02:31:23 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
class ShopOwner():
|
|
|
|
def __init__(self, usr_id: int = 0, usr_name: str = "", perms: int = 0) -> None:
|
|
|
|
self.user_id = usr_id
|
|
|
|
self.username = usr_name
|
|
|
|
self.permissions = perms
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
class UserSession():
|
2024-06-09 07:05:57 +00:00
|
|
|
def __init__(self, usr_id: int = 0, ip: str = "", perms: int = 0, ongeki_ver: int = 7, chunithm_ver: int = -1, maimai_version: int = -1):
|
2024-01-09 20:54:34 +00:00
|
|
|
self.user_id = usr_id
|
|
|
|
self.current_ip = ip
|
|
|
|
self.permissions = perms
|
|
|
|
self.ongeki_version = ongeki_ver
|
2024-04-14 17:35:27 +00:00
|
|
|
self.chunithm_version = chunithm_ver
|
2024-06-09 07:05:57 +00:00
|
|
|
self.maimai_version = maimai_version
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
class FrontendServlet():
|
2023-02-19 20:40:25 +00:00
|
|
|
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)
|
2023-02-21 02:55:12 +00:00
|
|
|
self.environment = jinja2.Environment(loader=jinja2.FileSystemLoader("."))
|
2024-01-09 20:54:34 +00:00
|
|
|
self.game_list: Dict[str, Dict[str, Any]] = {}
|
|
|
|
self.sn_cvt: Dict[str, str] = {}
|
|
|
|
|
|
|
|
self.logger = logging.getLogger("frontend")
|
|
|
|
if not hasattr(self.logger, "inited"):
|
|
|
|
fileHandler = TimedRotatingFileHandler(
|
|
|
|
"{0}/{1}.log".format(self.config.server.log_dir, "frontend"),
|
|
|
|
when="d",
|
|
|
|
backupCount=10,
|
|
|
|
)
|
|
|
|
fileHandler.setFormatter(log_fmt)
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
consoleHandler = logging.StreamHandler()
|
|
|
|
consoleHandler.setFormatter(log_fmt)
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
self.logger.addHandler(fileHandler)
|
|
|
|
self.logger.addHandler(consoleHandler)
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
self.logger.setLevel(cfg.frontend.loglevel)
|
|
|
|
coloredlogs.install(
|
|
|
|
level=cfg.frontend.loglevel, logger=self.logger, fmt=log_fmt_str
|
|
|
|
)
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
self.logger.inited = True
|
|
|
|
|
2023-02-19 20:40:25 +00:00
|
|
|
games = Utils.get_all_titles()
|
|
|
|
for game_dir, game_mod in games.items():
|
2024-01-09 20:54:34 +00:00
|
|
|
if hasattr(game_mod, "frontend") and hasattr(game_mod, "index") and hasattr(game_mod, "game_codes"):
|
2023-02-19 20:40:25 +00:00
|
|
|
try:
|
2024-01-09 20:54:34 +00:00
|
|
|
if game_mod.index.is_game_enabled(game_mod.game_codes[0], self.config, config_dir):
|
|
|
|
game_fe = game_mod.frontend(cfg, self.environment, config_dir)
|
|
|
|
self.game_list[game_fe.nav_name] = {"url": f"/{game_dir}", "class": game_fe }
|
|
|
|
|
|
|
|
if hasattr(game_fe, "SN_PREFIX") and hasattr(game_fe, "NETID_PREFIX"):
|
|
|
|
if len(game_fe.SN_PREFIX) == len(game_fe.NETID_PREFIX):
|
|
|
|
for x in range(len(game_fe.SN_PREFIX)):
|
|
|
|
self.sn_cvt[game_fe.SN_PREFIX[x]] = game_fe.NETID_PREFIX[x]
|
2023-04-24 01:04:52 +00:00
|
|
|
|
2023-04-11 15:40:05 +00:00
|
|
|
except Exception as e:
|
2023-04-24 01:04:52 +00:00
|
|
|
self.logger.error(
|
|
|
|
f"Failed to import frontend from {game_dir} because {e}"
|
|
|
|
)
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
|
2023-02-21 02:55:12 +00:00
|
|
|
self.environment.globals["game_list"] = self.game_list
|
2024-01-09 20:54:34 +00:00
|
|
|
self.environment.globals["sn_cvt"] = self.sn_cvt
|
|
|
|
self.base = FE_Base(cfg, self.environment)
|
|
|
|
self.gate = FE_Gate(cfg, self.environment)
|
|
|
|
self.user = FE_User(cfg, self.environment)
|
|
|
|
self.system = FE_System(cfg, self.environment)
|
|
|
|
self.arcade = FE_Arcade(cfg, self.environment)
|
|
|
|
self.machine = FE_Machine(cfg, self.environment)
|
2024-01-09 08:07:04 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
def get_routes(self) -> List[Route]:
|
|
|
|
g_routes = []
|
|
|
|
for nav_name, g_data in self.environment.globals["game_list"].items():
|
|
|
|
g_routes.append(Mount(g_data['url'], routes=g_data['class'].get_routes()))
|
|
|
|
return [
|
|
|
|
Route("/", self.base.render_GET, methods=['GET']),
|
|
|
|
Mount("/user", routes=[
|
|
|
|
Route("/", self.user.render_GET, methods=['GET']),
|
|
|
|
Route("/{user_id:int}", self.user.render_GET, methods=['GET']),
|
|
|
|
Route("/update.pw", self.user.render_POST, methods=['POST']),
|
|
|
|
Route("/update.name", self.user.update_username, methods=['POST']),
|
|
|
|
Route("/edit.card", self.user.edit_card, methods=['POST']),
|
|
|
|
Route("/add.card", self.user.add_card, methods=['POST']),
|
|
|
|
Route("/logout", self.user.render_logout, methods=['GET']),
|
|
|
|
]),
|
|
|
|
Mount("/gate", routes=[
|
|
|
|
Route("/", self.gate.render_GET, methods=['GET', 'POST']),
|
|
|
|
Route("/gate.login", self.gate.render_login, methods=['POST']),
|
|
|
|
Route("/gate.create", self.gate.render_create, methods=['POST']),
|
|
|
|
Route("/create", self.gate.render_create_get, methods=['GET']),
|
|
|
|
]),
|
|
|
|
Mount("/sys", routes=[
|
|
|
|
Route("/", self.system.render_GET, methods=['GET']),
|
2024-05-22 05:36:41 +00:00
|
|
|
Route("/logs", self.system.render_logs, methods=['GET']),
|
2024-01-09 20:54:34 +00:00
|
|
|
Route("/lookup.user", self.system.lookup_user, methods=['GET']),
|
|
|
|
Route("/lookup.shop", self.system.lookup_shop, methods=['GET']),
|
2024-03-24 00:01:32 +00:00
|
|
|
Route("/add.user", self.system.add_user, methods=['POST']),
|
|
|
|
Route("/add.card", self.system.add_card, methods=['POST']),
|
|
|
|
Route("/add.shop", self.system.add_shop, methods=['POST']),
|
|
|
|
Route("/add.cab", self.system.add_cab, methods=['POST']),
|
2024-01-09 20:54:34 +00:00
|
|
|
]),
|
|
|
|
Mount("/shop", routes=[
|
|
|
|
Route("/", self.arcade.render_GET, methods=['GET']),
|
|
|
|
Route("/{shop_id:int}", self.arcade.render_GET, methods=['GET']),
|
|
|
|
]),
|
|
|
|
Mount("/cab", routes=[
|
|
|
|
Route("/", self.machine.render_GET, methods=['GET']),
|
|
|
|
Route("/{machine_id:int}", self.machine.render_GET, methods=['GET']),
|
|
|
|
]),
|
|
|
|
Mount("/game", routes=g_routes),
|
|
|
|
Route("/robots.txt", self.robots)
|
|
|
|
]
|
|
|
|
|
2024-01-12 02:01:51 +00:00
|
|
|
def startup(self) -> None:
|
2024-01-09 20:54:34 +00:00
|
|
|
self.config.update({
|
|
|
|
"frontend": {
|
|
|
|
"standalone": True,
|
|
|
|
"loglevel": CoreConfig.loglevel_to_str(self.config.frontend.loglevel),
|
|
|
|
"secret": self.config.frontend.secret
|
|
|
|
}
|
|
|
|
})
|
|
|
|
self.logger.info(f"Serving {len(self.game_list)} games")
|
2024-01-09 08:07:04 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def robots(cls, request: Request) -> PlainTextResponse:
|
|
|
|
return PlainTextResponse("User-agent: *\nDisallow: /\n\nUser-agent: AdsBot-Google\nDisallow: /")
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
class FE_Base():
|
2023-02-19 20:40:25 +00:00
|
|
|
"""
|
|
|
|
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-02-21 02:55:12 +00:00
|
|
|
def __init__(self, cfg: CoreConfig, environment: jinja2.Environment) -> None:
|
2023-02-19 20:40:25 +00:00
|
|
|
self.core_config = cfg
|
|
|
|
self.data = Data(cfg)
|
2023-03-09 16:38:58 +00:00
|
|
|
self.logger = logging.getLogger("frontend")
|
2023-02-19 20:40:25 +00:00
|
|
|
self.environment = environment
|
2024-01-09 20:54:34 +00:00
|
|
|
self.nav_name = "index"
|
|
|
|
|
|
|
|
async def render_GET(self, request: Request):
|
|
|
|
self.logger.debug(f"{Utils.get_ip_addr(request)} -> {request.url}")
|
|
|
|
template = self.environment.get_template("core/templates/index.jinja")
|
|
|
|
sesh = self.validate_session(request)
|
|
|
|
resp = Response(template.render(
|
|
|
|
server_name=self.core_config.server.name,
|
|
|
|
title=self.core_config.server.name,
|
|
|
|
game_list=self.environment.globals["game_list"],
|
|
|
|
sesh=vars(sesh) if sesh is not None else vars(UserSession()),
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2024-01-09 20:54:34 +00:00
|
|
|
|
|
|
|
if sesh is None:
|
2024-06-09 07:14:43 +00:00
|
|
|
resp.delete_cookie("ARTEMIS_SESH")
|
2024-01-09 20:54:34 +00:00
|
|
|
return resp
|
|
|
|
|
|
|
|
def get_routes(self) -> List[Route]:
|
|
|
|
return []
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def test_perm(cls, permission: int, offset: Union[PermissionOffset, ShopPermissionOffset]) -> bool:
|
|
|
|
logging.getLogger('frontend').debug(f"{permission} vs {1 << offset.value}")
|
|
|
|
return permission & 1 << offset.value == 1 << offset.value
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def test_perm_minimum(cls, permission: int, offset: Union[PermissionOffset, ShopPermissionOffset]) -> bool:
|
|
|
|
return permission >= 1 << offset.value
|
|
|
|
|
|
|
|
def decode_session(self, token: str) -> UserSession:
|
|
|
|
sesh = UserSession()
|
|
|
|
if not token: return sesh
|
|
|
|
try:
|
|
|
|
tk = jwt.decode(token, b64decode(self.core_config.frontend.secret), options={"verify_signature": True}, algorithms=["HS256"])
|
|
|
|
sesh.user_id = tk['user_id']
|
|
|
|
sesh.current_ip = tk['current_ip']
|
|
|
|
sesh.permissions = tk['permissions']
|
2024-04-14 17:35:27 +00:00
|
|
|
sesh.chunithm_version = tk['chunithm_version']
|
2024-06-09 07:05:57 +00:00
|
|
|
sesh.maimai_version = tk['maimai_version']
|
2024-06-09 07:14:43 +00:00
|
|
|
sesh.ongeki_version = tk['ongeki_version']
|
2024-04-14 17:35:27 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
if sesh.user_id <= 0:
|
|
|
|
self.logger.error("User session failed to validate due to an invalid ID!")
|
|
|
|
return UserSession()
|
|
|
|
return sesh
|
|
|
|
except jwt.ExpiredSignatureError:
|
|
|
|
self.logger.error("User session failed to validate due to an expired signature!")
|
|
|
|
return sesh
|
|
|
|
except jwt.InvalidSignatureError:
|
|
|
|
self.logger.error("User session failed to validate due to an invalid signature!")
|
|
|
|
return sesh
|
|
|
|
except jwt.DecodeError as e:
|
|
|
|
self.logger.error(f"User session failed to decode! {e}")
|
|
|
|
return sesh
|
|
|
|
except jwt.InvalidTokenError as e:
|
|
|
|
self.logger.error(f"User session is invalid! {e}")
|
|
|
|
return sesh
|
|
|
|
except KeyError as e:
|
|
|
|
self.logger.error(f"{e} missing from User session!")
|
|
|
|
return UserSession()
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.error(f"Unknown exception occoured when decoding User session! {e}")
|
|
|
|
return UserSession()
|
|
|
|
|
|
|
|
def validate_session(self, request: Request) -> Optional[UserSession]:
|
2024-06-09 07:14:43 +00:00
|
|
|
sesh = request.cookies.get('ARTEMIS_SESH', "")
|
2024-01-09 20:54:34 +00:00
|
|
|
if not sesh:
|
|
|
|
return None
|
|
|
|
|
|
|
|
usr_sesh = self.decode_session(sesh)
|
|
|
|
req_ip = Utils.get_ip_addr(request)
|
|
|
|
|
|
|
|
if usr_sesh.current_ip != req_ip:
|
|
|
|
self.logger.error(f"User session failed to validate due to mismatched IPs! {usr_sesh.current_ip} -> {req_ip}")
|
|
|
|
return None
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
if usr_sesh.permissions <= 0 or usr_sesh.permissions > 255:
|
|
|
|
self.logger.error(f"User session failed to validate due to an invalid permission value! {usr_sesh.permissions}")
|
|
|
|
return None
|
2024-04-14 17:35:27 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
return usr_sesh
|
|
|
|
|
|
|
|
def encode_session(self, sesh: UserSession, exp_seconds: int = 86400) -> str:
|
|
|
|
try:
|
2024-06-09 07:05:57 +00:00
|
|
|
return jwt.encode({
|
|
|
|
"user_id": sesh.user_id,
|
|
|
|
"current_ip": sesh.current_ip,
|
|
|
|
"permissions": sesh.permissions,
|
|
|
|
"ongeki_version": sesh.ongeki_version,
|
|
|
|
"chunithm_version": sesh.chunithm_version,
|
|
|
|
"maimai_version": sesh.maimai_version,
|
|
|
|
"exp": int(datetime.now(tz=timezone.utc).timestamp()) + exp_seconds },
|
|
|
|
b64decode(self.core_config.frontend.secret),
|
|
|
|
algorithm="HS256"
|
|
|
|
)
|
2024-01-09 20:54:34 +00:00
|
|
|
except jwt.InvalidKeyError:
|
|
|
|
self.logger.error("Failed to encode User session because the secret is invalid!")
|
|
|
|
return ""
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.error(f"Unknown exception occoured when encoding User session! {e}")
|
|
|
|
return ""
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-19 20:40:25 +00:00
|
|
|
class FE_Gate(FE_Base):
|
2024-01-09 20:54:34 +00:00
|
|
|
async def render_GET(self, request: Request):
|
|
|
|
self.logger.debug(f"{Utils.get_ip_addr(request)} -> {request.url.path}")
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if usr_sesh and usr_sesh.user_id > 0:
|
|
|
|
return RedirectResponse("/user/", 303)
|
|
|
|
|
|
|
|
|
|
|
|
if "e" in request.query_params:
|
2023-02-19 20:40:25 +00:00
|
|
|
try:
|
2024-01-09 20:54:34 +00:00
|
|
|
err = int(request.query_params.get("e", ["0"])[0])
|
2023-07-16 20:58:34 +00:00
|
|
|
except Exception:
|
2023-02-19 20:40:25 +00:00
|
|
|
err = 0
|
|
|
|
|
2023-03-09 16:38:58 +00:00
|
|
|
else:
|
|
|
|
err = 0
|
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
template = self.environment.get_template("core/templates/gate/gate.jinja")
|
|
|
|
resp = Response(template.render(
|
2023-03-09 16:38:58 +00:00
|
|
|
title=f"{self.core_config.server.name} | Login Gate",
|
|
|
|
error=err,
|
2024-01-09 20:54:34 +00:00
|
|
|
sesh=vars(UserSession()),
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2024-06-09 07:14:43 +00:00
|
|
|
resp.delete_cookie("ARTEMIS_SESH")
|
2024-01-09 20:54:34 +00:00
|
|
|
return resp
|
|
|
|
|
|
|
|
async def render_login(self, request: Request):
|
2023-03-12 06:00:51 +00:00
|
|
|
ip = Utils.get_ip_addr(request)
|
2024-01-09 20:54:34 +00:00
|
|
|
frm = await request.form()
|
|
|
|
access_code: str = frm.get("access_code", None)
|
|
|
|
if not access_code:
|
|
|
|
return RedirectResponse("/gate/?e=1", 303)
|
|
|
|
|
|
|
|
passwd: bytes = frm.get("passwd", "").encode()
|
|
|
|
if passwd == b"":
|
|
|
|
passwd = None
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
uid = await self.data.card.get_user_id_from_card(access_code)
|
|
|
|
if uid is None:
|
2024-06-08 23:18:15 +00:00
|
|
|
user = await self.data.user.get_user_by_username(access_code) # Lookup as username
|
|
|
|
if not user:
|
|
|
|
self.logger.debug(f"Failed to find user for card/username {access_code}")
|
|
|
|
return RedirectResponse("/gate/?e=1", 303)
|
|
|
|
|
|
|
|
uid = user['id']
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
user = await self.data.user.get_user(uid)
|
|
|
|
if user is None:
|
|
|
|
self.logger.error(f"Failed to load user {uid}")
|
|
|
|
return RedirectResponse("/gate/?e=1", 303)
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
if passwd is None:
|
|
|
|
sesh = await self.data.user.check_password(uid)
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
if sesh is not None:
|
2024-03-02 22:48:48 +00:00
|
|
|
return RedirectResponse(f"/gate/create?ac={access_code}", 303)
|
2024-01-09 20:54:34 +00:00
|
|
|
|
|
|
|
return RedirectResponse("/gate/?e=1", 303)
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
if not await self.data.user.check_password(uid, passwd):
|
|
|
|
self.logger.debug(f"Failed password for access code {access_code}")
|
|
|
|
return RedirectResponse("/gate/?e=1", 303)
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
self.logger.info(f"Successful login of user {uid} at {ip}")
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
sesh = UserSession()
|
|
|
|
sesh.user_id = uid
|
|
|
|
sesh.current_ip = ip
|
|
|
|
sesh.permissions = user['permissions']
|
|
|
|
|
|
|
|
usr_sesh = self.encode_session(sesh)
|
|
|
|
self.logger.debug(f"Created session with JWT {usr_sesh}")
|
|
|
|
resp = RedirectResponse("/user/", 303)
|
2024-06-09 07:14:43 +00:00
|
|
|
resp.set_cookie("ARTEMIS_SESH", usr_sesh)
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
return resp
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
async def render_create(self, request: Request):
|
|
|
|
ip = Utils.get_ip_addr(request)
|
2024-03-02 22:48:48 +00:00
|
|
|
frm = await request.form()
|
|
|
|
access_code: str = frm.get("access_code", "")
|
|
|
|
username: str = frm.get("username", "")
|
|
|
|
email: str = frm.get("email", "")
|
|
|
|
passwd: bytes = frm.get("passwd", "").encode()
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
if not access_code or not username or not email or not passwd:
|
|
|
|
return RedirectResponse("/gate/?e=1", 303)
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
uid = await self.data.card.get_user_id_from_card(access_code)
|
|
|
|
if uid is None:
|
|
|
|
return RedirectResponse("/gate/?e=1", 303)
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
salt = bcrypt.gensalt()
|
|
|
|
hashed = bcrypt.hashpw(passwd, salt)
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
result = await self.data.user.create_user(
|
|
|
|
uid, username, email.lower(), hashed.decode(), 1
|
|
|
|
)
|
|
|
|
if result is None:
|
|
|
|
return RedirectResponse("/gate/?e=3", 303)
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
if not await self.data.user.check_password(uid, passwd):
|
|
|
|
return RedirectResponse("/gate/", 303)
|
|
|
|
|
|
|
|
sesh = UserSession()
|
|
|
|
sesh.user_id = uid
|
|
|
|
sesh.current_ip = ip
|
|
|
|
sesh.permissions = 1
|
|
|
|
|
|
|
|
usr_sesh = self.encode_session(sesh)
|
|
|
|
self.logger.debug(f"Created session with JWT {usr_sesh}")
|
2024-03-02 22:48:48 +00:00
|
|
|
resp = RedirectResponse("/user/", 303)
|
2024-06-09 07:14:43 +00:00
|
|
|
resp.set_cookie("ARTEMIS_SESH", usr_sesh)
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
return resp
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
async def render_create_get(self, request: Request):
|
2024-03-02 22:48:48 +00:00
|
|
|
ac = request.query_params.get("ac", "")
|
2024-01-09 20:54:34 +00:00
|
|
|
if len(ac) != 20:
|
|
|
|
return RedirectResponse("/gate/?e=2", 303)
|
2023-02-19 20:40:25 +00:00
|
|
|
|
2024-01-09 19:42:17 +00:00
|
|
|
card = await self.data.card.get_card_by_access_code(ac)
|
2023-07-24 02:21:49 +00:00
|
|
|
if card is None:
|
2024-01-09 20:54:34 +00:00
|
|
|
return RedirectResponse("/gate/?e=1", 303)
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-01-09 19:42:17 +00:00
|
|
|
user = await self.data.user.get_user(card['user'])
|
2023-07-24 02:21:49 +00:00
|
|
|
if user is None:
|
2023-08-08 14:17:56 +00:00
|
|
|
self.logger.warning(f"Card {ac} exists with no/invalid associated user ID {card['user']}")
|
2024-01-09 20:54:34 +00:00
|
|
|
return RedirectResponse("/gate/?e=0", 303)
|
2023-07-24 02:21:49 +00:00
|
|
|
|
|
|
|
if user['password'] is not None:
|
2024-01-09 20:54:34 +00:00
|
|
|
return RedirectResponse("/gate/?e=1", 303)
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
template = self.environment.get_template("core/templates/gate/create.jinja")
|
|
|
|
return Response(template.render(
|
2023-03-09 16:38:58 +00:00
|
|
|
title=f"{self.core_config.server.name} | Create User",
|
|
|
|
code=ac,
|
2024-01-09 20:54:34 +00:00
|
|
|
sesh={"user_id": 0, "permissions": 0},
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2023-02-19 20:40:25 +00:00
|
|
|
|
|
|
|
class FE_User(FE_Base):
|
2024-01-09 19:42:17 +00:00
|
|
|
async def render_GET(self, request: Request):
|
2024-01-09 20:54:34 +00:00
|
|
|
uri = request.url.path
|
|
|
|
user_id = request.path_params.get('user_id', None)
|
|
|
|
self.logger.debug(f"{Utils.get_ip_addr(request)} -> {uri}")
|
|
|
|
template = self.environment.get_template("core/templates/user/index.jinja")
|
|
|
|
|
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh:
|
|
|
|
return RedirectResponse("/gate/", 303)
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
if user_id:
|
|
|
|
if not self.test_perm(usr_sesh.permissions, PermissionOffset.USERMOD) and user_id != usr_sesh.user_id:
|
|
|
|
self.logger.warn(f"User {usr_sesh.user_id} does not have permission to view user {user_id}")
|
|
|
|
return RedirectResponse("/user/", 303)
|
2023-07-24 02:21:49 +00:00
|
|
|
|
|
|
|
else:
|
2024-01-09 20:54:34 +00:00
|
|
|
user_id = usr_sesh.user_id
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
user = await self.data.user.get_user(user_id)
|
2023-07-24 02:21:49 +00:00
|
|
|
if user is None:
|
2024-01-09 20:54:34 +00:00
|
|
|
self.logger.debug(f"User {user_id} not found")
|
|
|
|
return RedirectResponse("/user/", 303)
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
cards = await self.data.card.get_user_cards(user_id)
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2023-05-20 19:32:02 +00:00
|
|
|
card_data = []
|
2023-07-24 02:21:49 +00:00
|
|
|
arcade_data = []
|
|
|
|
|
2023-05-20 19:32:02 +00:00
|
|
|
for c in cards:
|
|
|
|
if c['is_locked']:
|
|
|
|
status = 'Locked'
|
|
|
|
elif c['is_banned']:
|
|
|
|
status = 'Banned'
|
|
|
|
else:
|
|
|
|
status = 'Active'
|
|
|
|
|
2024-01-10 00:45:39 +00:00
|
|
|
#idm = c['idm']
|
2024-01-09 20:54:34 +00:00
|
|
|
ac = c['access_code']
|
|
|
|
|
2024-01-10 00:45:39 +00:00
|
|
|
if ac.startswith("5"): #or idm is not None:
|
2024-01-09 20:54:34 +00:00
|
|
|
c_type = "AmusementIC"
|
|
|
|
elif ac.startswith("3"):
|
|
|
|
c_type = "Banapass"
|
|
|
|
elif ac.startswith("010"):
|
2024-01-22 21:35:43 +00:00
|
|
|
c_type = "Aime" # TODO: Aime verification
|
2024-01-10 00:45:39 +00:00
|
|
|
elif ac.startswith("0008"):
|
|
|
|
c_type = "Generated AIC"
|
2024-01-09 20:54:34 +00:00
|
|
|
else:
|
|
|
|
c_type = "Unknown"
|
|
|
|
|
2024-01-10 00:45:39 +00:00
|
|
|
card_data.append({
|
|
|
|
'access_code': ac,
|
|
|
|
'status': status,
|
2024-07-07 03:13:41 +00:00
|
|
|
'chip_id': c['chip_id'],
|
|
|
|
'idm': c['idm'],
|
2024-01-10 00:45:39 +00:00
|
|
|
'type': c_type,
|
2024-08-09 06:37:34 +00:00
|
|
|
"memo": c['memo'],
|
|
|
|
"id": c['id'],
|
2024-01-10 00:45:39 +00:00
|
|
|
})
|
2024-01-09 20:54:34 +00:00
|
|
|
|
|
|
|
if "e" in request.query_params:
|
|
|
|
try:
|
|
|
|
err = int(request.query_params.get("e", 0))
|
|
|
|
except Exception:
|
|
|
|
err = 0
|
|
|
|
|
|
|
|
else:
|
|
|
|
err = 0
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
if "s" in request.query_params:
|
|
|
|
try:
|
|
|
|
succ = int(request.query_params.get("s", 0))
|
|
|
|
except Exception:
|
|
|
|
succ = 0
|
|
|
|
|
|
|
|
else:
|
|
|
|
succ = 0
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
return Response(template.render(
|
2023-07-24 02:21:49 +00:00
|
|
|
title=f"{self.core_config.server.name} | Account",
|
|
|
|
sesh=vars(usr_sesh),
|
2024-01-09 20:54:34 +00:00
|
|
|
cards=card_data,
|
|
|
|
error=err,
|
|
|
|
success=succ,
|
2023-07-24 02:21:49 +00:00
|
|
|
username=user['username'],
|
|
|
|
arcades=arcade_data
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2024-01-09 20:54:34 +00:00
|
|
|
|
|
|
|
async def render_logout(self, request: Request):
|
|
|
|
resp = RedirectResponse("/gate/", 303)
|
2024-06-09 07:14:43 +00:00
|
|
|
resp.delete_cookie("ARTEMIS_SESH")
|
2024-01-09 20:54:34 +00:00
|
|
|
return resp
|
|
|
|
|
|
|
|
async def edit_card(self, request: Request) -> RedirectResponse:
|
2024-07-07 03:13:41 +00:00
|
|
|
frm = await request.form()
|
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh or not self.test_perm(usr_sesh.permissions, PermissionOffset.USERMOD):
|
|
|
|
return RedirectResponse("/gate/", 303)
|
|
|
|
|
|
|
|
frm = await request.form()
|
2024-08-09 06:37:34 +00:00
|
|
|
cid = frm.get("card_edit_frm_card_id", None)
|
|
|
|
if not cid:
|
|
|
|
return RedirectResponse("/user/?e=999", 303)
|
|
|
|
|
|
|
|
ac = frm.get("card_edit_frm_access_code", None)
|
2024-07-07 03:13:41 +00:00
|
|
|
if not ac:
|
|
|
|
return RedirectResponse("/user/?e=999", 303)
|
|
|
|
|
2024-08-09 06:37:34 +00:00
|
|
|
card = await self.data.card.get_card_by_id(cid)
|
2024-07-07 03:13:41 +00:00
|
|
|
if not card:
|
|
|
|
return RedirectResponse("/user/?e=2", 303)
|
|
|
|
|
|
|
|
if card['user'] != usr_sesh.user_id and not self.test_perm_minimum(usr_sesh.permissions, PermissionOffset.USERMOD):
|
|
|
|
return RedirectResponse("/user/?e=11", 303)
|
|
|
|
|
2024-08-09 06:37:34 +00:00
|
|
|
if frm.get("add_memo", None) or frm.get("add_memo", None) == "":
|
2024-07-07 03:13:41 +00:00
|
|
|
memo = frm.get("add_memo")
|
2024-08-09 06:37:34 +00:00
|
|
|
if len(memo) > 16:
|
2024-07-07 03:13:41 +00:00
|
|
|
return RedirectResponse("/user/?e=4", 303)
|
|
|
|
await self.data.card.set_memo_by_access_code(ac, memo)
|
|
|
|
|
2024-08-09 06:37:34 +00:00
|
|
|
if False: # Saving this in case I want to allow editing idm/chip ID down the line
|
|
|
|
if frm.get("add_felica_idm", None):
|
|
|
|
idm = frm.get('add_felica_idm')
|
|
|
|
if not all(c in string.hexdigits for c in idm):
|
|
|
|
return RedirectResponse("/user/?e=4", 303)
|
|
|
|
await self.data.card.set_idm_by_access_code(ac, idm)
|
2024-07-07 03:13:41 +00:00
|
|
|
|
2024-08-09 06:37:34 +00:00
|
|
|
if frm.get("add_mifare_chip_id", None):
|
|
|
|
chip_id: str = frm.get('add_mifare_chip_id')
|
|
|
|
if not all(c in string.hexdigits for c in idm):
|
|
|
|
return RedirectResponse("/user/?e=4", 303)
|
|
|
|
await self.data.card.set_chip_id_by_access_code(ac, int(chip_id, 16))
|
2024-07-07 03:13:41 +00:00
|
|
|
|
|
|
|
return RedirectResponse("/user/?s=4", 303)
|
2024-01-09 20:54:34 +00:00
|
|
|
|
|
|
|
async def add_card(self, request: Request) -> RedirectResponse:
|
2024-08-09 06:37:34 +00:00
|
|
|
frm = await request.form()
|
|
|
|
card_type = frm.get("card_add_frm_type", None)
|
|
|
|
access_code = frm.get("add_access_code", None)
|
|
|
|
idm = frm.get("add_idm", None)
|
|
|
|
idm_caps = None
|
|
|
|
|
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh or not self.test_perm(usr_sesh.permissions, PermissionOffset.USERMOD):
|
|
|
|
return RedirectResponse("/gate/", 303)
|
|
|
|
|
|
|
|
if not len(access_code) == 20 or (not access_code.startswith("5") and not access_code.startswith("3") \
|
|
|
|
and not access_code.startswith("010") and not access_code.startswith("0008")):
|
|
|
|
return RedirectResponse("/user/?e=4", 303)
|
|
|
|
|
|
|
|
if card_type == "0" and access_code.startswith("5") and len(idm) == 16:
|
|
|
|
idm_caps = idm.upper()
|
|
|
|
|
|
|
|
if not all([x in string.hexdigits for x in idm_caps]):
|
|
|
|
return RedirectResponse("/user/?e=4", 303)
|
|
|
|
|
|
|
|
if access_code.startswith("5") and not idm_caps:
|
|
|
|
return RedirectResponse("/user/?e=13", 303)
|
|
|
|
|
|
|
|
test = await self.data.card.get_card_by_access_code(access_code)
|
|
|
|
if test:
|
|
|
|
return RedirectResponse("/user/?e=12", 303)
|
|
|
|
|
|
|
|
if idm_caps:
|
|
|
|
test = await self.data.card.get_card_by_idm(idm_caps)
|
|
|
|
if test and test['user'] != usr_sesh.user_id:
|
|
|
|
return RedirectResponse("/user/?e=12", 303)
|
|
|
|
|
|
|
|
test = await self.data.card.get_card_by_access_code(self.data.card.to_access_code(idm_caps))
|
|
|
|
if test:
|
|
|
|
if test['user'] != usr_sesh.user_id:
|
|
|
|
return RedirectResponse("/user/?e=12", 303)
|
|
|
|
|
|
|
|
await self.data.card.set_access_code_by_access_code(test['access_code'], access_code)
|
|
|
|
self.logger.info(f"Update card {test['id']} from {test['access_code']} to {access_code} for user {usr_sesh.user_id}")
|
|
|
|
|
|
|
|
await self.data.card.set_idm_by_access_code(access_code, idm_caps)
|
|
|
|
self.logger.info(f"Set IDm for card {access_code} to {idm_caps}")
|
|
|
|
return RedirectResponse("/user/?s=1", 303)
|
|
|
|
|
|
|
|
if card_type == "0" and access_code.startswith("0008"):
|
|
|
|
test = await self.data.card.get_card_by_idm(self.data.card.to_idm(access_code))
|
|
|
|
if test:
|
|
|
|
return RedirectResponse("/user/?e=12", 303)
|
|
|
|
|
|
|
|
new_card = await self.data.card.create_card(usr_sesh.user_id, access_code)
|
|
|
|
self.logger.info(f"Created new card {new_card} with access code {access_code} for user {usr_sesh.user_id}")
|
|
|
|
|
|
|
|
if idm_caps:
|
|
|
|
await self.data.card.set_idm_by_access_code(access_code, idm_caps)
|
|
|
|
self.logger.info(f"Set IDm for card {access_code} to {idm_caps}")
|
|
|
|
|
|
|
|
return RedirectResponse("/user/?s=1", 303)
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-01-09 19:42:17 +00:00
|
|
|
async def render_POST(self, request: Request):
|
2024-01-09 20:54:34 +00:00
|
|
|
frm = await request.form()
|
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh or not self.test_perm(usr_sesh.permissions, PermissionOffset.USERMOD):
|
|
|
|
return RedirectResponse("/gate/", 303)
|
|
|
|
|
|
|
|
old_pw: str = frm.get('current_pw', None)
|
|
|
|
pw1: str = frm.get('password1', None)
|
|
|
|
pw2: str = frm.get('password2', None)
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
if old_pw is None or pw1 is None or pw2 is None:
|
|
|
|
return RedirectResponse("/user/?e=4", 303)
|
|
|
|
|
|
|
|
if pw1 != pw2:
|
|
|
|
return RedirectResponse("/user/?e=6", 303)
|
|
|
|
|
|
|
|
if not await self.data.user.check_password(usr_sesh.user_id, old_pw.encode()):
|
|
|
|
return RedirectResponse("/user/?e=5", 303)
|
|
|
|
|
|
|
|
if len(pw1) < 10 or not any(ele.isupper() for ele in pw1) or not any(ele.islower() for ele in pw1) \
|
|
|
|
or not any(ele.isdigit() for ele in pw1) or not any(not ele.isalnum() for ele in pw1):
|
|
|
|
return RedirectResponse("/user/?e=7", 303)
|
|
|
|
|
|
|
|
salt = bcrypt.gensalt()
|
|
|
|
hashed = bcrypt.hashpw(pw1.encode(), salt)
|
|
|
|
if not await self.data.user.change_password(usr_sesh.user_id, hashed.decode()):
|
|
|
|
return RedirectResponse("/gate/?e=1", 303)
|
|
|
|
|
|
|
|
return RedirectResponse("/user/?s=1", 303)
|
|
|
|
|
|
|
|
async def update_username(self, request: Request):
|
|
|
|
frm = await request.form()
|
|
|
|
new_name: bytes = frm.get('new_name', "")
|
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh or not self.test_perm(usr_sesh.permissions, PermissionOffset.USERMOD):
|
|
|
|
return RedirectResponse("/gate/", 303)
|
|
|
|
|
|
|
|
if new_name is None or not new_name:
|
|
|
|
return RedirectResponse("/user/?e=4", 303)
|
|
|
|
|
|
|
|
if len(new_name) > 10:
|
|
|
|
return RedirectResponse("/user/?e=8", 303)
|
|
|
|
|
|
|
|
if not await self.data.user.change_username(usr_sesh.user_id, new_name):
|
|
|
|
return RedirectResponse("/user/?e=8", 303)
|
|
|
|
|
|
|
|
return RedirectResponse("/user/?s=2", 303)
|
2023-07-24 02:21:49 +00:00
|
|
|
|
|
|
|
class FE_System(FE_Base):
|
2024-01-09 19:42:17 +00:00
|
|
|
async def render_GET(self, request: Request):
|
2024-01-09 20:54:34 +00:00
|
|
|
template = self.environment.get_template("core/templates/sys/index.jinja")
|
|
|
|
self.logger.debug(f"{Utils.get_ip_addr(request)} -> {request.url.path}")
|
|
|
|
|
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh or not self.test_perm_minimum(usr_sesh.permissions, PermissionOffset.USERMOD):
|
|
|
|
return RedirectResponse("/gate/", 303)
|
|
|
|
|
2024-03-24 00:01:32 +00:00
|
|
|
if request.query_params.get("e", None):
|
|
|
|
err = int(request.query_params.get("e"))
|
|
|
|
else:
|
|
|
|
err = 0
|
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
return Response(template.render(
|
|
|
|
title=f"{self.core_config.server.name} | System",
|
|
|
|
sesh=vars(usr_sesh),
|
|
|
|
usrlist=[],
|
2024-03-24 00:01:32 +00:00
|
|
|
error = err
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2024-01-09 20:54:34 +00:00
|
|
|
|
|
|
|
async def lookup_user(self, request: Request):
|
|
|
|
template = self.environment.get_template("core/templates/sys/index.jinja")
|
2023-09-19 06:31:29 +00:00
|
|
|
usrlist: List[Dict] = []
|
2024-01-09 20:54:34 +00:00
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh or not self.test_perm(usr_sesh.permissions, PermissionOffset.USERMOD):
|
|
|
|
return RedirectResponse("/gate/", 303)
|
|
|
|
|
|
|
|
uid_search = request.query_params.get("usrId", None)
|
|
|
|
email_search = request.query_params.get("usrEmail", None)
|
|
|
|
uname_search = request.query_params.get("usrName", None)
|
|
|
|
|
|
|
|
if uid_search:
|
|
|
|
u = await self.data.user.get_user(uid_search)
|
|
|
|
if u is not None:
|
|
|
|
usrlist.append(u._asdict())
|
|
|
|
|
|
|
|
elif email_search:
|
|
|
|
u = await self.data.user.find_user_by_email(email_search)
|
|
|
|
if u is not None:
|
|
|
|
usrlist.append(u._asdict())
|
|
|
|
|
|
|
|
elif uname_search:
|
|
|
|
ul = await self.data.user.find_user_by_username(uname_search)
|
|
|
|
for u in ul:
|
|
|
|
usrlist.append(u._asdict())
|
|
|
|
|
|
|
|
return Response(template.render(
|
2023-07-24 02:21:49 +00:00
|
|
|
title=f"{self.core_config.server.name} | System",
|
|
|
|
sesh=vars(usr_sesh),
|
|
|
|
usrlist=usrlist,
|
2024-01-09 20:54:34 +00:00
|
|
|
shoplist=[],
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
async def lookup_shop(self, request: Request):
|
|
|
|
shoplist = []
|
|
|
|
template = self.environment.get_template("core/templates/sys/index.jinja")
|
|
|
|
|
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh or not self.test_perm(usr_sesh.permissions, PermissionOffset.ACMOD):
|
|
|
|
return RedirectResponse("/gate/", 303)
|
|
|
|
|
|
|
|
shopid_search = request.query_params.get("shopId", None)
|
|
|
|
sn_search = request.query_params.get("serialNum", None)
|
|
|
|
|
|
|
|
if shopid_search:
|
|
|
|
if shopid_search.isdigit():
|
|
|
|
shopid_search = int(shopid_search)
|
|
|
|
try:
|
|
|
|
sinfo = await self.data.arcade.get_arcade(shopid_search)
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.error(f"Failed to fetch shop info for shop {shopid_search} in lookup_shop - {e}")
|
|
|
|
sinfo = None
|
|
|
|
if sinfo:
|
|
|
|
shoplist.append({
|
2024-01-10 00:45:39 +00:00
|
|
|
"name": sinfo['name'],
|
|
|
|
"id": sinfo['id']
|
2024-01-09 20:54:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
else:
|
|
|
|
return Response(template.render(
|
|
|
|
title=f"{self.core_config.server.name} | System",
|
|
|
|
sesh=vars(usr_sesh),
|
|
|
|
usrlist=[],
|
|
|
|
shoplist=shoplist,
|
|
|
|
error=4
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2024-01-09 20:54:34 +00:00
|
|
|
|
|
|
|
if sn_search:
|
|
|
|
sn_search = sn_search.upper().replace("-", "").strip()
|
|
|
|
if sn_search.isdigit() and len(sn_search) == 12:
|
|
|
|
prefix = sn_search[:4]
|
|
|
|
suffix = sn_search[5:]
|
|
|
|
|
|
|
|
netid_prefix = self.environment.globals["sn_cvt"].get(prefix, "")
|
|
|
|
sn_search = netid_prefix + suffix
|
|
|
|
|
2024-01-10 00:45:39 +00:00
|
|
|
if re.match(r"^AB[DGL]N\d{7}$", sn_search) or re.match(r"^A\d{2}[EX]\d{2}[A-Z]\d{4,8}$", sn_search):
|
2024-01-09 20:54:34 +00:00
|
|
|
cabinfo = await self.data.arcade.get_machine(sn_search)
|
|
|
|
if cabinfo is None: sinfo = None
|
|
|
|
else:
|
|
|
|
sinfo = await self.data.arcade.get_arcade(cabinfo['arcade'])
|
|
|
|
if sinfo:
|
|
|
|
shoplist.append({
|
|
|
|
"name": sinfo['name'],
|
|
|
|
"id": sinfo['id']
|
|
|
|
})
|
|
|
|
|
|
|
|
else:
|
|
|
|
return Response(template.render(
|
|
|
|
title=f"{self.core_config.server.name} | System",
|
|
|
|
sesh=vars(usr_sesh),
|
|
|
|
usrlist=[],
|
|
|
|
shoplist=shoplist,
|
|
|
|
error=10
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2024-01-09 20:54:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
return Response(template.render(
|
|
|
|
title=f"{self.core_config.server.name} | System",
|
|
|
|
sesh=vars(usr_sesh),
|
|
|
|
usrlist=[],
|
|
|
|
shoplist=shoplist,
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-03-24 00:01:32 +00:00
|
|
|
async def add_user(self, request: Request):
|
|
|
|
template = self.environment.get_template("core/templates/sys/index.jinja")
|
|
|
|
|
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh or not self.test_perm(usr_sesh.permissions, PermissionOffset.ACMOD):
|
|
|
|
return RedirectResponse("/gate/", 303)
|
|
|
|
|
|
|
|
frm = await request.form()
|
|
|
|
username = frm.get("userName", None)
|
|
|
|
email = frm.get("userEmail", None)
|
|
|
|
perm = frm.get("usrPerm", "1")
|
|
|
|
passwd = "".join(
|
|
|
|
secrets.choice(string.ascii_letters + string.digits) for i in range(20)
|
|
|
|
)
|
|
|
|
hash = bcrypt.hashpw(passwd.encode(), bcrypt.gensalt())
|
|
|
|
|
|
|
|
if not email:
|
|
|
|
return RedirectResponse("/sys/?e=4", 303)
|
|
|
|
|
|
|
|
uid = await self.data.user.create_user(username=username if username else None, email=email, password=hash.decode(), permission=int(perm))
|
|
|
|
return Response(template.render(
|
|
|
|
title=f"{self.core_config.server.name} | System",
|
|
|
|
sesh=vars(usr_sesh),
|
|
|
|
usradd={"id": uid, "username": username, "password": passwd},
|
|
|
|
), media_type="text/html; charset=utf-8")
|
|
|
|
|
|
|
|
async def add_card(self, request: Request):
|
|
|
|
template = self.environment.get_template("core/templates/sys/index.jinja")
|
|
|
|
|
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh or not self.test_perm(usr_sesh.permissions, PermissionOffset.ACMOD):
|
|
|
|
return RedirectResponse("/gate/", 303)
|
|
|
|
|
|
|
|
frm = await request.form()
|
|
|
|
userid = frm.get("cardUsr", None)
|
|
|
|
access_code = frm.get("cardAc", None)
|
|
|
|
idm = frm.get("cardIdm", None)
|
|
|
|
|
|
|
|
if userid is None or access_code is None or not userid.isdigit() or not len(access_code) == 20 or not access_code.isdigit:
|
|
|
|
return RedirectResponse("/sys/?e=4", 303)
|
|
|
|
|
|
|
|
cardid = await self.data.card.create_card(int(userid), access_code)
|
|
|
|
if not cardid:
|
|
|
|
return RedirectResponse("/sys/?e=99", 303)
|
|
|
|
|
|
|
|
if idm is not None:
|
|
|
|
# TODO: save IDM
|
|
|
|
pass
|
|
|
|
|
|
|
|
return Response(template.render(
|
|
|
|
title=f"{self.core_config.server.name} | System",
|
|
|
|
sesh=vars(usr_sesh),
|
|
|
|
cardadd={"id": cardid, "user": userid, "access_code": access_code},
|
|
|
|
), media_type="text/html; charset=utf-8")
|
|
|
|
|
|
|
|
async def add_shop(self, request: Request):
|
|
|
|
template = self.environment.get_template("core/templates/sys/index.jinja")
|
|
|
|
|
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh or not self.test_perm(usr_sesh.permissions, PermissionOffset.ACMOD):
|
|
|
|
return RedirectResponse("/gate/", 303)
|
|
|
|
|
|
|
|
frm = await request.form()
|
|
|
|
name = frm.get("shopName", None)
|
|
|
|
country = frm.get("shopCountry", "JPN")
|
|
|
|
ip = frm.get("shopIp", None)
|
|
|
|
|
|
|
|
acid = await self.data.arcade.create_arcade(name if name else None, name if name else None, country)
|
|
|
|
if not acid:
|
|
|
|
return RedirectResponse("/sys/?e=99", 303)
|
|
|
|
|
|
|
|
if ip:
|
|
|
|
# TODO: set IP
|
|
|
|
pass
|
|
|
|
|
|
|
|
return Response(template.render(
|
|
|
|
title=f"{self.core_config.server.name} | System",
|
|
|
|
sesh=vars(usr_sesh),
|
|
|
|
shopadd={"id": acid},
|
|
|
|
), media_type="text/html; charset=utf-8")
|
|
|
|
|
|
|
|
async def add_cab(self, request: Request):
|
|
|
|
template = self.environment.get_template("core/templates/sys/index.jinja")
|
|
|
|
|
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh or not self.test_perm(usr_sesh.permissions, PermissionOffset.ACMOD):
|
|
|
|
return RedirectResponse("/gate/", 303)
|
|
|
|
|
|
|
|
frm = await request.form()
|
|
|
|
shopid = frm.get("cabShop", None)
|
|
|
|
serial = frm.get("cabSerial", None)
|
|
|
|
game_code = frm.get("cabGame", None)
|
|
|
|
|
|
|
|
if not shopid or not shopid.isdigit():
|
|
|
|
return RedirectResponse("/sys/?e=4", 303)
|
|
|
|
|
|
|
|
if not serial:
|
2024-07-09 19:10:42 +00:00
|
|
|
append = self.data.arcade.get_keychip_suffix(datetime.now().year, datetime.now().month)
|
|
|
|
generated = await self.data.arcade.get_num_generated_keychips()
|
|
|
|
if not generated:
|
|
|
|
generated = 0
|
|
|
|
serial = self.data.arcade.format_serial("A69A", 1, "A", generated + 1, int(append))
|
|
|
|
serial_dash = self.data.arcade.format_serial("A69A", 1, "A", generated + 1, int(append), True)
|
2024-03-24 00:01:32 +00:00
|
|
|
|
|
|
|
cab_id = await self.data.arcade.create_machine(int(shopid), serial, None, game_code if game_code else None)
|
|
|
|
|
|
|
|
return Response(template.render(
|
|
|
|
title=f"{self.core_config.server.name} | System",
|
|
|
|
sesh=vars(usr_sesh),
|
2024-07-09 19:10:42 +00:00
|
|
|
cabadd={"id": cab_id, "serial": serial_dash},
|
2024-03-24 00:01:32 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
|
|
|
|
2024-05-22 05:36:41 +00:00
|
|
|
async def render_logs(self, request: Request):
|
|
|
|
template = self.environment.get_template("core/templates/sys/logs.jinja")
|
|
|
|
events = []
|
|
|
|
|
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh or not self.test_perm(usr_sesh.permissions, PermissionOffset.SYSADMIN):
|
|
|
|
return RedirectResponse("/sys/?e=11", 303)
|
|
|
|
|
|
|
|
logs = await self.data.base.get_event_log()
|
|
|
|
if not logs:
|
|
|
|
logs = []
|
|
|
|
|
|
|
|
for log in logs:
|
|
|
|
evt = log._asdict()
|
|
|
|
if not evt['user']: evt["user"] = "NONE"
|
|
|
|
if not evt['arcade']: evt["arcade"] = "NONE"
|
|
|
|
if not evt['machine']: evt["machine"] = "NONE"
|
|
|
|
if not evt['ip']: evt["ip"] = "NONE"
|
|
|
|
if not evt['game']: evt["game"] = "NONE"
|
|
|
|
if not evt['version']: evt["version"] = "NONE"
|
|
|
|
evt['when_logged'] = evt['when_logged'].strftime("%x %X")
|
|
|
|
events.append(evt)
|
|
|
|
|
|
|
|
return Response(template.render(
|
|
|
|
title=f"{self.core_config.server.name} | Event Logs",
|
|
|
|
sesh=vars(usr_sesh),
|
|
|
|
events=events
|
|
|
|
), media_type="text/html; charset=utf-8")
|
|
|
|
|
2023-07-24 02:21:49 +00:00
|
|
|
class FE_Arcade(FE_Base):
|
2024-01-09 19:42:17 +00:00
|
|
|
async def render_GET(self, request: Request):
|
2024-01-09 20:54:34 +00:00
|
|
|
template = self.environment.get_template("core/templates/arcade/index.jinja")
|
|
|
|
shop_id = request.path_params.get('shop_id', None)
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh or not self.test_perm(usr_sesh.permissions, PermissionOffset.ACMOD):
|
|
|
|
self.logger.warn(f"User {usr_sesh.user_id} does not have permission to view shops!")
|
|
|
|
return RedirectResponse("/gate/", 303)
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
if not shop_id:
|
|
|
|
return Response(template.render(
|
|
|
|
title=f"{self.core_config.server.name} | Arcade",
|
|
|
|
sesh=vars(usr_sesh),
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-01-10 01:08:10 +00:00
|
|
|
sinfo = await self.data.arcade.get_arcade(shop_id)
|
2024-01-09 20:54:34 +00:00
|
|
|
if not sinfo:
|
|
|
|
return Response(template.render(
|
|
|
|
title=f"{self.core_config.server.name} | Arcade",
|
|
|
|
sesh=vars(usr_sesh),
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-01-10 01:08:10 +00:00
|
|
|
cabs = await self.data.arcade.get_arcade_machines(shop_id)
|
|
|
|
cablst = []
|
|
|
|
if cabs:
|
|
|
|
for x in cabs:
|
|
|
|
cablst.append({
|
|
|
|
"id": x['id'],
|
|
|
|
"serial": x['serial'],
|
|
|
|
"game": x['game'],
|
|
|
|
})
|
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
return Response(template.render(
|
2023-07-24 02:21:49 +00:00
|
|
|
title=f"{self.core_config.server.name} | Arcade",
|
|
|
|
sesh=vars(usr_sesh),
|
2024-01-09 20:54:34 +00:00
|
|
|
arcade={
|
|
|
|
"name": sinfo['name'],
|
|
|
|
"id": sinfo['id'],
|
2024-01-10 01:08:10 +00:00
|
|
|
"cabs": cablst
|
2024-01-09 20:54:34 +00:00
|
|
|
}
|
|
|
|
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2023-07-24 02:21:49 +00:00
|
|
|
|
|
|
|
class FE_Machine(FE_Base):
|
2024-01-09 19:42:17 +00:00
|
|
|
async def render_GET(self, request: Request):
|
2024-01-09 20:54:34 +00:00
|
|
|
template = self.environment.get_template("core/templates/machine/index.jinja")
|
|
|
|
cab_id = request.path_params.get('cab_id', None)
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh or not self.test_perm(usr_sesh.permissions, PermissionOffset.ACMOD):
|
|
|
|
self.logger.warn(f"User {usr_sesh.user_id} does not have permission to view shops!")
|
|
|
|
return RedirectResponse("/gate/", 303)
|
2023-07-24 02:21:49 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
if not cab_id:
|
|
|
|
return Response(template.render(
|
|
|
|
title=f"{self.core_config.server.name} | Machine",
|
|
|
|
sesh=vars(usr_sesh),
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2024-01-09 20:54:34 +00:00
|
|
|
|
|
|
|
return Response(template.render(
|
2023-07-24 02:21:49 +00:00
|
|
|
title=f"{self.core_config.server.name} | Machine",
|
2024-01-09 20:54:34 +00:00
|
|
|
sesh=vars(usr_sesh),
|
|
|
|
arcade={}
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2024-01-11 17:08:22 +00:00
|
|
|
|
2024-06-09 07:14:43 +00:00
|
|
|
cfg_dir = environ.get("ARTEMIS_CFG_DIR", "config")
|
2024-01-11 17:08:22 +00:00
|
|
|
cfg: CoreConfig = CoreConfig()
|
|
|
|
if path.exists(f"{cfg_dir}/core.yaml"):
|
|
|
|
cfg.update(yaml.safe_load(open(f"{cfg_dir}/core.yaml")))
|
|
|
|
|
|
|
|
if not path.exists(cfg.server.log_dir):
|
|
|
|
mkdir(cfg.server.log_dir)
|
|
|
|
|
|
|
|
if not access(cfg.server.log_dir, W_OK):
|
|
|
|
print(
|
|
|
|
f"Log directory {cfg.server.log_dir} NOT writable, please check permissions"
|
|
|
|
)
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
fe = FrontendServlet(cfg, cfg_dir)
|
|
|
|
app = Starlette(cfg.server.is_develop, fe.get_routes(), on_startup=[fe.startup])
|