mirror of
https://github.com/ArchipelagoMW/Archipelago.git
synced 2026-03-26 03:23:30 -07:00
Merge remote-tracking branch 'origin/main' into HEAD
# Conflicts: # WebHostLib/customserver.py
This commit is contained in:
@@ -29,6 +29,7 @@ app.config["SELFLAUNCHKEY"] = None # can point to a SSL Certificate Key to encr
|
||||
app.config["SELFGEN"] = True # application process is in charge of scheduling Generations.
|
||||
app.config["DEBUG"] = False
|
||||
app.config["PORT"] = 80
|
||||
app.config["GAME_PORTS"] = "49152-65535"
|
||||
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
||||
app.config['MAX_CONTENT_LENGTH'] = 64 * 1024 * 1024 # 64 megabyte limit
|
||||
# if you want to deploy, make sure you have a non-guessable secret key
|
||||
|
||||
@@ -130,6 +130,7 @@ class MultiworldInstance():
|
||||
self.cert = config["SELFLAUNCHCERT"]
|
||||
self.key = config["SELFLAUNCHKEY"]
|
||||
self.host = config["HOST_ADDRESS"]
|
||||
self.game_ports = config["GAME_PORTS"]
|
||||
|
||||
def start(self):
|
||||
if self.process and self.process.is_alive():
|
||||
@@ -138,7 +139,7 @@ class MultiworldInstance():
|
||||
logging.info(f"Spinning up {self.room_id}")
|
||||
process = multiprocessing.Process(group=None, target=run_server_process,
|
||||
args=(self.room_id, self.ponyconfig, get_static_server_data(),
|
||||
self.cert, self.key, self.host),
|
||||
self.cert, self.key, self.host, self.game_ports),
|
||||
name="MultiHost")
|
||||
process.start()
|
||||
# bind after start to prevent thread sync issues with guardian.
|
||||
|
||||
@@ -19,6 +19,8 @@ import Utils
|
||||
|
||||
from MultiServer import Context, server, auto_shutdown, ServerCommandProcessor, ClientMessageProcessor, load_server_cert
|
||||
from Utils import restricted_loads, cache_argsless
|
||||
|
||||
from . import app
|
||||
from .locker import Locker
|
||||
from .models import Command, GameDataPackage, Room, db
|
||||
|
||||
@@ -85,13 +87,13 @@ class WebHostContext(Context):
|
||||
time.sleep(5)
|
||||
|
||||
@db_session
|
||||
def load(self, room_id: int):
|
||||
def load(self, room_id: int, game_ports: str):
|
||||
self.room_id = room_id
|
||||
room = Room.get(id=room_id)
|
||||
if room.last_port:
|
||||
self.port = room.last_port
|
||||
else:
|
||||
self.port = get_random_port()
|
||||
self.port = get_random_port(game_ports)
|
||||
|
||||
multidata = self.decompress(room.seed.multidata)
|
||||
game_data_packages = {}
|
||||
@@ -134,8 +136,45 @@ class WebHostContext(Context):
|
||||
return d
|
||||
|
||||
|
||||
def get_random_port():
|
||||
return random.randint(49152, 65535)
|
||||
def get_random_port(game_ports):
|
||||
config_range_list = game_ports.split(",")
|
||||
available_ports = []
|
||||
for item in config_range_list:
|
||||
if '-' in item:
|
||||
start, end = map(int, item.split('-'))
|
||||
available_ports.extend(range(start, end+1))
|
||||
else:
|
||||
available_ports.append(int(item))
|
||||
|
||||
port = get_port_from_list(available_ports)
|
||||
if port == 0:
|
||||
logging.info("Unable to find port. Expanding search to the default ports. (49152-65535)")
|
||||
checked_ports = []
|
||||
while len(set(checked_ports)) < (65535-49152)+1:
|
||||
port = random.randint(49152, 65535)
|
||||
if not is_port_in_use(port):
|
||||
break
|
||||
else:
|
||||
checked_ports.append(port)
|
||||
|
||||
return port
|
||||
|
||||
|
||||
def get_port_from_list(available_ports: list) -> int:
|
||||
while available_ports:
|
||||
port = random.choice(available_ports)
|
||||
available_ports.remove(port)
|
||||
|
||||
if not is_port_in_use(port):
|
||||
break
|
||||
else:
|
||||
port = 0
|
||||
return port
|
||||
|
||||
|
||||
def is_port_in_use(port: int) -> bool:
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
return s.connect_ex(('localhost', port)) == 0
|
||||
|
||||
|
||||
@cache_argsless
|
||||
@@ -158,7 +197,7 @@ def get_static_server_data() -> dict:
|
||||
|
||||
def run_server_process(room_id, ponyconfig: dict, static_server_data: dict,
|
||||
cert_file: typing.Optional[str], cert_key_file: typing.Optional[str],
|
||||
host: str):
|
||||
host: str, game_ports: str):
|
||||
# establish DB connection for multidata and multisave
|
||||
db.bind(**ponyconfig)
|
||||
db.generate_mapping(check_tables=False)
|
||||
@@ -168,7 +207,7 @@ def run_server_process(room_id, ponyconfig: dict, static_server_data: dict,
|
||||
|
||||
Utils.init_logging(str(room_id), write_mode="a")
|
||||
ctx = WebHostContext(static_server_data)
|
||||
ctx.load(room_id)
|
||||
ctx.load(room_id, game_ports)
|
||||
ctx.init_save()
|
||||
ssl_context = load_server_cert(cert_file, cert_key_file) if cert_file else None
|
||||
gc.collect() # free intermediate objects used during setup
|
||||
|
||||
@@ -17,6 +17,11 @@
|
||||
# Web hosting port
|
||||
#PORT: 80
|
||||
|
||||
# Ports used for game hosting. Values can be specific ports, port ranges or both. Default is: "49152-65535"
|
||||
# Examples of valid values: "40000-41000,49152-65535"
|
||||
# If ports within the range(s) are already in use, the WebHost will fallback to the default "49152-65535" range.
|
||||
#GAME_PORTS: "49152-65535"
|
||||
|
||||
# Place where uploads go.
|
||||
#UPLOAD_FOLDER: uploads
|
||||
|
||||
|
||||
Reference in New Issue
Block a user