Compare commits

..

No commits in common. "7d37ec8145a7c01f86f038978a1c523fc2216b64" and "f7e1c6b0b88f6a50cde996f0b18d195cecc39155" have entirely different histories.

8 changed files with 120 additions and 97 deletions

View file

@ -26,18 +26,11 @@ Run the setup wizard to configure your relay.
## Config
Manage the relay config
List the current configuration key/value pairs
activityrelay config
### List
List the current config key/value pairs
activityrelay config list
### Set
Set a value for a config option
@ -118,13 +111,6 @@ Remove a domain from the whitelist.
activityrelay whitelist remove <domain>
### Import
Add all current inboxes to the whitelist
activityrelay whitelist import
## Instance
Manage the instance ban list.

View file

@ -59,6 +59,11 @@ class Application(web.Application):
return self['database']
@property
def semaphore(self):
return self['semaphore']
@property
def uptime(self):
if not self['starttime']:
@ -97,6 +102,9 @@ class Application(web.Application):
return logging.error(f'A server is already running on port {self.config.port}')
for route in routes:
if route[1] == '/stats' and logging.DEBUG < logging.root.level:
continue
self.router.add_route(*route)
logging.info(f'Starting webserver at {self.config.host} ({self.config.listen}:{self.config.port})')
@ -202,3 +210,4 @@ setattr(web.Request, 'signature', property(request_signature))
setattr(web.Request, 'config', property(lambda self: self.app.config))
setattr(web.Request, 'database', property(lambda self: self.app.database))
setattr(web.Request, 'semaphore', property(lambda self: self.app.semaphore))

View file

@ -9,22 +9,23 @@ from urllib.parse import urlparse
from .misc import DotDict, boolean
RELAY_SOFTWARE = [
relay_software_names = [
'activityrelay', # https://git.pleroma.social/pleroma/relay
'aoderelay', # https://git.asonix.dog/asonix/relay
'feditools-relay' # https://git.ptzo.gdn/feditools/relay
]
APKEYS = [
class RelayConfig(DotDict):
apkeys = {
'host',
'whitelist_enabled',
'blocked_software',
'blocked_instances',
'whitelist'
]
}
class RelayConfig(DotDict):
def __init__(self, path):
DotDict.__init__(self, {})
@ -242,7 +243,7 @@ class RelayConfig(DotDict):
'workers': self.workers,
'json_cache': self.json_cache,
'timeout': self.timeout,
'ap': {key: self[key] for key in APKEYS}
'ap': {key: self[key] for key in self.apkeys}
}
with open(self._path, 'w') as fd:

68
relay/http_debug.py Normal file
View file

@ -0,0 +1,68 @@
import logging
import aiohttp
from collections import defaultdict
STATS = {
'requests': defaultdict(int),
'response_codes': defaultdict(int),
'response_codes_per_domain': defaultdict(lambda: defaultdict(int)),
'delivery_codes': defaultdict(int),
'delivery_codes_per_domain': defaultdict(lambda: defaultdict(int)),
'exceptions': defaultdict(int),
'exceptions_per_domain': defaultdict(lambda: defaultdict(int)),
'delivery_exceptions': defaultdict(int),
'delivery_exceptions_per_domain': defaultdict(lambda: defaultdict(int))
}
async def on_request_start(session, trace_config_ctx, params):
global STATS
logging.debug("HTTP START [%r], [%r]", session, params)
STATS['requests'][params.url.host] += 1
async def on_request_end(session, trace_config_ctx, params):
global STATS
logging.debug("HTTP END [%r], [%r]", session, params)
host = params.url.host
status = params.response.status
STATS['response_codes'][status] += 1
STATS['response_codes_per_domain'][host][status] += 1
if params.method == 'POST':
STATS['delivery_codes'][status] += 1
STATS['delivery_codes_per_domain'][host][status] += 1
async def on_request_exception(session, trace_config_ctx, params):
global STATS
logging.debug("HTTP EXCEPTION [%r], [%r]", session, params)
host = params.url.host
exception = repr(params.exception)
STATS['exceptions'][exception] += 1
STATS['exceptions_per_domain'][host][exception] += 1
if params.method == 'POST':
STATS['delivery_exceptions'][exception] += 1
STATS['delivery_exceptions_per_domain'][host][exception] += 1
def http_debug():
if logging.DEBUG >= logging.root.level:
return
trace_config = aiohttp.TraceConfig()
trace_config.on_request_start.append(on_request_start)
trace_config.on_request_end.append(on_request_end)
trace_config.on_request_exception.append(on_request_exception)
return [trace_config]

View file

@ -8,7 +8,7 @@ from urllib.parse import urlparse
from . import misc, __version__
from .application import Application
from .config import RELAY_SOFTWARE
from .config import relay_software_names
app = None
@ -81,16 +81,14 @@ def cli_run():
# todo: add config default command for resetting config key
@cli.group('config')
def cli_config():
'Manage the relay config'
pass
@cli_config.command('list')
def cli_config_list():
@cli.group('config', invoke_without_command=True)
@click.pass_context
def cli_config(ctx):
'List the current relay config'
if ctx.invoked_subcommand:
return
click.echo('Relay Config:')
for key, value in app.config.items():
@ -314,7 +312,7 @@ def cli_software_ban(name, fetch_nodeinfo):
'Ban software. Use RELAYS for NAME to ban relays'
if name == 'RELAYS':
for name in RELAY_SOFTWARE:
for name in relay_software_names:
app.config.ban_software(name)
app.config.save()
@ -323,16 +321,14 @@ def cli_software_ban(name, fetch_nodeinfo):
if fetch_nodeinfo:
nodeinfo = asyncio.run(app.client.fetch_nodeinfo(name))
if not nodeinfo:
if not software:
click.echo(f'Failed to fetch software name from domain: {name}')
name = nodeinfo.sw_name
if app.config.ban_software(name):
if config.ban_software(nodeinfo.swname):
app.config.save()
return click.echo(f'Banned software: {name}')
return click.echo(f'Banned software: {nodeinfo.swname}')
click.echo(f'Software already banned: {name}')
click.echo(f'Software already banned: {nodeinfo.swname}')
@cli_software.command('unban')
@ -344,10 +340,10 @@ def cli_software_unban(name, fetch_nodeinfo):
'Ban software. Use RELAYS for NAME to unban relays'
if name == 'RELAYS':
for name in RELAY_SOFTWARE:
for name in relay_software_names:
app.config.unban_software(name)
app.config.save()
config.save()
return click.echo('Unbanned all relay software')
if fetch_nodeinfo:
@ -356,13 +352,12 @@ def cli_software_unban(name, fetch_nodeinfo):
if not nodeinfo:
click.echo(f'Failed to fetch software name from domain: {name}')
name = nodeinfo.sw_name
if app.config.unban_software(name):
if app.config.unban_software(nodeinfo.swname):
app.config.save()
return click.echo(f'Unbanned software: {name}')
return click.echo(f'Unbanned software: {nodeinfo.swname}')
click.echo(f'Software wasn\'t banned: {nodeinfo.swname}')
click.echo(f'Software wasn\'t banned: {name}')
@cli.group('whitelist')
@ -373,8 +368,6 @@ def cli_whitelist():
@cli_whitelist.command('list')
def cli_whitelist_list():
'List all the instances in the whitelist'
click.echo('Current whitelisted domains')
for domain in app.config.whitelist:
@ -410,14 +403,6 @@ def cli_whitelist_remove(instance):
click.echo(f'Removed instance from the whitelist: {instance}')
@cli_whitelist.command('import')
def cli_whitelist_import():
'Add all current inboxes to the whitelist'
for domain in app.database.hostnames:
cli_whitelist_add.callback(domain)
def main():
cli(prog_name='relay')

View file

@ -14,6 +14,8 @@ from json.decoder import JSONDecodeError
from urllib.parse import urlparse
from uuid import uuid4
from .http_debug import http_debug
app = None

View file

@ -10,16 +10,6 @@ from .misc import Message
cache = LRUCache(1024)
def person_check(actor, software):
## pleroma and akkoma use Person for the actor type for some reason
if software in {'akkoma', 'pleroma'} and actor.id != f'https://{actor.domain}/relay':
return True
## make sure the actor is an application
elif actor.type != 'Application':
return True
async def handle_relay(request):
if request.message.objectid in cache:
logging.verbose(f'already relayed {request.message.objectid}')
@ -60,40 +50,16 @@ async def handle_forward(request):
async def handle_follow(request):
nodeinfo = await request.app.client.fetch_nodeinfo(request.actor.domain)
software = nodeinfo.sw_name if nodeinfo else None
software = nodeinfo.swname if nodeinfo else None
## reject if software used by actor is banned
if request.config.is_banned_software(software):
request.app.push_message(
request.actor.shared_inbox,
Message.new_response(
host = request.config.host,
actor = request.actor.id,
followid = request.message.id,
accept = False
)
)
return logging.verbose(f'Rejected follow from actor for using specific software: actor={request.actor.id}, software={software}')
## reject if the actor is not an instance actor
if person_check(request.actor, software):
request.app.push_message(
request.actor.shared_inbox,
Message.new_response(
host = request.config.host,
actor = request.actor.id,
followid = request.message.id,
accept = False
)
)
return logging.verbose(f'Non-application actor tried to follow: {request.actor.id}')
request.database.add_inbox(request.actor.shared_inbox, request.message.id, software)
request.database.save()
request.app.push_message(
await request.app.push_message(
request.actor.shared_inbox,
Message.new_response(
host = request.config.host,
@ -106,7 +72,7 @@ async def handle_follow(request):
# Are Akkoma and Pleroma the only two that expect a follow back?
# Ignoring only Mastodon for now
if software != 'mastodon':
request.app.push_message(
await request.app.push_message(
request.actor.shared_inbox,
Message.new_follow(
host = request.config.host,
@ -125,7 +91,7 @@ async def handle_undo(request):
request.database.save()
request.app.push_message(
await request.app.push_message(
request.actor.shared_inbox,
Message.new_unfollow(
host = request.config.host,
@ -153,7 +119,7 @@ async def run_processor(request):
nodeinfo = await request.app.client.fetch_nodeinfo(request.instance['domain'])
if nodeinfo:
request.instance['software'] = nodeinfo.sw_name
request.instance['software'] = nodeinfo.swname
request.database.save()
logging.verbose(f'New "{request.message.type}" from actor: {request.actor.id}')

View file

@ -7,6 +7,7 @@ import traceback
from pathlib import Path
from . import __version__, misc
from .http_debug import STATS
from .misc import DotDict, Message, Response
from .processors import run_processor
@ -189,3 +190,8 @@ async def nodeinfo(request):
async def nodeinfo_wellknown(request):
data = aputils.WellKnownNodeinfo.new_template(request.config.host)
return Response.new(data, ctype='json')
@register_route('GET', '/stats')
async def stats(request):
return Response.new(STATS, ctype='json')