sedi-relay/relay/manage.py

722 lines
17 KiB
Python
Raw Permalink 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
2022-12-13 13:27:09 +00:00
import json
2022-05-06 07:04:51 +00:00
import logging
import platform
2022-12-13 13:27:09 +00:00
import yaml
from datetime import datetime
2022-11-07 14:53:04 +00:00
from urllib.parse import urlparse
2022-12-13 13:27:09 +00:00
from . import __version__
2022-11-07 12:54:32 +00:00
from .application import Application
2022-12-13 13:27:09 +00:00
from .database import DEFAULT_CONFIG, RELAY_SOFTWARE
from .http_client import get, post, fetch_nodeinfo
from .misc import Message, boolean, check_open_port
2022-11-07 12:54:32 +00:00
app = None
2022-12-13 13:27:09 +00:00
CONFIG_IGNORE = {
'privkey',
'version'
}
2022-05-06 07:04:51 +00:00
@click.group('cli', context_settings={'show_default': True}, invoke_without_command=True)
2022-12-13 13:27:09 +00:00
@click.option('--config', '-c', help='path to the relay\'s config')
2022-05-06 07:04:51 +00:00
@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-12-20 11:05:06 +00:00
if ctx.invoked_subcommand != 'convert':
app.setup()
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-11-20 11:24:33 +00:00
cli_setup.callback()
2022-05-06 07:04:51 +00:00
else:
2022-11-20 11:24:33 +00:00
cli_run.callback()
2022-12-13 13:27:09 +00:00
@cli.command('convert')
@click.option('--old-config', '-o', help='path to the old relay config')
def cli_convert(old_config):
'Convert an old relay.yaml and relay.jsonld to the the new formats'
2022-12-13 13:27:09 +00:00
with open(old_config or 'relay.yaml') as fd:
config = yaml.load(fd.read(), Loader=yaml.SafeLoader)
ap = config.get('ap', {})
with open(config.get('db', 'relay.jsonld')) as fd:
db = json.load(fd)
app.config['general_host'] = ap.get('host', '__DEFAULT__')
app.config['general_listen'] = config.get('listen', '__DEFAULT__')
app.config['general_port'] = config.get('port', '__DEFAULT__')
with app.database.session as s:
s.put_config('description', config.get('note', '__DEFAULT__'))
s.put_config('push_limit', config.get('push_limit', '__DEFAULT__'))
s.put_config('json_cache', config.get('json_cache', '__DEFAULT__'))
s.put_config('workers', config.get('workers', '__DEFAULT__'))
s.put_config('http_timeout', config.get('timeout', '__DEFAULT__'))
s.put_config('privkey', db.get('private-key'))
for name in ap.get('blocked_software', []):
try: s.put_ban('software', name)
except KeyError: print(f'Already banned software: {name}')
for name in ap.get('blocked_instances', []):
try: s.put_ban('domain', name)
except KeyError: print(f'Already banned instance: {name}')
for name in ap.get('whitelist', []):
try: s.put_whitelist(name)
except KeyError: print(f'Already whitelisted domain: {name}')
for instance in db.get('relay-list', {}).values():
domain = instance['domain']
software = instance.get('software')
actor = None
if software == 'mastodon':
actor = f'https://{domain}/actor'
elif software in {'pleroma', 'akkoma'}:
actor = f'https://{domain}/relay'
s.put_instance(
domain = domain,
inbox = instance.get('inbox'),
software = software,
actor = actor,
followid = instance.get('followid'),
accept = True
)
app.config.save()
print('Config and database converted :3')
2022-11-20 11:24:33 +00:00
@cli.command('setup')
def cli_setup():
'Generate a new config'
while True:
app.config['general_host'] = click.prompt(
'What domain will the relay be hosted on?',
default = app.config.host
)
2022-11-20 11:24:33 +00:00
2022-11-27 04:01:18 +00:00
if not app.config.host.endswith('example.com'):
2022-11-20 11:24:33 +00:00
break
click.echo('The domain must not be example.com')
if not app.config.is_docker:
app.config['general_listen'] = click.prompt(
'Which address should the relay listen on?',
default = app.config.listen
)
2022-11-20 11:24:33 +00:00
while True:
app.config['general_port'] = click.prompt(
'What TCP port should the relay listen on?',
default = app.config.port,
type = int
)
break
2022-11-20 11:24:33 +00:00
app.config['database_type'] = click.prompt(
'What database backend would you like to use for the relay?',
default = app.config.dbtype,
type = click.Choice(['sqlite', 'postgresql', 'mysql']),
show_choices = True
)
if app.config.dbtype == 'sqlite':
app.config['sqlite_database'] = click.prompt(
'Where would you like to store your database file? Relative paths are relative to the config file location.',
default = app.config['sqlite_database']
)
else:
dbconfig = app.config.dbconfig
app.config.hostname = click.prompt(
'What address is your database listening on?',
default = dbconfig.hostname
) or None
app.config.port = click.prompt(
'What port is your database listening on?',
default = dbconfig.port
) or None
app.config.database = click.prompt(
'What would you like the name of the database be?',
default = dbconfig.database
) or None
app.config.username = click.prompt(
'Which user will be connecting to the database?',
default = dbconfig.username
) or None
app.config.password = click.prompt(
'What is the database user\'s password?',
default = dbconfig.password
) or None
2022-11-20 11:24:33 +00:00
app.config.save()
2022-12-13 13:27:09 +00:00
with app.database.session as s:
s.put_config('name', click.prompt(
'What do you want to name your relay?',
default = s.get_config('name')
))
s.put_config('description', click.prompt(
'Provide a small description of your relay. This will be on the front page',
default = s.get_config('description')
))
s.put_config('whitelist', click.prompt(
'Enable the whitelist?',
default = s.get_config('whitelist'),
type = boolean
))
s.put_config('require_approval', click.prompt(
'Require instances to be approved when following?',
default = s.get_config('require_approval'),
type = boolean
))
2022-11-27 00:59:20 +00:00
if not app.config.is_docker and click.confirm('Relay all setup! Would you like to run it now?'):
2022-11-20 11:24:33 +00:00
cli_run.callback()
@cli.command('run')
def cli_run():
'Run the relay'
2022-12-13 13:27:09 +00:00
with app.database.session as s:
if not s.get_config('privkey') or app.config.host.endswith('example.com'):
return click.echo('Relay is not set up. Please run "activityrelay setup".')
2022-11-20 11:24:33 +00:00
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-12-13 13:27:09 +00:00
if not check_open_port(app.config.listen, app.config.port):
2022-11-20 11:24:33 +00:00
return click.echo(f'Error: A server is already running on port {app.config.port}')
app.run()
2022-11-20 11:14:37 +00:00
# todo: add config default command for resetting config key
@cli.group('config')
def cli_config():
'Manage the relay config'
pass
2022-11-20 11:14:37 +00:00
@cli_config.command('list')
def cli_config_list():
'List the current relay config'
2022-11-20 11:14:37 +00:00
click.echo('Relay Config:')
2022-12-13 13:27:09 +00:00
with app.database.session as s:
config = s.get_config_all()
for key in DEFAULT_CONFIG.keys():
if key in CONFIG_IGNORE:
continue
keystr = f'{key}:'.ljust(20)
click.echo(f'- {keystr} {config[key]}')
2022-11-20 11:14:37 +00:00
@cli_config.command('set')
@click.argument('key')
2022-12-13 13:27:09 +00:00
@click.argument('value', nargs=-1)
2022-11-20 11:14:37 +00:00
def cli_config_set(key, value):
'Set a config value'
2022-12-13 13:27:09 +00:00
with app.database.session as s:
s.put_config(key, ' '.join(value))
value = s.get_config(key)
2022-11-20 11:14:37 +00:00
2022-12-13 13:27:09 +00:00
print(f'{key}: {value}')
2022-11-20 11:14:37 +00:00
@cli.group('inbox')
def cli_inbox():
2022-05-06 07:04:51 +00:00
'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-12-13 13:27:09 +00:00
with app.database.session as s:
for instance in s.get_instances():
click.echo(f'- {instance.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)'
if not actor.startswith('http'):
2022-11-07 14:53:04 +00:00
domain = actor
actor = f'https://{actor}/actor'
2022-11-07 14:53:04 +00:00
else:
domain = urlparse(actor).hostname
2022-12-13 13:27:09 +00:00
with app.database.session as s:
if s.get_ban('domain', domain):
return click.echo(f'Error: Refusing to follow banned actor: {actor}')
instance = s.get_instance(domain)
2022-12-13 13:27:09 +00:00
if not instance:
actor_data = asyncio.run(get(actor, sign_headers=True))
2022-12-13 13:27:09 +00:00
if not actor_data:
return click.echo(f'Failed to fetch actor: {actor}')
2022-12-13 13:27:09 +00:00
inbox = actor_data.shared_inbox
2022-12-13 13:27:09 +00:00
else:
inbox = instance.inbox
if instance.actor:
actor = instance.actor
message = Message.new_follow(
2022-11-07 14:53:04 +00:00
host = app.config.host,
actor = actor
2022-11-07 14:53:04 +00:00
)
2022-12-13 13:27:09 +00:00
asyncio.run(post(inbox, message))
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)'
2022-12-13 13:27:09 +00:00
followid = None
if not actor.startswith('http'):
2022-11-07 14:53:04 +00:00
domain = actor
actor = f'https://{actor}/actor'
2022-11-07 14:53:04 +00:00
else:
domain = urlparse(actor).hostname
2022-12-13 13:27:09 +00:00
with app.database.session as s:
instance = s.get_instance(domain)
if not instance:
actor_data = asyncio.run(get(actor, sign_headers=True))
if not actor_data:
return click.echo(f'Failed to fetch actor: {actor}')
inbox = actor_data.shared_inbox
else:
inbox = instance.inbox
followid = instance.followid
if instance.actor:
actor = instance.actor
if followid:
message = Message.new_unfollow(
2022-11-07 14:53:04 +00:00
host = app.config.host,
actor = actor,
2022-12-13 13:27:09 +00:00
follow = followid
2022-11-07 14:53:04 +00:00
)
2022-12-13 13:27:09 +00:00
else:
2022-11-07 14:53:04 +00:00
message = misc.Message.new_unfollow(
host = app.config.host,
actor = actor,
follow = {
'type': 'Follow',
'object': actor,
2022-12-13 13:27:09 +00:00
'actor': app.config.actor
2022-11-07 14:53:04 +00:00
}
)
2022-12-13 13:27:09 +00:00
asyncio.run(post(inbox, message))
2022-11-07 14:53:04 +00:00
click.echo(f'Sent unfollow message to: {actor}')
2022-05-06 07:04:51 +00:00
@cli_inbox.command('add')
2022-12-13 13:27:09 +00:00
@click.argument('actor')
def cli_inbox_add(actor):
'Add an instance to the database'
if not actor.startswith('http'):
domain = actor
actor = f'https://{actor}/inbox'
else:
domain = urlparse(actor).hostname
with app.database.session as s:
data = {
'domain': domain,
'actor': actor,
'inbox': f'https://{domain}/inbox'
}
if s.get_instance(domain):
return click.echo(f'Error: Instance already in database: {domain}')
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
if s.get_ban('domain', domain):
return click.echo(f'Error: Refusing to add banned domain: {domain}')
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
nodeinfo = asyncio.run(fetch_nodeinfo(domain))
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
if nodeinfo:
if s.get_ban('software', nodeinfo.sw_name):
return click.echo(f'Error: Refusing to add banned software: {nodeinfo.sw_name}')
2022-12-13 13:27:09 +00:00
data['software'] = nodeinfo.sw_name
2022-11-18 21:57:34 +00:00
2022-12-13 13:27:09 +00:00
actor_data = asyncio.run(get(actor, sign_headers=True))
if actor_data:
instance = s.put_instance_actor(actor, nodeinfo)
else:
instance = s.put_instance(**data)
click.echo(f'Added instance to the database: {instance.domain}')
2022-05-06 07:04:51 +00:00
@cli_inbox.command('remove')
2022-12-13 13:27:09 +00:00
@click.argument('domain')
def cli_inbox_remove(domain):
2022-05-06 07:04:51 +00:00
'Remove an inbox from the database'
2022-12-13 13:27:09 +00:00
if domain.startswith('http'):
domain = urlparse(domain).hostname
2022-12-13 13:27:09 +00:00
with app.database.session as s:
try:
s.delete_instance(domain)
click.echo(f'Removed inbox from the database: {domain}')
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
except KeyError:
return click.echo(f'Error: Inbox does not exist: {domain}')
2022-05-06 07:04:51 +00:00
@cli.group('request')
def cli_request():
'Manage follow requests'
@cli_request.command('list')
def cli_request_list():
'List all the current follow requests'
click.echo('Follow requests:')
with app.database.session as s:
for row in s.get_requests():
click.echo(f'- {row.domain}')
@cli_request.command('approve')
@click.argument('domain')
def cli_request_approve(domain):
'Approve a follow request'
with app.database.session as s:
try:
instance = s.get_request(domain)
except KeyError:
return click.echo(f'No request for domain exists: {domain}')
data = {'joined': datetime.now()}
s.update('instances', data, id=instance.id)
asyncio.run(post(
instance.inbox,
Message.new_response(
host = app.config.host,
actor = instance.actor,
followid = instance.followid,
accept = True
)
))
return click.echo(f'Accepted follow request for domain: {domain}')
@cli_request.command('deny')
@click.argument('domain')
def cli_request_deny(domain):
'Deny a follow request'
with app.database.session as s:
try:
instance = s.get_request(domain)
except KeyError:
return click.echo(f'No request for domain exists: {domain}')
s.delete_instance(domain)
asyncio.run(post(
instance.inbox,
Message.new_response(
host = app.config.host,
actor = instance.actor,
followid = instance.followid,
accept = False
)
))
return click.echo(f'Denied follow request for domain: {domain}')
2022-05-06 07:04:51 +00:00
@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-12-13 13:27:09 +00:00
with app.database.session as s:
for row in s.get_bans('domain'):
click.echo(f'- {row.name}')
2022-05-06 07:04:51 +00:00
@cli_instance.command('ban')
2022-12-13 13:27:09 +00:00
@click.argument('domain')
def cli_instance_ban(domain):
2022-05-06 07:04:51 +00:00
'Ban an instance and remove the associated inbox if it exists'
2022-12-13 13:27:09 +00:00
if domain.startswith('http'):
domain = urlparse(domain).hostname
with app.database.session as s:
try:
s.put_ban('domain', domain)
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
except KeyError:
return click.echo(f'Instance already banned: {domain}')
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
try:
s.delete_instance(domain)
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
except KeyError:
pass
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
click.echo(f'Banned instance: {domain}')
2022-05-06 07:04:51 +00:00
@cli_instance.command('unban')
2022-12-13 13:27:09 +00:00
@click.argument('domain')
def cli_instance_unban(domain):
2022-05-06 07:04:51 +00:00
'Unban an instance'
2022-12-13 13:27:09 +00:00
if domain.startswith('http'):
domain = urlparse(domain).hostname
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
with app.database.session as s:
try:
s.delete_ban('domain', domain)
click.echo(f'Unbanned instance: {domain}')
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
except KeyError:
click.echo(f'Instance wasn\'t banned: {domain}')
2022-05-06 07:04:51 +00:00
@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-12-13 13:27:09 +00:00
with app.database.session as s:
for row in s.get_bans('software'):
click.echo(f'- {row.name}')
2022-05-06 07:04:51 +00:00
@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'
2022-12-13 13:27:09 +00:00
with app.database.session as s:
if name == 'RELAYS':
for name in RELAY_SOFTWARE:
s.put_ban('software', name)
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
return click.echo('Banned all relay software')
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
if fetch_nodeinfo:
nodeinfo = asyncio.run(fetch_nodeinfo(name))
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
if not nodeinfo:
return click.echo(f'Failed to fetch software name from domain: {name}')
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
name = nodeinfo.sw_name
2022-12-13 13:27:09 +00:00
try:
s.put_ban('software', name)
click.echo(f'Banned software: {name}')
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
except KeyError:
click.echo(f'Software already banned: {name}')
2022-05-06 07:04:51 +00:00
@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'
2022-12-13 13:27:09 +00:00
with app.database.session as s:
if name == 'RELAYS':
for name in RELAY_SOFTWARE:
s.put_ban('software', name)
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
return click.echo('Unbanned all relay software')
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
if fetch_nodeinfo:
nodeinfo = asyncio.run(fetch_nodeinfo(name))
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
if not nodeinfo:
return click.echo(f'Failed to fetch software name from domain: {name}')
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
name = nodeinfo.sw_name
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
try:
s.put_ban('software', name)
click.echo(f'Unbanned software: {name}')
2022-12-13 13:27:09 +00:00
except KeyError:
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():
'List all the instances in the whitelist'
2022-12-13 13:27:09 +00:00
click.echo('Current whitelisted domains:')
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
with app.database.session as s:
for row in s.get_whitelist():
click.echo(f'- {row.domain}')
2022-05-06 07:04:51 +00:00
@cli_whitelist.command('add')
2022-12-13 13:27:09 +00:00
@click.argument('domain')
def cli_whitelist_add(domain):
'Add a domain to the whitelist'
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
with app.database.session as s:
try:
s.put_whitelist(domain)
click.echo(f'Instance added to the whitelist: {domain}')
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
except KeyError:
return click.echo(f'Instance already in the whitelist: {domain}')
2022-05-06 07:04:51 +00:00
@cli_whitelist.command('remove')
2022-12-13 13:27:09 +00:00
@click.argument('domain')
def cli_whitelist_remove(domain):
'Remove a domain from the whitelist'
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
with app.database.session as s:
try:
s.delete_whitelist(domain)
click.echo(f'Removed instance from the whitelist: {domain}')
2022-05-06 07:04:51 +00:00
2022-12-13 13:27:09 +00:00
except KeyError:
click.echo(f'Instance not in the whitelist: {domain}')
2022-05-06 07:04:51 +00:00
@cli_whitelist.command('import')
def cli_whitelist_import():
'Add all current inboxes to the whitelist'
2022-12-13 13:27:09 +00:00
with app.database.session as s:
for row in s.get_instances():
try:
s.put_whitelist(row.domain)
click.echo(f'Instance added to the whitelist: {row.domain}')
except KeyError:
click.echo(f'Instance already in the whitelist: {row.domain}')
@cli_whitelist.command('clear')
def cli_whitelist_clear():
'Clear all items out of the whitelist'
with app.database.session as s:
s.delete('whitelist')
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.')