mirror of
https://github.com/hubHarmony/servii-backend.git
synced 2024-11-17 21:40:31 +00:00
[V2] Final commit, production ready
Signed-off-by: Charles Le Maux <charles.le-maux@epitech.eu>
This commit is contained in:
parent
35689e4dd0
commit
b6d916bf9a
9
Dockerfile
Normal file
9
Dockerfile
Normal file
@ -0,0 +1,9 @@
|
||||
FROM python:3.12-alpine3.20
|
||||
LABEL authors="hapso"
|
||||
|
||||
WORKDIR /app
|
||||
COPY . /app
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
EXPOSE 3000/tcp
|
||||
EXPOSE 3000/udp
|
||||
CMD ["python3", "app.py"]
|
@ -68,7 +68,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
data = {email, port, token};
|
||||
break;
|
||||
case 'AccountDelete':
|
||||
data = {email, port, token};
|
||||
data = {subdomain, port, token};
|
||||
break;
|
||||
case 'ServerCreate':
|
||||
data = {port, name, version, token, framework};
|
||||
@ -100,7 +100,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
});
|
||||
|
||||
function sendRequest(endpoint, payload) {
|
||||
return fetch(`http://localhost:3005/${endpoint}`, {
|
||||
return fetch(`http://localhost:3000/${endpoint}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
|
@ -1,6 +1,5 @@
|
||||
import http
|
||||
import inspect
|
||||
import json
|
||||
|
||||
from flask import Flask, Response, jsonify, request
|
||||
from flask_cors import CORS
|
||||
@ -109,4 +108,4 @@ def dynamic_route_handler(path):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=3005, debug=True)
|
||||
app.run(host='0.0.0.0', port=3000, debug=True)
|
@ -2,6 +2,7 @@ import logging
|
||||
import os
|
||||
import shutil
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
supported_versions = ["bukkit", "paper", "spigot"]
|
||||
|
||||
@ -62,6 +63,44 @@ def update_server_property(file_path, property_name, new_value):
|
||||
file.writelines(content)
|
||||
|
||||
|
||||
def reload_nginx() -> bool:
|
||||
try:
|
||||
subprocess.run(['nginx', '-s', 'reload'], check=True)
|
||||
return True
|
||||
except subprocess.CalledProcessError as e:
|
||||
return False
|
||||
|
||||
|
||||
def append_stream_config(port, subdomain, config_file_path='/etc/nginx/sites-available/servii.fr'):
|
||||
os.makedirs(os.path.dirname(config_file_path), exist_ok=True)
|
||||
with open(config_file_path, 'r') as file:
|
||||
content = file.read()
|
||||
new_content = f"""stream {{
|
||||
server {{
|
||||
listen 25565;
|
||||
listen [::]:25565;
|
||||
server_name {subdomain}.servii.fr;
|
||||
proxy_pass 127.0.0.1:{port};
|
||||
}}
|
||||
}}\n"""
|
||||
final_content = content + new_content
|
||||
with open(config_file_path, 'w') as file:
|
||||
file.write(final_content)
|
||||
|
||||
|
||||
def remove_stream_config(port, subdomain, config_file_path='/etc/nginx/sites-available/servii.fr'):
|
||||
pattern = (r'stream {{\s*server {{\s*listen 25565;\s*listen \[\(::\]\):'
|
||||
r'25565;\s*server_name {}.servii\.fr;\s*proxy_pass 127\.0\.0\.1:{};\s*}}}\n'
|
||||
.format(re.escape(subdomain), port))
|
||||
with open(config_file_path, 'r') as file:
|
||||
content = file.read()
|
||||
cleaned_content = re.sub(pattern, '', content, flags=re.MULTILINE | re.DOTALL)
|
||||
with open(config_file_path, 'w') as file:
|
||||
file.write(cleaned_content)
|
||||
|
||||
print(f"Stream configuration removed from {config_file_path}")
|
||||
|
||||
|
||||
def log_error(error_type: str, error_message: str):
|
||||
logging.basicConfig(filename='logs.txt', level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s',
|
||||
|
@ -35,14 +35,24 @@ def verify_jwt_token(token):
|
||||
|
||||
|
||||
def fetch_port() -> int or None:
|
||||
servers_ref = firestore_database.collection_group("servers")
|
||||
query = servers_ref.order_by("port", "DESCENDING").limit(1)
|
||||
servers_ref = firestore_database.collection("users")
|
||||
query = servers_ref.order_by("port", direction="DESCENDING").limit(1)
|
||||
highest_port_doc = next(query.stream(), None)
|
||||
if highest_port_doc:
|
||||
return highest_port_doc.get('port')
|
||||
return None
|
||||
|
||||
|
||||
def user_field_exists(user_id: str, field: str) -> bool:
|
||||
try:
|
||||
doc = firestore_database.collection('users').document(user_id).get()
|
||||
if doc.exists:
|
||||
return True
|
||||
return False
|
||||
except NotFound:
|
||||
return False
|
||||
|
||||
|
||||
def server_name_taken(user_id: str, server_name: str) -> bool:
|
||||
servers = firestore_database.collection('users').document(user_id).collection('servers')
|
||||
query = servers.where('name', '==', server_name)
|
||||
@ -51,11 +61,20 @@ def server_name_taken(user_id: str, server_name: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def get_server_port(user_id, server_name) -> int or None:
|
||||
def get_user_field(user_id, field_name):
|
||||
user_doc_ref = firestore_database.collection('users').document(user_id)
|
||||
user_doc = user_doc_ref.get()
|
||||
if user_doc.exists:
|
||||
field_value = user_doc.to_dict().get(field_name)
|
||||
return field_value
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def get_server_port(user_id: str) -> int or None:
|
||||
try:
|
||||
servers_ref = firestore_database.collection('users').document(user_id).collection('servers')
|
||||
server_doc_ref = servers_ref.document(server_name)
|
||||
server_doc = server_doc_ref.get()
|
||||
servers_ref = firestore_database.collection('users').document(user_id)
|
||||
server_doc = servers_ref.get()
|
||||
port = server_doc.get('port')
|
||||
return port if port else None
|
||||
except Exception:
|
||||
@ -95,11 +114,12 @@ def set_firestore(user_id: str, data: dict) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def create_server(user_id: str, server_name: str, version: str, port: str):
|
||||
def create_server(user_id: str, server_name: str, version: str, port: str, framework: str = "paper"):
|
||||
port: int = int(port)
|
||||
servers_ref = firestore_database.collection('users').document(user_id).collection('servers')
|
||||
server_doc_ref = servers_ref.document(server_name)
|
||||
server_doc_ref.set({'name': server_name, 'port': port, 'running': False, 'version': version})
|
||||
server_doc_ref.set(
|
||||
{'name': server_name, 'port': port, 'running': False, 'version': version, 'framework': framework})
|
||||
|
||||
|
||||
def delete_server(user_id: str, server_name: str):
|
||||
|
@ -10,16 +10,28 @@ mc_manager: MinecraftServerManager = MinecraftServerManager()
|
||||
|
||||
|
||||
def set_subdomain(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, str or None]:
|
||||
user_id: str = user.uid
|
||||
store = firebase_manager.firestore_database
|
||||
_users = store.collection("users")
|
||||
query = _users.where("subdomain", "==", subdomain)
|
||||
for _ in query.stream():
|
||||
return HTTPStatus.ALREADY_REPORTED, "Subdomain already associated."
|
||||
try:
|
||||
exists: bool = firebase_manager.update_firestore(user.uid, {'subdomain': subdomain})
|
||||
if firebase_manager.get_user_field(user_id, "subdomain"):
|
||||
return HTTPStatus.OK, "You cannot change your subdomain."
|
||||
exists: bool = firebase_manager.update_firestore(user_id, {'subdomain': subdomain})
|
||||
if not exists:
|
||||
account_create(user)
|
||||
return set_subdomain(user, subdomain)
|
||||
port = firebase_manager.get_server_port(user_id)
|
||||
if port is None:
|
||||
return HTTPStatus.NOT_FOUND, "Port not found on database"
|
||||
try:
|
||||
file_manager.append_stream_config(port, subdomain)
|
||||
file_manager.reload_nginx()
|
||||
except Exception as e:
|
||||
firebase_manager.update_firestore(user_id, {'subdomain': None})
|
||||
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Nginx configuration failed. {str(e)}"
|
||||
return HTTPStatus.OK, "Successfully associated subdomain."
|
||||
except Exception as e:
|
||||
return HTTPStatus.FORBIDDEN, str(e)
|
||||
@ -31,18 +43,23 @@ def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, str or list]:
|
||||
try:
|
||||
servers = list(server_ref.stream())
|
||||
servers_data = [doc.to_dict() for doc in servers]
|
||||
if not servers_data:
|
||||
account_create(user)
|
||||
return HTTPStatus.OK, servers_data
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
return account_create(user)
|
||||
|
||||
|
||||
def account_create(user: UserRecord) -> tuple[HTTPStatus, str or None]:
|
||||
if firebase_manager.user_field_exists(user.uid, 'port'):
|
||||
return HTTPStatus.FORBIDDEN, "User already exists."
|
||||
try:
|
||||
file_manager.create_folder("users/" + str(25444))
|
||||
file_manager.create_folder("users/" + user.uid)
|
||||
firebase_manager.set_firestore(user.uid, {'mail': user.email,
|
||||
'name': user.display_name,
|
||||
'photo_url': user.photo_url,
|
||||
'subdomain': None
|
||||
'subdomain': None,
|
||||
'port': firebase_manager.fetch_port() + 1
|
||||
})
|
||||
return HTTPStatus.CREATED, "Successfully created account."
|
||||
except Exception as e:
|
||||
@ -60,14 +77,16 @@ def server_create(user: UserRecord, name: str, version: str, framework: str = "p
|
||||
server_path: str = f"users/{user_id}/{name}"
|
||||
server_template_path: str = f"servers/{framework}/{version}"
|
||||
try:
|
||||
port = firebase_manager.fetch_port() + 1
|
||||
port = firebase_manager.get_server_port(user_id)
|
||||
if port is None:
|
||||
return HTTPStatus.NOT_FOUND, "Port not found on database"
|
||||
subdomain = firebase_manager.firestore_database.collection("users").document(user_id).get().get("subdomain")
|
||||
if subdomain is None:
|
||||
firebase_manager.delete_server(user_id, name)
|
||||
return HTTPStatus.NOT_FOUND, f"You haven't associated a subdomain yet."
|
||||
if firebase_manager.server_name_taken(user_id, name):
|
||||
return HTTPStatus.CONFLICT, f"Server name '{name}' already in use."
|
||||
firebase_manager.create_server(user_id, name, version, port)
|
||||
firebase_manager.create_server(user_id, name, version, port, framework)
|
||||
file_manager.create_folder(server_path)
|
||||
file_manager.copy_folder_contents(server_template_path, server_path)
|
||||
file_manager.copy_folder_contents("servers/shared", server_path)
|
||||
@ -88,37 +107,52 @@ def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, str or None]
|
||||
return HTTPStatus.INTERNAL_SERVER_ERROR, None
|
||||
|
||||
|
||||
def account_delete(user: UserRecord) -> tuple[HTTPStatus, str or None]:
|
||||
def account_delete(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, str or None]:
|
||||
user_id = user.uid
|
||||
try:
|
||||
|
||||
file_manager.delete_non_empty_folder("users/" + user_id)
|
||||
try:
|
||||
port = firebase_manager.get_server_port(user_id)
|
||||
if port is None:
|
||||
return HTTPStatus.NOT_FOUND, "User not found on firestore."
|
||||
firebase_manager.delete_user(user_id)
|
||||
except Exception as e:
|
||||
return HTTPStatus.NOT_FOUND, f"Database deletion failed | {e}"
|
||||
try:
|
||||
file_manager.remove_stream_config()
|
||||
except Exception as e:
|
||||
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Nginx deletion failed. {str(e)}"
|
||||
return HTTPStatus.OK, f"Successfully deleted user {user_id}."
|
||||
except Exception as e:
|
||||
print(f"Error deleting account: {e}")
|
||||
return HTTPStatus.INTERNAL_SERVER_ERROR, "Error deleting account."
|
||||
file_manager.log_error(type(e).__name__, str(e))
|
||||
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]:
|
||||
user_id = user.uid
|
||||
try:
|
||||
port: int = firebase_manager.get_server_port(user_id, name)
|
||||
port: int = firebase_manager.get_server_port(user_id)
|
||||
server_id = mc_manager.start_server(f"users/{user_id}/{name}", port)
|
||||
if server_id is None:
|
||||
return HTTPStatus.OK, f"You cannot run multiples instances at this time."
|
||||
mc_manager.servers[server_id]['port'] = int(port)
|
||||
return HTTPStatus.ACCEPTED, f"Successfully started server {name}."
|
||||
except Exception as e:
|
||||
print(f"Error starting server: {e}")
|
||||
return HTTPStatus.INTERNAL_SERVER_ERROR, None
|
||||
print(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]:
|
||||
port = None
|
||||
try:
|
||||
port = firebase_manager.get_server_port(user.uid, name)
|
||||
port = firebase_manager.get_server_port(user.uid)
|
||||
if port is None:
|
||||
return HTTPStatus.NOT_FOUND, f"Server {name} not found in firestore."
|
||||
mc_manager.stop_server(port)
|
||||
exists: bool = mc_manager.stop_server(port)
|
||||
if exists:
|
||||
return HTTPStatus.OK, f"Successfully stopped server {name}."
|
||||
return HTTPStatus.OK, f"Server {name} already stopped."
|
||||
except Exception as e:
|
||||
if port:
|
||||
mc_manager.stop_server_forcefully(port)
|
||||
@ -133,6 +167,8 @@ def update_property(user: UserRecord, name: str, prop: str, value: str) -> tuple
|
||||
except ValueError as e:
|
||||
file_manager.log_error(type(e).__name__, str(e))
|
||||
return HTTPStatus.BAD_REQUEST, f"Property '{prop}' not found."
|
||||
except FileNotFoundError:
|
||||
return HTTPStatus.NOT_FOUND, f"Requested server '{name}' not found."
|
||||
except Exception as e:
|
||||
print(f"Unhandled error: {type(e).__name__}, {str(e)}")
|
||||
return HTTPStatus.INTERNAL_SERVER_ERROR, None
|
||||
@ -140,7 +176,7 @@ def update_property(user: UserRecord, name: str, prop: str, value: str) -> tuple
|
||||
|
||||
def run_command(user: UserRecord, command: str, name: str) -> tuple[HTTPStatus, str or None]:
|
||||
try:
|
||||
port = firebase_manager.get_server_port(user.uid, name)
|
||||
port = firebase_manager.get_server_port(user.uid)
|
||||
if port is None:
|
||||
return HTTPStatus.NOT_FOUND, f"Server {name} not found in firestore."
|
||||
mc_manager.execute_server_command(port, command)
|
||||
|
@ -3,3 +3,4 @@ Flask==3.0.3
|
||||
Flask_Cors==4.0.1
|
||||
google-api-core==2.19.0
|
||||
PyJWT==2.8.0
|
||||
gunicorn==20.1.0
|
||||
|
@ -8,12 +8,15 @@ class MinecraftServerManager:
|
||||
self.servers_count: int = 0
|
||||
|
||||
def start_server(self, server_directory: str, port: int,
|
||||
java_executable='java', jar_file='server.jar', memory_size='2048M'):
|
||||
java_executable='java', jar_file='server.jar', memory_size='2048M') -> int or None:
|
||||
|
||||
if port in self.servers:
|
||||
return None
|
||||
|
||||
command = f"{java_executable} -Xmx{memory_size} -Xms{memory_size} -jar {jar_file} > /dev/null"
|
||||
process = subprocess.Popen(shlex.split(command), cwd=server_directory, stdin=subprocess.PIPE)
|
||||
|
||||
self.servers_count = len(self.servers) + 1
|
||||
|
||||
self.servers[port] = {
|
||||
'process': process,
|
||||
'directory': server_directory,
|
||||
@ -21,20 +24,20 @@ class MinecraftServerManager:
|
||||
}
|
||||
return port
|
||||
|
||||
def execute_server_command(self, server_id, command) -> bool:
|
||||
if server_id not in self.servers:
|
||||
def execute_server_command(self, port, command) -> bool:
|
||||
if port not in self.servers:
|
||||
return False
|
||||
process = self.servers[server_id]['process']
|
||||
process = self.servers[port]['process']
|
||||
process.stdin.write(command.encode('utf-8') + b'\n')
|
||||
process.stdin.flush()
|
||||
return True
|
||||
|
||||
def stop_server(self, server_id) -> bool:
|
||||
if server_id not in self.servers:
|
||||
def stop_server(self, port: int) -> bool:
|
||||
if port not in self.servers:
|
||||
return False
|
||||
process = self.servers[server_id]['process']
|
||||
process = self.servers[port]['process']
|
||||
process.communicate(input=b"stop\n")
|
||||
del self.servers[server_id]
|
||||
del self.servers[port]
|
||||
return True
|
||||
|
||||
def stop_server_forcefully(self, server_id) -> bool:
|
||||
|
@ -1,6 +1,7 @@
|
||||
from firebase_admin import functions
|
||||
|
||||
import file_manager
|
||||
import firebase_manager
|
||||
|
||||
|
||||
firebase_manager.set_firestore("test", {'test': 'test'})
|
||||
file_manager.append_stream_config(3005, "antho")
|
Loading…
Reference in New Issue
Block a user