fix(pterodactyl): only json.loads() once in the websocket handling code

This commit is contained in:
Seaswimmer 2024-03-03 02:01:01 -05:00
parent e2389cec08
commit 0496454030
Signed by untrusted user: cswimr
GPG key ID: B8953EC01E5C4063

View file

@ -33,24 +33,23 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
coginstance.websocket = websocket coginstance.websocket = websocket
while True: # pylint: disable=too-many-nested-blocks while True: # pylint: disable=too-many-nested-blocks
message = await websocket.recv() message = json.loads(await websocket.recv())
if json.loads(message)['event'] in ('token expiring', 'token expired'): if message['event'] in ('token expiring', 'token expired'):
logger.info("Received token expiring/expired event. Refreshing token.") logger.info("Received token expiring/expired event. Refreshing token.")
websocket_credentials = await retrieve_websocket_credentials(coginstance) websocket_credentials = await retrieve_websocket_credentials(coginstance)
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]}) auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
await websocket.send(auth_message) await websocket.send(auth_message)
logger.info("Authentication message sent") logger.info("Authentication message sent")
if json.loads(message)['event'] == 'auth success': if message['event'] == 'auth success':
logger.info("WebSocket authentication successful") logger.info("WebSocket authentication successful")
if json.loads(message)['event'] == 'console output' and await config.console_channel() is not None: if message['event'] == 'console output' and await config.console_channel() is not None:
msg = json.loads(message)['args'][0]
regex_blacklist: dict = await config.regex_blacklist() regex_blacklist: dict = await config.regex_blacklist()
matches = [re.search(regex, msg) for regex in regex_blacklist.values()] matches = [re.search(regex, message['args'][0]) for regex in regex_blacklist.values()]
if await config.current_status() in ('running', '') and not any(matches): if await config.current_status() in ('running', '') and not any(matches):
content = remove_ansi_escape_codes(msg) content = remove_ansi_escape_codes(message['args'][0])
if await config.mask_ip() is True: if await config.mask_ip() is True:
content = mask_ip(content) content = mask_ip(content)