From 6567f1441558accbaf20a8fcfc0e66a3025818e2 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Wed, 7 Apr 2021 02:37:21 +0200 Subject: [PATCH] add log_network Server argument --- CommonClient.py | 10 +++++----- MultiServer.py | 14 +++++++++----- NetUtils.py | 7 +++++++ Utils.py | 1 + host.yaml | 2 ++ 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/CommonClient.py b/CommonClient.py index 4a155a00..e834e83a 100644 --- a/CommonClient.py +++ b/CommonClient.py @@ -311,11 +311,11 @@ async def process_server_cmd(ctx: CommonContext, args: dict): args['players'].sort() current_team = -1 logger.info('Players:') - for team, slot, name in args['players']: - if team != current_team: - logger.info(f' Team #{team + 1}') - current_team = team - logger.info(' %s (Player %d)' % (name, slot)) + for network_player in args['players']: + if network_player.team != current_team: + logger.info(f' Team #{network_player.team + 1}') + current_team = network_player.team + logger.info(' %s (Player %d)' % (network_player.alias, network_player.slot)) if args["datapackage_version"] > network_data_package["version"]: await ctx.send_msgs([{"cmd": "GetDataPackage"}]) await ctx.server_auth(args['password']) diff --git a/MultiServer.py b/MultiServer.py index d540a06c..a43756b2 100644 --- a/MultiServer.py +++ b/MultiServer.py @@ -332,11 +332,14 @@ async def server(websocket, path, ctx: Context): ctx.endpoints.append(client) try: - logging.info("Incoming") + if ctx.log_network: + logging.info("Incoming connection") await on_client_connected(ctx, client) - logging.info("Sent Room Info") + if ctx.log_network: + logging.info("Sent Room Info") async for data in websocket: - logging.info(data) + if ctx.log_network: + logging.info(f"Incoming message: {data}") for msg in decode(data): await process_client_cmd(ctx, client, msg) except Exception as e: @@ -351,7 +354,7 @@ async def on_client_connected(ctx: Context, client: Client): await ctx.send_msgs(client, [{ 'cmd': 'RoomInfo', 'password': ctx.password is not None, - 'players': [NetworkPlayer(client.team, client.slot, ctx.name_aliases.get((client.team, client.slot), client.name)) for client + 'players': [NetworkPlayer(client.team, client.slot, ctx.name_aliases.get((client.team, client.slot), client.name), client.name) for client in ctx.endpoints if client.auth], # tags are for additional features in the communication. # Name them by feature or fork, as you feel is appropriate. @@ -1315,6 +1318,7 @@ def parse_args() -> argparse.Namespace: #1 -> recommended for friendly racing, tries to block third party clients #0 -> recommended for tournaments to force a level playing field, only allow an exact version match """) + parser.add_argument('--log_network', default=defaults["log_network"], action="store_true") args = parser.parse_args() return args @@ -1351,7 +1355,7 @@ async def main(args: argparse.Namespace): ctx = Context(args.host, args.port, args.server_password, args.password, args.location_check_points, args.hint_cost, not args.disable_item_cheat, args.forfeit_mode, args.remaining_mode, args.auto_shutdown, args.compatibility) - + ctx.log_network = args.log_network data_filename = args.multidata try: diff --git a/NetUtils.py b/NetUtils.py index 63764681..ae89d681 100644 --- a/NetUtils.py +++ b/NetUtils.py @@ -99,6 +99,7 @@ class Node: def __init__(self): self.endpoints = [] super(Node, self).__init__() + self.log_network = 0 def broadcast_all(self, msgs): msgs = self.dumper(msgs) @@ -114,6 +115,9 @@ class Node: except websockets.ConnectionClosed: logging.exception(f"Exception during send_msgs, could not send {msg}") await self.disconnect(endpoint) + else: + if self.log_network: + logging.info(f"Outgoing message: {msg}") async def send_encoded_msgs(self, endpoint: Endpoint, msg: str): if not endpoint.socket or not endpoint.socket.open or endpoint.socket.closed: @@ -123,6 +127,9 @@ class Node: except websockets.ConnectionClosed: logging.exception("Exception during send_msgs") await self.disconnect(endpoint) + else: + if self.log_network: + logging.info(f"Outgoing message: {msg}") async def disconnect(self, endpoint): if endpoint in self.endpoints: diff --git a/Utils.py b/Utils.py index 3aba13b6..35c9cc58 100644 --- a/Utils.py +++ b/Utils.py @@ -194,6 +194,7 @@ def get_default_options() -> dict: "remaining_mode": "goal", "auto_shutdown": 0, "compatibility": 2, + "log_network": 0 }, "multi_mystery_options": { "teams": 1, diff --git a/host.yaml b/host.yaml index bd3be5c8..bac3afa9 100644 --- a/host.yaml +++ b/host.yaml @@ -40,6 +40,8 @@ server_options: # 1 -> Recommended for friendly racing, only allow Berserker's Multiworld, to disallow old /getitem for example # 0 -> Recommended for tournaments to force a level playing field, only allow an exact version match compatibility: 2 + # log all server traffic, mostly for dev use + log_network: 0 # Options for MultiMystery.py multi_mystery_options: # Teams