Put version checking a custom function

This commit is contained in:
Christoph Stahl 2025-02-02 12:28:35 +01:00
parent bdc057026f
commit 1dcdb8d405

View file

@ -735,6 +735,49 @@ class Server:
return key in keys return key in keys
async def check_client_version(self, client_version: tuple[int, int, int], sid: str) -> bool:
"""
Check if a given version is compatible with the server.
:param version: The version to check
:type version: tuple[int, int, int]
:return: True if the version is compatible, False otherwise
:rtype: bool
"""
if client_version < SYNG_PROTOCOL_VERSION:
await self.sio.emit(
"msg",
{"type": "error", "msg": "Client is incompatible and outdated. Please update."},
room=sid,
)
await self.sio.emit(
"client-registered",
{"success": False, "room": None, "reason": "PROTOCOL_VERSION"},
room=sid,
)
return False
if client_version > SYNG_VERSION:
await self.sio.emit(
"msg",
{"type": "error", "msg": "Server is outdated. Please update."},
room=sid,
)
await self.sio.emit(
"client-registered",
{"success": False, "room": None, "reason": "PROTOCOL_VERSION"},
room=sid,
)
return False
if client_version < SYNG_VERSION:
await self.sio.emit(
"msg",
{"type": "warning", "msg": "Client is compatible but outdated. Please update."},
room=sid,
)
return True
async def handle_register_client(self, sid: str, data: dict[str, Any]) -> None: async def handle_register_client(self, sid: str, data: dict[str, Any]) -> None:
""" """
Handle the "register-client" message. Handle the "register-client" message.
@ -790,40 +833,9 @@ class Server:
return return
client_version = tuple(data["version"]) client_version = tuple(data["version"])
if not await self.check_client_version(client_version, sid):
if client_version < SYNG_PROTOCOL_VERSION:
await self.sio.emit(
"msg",
{"type": "error", "msg": "Client is incompatible and outdated. Please update."},
room=sid,
)
await self.sio.emit(
"client-registered",
{"success": False, "room": None, "reason": "PROTOCOL_VERSION"},
room=sid,
)
return return
if client_version > SYNG_VERSION:
await self.sio.emit(
"msg",
{"type": "error", "msg": "Server is outdated. Please update."},
room=sid,
)
await self.sio.emit(
"client-registered",
{"success": False, "room": None, "reason": "PROTOCOL_VERSION"},
room=sid,
)
return
if client_version < SYNG_VERSION:
await self.sio.emit(
"msg",
{"type": "warning", "msg": "Client is compatible but outdated. Please update."},
room=sid,
)
def gen_id(length: int = 4) -> str: def gen_id(length: int = 4) -> str:
client_id = "".join([random.choice(string.ascii_letters) for _ in range(length)]) client_id = "".join([random.choice(string.ascii_letters) for _ in range(length)])
if client_id in self.clients: if client_id in self.clients: