From 91066c71eada977a75da3e90370ca8eeb0c20ec3 Mon Sep 17 00:00:00 2001 From: PC-Admin Date: Mon, 14 Aug 2023 01:05:16 +0800 Subject: [PATCH] add extra logging --- redlight_client_module.py | 11 +++++++++-- redlight_server_module.py | 7 ++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/redlight_client_module.py b/redlight_client_module.py index 7edd805..9bec58c 100755 --- a/redlight_client_module.py +++ b/redlight_client_module.py @@ -47,10 +47,11 @@ class RedlightClientModule: def __init__(self, config: dict, api: ModuleApi): self._api = api # URL where we'll check if the room/user combination is allowed. - self._redlight_url = config.get("redlight_url", "https://duckdomain.xyz/_matrix/loj/v1/abuse_lookup") + self._redlight_url = config.get("redlight_url", "http://127.0.0.1:8008/_matrix/loj/v1/abuse_lookup") self._agent = Agent(reactor) # Twisted agent for making HTTP requests. logger.info("RedLightClientModule initialized.") + logger.info(f"Redlight Server URL set to: {self._redlight_url}") # Register the user_may_join_room function to be called by Synapse before a user joins a room. api.register_spam_checker_callbacks( @@ -92,6 +93,9 @@ class RedlightClientModule: response_body_bytes = await readBody(response) response_body = response_body_bytes.decode("utf-8") + # Log the response content + logger.info(f"Received response with code {response.code}. Content: {response_body}") + try: # Try to parse the response body as JSON. response_json = json.loads(response_body) @@ -100,12 +104,15 @@ class RedlightClientModule: # Handle the response based on its HTTP status code. if response.code == 200: - raise AuthError(403, "User not allowed to join this room") + logger.warn(f"User {user} not allowed to join room {room}.") + raise AuthError(403, "User not allowed to join this room.") elif response.code == 204: + logger.info(f"User {user} allowed to join room {room}.") return NOT_SPAM # Allow the user to join. else: # Handle unexpected responses by logging them and allowing the user to join as a fallback. logger.error(f"Unexpected response code {response.code} with body: {response_body}") + logger.warn(f"Defaulting to allowing user {user} to join due to unexpected response code.") return NOT_SPAM # Function to parse the module's configuration. diff --git a/redlight_server_module.py b/redlight_server_module.py index c52c44e..761a66f 100755 --- a/redlight_server_module.py +++ b/redlight_server_module.py @@ -66,12 +66,14 @@ class RedlightServerResource: d.addCallbacks(_respond, _error) return NOT_DONE_YET # indicates asynchronous processing else: + logger.warning(f"Received a request with unsupported method: {method}") # If no handler is found for the method, return "Method Not Allowed". return self.method_not_allowed(request) # Handle PUT requests to the endpoint. @inlineCallbacks def on_PUT(self, request): + logger.info(f"Processing PUT request from {request.getClientIP()}.") try: # Read and decode the request body. body = yield request.content.read() @@ -89,17 +91,19 @@ class RedlightServerResource: # Respond based on whether the request is identified as abusive or not. if is_abuse: + logger.warning(f"Abuse detected from {request.getClientIP()}, user_id_hash: {user_id_hash} room_id_hash: {room_id_hash}.") request.setResponseCode(http.OK) defer.returnValue(json.dumps({ "error": None, "report_id": "b973d82a-6932-4cad-ac9f-f647a3a9d204", }).encode("utf-8")) else: + logger.info(f"No abuse detected for request from {request.getClientIP()}.") request.setResponseCode(http.NO_CONTENT) defer.returnValue(b"") except Exception as e: - logger.error(f"Error processing abuse lookup request: {e}") + logger.error(f"Error processing abuse lookup PUT request from {request.getClientIP()}: {e}") request.setResponseCode(400) defer.returnValue(json.dumps({"error": "Bad Request"}).encode("utf-8")) @@ -113,6 +117,7 @@ class RedlightServerResource: # General method to respond with "Method Not Allowed" for disallowed or unrecognized HTTP methods. def method_not_allowed(self, request): + logger.warning(f"Method Not Allowed: {request.method.decode('ascii')} from {request.getClientIP()}.") request.setResponseCode(405) return json.dumps({"error": "Method Not Allowed"}).encode("utf-8")