add extra logging

This commit is contained in:
PC-Admin 2023-08-14 01:05:16 +08:00
parent 256c693f76
commit 91066c71ea
2 changed files with 15 additions and 3 deletions

View File

@ -47,10 +47,11 @@ class RedlightClientModule:
def __init__(self, config: dict, api: ModuleApi): def __init__(self, config: dict, api: ModuleApi):
self._api = api self._api = api
# URL where we'll check if the room/user combination is allowed. # 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. self._agent = Agent(reactor) # Twisted agent for making HTTP requests.
logger.info("RedLightClientModule initialized.") 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. # Register the user_may_join_room function to be called by Synapse before a user joins a room.
api.register_spam_checker_callbacks( api.register_spam_checker_callbacks(
@ -92,6 +93,9 @@ class RedlightClientModule:
response_body_bytes = await readBody(response) response_body_bytes = await readBody(response)
response_body = response_body_bytes.decode("utf-8") 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:
# Try to parse the response body as JSON. # Try to parse the response body as JSON.
response_json = json.loads(response_body) response_json = json.loads(response_body)
@ -100,12 +104,15 @@ class RedlightClientModule:
# Handle the response based on its HTTP status code. # Handle the response based on its HTTP status code.
if response.code == 200: 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: elif response.code == 204:
logger.info(f"User {user} allowed to join room {room}.")
return NOT_SPAM # Allow the user to join. return NOT_SPAM # Allow the user to join.
else: else:
# Handle unexpected responses by logging them and allowing the user to join as a fallback. # 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.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 return NOT_SPAM
# Function to parse the module's configuration. # Function to parse the module's configuration.

View File

@ -66,12 +66,14 @@ class RedlightServerResource:
d.addCallbacks(_respond, _error) d.addCallbacks(_respond, _error)
return NOT_DONE_YET # indicates asynchronous processing return NOT_DONE_YET # indicates asynchronous processing
else: else:
logger.warning(f"Received a request with unsupported method: {method}")
# If no handler is found for the method, return "Method Not Allowed". # If no handler is found for the method, return "Method Not Allowed".
return self.method_not_allowed(request) return self.method_not_allowed(request)
# Handle PUT requests to the endpoint. # Handle PUT requests to the endpoint.
@inlineCallbacks @inlineCallbacks
def on_PUT(self, request): def on_PUT(self, request):
logger.info(f"Processing PUT request from {request.getClientIP()}.")
try: try:
# Read and decode the request body. # Read and decode the request body.
body = yield request.content.read() body = yield request.content.read()
@ -89,17 +91,19 @@ class RedlightServerResource:
# Respond based on whether the request is identified as abusive or not. # Respond based on whether the request is identified as abusive or not.
if is_abuse: 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) request.setResponseCode(http.OK)
defer.returnValue(json.dumps({ defer.returnValue(json.dumps({
"error": None, "error": None,
"report_id": "b973d82a-6932-4cad-ac9f-f647a3a9d204", "report_id": "b973d82a-6932-4cad-ac9f-f647a3a9d204",
}).encode("utf-8")) }).encode("utf-8"))
else: else:
logger.info(f"No abuse detected for request from {request.getClientIP()}.")
request.setResponseCode(http.NO_CONTENT) request.setResponseCode(http.NO_CONTENT)
defer.returnValue(b"") defer.returnValue(b"")
except Exception as e: 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) request.setResponseCode(400)
defer.returnValue(json.dumps({"error": "Bad Request"}).encode("utf-8")) 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. # General method to respond with "Method Not Allowed" for disallowed or unrecognized HTTP methods.
def method_not_allowed(self, request): def method_not_allowed(self, request):
logger.warning(f"Method Not Allowed: {request.method.decode('ascii')} from {request.getClientIP()}.")
request.setResponseCode(405) request.setResponseCode(405)
return json.dumps({"error": "Method Not Allowed"}).encode("utf-8") return json.dumps({"error": "Method Not Allowed"}).encode("utf-8")