Compare commits

..

4 commits

Author SHA1 Message Date
Izalia Mae 1028256065 add cli commands to approve/deny follow requests 2022-12-13 20:46:03 -05:00
Izalia Mae 556ac420e6 a few extra comments 2022-12-13 19:44:08 -05:00
Izalia Mae 2c287d301f prevent (un)follows from users 2022-12-13 10:14:27 -05:00
Izalia Mae ed066d94af add more info to actor endpoint 2022-12-13 10:09:12 -05:00
6 changed files with 139 additions and 35 deletions

View file

@ -177,7 +177,7 @@ class Connection(tinysql.ConnectionMixin):
def get_hostnames(self):
return tuple(row.domain for row in self.select('instances'))
return tuple(row.domain for row in self.get_instances())
def get_instance(self, data):
@ -185,7 +185,8 @@ class Connection(tinysql.ConnectionMixin):
data = data.split('#', 1)[0]
query = 'SELECT * FROM instances WHERE domain = :data OR actor = :data OR inbox = :data'
return self.execute(query, dict(data=data), table='instances').one()
row = self.execute(query, dict(data=data), table='instances').one()
return row if row.joined else None
def get_instances(self):
@ -194,11 +195,16 @@ class Connection(tinysql.ConnectionMixin):
def get_request(self, domain):
return self.select('instances', domain=domain, joined=None).one()
for instance in self.get_requests():
if instance.domain == domain:
return instance
raise KeyError(domain)
def get_requests(self):
self.select('instances', joined=None).all()
query = 'SELECT * FROM instances WHERE joined IS NULL'
return self.execute(query, table='instances').all()
def get_whitelist(self):

View file

@ -212,6 +212,8 @@ class HttpClient(AppBase):
return await self.get(nodeinfo_url, loads=Nodeinfo.new_from_json) or False
## http client methods can't be called directly from manage.py,
## so here's some wrapper functions
async def get(*args, **kwargs):
async with HttpClient() as client:
return await client.get(*args, **kwargs)

View file

@ -6,6 +6,7 @@ import logging
import platform
import yaml
from datetime import datetime
from urllib.parse import urlparse
from . import __version__
@ -384,6 +385,77 @@ def cli_inbox_remove(domain):
return click.echo(f'Error: Inbox does not exist: {domain}')
@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}')
@cli.group('instance')
def cli_instance():
'Manage instance bans'

View file

@ -192,14 +192,15 @@ class DotDict(dict):
class Message(DotDict):
@classmethod
def new_actor(cls, host, pubkey, description=None):
def new_actor(cls, host, pubkey, name=None, description=None, locked=False):
return cls({
'@context': 'https://www.w3.org/ns/activitystreams',
'id': f'https://{host}/actor',
'type': 'Application',
'preferredUsername': 'relay',
'name': 'ActivityRelay',
'name': name or 'ActivityRelay',
'summary': description or 'ActivityRelay bot',
'manuallyApprovesFollowers': locked,
'followers': f'https://{host}/followers',
'following': f'https://{host}/following',
'inbox': f'https://{host}/inbox',

View file

@ -26,6 +26,11 @@ async def handle_relay(request, s):
logging.verbose(f'already relayed {request.message.objectid}')
return
if request.message.get('to') != ['https://www.w3.org/ns/activitystreams#Public']:
logging.verbose('Message was not public')
logging.verbose(request.message.get('to'))
return
message = Message.new_announce(
host = request.config.host,
object = request.message.objectid
@ -68,17 +73,17 @@ async def handle_follow(request, s):
## reject if the actor isn't whitelisted while the whiltelist is enabled
if s.get_config('whitelist') and not s.get_whitelist(request.actor.domain):
logging.verbose(f'Rejected actor for not being in the whitelist: {request.actor.id}')
accept = False
approve = False
## reject if software used by actor is banned
if s.get_banned_software(software):
if s.get_ban('software', software):
logging.verbose(f'Rejected follow from actor for using specific software: actor={request.actor.id}, software={software}')
accept = False
approve = False
## reject if the actor is not an instance actor
if person_check(request.actor, software):
logging.verbose(f'Non-application actor tried to follow: {request.actor.id}')
accept = False
approve = False
if approve:
if not request.instance:
@ -89,7 +94,7 @@ async def handle_follow(request, s):
actor_data = request.actor,
software = software,
followid = request.message.id,
accept = s.get_config('require_approval')
accept = not s.get_config('require_approval')
)
if s.get_config('require_approval'):
@ -101,11 +106,12 @@ async def handle_follow(request, s):
followid = request.message.id
)
# Rejects don't seem to work right with mastodon
request.app.push_message(
request.actor.shared_inbox,
request.actor.inbox,
Message.new_response(
host = request.config.host,
actor = request.actor.id,
actor = request.message.actorid,
followid = request.message.id,
accept = approve
)
@ -136,16 +142,27 @@ async def handle_undo(request, s):
if request.message.object.type != 'Follow':
return await handle_forward(request)
s.delete('instances', id=request.instance.id)
instance_follow = request.instance.followid
message_follow = request.message.object.id
request.app.push_message(
request.actor.shared_inbox,
Message.new_unfollow(
host = request.config.host,
actor = request.actor.id,
follow = request.message
if person_check(request.actor, request.instance.software):
return logging.verbose(f'Non-application actor tried to unfollow: {request.actor.id}')
if instance_follow and instance_follow != message_follow:
return logging.verbose(f'Followid does not match: {instance_follow}, {message_follow}')
s.delete('instances', id=request.instance.id)
logging.verbose(f'Removed inbox: {request.instance.inbox}')
if request.instance.software != 'mastodon':
request.app.push_message(
request.actor.shared_inbox,
Message.new_unfollow(
host = request.config.host,
actor = request.actor.id,
follow = request.message
)
)
)
processors = {
@ -163,22 +180,25 @@ async def run_processor(request):
return
with request.database.session as s:
new_data = {}
if request.instance:
new_data = {}
if request.instance and not request.instance.software:
nodeinfo = await request.app.client.fetch_nodeinfo(request.instance.domain)
if not request.instance.software:
logging.verbose(f'Fetching nodeinfo for instance: {request.instance.domain}')
nodeinfo = await request.app.client.fetch_nodeinfo(request.instance.domain)
if nodeinfo:
new_data['software'] = nodeinfo.sw_name
if nodeinfo:
new_data['software'] = nodeinfo.sw_name
if not request.instance.actor:
new_data['actor'] = request.signature.keyid.split('#', 1)[0]
if not request.instance.actor:
logging.verbose(f'Fetching actor for instance: {request.instance.domain}')
new_data['actor'] = request.signature.keyid.split('#', 1)[0]
if not request.instance.actor_data:
new_data['actor_data'] = request.actor
if not request.instance.actor_data:
new_data['actor_data'] = request.actor
if new_data:
s.put_instance(request.actor.domain, **new_data)
if new_data:
s.put_instance(request.actor.domain, **new_data)
logging.verbose(f'New "{request.message.type}" from actor: {request.actor.id}')
return await processors[request.message.type](request, s)

View file

@ -68,10 +68,13 @@ a:hover {{ color: #8AF; }}
@register_route('GET', '/actor', '/inbox')
async def actor(request):
async def actor(request, s):
data = Message.new_actor(
host = request.config.host,
pubkey = request.app.signer.pubkey
pubkey = request.app.signer.pubkey,
name = s.get_config('name'),
description = s.get_config('description'),
locked = s.get_config('require_approval')
)
return Response.new(data, ctype='activity')
@ -135,7 +138,7 @@ async def inbox(request, s):
return Response.new_error(401, str(e), 'json')
## reject if activity type isn't 'Follow' and the actor isn't following
if request.message.type != 'Follow' and not request.instance:
if request.message.type != 'Follow' and (not request.instance or not request.instance.joined):
logging.verbose(f'Rejected actor for trying to post while not following: {request.actor.id}')
return Response.new_error(401, 'access denied', 'json')