From fc7de1a3bc3024b4e56c60a0e18788e09c272524 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Fri, 12 Aug 2022 02:36:08 -0400 Subject: [PATCH 1/4] use proper accept header for nodeinfo fetching --- relay/misc.py | 11 ++++++----- relay/processors.py | 3 +++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/relay/misc.py b/relay/misc.py index 91f6f3f..ee06f39 100644 --- a/relay/misc.py +++ b/relay/misc.py @@ -136,7 +136,7 @@ async def fetch_actor_key(actor): async def fetch_nodeinfo(domain): nodeinfo_url = None - wk_nodeinfo = await request(f'https://{domain}/.well-known/nodeinfo', sign_headers=False) + wk_nodeinfo = await request(f'https://{domain}/.well-known/nodeinfo', sign_headers=False, activity=False) if not wk_nodeinfo: return @@ -149,7 +149,7 @@ async def fetch_nodeinfo(domain): if not nodeinfo_url: return - nodeinfo_data = await request(nodeinfo_url, sign_headers=False) + nodeinfo_data = await request(nodeinfo_url, sign_headers=False, activity=False) try: return nodeinfo_data['software']['name'] @@ -211,7 +211,7 @@ async def unfollow_remote_actor(actor_uri): await request(inbox, message) -async def request(uri, data=None, force=False, sign_headers=True): +async def request(uri, data=None, force=False, sign_headers=True, activity=True): ## If a get request and not force, try to use the cache first if not data and not force: try: @@ -223,14 +223,15 @@ async def request(uri, data=None, force=False, sign_headers=True): url = urlparse(uri) method = 'POST' if data else 'GET' headers = {'User-Agent': 'ActivityRelay'} + mimetype = 'application/activity+json' if activity else 'application/json' ## Set the content type for a POST if data and 'Content-Type' not in headers: - headers['Content-Type'] = 'application/activity+json' + headers['Content-Type'] = mimetype ## Set the accepted content type for a GET elif not data and 'Accept' not in headers: - headers['Accept'] = 'application/activity+json' + headers['Accept'] = mimetype if sign_headers: signing_headers = { diff --git a/relay/processors.py b/relay/processors.py index de8cbb4..2f8b351 100644 --- a/relay/processors.py +++ b/relay/processors.py @@ -118,5 +118,8 @@ processors = { async def run_processor(request, data, actor): + if data['type'] not in processors: + return + logging.verbose(f'New activity from actor: {actor["id"]} {data["type"]}') return await processors[data['type']](actor, data, request) From 59a05224ff56d3e15e465a21f323d59e1c1ad4cb Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Fri, 12 Aug 2022 03:22:30 -0400 Subject: [PATCH 2/4] add LOG_FILE env var --- relay/logger.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/relay/logger.py b/relay/logger.py index 6351bfc..166cbf2 100644 --- a/relay/logger.py +++ b/relay/logger.py @@ -1,6 +1,8 @@ import logging import os +from pathlib import Path + ## Add the verbose logging level def verbose(message, *args, **kwargs): @@ -14,9 +16,15 @@ setattr(logging, 'VERBOSE', 15) logging.addLevelName(15, 'VERBOSE') -## Get log level from environment if possible +## Get log level and file from environment if possible env_log_level = os.environ.get('LOG_LEVEL', 'INFO').upper() +try: + env_log_file = Path(os.environ.get('LOG_FILE')).expanduser().resolve() + +except TypeError: + env_log_file = None + ## Make sure the level from the environment is valid try: @@ -27,8 +35,13 @@ except AttributeError: ## Set logging config +handlers = [logging.StreamHandler()] + +if env_log_file: + handlers.append(logging.FileHandler(env_log_file)) + logging.basicConfig( level = log_level, format = "[%(asctime)s] %(levelname)s: %(message)s", - handlers = [logging.StreamHandler()] + handlers = handlers ) From 6fcaf47f3996ff9114766d40585d20b3b4cebf95 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Fri, 12 Aug 2022 15:43:24 -0400 Subject: [PATCH 3/4] fix debug logging for distill_object_id --- relay/misc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/relay/misc.py b/relay/misc.py index ee06f39..50a575b 100644 --- a/relay/misc.py +++ b/relay/misc.py @@ -58,7 +58,7 @@ def create_signature_header(headers): def distill_object_id(activity): - logging.debug('>> determining object ID for', activity['object']) + logging.debug(f'>> determining object ID for {activity["object"]}') try: return activity['object']['id'] From b6f311c42d08469d044d2de474c25427bee3bab2 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Fri, 12 Aug 2022 16:38:33 -0400 Subject: [PATCH 4/4] version bump --- relay/__init__.py | 2 +- setup.cfg | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/relay/__init__.py b/relay/__init__.py index 5afabae..9489f87 100644 --- a/relay/__init__.py +++ b/relay/__init__.py @@ -1,4 +1,4 @@ -__version__ = '0.2.1' +__version__ = '0.2.2' from aiohttp.web import Application diff --git a/setup.cfg b/setup.cfg index 4009a43..fb2ce75 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = relay -version = 0.2.1 +version = 0.2.2 description = Generic LitePub relay (works with all LitePub consumers and Mastodon) long_description = file: README.md long_description_content_type = text/markdown; charset=UTF-8