Skip to content

Commit

Permalink
2024-12-16T0958Z
Browse files Browse the repository at this point in the history
  • Loading branch information
Windows81 committed Dec 16, 2024
1 parent 8cbf196 commit 0f9fbdf
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
3 changes: 1 addition & 2 deletions Source/launcher/routines/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def __run_servers(
*args, **kwargs,
) -> None:
hts = [
web_server.make_server(
*args, port, game_config, **kwargs) # type: ignore
web_server.make_server(*args, port, game_config, **kwargs)
for port in web_ports
]
self.httpds.extend(hts)
Expand Down
9 changes: 7 additions & 2 deletions Source/storage/players.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ def first_time_setup(self) -> None:
)
self.sqlite.commit()

def add_player(self, user_code: str, id_num: int, username: str) -> tuple[str, int, str]:
def add_player(self, user_code: str, id_num: int, username: str) -> tuple[str, int, str] | None:
'''
Adds a new player to the database and returns the first entry which corresponds with the newly-added player.
Tries to get the entry whose username matches, else fail.
'''
self.sqlite.execute(
f"""
INSERT INTO "{self.TABLE_NAME}"
Expand All @@ -54,7 +58,8 @@ def add_player(self, user_code: str, id_num: int, username: str) -> tuple[str, i
{self.player_field.USERNAME.value}
FROM "{self.TABLE_NAME}"
WHERE {self.player_field.USER_CODE.value} = {repr(user_code)}
WHERE
{self.player_field.USER_CODE.value} = {repr(user_code)}
""",
).fetchone()
return result
Expand Down
5 changes: 4 additions & 1 deletion Source/web_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def make_server(
*args,
**kwargs,
) -> web_server_logic.web_server:
print("[TCP %d]: initialising Webserver" % port.port_num)
print(
"[TCP %d %s]: initialising webserver" %
(port.port_num, 'IPv6' if port.is_ipv6 else 'IPv4',),
)
cls = web_server_logic.web_server_ssl if port.is_ssl else web_server_logic.web_server
return cls(port, game_config, *args, **kwargs)
2 changes: 2 additions & 0 deletions Source/web_server/_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ def send_data(
except ssl.SSLEOFError:
# A `ssl.SSLEOFError` is likely thrown whenever a request is interrupted.
pass
except ConnectionResetError:
pass

def send_redirect(self, url: str) -> None:
self.send_response(301)
Expand Down
56 changes: 34 additions & 22 deletions Source/web_server/endpoints/joinscript.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
from web_server._logic import web_server_handler, server_path
from typing import Any
import util.versions as versions
from typing import Any
import util.resource
import util.const
import util.ssl
import json


def init_player(self: web_server_handler, user_code: str, id_num: int) -> tuple[str, int, str]:
def init_player(self: web_server_handler, user_code: str) -> tuple[str, int, str] | None:
config = self.game_config
username = config.server_core.retrieve_username(id_num, user_code)

(user_code, id_num, username) = self.server.storage.players.add_player(
user_code, id_num, username,
)
# This method only affects a player's fund balance if they're joining for the first time.
# Very hacky to call `send_error` when the webserver will later call `send_json`.
if user_code is None:
return None

# Keeps generating an iden number until it finds one that is not yet in the database.
while True:
id_num = config.server_core.retrieve_user_id(user_code)

# The `check_user_allowed` function will also be called after the player is added.
# (Potentially) for additional protection.
if not config.server_core.check_user_allowed(id_num, user_code):
return None

username = config.server_core.retrieve_username(id_num, user_code)

result = self.server.storage.players.add_player(
user_code, id_num, username,
)

if result is not None:
break

(user_code, id_num, username) = result

# The player's fund balance is only affected if they're joining for the first time.
self.server.storage.funds.first_init(
id_num, config.server_core.retrieve_default_funds(id_num, user_code),
)
Expand All @@ -29,7 +49,10 @@ def perform_join(self: web_server_handler) -> dict[str, Any]:
Some methods (such as retrieving a user fund balance or rejoining in 2021E)
need data from `Roblox-Session-Id`.
'''
server_core = self.game_config.server_core
config = self.game_config
database = self.server.storage.players
server_core = config.server_core

query_args = json.loads(
self.headers.get('Roblox-Session-Id', '{}'),
) | self.query
Expand All @@ -39,22 +62,11 @@ def perform_join(self: web_server_handler) -> dict[str, Any]:
user_code = query_args.get('user-code')

# Very hacky to call `send_error` when the webserver will later call `send_json`.
if user_code is None:
self.send_error(404)
return {}

config = self.game_config
id_num = config.server_core.retrieve_user_id(user_code)

# The `check_user_allowed` function will also be called after the player is added.
# (Potentially) for additional protection.
if not server_core.check_user_allowed(id_num, user_code):
result = init_player(self, user_code)
if result is None:
self.send_error(403)
return {}

(user_code, id_num, username) = init_player(
self, user_code, id_num,
)
(user_code, id_num, username) = result

join_data = {
'ServerConnections': [
Expand Down

0 comments on commit 0f9fbdf

Please sign in to comment.