sedi-relay/relay/manage.py

351 lines
8.4 KiB
Python
Raw Normal View History

2022-05-06 07:04:51 +00:00
import Crypto
import asyncio
2022-05-06 07:04:51 +00:00
import click
import logging
import platform
2022-11-07 12:54:32 +00:00
from . import misc, __version__
from .application import Application
from .config import relay_software_names
2022-11-07 12:54:32 +00:00
app = None
2022-05-06 07:04:51 +00:00
@click.group('cli', context_settings={'show_default': True}, invoke_without_command=True)
@click.option('--config', '-c', default='relay.yaml', help='path to the relay\'s config')
@click.version_option(version=__version__, prog_name='ActivityRelay')
@click.pass_context
def cli(ctx, config):
2022-11-07 12:54:32 +00:00
global app
app = Application(config)
2022-05-06 07:04:51 +00:00
if not ctx.invoked_subcommand:
2022-11-07 12:54:32 +00:00
if app.config.host.endswith('example.com'):
2022-05-06 07:04:51 +00:00
relay_setup.callback()
2022-05-06 07:04:51 +00:00
else:
relay_run.callback()
2022-05-06 07:04:51 +00:00
@cli.group('inbox')
@click.pass_context
def cli_inbox(ctx):
'Manage the inboxes in the database'
pass
2022-05-06 07:04:51 +00:00
@cli_inbox.command('list')
def cli_inbox_list():
'List the connected instances or relays'
2022-05-06 07:04:51 +00:00
click.echo('Connected to the following instances or relays:')
2022-11-07 12:54:32 +00:00
for inbox in app.database.inboxes:
2022-05-06 07:04:51 +00:00
click.echo(f'- {inbox}')
2022-05-06 07:04:51 +00:00
@cli_inbox.command('follow')
@click.argument('actor')
def cli_inbox_follow(actor):
'Follow an actor (Relay must be running)'
2022-11-07 12:54:32 +00:00
if app.config.is_banned(actor):
return click.echo(f'Error: Refusing to follow banned actor: {actor}')
if not actor.startswith('http'):
actor = f'https://{actor}/actor'
2022-11-07 12:54:32 +00:00
if app.database.get_inbox(actor):
return click.echo(f'Error: Already following actor: {actor}')
2022-11-07 10:40:08 +00:00
actor_data = asyncio.run(misc.request(actor, sign_headers=True))
if not actor_data:
return click.echo(f'Error: Failed to fetch actor: {actor}')
2022-11-07 12:54:32 +00:00
app.database.add_inbox(actor_data.shared_inbox)
app.database.save()
2022-11-07 10:40:08 +00:00
asyncio.run(misc.follow_remote_actor(actor))
click.echo(f'Sent follow message to actor: {actor}')
2022-05-06 07:04:51 +00:00
@cli_inbox.command('unfollow')
@click.argument('actor')
def cli_inbox_unfollow(actor):
'Unfollow an actor (Relay must be running)'
if not actor.startswith('http'):
actor = f'https://{actor}/actor'
2022-11-07 12:54:32 +00:00
if app.database.del_inbox(actor):
app.database.save()
2022-11-07 10:40:08 +00:00
asyncio.run(misc.unfollow_remote_actor(actor))
return click.echo(f'Sent unfollow message to: {actor}')
return click.echo(f'Error: Not following actor: {actor}')
2022-05-06 07:04:51 +00:00
@cli_inbox.command('add')
@click.argument('inbox')
def cli_inbox_add(inbox):
'Add an inbox to the database'
if not inbox.startswith('http'):
inbox = f'https://{inbox}/inbox'
2022-11-07 12:54:32 +00:00
if app.config.is_banned(inbox):
return click.echo(f'Error: Refusing to add banned inbox: {inbox}')
2022-05-06 07:04:51 +00:00
2022-11-07 12:54:32 +00:00
if app.database.add_inbox(inbox):
app.database.save()
return click.echo(f'Added inbox to the database: {inbox}')
click.echo(f'Error: Inbox already in database: {inbox}')
2022-05-06 07:04:51 +00:00
@cli_inbox.command('remove')
@click.argument('inbox')
def cli_inbox_remove(inbox):
'Remove an inbox from the database'
try:
2022-11-07 12:54:32 +00:00
dbinbox = app.database.get_inbox(inbox, fail=True)
except KeyError:
2022-05-06 07:04:51 +00:00
click.echo(f'Error: Inbox does not exist: {inbox}')
return
2022-11-07 12:54:32 +00:00
app.database.del_inbox(dbinbox['domain'])
app.database.save()
2022-05-06 07:04:51 +00:00
click.echo(f'Removed inbox from the database: {inbox}')
@cli.group('instance')
def cli_instance():
'Manage instance bans'
pass
@cli_instance.command('list')
def cli_instance_list():
'List all banned instances'
click.echo('Banned instances or relays:')
2022-11-07 12:54:32 +00:00
for domain in app.config.blocked_instances:
2022-05-06 07:04:51 +00:00
click.echo(f'- {domain}')
@cli_instance.command('ban')
@click.argument('target')
def cli_instance_ban(target):
'Ban an instance and remove the associated inbox if it exists'
if target.startswith('http'):
target = urlparse(target).hostname
2022-05-06 07:04:51 +00:00
2022-11-07 12:54:32 +00:00
if app.config.ban_instance(target):
app.config.save()
2022-05-06 07:04:51 +00:00
2022-11-07 12:54:32 +00:00
if app.database.del_inbox(target):
app.database.save()
2022-05-06 07:04:51 +00:00
click.echo(f'Banned instance: {target}')
return
click.echo(f'Instance already banned: {target}')
@cli_instance.command('unban')
@click.argument('target')
def cli_instance_unban(target):
'Unban an instance'
2022-11-07 12:54:32 +00:00
if app.config.unban_instance(target):
app.config.save()
2022-05-06 07:04:51 +00:00
click.echo(f'Unbanned instance: {target}')
return
click.echo(f'Instance wasn\'t banned: {target}')
@cli.group('software')
def cli_software():
'Manage banned software'
pass
@cli_software.command('list')
def cli_software_list():
'List all banned software'
click.echo('Banned software:')
2022-11-07 12:54:32 +00:00
for software in app.config.blocked_software:
2022-05-06 07:04:51 +00:00
click.echo(f'- {software}')
@cli_software.command('ban')
@click.option('--fetch-nodeinfo/--ignore-nodeinfo', '-f', 'fetch_nodeinfo', default=False,
help='Treat NAME like a domain and try to fet the software name from nodeinfo'
)
@click.argument('name')
def cli_software_ban(name, fetch_nodeinfo):
'Ban software. Use RELAYS for NAME to ban relays'
if name == 'RELAYS':
for name in relay_software_names:
2022-11-07 12:54:32 +00:00
app.config.ban_software(name)
2022-05-06 07:04:51 +00:00
2022-11-07 12:54:32 +00:00
app.config.save()
2022-05-06 07:04:51 +00:00
return click.echo('Banned all relay software')
if fetch_nodeinfo:
2022-11-07 10:40:08 +00:00
software = asyncio.run(misc.fetch_nodeinfo(name))
2022-05-06 07:04:51 +00:00
if not software:
click.echo(f'Failed to fetch software name from domain: {name}')
name = software
if config.ban_software(name):
2022-11-07 12:54:32 +00:00
app.config.save()
2022-05-06 07:04:51 +00:00
return click.echo(f'Banned software: {name}')
click.echo(f'Software already banned: {name}')
@cli_software.command('unban')
@click.option('--fetch-nodeinfo/--ignore-nodeinfo', '-f', 'fetch_nodeinfo', default=False,
help='Treat NAME like a domain and try to fet the software name from nodeinfo'
)
@click.argument('name')
def cli_software_unban(name, fetch_nodeinfo):
'Ban software. Use RELAYS for NAME to unban relays'
if name == 'RELAYS':
for name in relay_software_names:
2022-11-07 12:54:32 +00:00
app.config.unban_software(name)
2022-05-06 07:04:51 +00:00
config.save()
return click.echo('Unbanned all relay software')
if fetch_nodeinfo:
2022-11-07 10:40:08 +00:00
software = asyncio.run(misc.fetch_nodeinfo(name))
2022-05-06 07:04:51 +00:00
if not software:
click.echo(f'Failed to fetch software name from domain: {name}')
name = software
2022-11-07 12:54:32 +00:00
if app.config.unban_software(name):
app.config.save()
2022-05-06 07:04:51 +00:00
return click.echo(f'Unbanned software: {name}')
click.echo(f'Software wasn\'t banned: {name}')
2022-05-06 07:04:51 +00:00
@cli.group('whitelist')
def cli_whitelist():
'Manage the instance whitelist'
pass
@cli_whitelist.command('list')
def cli_whitelist_list():
click.echo('Current whitelisted domains')
2022-11-07 12:54:32 +00:00
for domain in app.config.whitelist:
2022-05-06 07:04:51 +00:00
click.echo(f'- {domain}')
@cli_whitelist.command('add')
@click.argument('instance')
def cli_whitelist_add(instance):
'Add an instance to the whitelist'
2022-11-07 12:54:32 +00:00
if not app.config.add_whitelist(instance):
2022-05-06 07:04:51 +00:00
return click.echo(f'Instance already in the whitelist: {instance}')
2022-11-07 12:54:32 +00:00
app.config.save()
2022-05-06 07:04:51 +00:00
click.echo(f'Instance added to the whitelist: {instance}')
@cli_whitelist.command('remove')
@click.argument('instance')
def cli_whitelist_remove(instance):
'Remove an instance from the whitelist'
2022-11-07 12:54:32 +00:00
if not app.config.del_whitelist(instance):
2022-05-06 07:04:51 +00:00
return click.echo(f'Instance not in the whitelist: {instance}')
2022-11-07 12:54:32 +00:00
app.config.save()
2022-05-06 07:04:51 +00:00
2022-11-07 12:54:32 +00:00
if app.config.whitelist_enabled:
if app.database.del_inbox(inbox):
app.database.save()
2022-05-06 07:04:51 +00:00
click.echo(f'Removed instance from the whitelist: {instance}')
@cli.command('setup')
def relay_setup():
'Generate a new config'
while True:
2022-11-07 12:54:32 +00:00
app.config.host = click.prompt('What domain will the relay be hosted on?', default=app.config.host)
2022-05-06 07:04:51 +00:00
if not config.host.endswith('example.com'):
break
click.echo('The domain must not be example.com')
2022-11-07 12:54:32 +00:00
app.config.listen = click.prompt('Which address should the relay listen on?', default=app.config.listen)
2022-05-06 07:04:51 +00:00
while True:
2022-11-07 12:54:32 +00:00
app.config.port = click.prompt('What TCP port should the relay listen on?', default=app.config.port, type=int)
2022-05-06 07:04:51 +00:00
break
2022-11-07 12:54:32 +00:00
app.config.save()
2022-05-06 07:04:51 +00:00
if not app['is_docker'] and click.confirm('Relay all setup! Would you like to run it now?'):
relay_run.callback()
@cli.command('run')
def relay_run():
'Run the relay'
2022-11-07 12:54:32 +00:00
if app.config.host.endswith('example.com'):
2022-05-06 07:04:51 +00:00
return click.echo('Relay is not set up. Please edit your relay config or run "activityrelay setup".')
vers_split = platform.python_version().split('.')
pip_command = 'pip3 uninstall pycrypto && pip3 install pycryptodome'
if Crypto.__version__ == '2.6.1':
if int(vers_split[1]) > 7:
click.echo('Error: PyCrypto is broken on Python 3.8+. Please replace it with pycryptodome before running again. Exiting...')
return click.echo(pip_command)
else:
click.echo('Warning: PyCrypto is old and should be replaced with pycryptodome')
return click.echo(pip_command)
2022-11-07 12:54:32 +00:00
if not misc.check_open_port(app.config.listen, app.config.port):
return click.echo(f'Error: A server is already running on port {app.config.port}')
2022-05-06 07:04:51 +00:00
2022-11-07 12:54:32 +00:00
app.run()
2022-05-06 07:04:51 +00:00
def main():
cli(prog_name='relay')
if __name__ == '__main__':
2022-05-06 07:04:51 +00:00
click.echo('Running relay.manage is depreciated. Run `activityrelay [command]` instead.')