update redlight client to first working version :)
This commit is contained in:
parent
17a750eb54
commit
bd63592b05
@ -1,20 +1,41 @@
|
|||||||
import logging
|
import logging
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import http.client
|
|
||||||
from typing import Union
|
from typing import Union
|
||||||
from synapse.module_api import ModuleApi
|
from synapse.module_api import ModuleApi, NOT_SPAM
|
||||||
from synapse.api.errors import AuthError
|
from synapse.api.errors import AuthError
|
||||||
from twisted.internet import defer
|
|
||||||
from twisted.web.client import Agent, readBody
|
from twisted.web.client import Agent, readBody
|
||||||
from twisted.web.http_headers import Headers
|
from twisted.web.http_headers import Headers
|
||||||
|
from twisted.web.iweb import IBodyProducer
|
||||||
|
from twisted.internet import reactor
|
||||||
|
from twisted.internet import defer
|
||||||
|
from twisted.web.iweb import IBodyProducer
|
||||||
|
from zope.interface import implementer
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@implementer(IBodyProducer)
|
||||||
|
class _JsonProducer:
|
||||||
|
def __init__(self, data):
|
||||||
|
self._data = json.dumps(data).encode("utf-8")
|
||||||
|
self.length = len(self._data)
|
||||||
|
|
||||||
|
def startProducing(self, consumer):
|
||||||
|
consumer.write(self._data)
|
||||||
|
return defer.succeed(None)
|
||||||
|
|
||||||
|
def pauseProducing(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def stopProducing(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class RedlightClientModule:
|
class RedlightClientModule:
|
||||||
def __init__(self, config: dict, api: ModuleApi):
|
def __init__(self, config: dict, api: ModuleApi):
|
||||||
self._api = api
|
self._api = api
|
||||||
self._redlight_url = config.get("redlight_url", "https://duckdomain.xyz/_matrix/loj/v1/abuse_lookup")
|
self._redlight_url = config.get("redlight_url", "https://duckdomain.xyz/_matrix/loj/v1/abuse_lookup")
|
||||||
|
self._agent = Agent(reactor)
|
||||||
|
|
||||||
logger.info("RedLightClientModule initialized.")
|
logger.info("RedLightClientModule initialized.")
|
||||||
|
|
||||||
@ -37,28 +58,34 @@ class RedlightClientModule:
|
|||||||
hashed_room_id = self.double_hash_sha256(room)
|
hashed_room_id = self.double_hash_sha256(room)
|
||||||
hashed_user_id = self.double_hash_sha256(user)
|
hashed_user_id = self.double_hash_sha256(user)
|
||||||
|
|
||||||
# Send the PUT request
|
body = _JsonProducer({
|
||||||
connection = http.client.HTTPSConnection("localhost:8008")
|
|
||||||
headers = {
|
|
||||||
"Content-Type": "application/json"
|
|
||||||
}
|
|
||||||
body = json.dumps({
|
|
||||||
"room_id_hash": hashed_room_id,
|
"room_id_hash": hashed_room_id,
|
||||||
"user_id_hash": hashed_user_id
|
"user_id_hash": hashed_user_id
|
||||||
})
|
})
|
||||||
connection.request("PUT", "/_matrix/loj/v1/abuse_lookup", body, headers)
|
|
||||||
response = connection.getresponse()
|
|
||||||
response_body = response.read()
|
|
||||||
|
|
||||||
# Process the response from the server
|
response = await self._agent.request(
|
||||||
result = json.loads(response_body.decode())
|
b"PUT",
|
||||||
#logger.info(f'response.status = {response.status} and result["error"] = {result["error"]}')
|
self._redlight_url.encode(),
|
||||||
if response.status == 200:
|
Headers({'Content-Type': [b'application/json']}),
|
||||||
# Raise an AuthError if the API returns OK 200
|
body
|
||||||
|
)
|
||||||
|
|
||||||
|
response_body_bytes = await readBody(response)
|
||||||
|
response_body = response_body_bytes.decode("utf-8")
|
||||||
|
|
||||||
|
try:
|
||||||
|
response_json = json.loads(response_body)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
logger.error(f"Failed to decode response body: {response_body}")
|
||||||
|
#return NOT_SPAM # default to allowing if there's an error
|
||||||
|
|
||||||
|
if response.code == 200:
|
||||||
raise AuthError(403, "User not allowed to join this room")
|
raise AuthError(403, "User not allowed to join this room")
|
||||||
|
elif response.code == 204:
|
||||||
|
return NOT_SPAM
|
||||||
else:
|
else:
|
||||||
# Allow joining if no issue detected
|
logger.error(f"Unexpected response code {response.code} with body: {response_body}")
|
||||||
return self._api.NOT_SPAM
|
return NOT_SPAM # default to allowing if there's an unexpected response
|
||||||
|
|
||||||
def parse_config(config: dict) -> dict:
|
def parse_config(config: dict) -> dict:
|
||||||
return config
|
return config
|
||||||
|
@ -65,7 +65,7 @@ class RedlightServerResource:
|
|||||||
user_id_hash = data["user_id_hash"]
|
user_id_hash = data["user_id_hash"]
|
||||||
|
|
||||||
# Check the room_id_hash against your list/database or hardcoded value
|
# Check the room_id_hash against your list/database or hardcoded value
|
||||||
is_abuse = room_id_hash == "5dd9968ad279b8d918b1340dde1923ed0b99f59337f4905188955bf0f1d51d9f"
|
is_abuse = room_id_hash == "ee180279a57f716e5801335a2914e228667f363e460ccabcc49e8fd879e1be4a"
|
||||||
|
|
||||||
if is_abuse:
|
if is_abuse:
|
||||||
request.setResponseCode(http.OK)
|
request.setResponseCode(http.OK)
|
||||||
|
Loading…
Reference in New Issue
Block a user