Merge branch 'master' into query-rcon-update

This commit is contained in:
charleslemaux 2024-07-14 22:26:46 +02:00 committed by GitHub
commit 30c2ed6a2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 20 additions and 16 deletions

View File

@ -100,7 +100,7 @@ document.addEventListener('DOMContentLoaded', () => {
}); });
function sendRequest(endpoint, payload) { function sendRequest(endpoint, payload) {
return fetch(`https://www.servii.fr/api/${endpoint}`, { return fetch(`http://127.0.0.1:3000/${endpoint}`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'

3
app.py
View File

@ -1,5 +1,6 @@
import http import http
import inspect import inspect
from typing import Union
from flask import (Blueprint, Flask, Response, jsonify, request) from flask import (Blueprint, Flask, Response, jsonify, request)
from flask_cors import CORS from flask_cors import CORS
@ -62,7 +63,7 @@ def authenticate_request(data: dict):
return user return user
def parse_and_validate_request(parameters: [str]) -> list[str] or None: def parse_and_validate_request(parameters: list[str]) -> Union[list[str], None]:
args = [] args = []
data = request.get_json() data = request.get_json()
if not data: if not data:

View File

@ -1,4 +1,5 @@
from datetime import datetime from datetime import datetime
from typing import Union
import firebase_admin import firebase_admin
import jwt import jwt
@ -36,7 +37,7 @@ def verify_jwt_token(token):
return False, None return False, None
def fetch_port() -> int or None: def fetch_port() -> Union[int, None]:
servers_ref = firestore_database.collection("users") servers_ref = firestore_database.collection("users")
query = servers_ref.order_by("port", direction="DESCENDING").limit(1) query = servers_ref.order_by("port", direction="DESCENDING").limit(1)
highest_port_doc = next(query.stream(), None) highest_port_doc = next(query.stream(), None)
@ -73,7 +74,7 @@ def get_user_field(user_id, field_name):
return None return None
def get_server_port(user_id: str) -> int or None: def get_server_port(user_id: str) -> Union[int, None]:
try: try:
servers_ref = firestore_database.collection('users').document(user_id) servers_ref = firestore_database.collection('users').document(user_id)
server_doc = servers_ref.get() server_doc = servers_ref.get()

View File

@ -1,4 +1,5 @@
from http import HTTPStatus from http import HTTPStatus
from typing import Union
from cloudflare.types.dns import SRVRecord from cloudflare.types.dns import SRVRecord
from firebase_admin.auth import UserRecord from firebase_admin.auth import UserRecord
@ -11,7 +12,7 @@ from server_mc_manager import MinecraftServerManager
mc_manager: MinecraftServerManager = MinecraftServerManager() mc_manager: MinecraftServerManager = MinecraftServerManager()
def set_subdomain(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, str or None]: def set_subdomain(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, Union[str, None]]:
user_id: str = user.uid user_id: str = user.uid
store = firebase_manager.firestore_database store = firebase_manager.firestore_database
_users = store.collection("users") _users = store.collection("users")
@ -42,7 +43,7 @@ def set_subdomain(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, str or
return HTTPStatus.FORBIDDEN, str(e) return HTTPStatus.FORBIDDEN, str(e)
def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, str or list]: def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, Union[str, list]]:
user_id: str = user.uid user_id: str = user.uid
server_ref = firebase_manager.firestore_database.collection('users').document(user_id).collection('servers') server_ref = firebase_manager.firestore_database.collection('users').document(user_id).collection('servers')
try: try:
@ -65,7 +66,7 @@ def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, str or list]:
return account_create(user) return account_create(user)
def account_create(user: UserRecord) -> tuple[HTTPStatus, str or None]: def account_create(user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]:
if firebase_manager.user_field_exists(user.uid): if firebase_manager.user_field_exists(user.uid):
return HTTPStatus.FORBIDDEN, "User already exists." return HTTPStatus.FORBIDDEN, "User already exists."
try: try:
@ -87,7 +88,7 @@ allowed_frameworks: list[str] = ["paper", "spigot", "bukkit"]
def server_create(user: UserRecord, name: str, version: str, framework: str = "paper") -> ( def server_create(user: UserRecord, name: str, version: str, framework: str = "paper") -> (
tuple)[HTTPStatus, str or None]: tuple)[HTTPStatus, Union[str, None]]:
if framework not in allowed_frameworks: if framework not in allowed_frameworks:
return HTTPStatus.METHOD_NOT_ALLOWED, f"Framework {framework} not recognized." return HTTPStatus.METHOD_NOT_ALLOWED, f"Framework {framework} not recognized."
user_id = user.uid user_id = user.uid
@ -117,7 +118,7 @@ def server_create(user: UserRecord, name: str, version: str, framework: str = "p
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Server creation failed | {e}" return HTTPStatus.INTERNAL_SERVER_ERROR, f"Server creation failed | {e}"
def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, str or None]: def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]:
user_id = user.uid user_id = user.uid
try: try:
port: int = firebase_manager.get_server_port(user_id) port: int = firebase_manager.get_server_port(user_id)
@ -134,7 +135,7 @@ def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, str or None]
return HTTPStatus.INTERNAL_SERVER_ERROR, None return HTTPStatus.INTERNAL_SERVER_ERROR, None
def account_delete(user: UserRecord) -> tuple[HTTPStatus, str or None]: def account_delete(user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]:
user_id = user.uid user_id = user.uid
try: try:
dns_record_id = firebase_manager.get_user_field(user_id, "cloudflare_entry") dns_record_id = firebase_manager.get_user_field(user_id, "cloudflare_entry")
@ -155,7 +156,7 @@ def account_delete(user: UserRecord) -> tuple[HTTPStatus, str or None]:
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error deleting user '{user_id}' on the server." return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error deleting user '{user_id}' on the server."
def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]: def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[str, None]]:
user_id = user.uid user_id = user.uid
mc_manager.set_cooldown(user_id=user_id) mc_manager.set_cooldown(user_id=user_id)
try: try:
@ -175,7 +176,7 @@ def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]:
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error when running server: {e}" return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error when running server: {e}"
def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]: def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[str, None]]:
port = None port = None
user_id: str = user.uid user_id: str = user.uid
if mc_manager.has_cooldown(user_id=user_id): if mc_manager.has_cooldown(user_id=user_id):
@ -199,7 +200,7 @@ def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]:
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error occurred when stopping server '{name}'." return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error occurred when stopping server '{name}'."
def update_property(uid: str, name: str, prop: str, value: str) -> tuple[HTTPStatus, str or None]: def update_property(uid: str, name: str, prop: str, value: str) -> tuple[HTTPStatus, Union[str, None]]:
property_file_path: str = f"users/{uid}/{name}/server.properties" property_file_path: str = f"users/{uid}/{name}/server.properties"
try: try:
file_manager.update_server_property(property_file_path, prop, value) file_manager.update_server_property(property_file_path, prop, value)
@ -215,7 +216,7 @@ def update_property(uid: str, name: str, prop: str, value: str) -> tuple[HTTPSta
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Unhandled error: {type(e).__name__}, {str(e)}" return HTTPStatus.INTERNAL_SERVER_ERROR, f"Unhandled error: {type(e).__name__}, {str(e)}"
def update_properties(user: UserRecord, name: str, props: list[tuple[str, str]]) -> tuple[HTTPStatus, str or None]: def update_properties(user: UserRecord, name: str, props: list[tuple[str, str]]) -> tuple[HTTPStatus, Union[str, None]]:
errors: list[str] = [] errors: list[str] = []
for prop, value in props: for prop, value in props:
if prop not in MinecraftServerManager.allowed_properties: if prop not in MinecraftServerManager.allowed_properties:
@ -228,7 +229,7 @@ def update_properties(user: UserRecord, name: str, props: list[tuple[str, str]])
return HTTPStatus.OK, f"Successfully updated server '{name}'." return HTTPStatus.OK, f"Successfully updated server '{name}'."
def run_command(user: UserRecord, command: str, name: str) -> tuple[HTTPStatus, str or None]: def run_command(user: UserRecord, command: str, name: str) -> tuple[HTTPStatus, Union[str, None]]:
try: try:
port = firebase_manager.get_server_port(user.uid) port = firebase_manager.get_server_port(user.uid)
if port is None: if port is None:

View File

@ -3,6 +3,7 @@ import shlex
import time import time
import mcipc.query import mcipc.query
import mcipc.query.client import mcipc.query.client
from typing import Union
class MinecraftServerManager: class MinecraftServerManager:
@ -15,7 +16,7 @@ class MinecraftServerManager:
self.cooldowns = {} self.cooldowns = {}
def start_server(self, server_directory: str, port: int, def start_server(self, server_directory: str, port: int,
java_executable='java', jar_file='server.jar', memory_size='2048M') -> int or None: java_executable='java', jar_file='server.jar', memory_size='2048M') -> Union[int, None]:
if port in self.servers: if port in self.servers:
return None return None