Update Dockerfile and utils.py

This commit is contained in:
dertyp7
2023-12-27 16:21:45 +01:00
parent e0bfc6a0ef
commit 0eb86721ff
10 changed files with 556 additions and 563 deletions

View File

@@ -3,10 +3,8 @@ FROM nginx:1.25.3
ENV PORT_MAP "" ENV PORT_MAP ""
ENV PYTHONUNBUFFERED=1 ENV PYTHONUNBUFFERED=1
ENV PLACEHOLDER_SERVER_SLEEPING_IP ""
ENV PLACEHOLDER_SERVER_STARTING_IP ""
# Install Python and pip # Install Python, pip and git
RUN apt-get update && apt-get install -y python3 python3-pip python3-venv RUN apt-get update && apt-get install -y python3 python3-pip python3-venv
# Create a virtual environment and activate it # Create a virtual environment and activate it

1
app/FakeMCServer Submodule

Submodule app/FakeMCServer added at d7d7bedb2b

View File

@@ -1,17 +1,37 @@
from math import log import os
import time import time
from dockerHandler import DockerHandler from dockerHandler import DockerHandler
from nginxHandler import NginxHandler from nginxHandler import NginxHandler
from minecraftServerHandler import MinecraftServerHandler from minecraftServerHandler import MinecraftServerHandler
from requestHandler import RequestHandler from requestHandler import RequestHandler
import logging import logging
from FakeMCServer.fake_mc_server import FakeMCServer
import threading
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
def init_placeholder_servers():
sleeping = FakeMCServer(port=20000, motd={
"1": "sleeping!", "2": "§aCheck example.com for more information!"})
starting = FakeMCServer(port=20001, motd={
"1": "starting!", "2": "§aCheck example.com for more information!"})
# Create threads for each server initialization
sleeping_thread = threading.Thread(target=sleeping.start_server)
starting_thread = threading.Thread(target=starting.start_server)
# Start the threads
sleeping_thread.start()
starting_thread.start()
def main() -> None: def main() -> None:
try: try:
logging.info('[INIT] initializing placeholder servers...')
init_placeholder_servers()
logging.info('[INIT] placeholder servers initialized')
logging.info('[INIT] initializing auto starter...') logging.info('[INIT] initializing auto starter...')
logging.info('[INIT] initializing docker handler...') logging.info('[INIT] initializing docker handler...')
docker_handler: DockerHandler = DockerHandler( docker_handler: DockerHandler = DockerHandler(

View File

@@ -30,7 +30,8 @@ class RequestHandler(threading.Thread):
logging.info( logging.info(
f'[RequestHandler:{self.port}] starting up on {server_address[0]} port {server_address[1]}') f'[RequestHandler:{self.port}] starting up on {server_address[0]} port {server_address[1]}')
self.sock.bind(server_address) self.sock.bind(server_address)
self.sock.listen(1) self.sock.settimeout(5)
self.sock.listen(30)
logging.info( logging.info(
f'[RequestHandler:{self.port}] request handler initialized') f'[RequestHandler:{self.port}] request handler initialized')
@@ -124,23 +125,19 @@ class RequestHandler(threading.Thread):
logging.info( logging.info(
'[RequestHandler:{self.port}] forwarding request to placeholder server') '[RequestHandler:{self.port}] forwarding request to placeholder server')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
ip = self.docker_handler.get_ip_by_service_name( ip = "127.0.0.1"
os.environ.get('PLACEHOLDER_SERVER_SLEEPING_SERVICE'))
if minecraft_server.is_starting() == True:
logging.info(
'[RequestHandler:{self.port}] container is starting. Using starting placeholder ip')
ip = self.docker_handler.get_ip_by_service_name(
os.environ.get('PLACEHOLDER_SERVER_STARTING_SERVICE'))
if not ip:
logging.info(
'[RequestHandler:{self.port}] no placeholder server ip found')
return
logging.info( logging.info(
f'[RequestHandler:{self.port}] placeholder server ip: {ip}') f'[RequestHandler:{self.port}] placeholder server ip: {ip}')
try: try:
server_socket.connect((ip, 25565)) if minecraft_server.is_starting() == True:
logging.info(
'[RequestHandler:{self.port}] container is starting. Using placeholder port 20001')
server_socket.connect((ip, 20001))
else:
logging.info(
'[RequestHandler:{self.port}] container is not starting. Using placeholder port 20000')
server_socket.connect((ip, 20000))
server_socket.sendall(request) server_socket.sendall(request)
response = server_socket.recv(1024) response = server_socket.recv(1024)
self.connection.sendall(response) self.connection.sendall(response)

View File

@@ -1,9 +1,11 @@
import logging import logging
import os import os
import socket import json
import json
from typing import List, Dict
def docker_container_mapping(): def docker_container_mapping() -> Dict[str, str]:
port_map_str = os.environ.get('PORT_MAP') port_map_str = os.environ.get('PORT_MAP')
port_map = {} port_map = {}
@@ -18,3 +20,31 @@ def docker_container_mapping():
for port in port_map: for port in port_map:
logging.info(f'{port} -> {port_map[port]}') logging.info(f'{port} -> {port_map[port]}')
return port_map return port_map
# motd = {
# "1": "§4Maintenance!",
# "2": "§aCheck example.com for more information!"
# }
# version_text = "§4Maintenance"
# samples = ["§bexample.com", "", "§4Maintenance"]
# kick_message = ["§bSorry", "", "§aThis server is offline!"]
def generate_placeholder_server_config_file(path: str, ip: str, port: int, motd: Dict[str, str], version_text: str, samples: List[str], kick_message: List[str]) -> None:
config = {
"ip": ip,
"kick_message": kick_message,
"motd": motd,
"player_max": 0,
"player_online": 0,
"port": port,
"protocol": 2,
"samples": samples,
"server_icon": "server_icon.png",
"show_hostname_if_available": True,
"show_ip_if_hostname_available": True,
"version_text": version_text
}
with open(path, 'w') as f:
json.dump(config, f, indent=4)

View File

@@ -9,10 +9,6 @@ services:
- 25565:25565 - 25565:25565
- 25566:25566 - 25566:25566
environment: environment:
# The ip of the placeholder servers below
PLACEHOLDER_SERVER_SLEEPING_SERVICE: "mc_placeholder_server_sleeping"
PLACEHOLDER_SERVER_STARTING_SERVICE: "mc_placeholder_server_starting"
# Port mapping for the servers # Port mapping for the servers
# The key is the external port of the placeholder server # The key is the external port of the placeholder server
# The value is the internal ip of the actual server # The value is the internal ip of the actual server
@@ -23,55 +19,6 @@ services:
volumes: volumes:
- /var/run/docker.sock:/var/run/docker.sock - /var/run/docker.sock:/var/run/docker.sock
# These are the placeholder servers. They are used to show the player a message
# They are not needed, but they are nice to have
# Keep in mind these servers are consuming some resources
mc_placeholder_server_sleeping:
container_name: mc_placeholder_server_sleeping
restart: always
image: itzg/minecraft-server:java8
environment:
VERSION: "1.12.2"
EULA: "TRUE"
MOTD: "Sleeping | Join to wake up"
# The placeholder servers should be as lightweight as possible
MAX_PLAYERS: "0"
MAX_MEMORY: "512M"
INIT_MEMORY: "512M"
LEVEL_TYPE: "FLAT"
JVM_XX_OPTS: "-XX:+UseConcMarkSweepGC -XX:+DisableExplicitGC -XX:+UseCompressedOops"
VIEW_DISTANCE: "1"
SPAWN_ANIMALS: "false"
SPAWN_MONSTERS: "false"
SNOOPER_ENABLED: "false"
GENERATE_STRUCTURES: "false"
ALLOW_NETHER: "false"
ALLOW_END: "false"
mc_placeholder_server_starting:
container_name: mc_placeholder_server_starting
restart: always
image: itzg/minecraft-server:java8
environment:
VERSION: "1.12.2"
EULA: "TRUE"
MOTD: "Starting, please wait..."
# The placeholder servers should be as lightweight as possible
MAX_PLAYERS: "0"
MAX_MEMORY: "512M"
INIT_MEMORY: "512M"
LEVEL_TYPE: "FLAT"
JVM_XX_OPTS: "-XX:+UseConcMarkSweepGC -XX:+DisableExplicitGC -XX:+UseCompressedOops"
VIEW_DISTANCE: "1"
SPAWN_ANIMALS: "false"
SPAWN_MONSTERS: "false"
SNOOPER_ENABLED: "false"
GENERATE_STRUCTURES: "false"
ALLOW_NETHER: "false"
ALLOW_END: "false"
# These are the actual servers # These are the actual servers
# For itzg/minecraft-server you can find the documentation here: https://docker-minecraft-server.readthedocs.io/en/latest/variables/ # For itzg/minecraft-server you can find the documentation here: https://docker-minecraft-server.readthedocs.io/en/latest/variables/
mc: mc: