From 5a859d3295f745a509273cf94ef7dec2b922935a Mon Sep 17 00:00:00 2001 From: flifloo Date: Sun, 27 Oct 2019 13:42:41 +0100 Subject: [PATCH] Organise flask app and add update_users and mc to take count of modifications in config --- app/__init__.py | 12 +++++ app.py => app/routes.py | 112 +++------------------------------------- configuration.py | 109 ++++++++++++++++++++++++++++++++++++++ main.py | 5 ++ 4 files changed, 133 insertions(+), 105 deletions(-) create mode 100644 app/__init__.py rename app.py => app/routes.py (65%) mode change 100755 => 100644 create mode 100644 configuration.py create mode 100644 main.py diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..4d38c99 --- /dev/null +++ b/app/__init__.py @@ -0,0 +1,12 @@ +from flask import Flask +from flask_jwt import JWT +from configuration import FlaskConfig, authenticate, identity, update_mc, update_users + +app = Flask(__name__) # Setup Flask's app +app.config.from_object(FlaskConfig) # Import Flask configuration +jwt = JWT(app, authenticate, identity) # Setup JWT +server = None # Init server +update_mc() +update_users() + +from app import routes diff --git a/app.py b/app/routes.py old mode 100755 new mode 100644 similarity index 65% rename from app.py rename to app/routes.py index 0699960..4dae9c7 --- a/app.py +++ b/app/routes.py @@ -1,99 +1,14 @@ -import json import logging +import json import subprocess -from os import urandom from os.path import isfile from pathlib import Path from time import sleep -from flask import Flask, abort, jsonify, request -from flask_jwt import JWT, jwt_required -from mcrcon import MCRcon -from mcstatus import MinecraftServer -from werkzeug.security import check_password_hash, generate_password_hash - - -# Create default configuration file -if not isfile("config.json"): - logging.info("No config file, creating nwe one") - conf = { - "Key": str(urandom(24)), - "Users": {"admin": generate_password_hash("admin")}, - "Path": "", - "Jar server": "server.jar", - "Server min ram": "1024M", - "Server max ram": "1024M", - "Server ip": "127.0.0.1", - "Rcon port": 25575, - "Rcon passwd": "admin", - "Query port": 25565, - "Properties": {} - } - with open("config.json", "w") as conf_file: - json.dump(conf, conf_file) - -# Load configuration file -with open("config.json", "r") as conf_file: - logging.info("Loading configurations") - conf = json.load(conf_file) - -# Check s server jar exist -if not isfile(Path(conf["Path"])/conf["Jar server"]): - logging.warning("No server jar found !") - exit() -# Enable elua by default -if not isfile(Path(conf["Path"])/"eula.txt"): - logging.info("No elua.txt, creating new one") - with open(Path(conf["Path"])/"eula.txt") as elua: - elua.write("eula=true") - - -# Configuration of flask -class Config(object): - SECRET_KEY = conf["Key"] - JWT_AUTH_USERNAME_KEY = "username" - - -# User object for JWT -class User(object): - def __init__(self, id, username, password): - self.id = id - self.username = username - self.password = password - - def __str__(self): - return "User(id='%s')" % self.id - - -# Convert user form config to User object -# TODO: Support config change -users = list() -for i, u in enumerate(conf["Users"]): - users.append(User(i+1, u, conf["Users"][u])) -username_table = {u.username: u for u in users} -userid_table = {u.id: u for u in users} - - -def authenticate(username, password): - """ - Authentication for JWT - :param username: User's username (str) - :param password: User's password - :return: User's object if correct username and password - """ - user = username_table.get(username, None) - if user and check_password_hash(user.password, password): - return user - - -def identity(payload): - """ - Get identity for JWT - :param payload: JWT payload - :return: User's object - """ - user_id = payload["identity"] - return userid_table.get(user_id, None) +from app import app, server +from configuration import conf, mcr, mcq, update_users, update_mc +from flask import abort, jsonify, request +from flask_jwt import jwt_required def update_properties(): @@ -122,16 +37,6 @@ def update_properties(): properties_file.write(properties) -# Setup of globals variables -app = Flask(__name__) # Setup Flask's app -app.config.from_object(Config) # Import Flask configuration -# TODO: support config change -mcr = MCRcon(conf["Server ip"], conf["Rcon passwd"], conf["Rcon port"]) # Setup Rcon -mcq = MinecraftServer(conf["Server ip"], conf["Query port"]) # Setup Query -jwt = JWT(app, authenticate, identity) # Setup JWT -server = None # Init server - - @app.route("/") @jwt_required() def root(): @@ -261,9 +166,6 @@ def update_config(): # Save configuration changes with open("config.json", "w") as conf_file: json.dump(conf, conf_file) + update_users() + update_mc() return jsonify(conf) - - -# Start of the program -if __name__ == "__main__": - app.run(ssl_context="adhoc") diff --git a/configuration.py b/configuration.py new file mode 100644 index 0000000..da228e3 --- /dev/null +++ b/configuration.py @@ -0,0 +1,109 @@ +import json +import logging +from os import urandom +from os.path import isfile +from pathlib import Path + +from mcrcon import MCRcon +from mcstatus import MinecraftServer +from werkzeug.security import check_password_hash, generate_password_hash + + +# Create default configuration file +if not isfile("config.json"): + logging.info("No config file, creating nwe one") + conf = { + "Key": str(urandom(24)), + "Users": {"admin": generate_password_hash("admin")}, + "Path": "server", + "Jar server": "server.jar", + "Server min ram": "1024M", + "Server max ram": "1024M", + "Server ip": "127.0.0.1", + "Rcon port": 25575, + "Rcon passwd": "admin", + "Query port": 25565, + "Properties": {} + } + with open("config.json", "w") as conf_file: + json.dump(conf, conf_file) + +# Load configuration file +with open("config.json", "r") as conf_file: + logging.info("Loading configurations") + conf = json.load(conf_file) + +# Check s server jar exist +if not isfile(Path(conf["Path"])/conf["Jar server"]): + logging.warning("No server jar found !") + exit() +# Enable elua by default +if not isfile(Path(conf["Path"])/"eula.txt"): + logging.info("No elua.txt, creating new one") + with open(Path(conf["Path"])/"eula.txt", "w") as elua: + elua.write("eula=true") + + +mcr = None # Setup Rcon +mcq = None # Setup Query + + +def update_mc(): + global mcr, mcq + mcr = MCRcon(conf["Server ip"], conf["Rcon passwd"], conf["Rcon port"]) + mcq = MinecraftServer(conf["Server ip"], conf["Query port"]) + + +# Configuration of flask +class FlaskConfig(object): + SECRET_KEY = conf["Key"] + JWT_AUTH_USERNAME_KEY = "username" + + +# User object for JWT +class User(object): + def __init__(self, id, username, password): + self.id = id + self.username = username + self.password = password + + def __str__(self): + return "User(id='%s')" % self.id + + +# Users variables init +users = list() +username_table = dict() +userid_table = dict() + + +def update_users(): + """ + Convert user from config to user object and put in users variables + """ + global users, username_table, userid_table + users = [User(i + 1, u, conf["Users"][u]) for i, u in enumerate(conf["Users"])] + username_table = {u.username: u for u in users} + userid_table = {u.id: u for u in users} + + +def authenticate(username, password): + """ + Authentication for JWT + :param username: User's username (str) + :param password: User's password + :return: User's object if correct username and password + """ + user = username_table.get(username, None) + if user and check_password_hash(user.password, password): + return user + + +def identity(payload): + """ + Get identity for JWT + :param payload: JWT payload + :return: User's object + """ + user_id = payload["identity"] + return userid_table.get(user_id, None) diff --git a/main.py b/main.py new file mode 100644 index 0000000..63a6f79 --- /dev/null +++ b/main.py @@ -0,0 +1,5 @@ +from app import app + +# Start of the program +if __name__ == "__main__": + app.run()