mirror of
https://git.pleroma.social/pleroma/relay.git
synced 2024-11-23 15:08:00 +00:00
Compare commits
4 commits
c41cd6e015
...
1028256065
Author | SHA1 | Date | |
---|---|---|---|
1028256065 | |||
556ac420e6 | |||
2c287d301f | |||
ed066d94af |
|
@ -177,7 +177,7 @@ class Connection(tinysql.ConnectionMixin):
|
||||||
|
|
||||||
|
|
||||||
def get_hostnames(self):
|
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):
|
def get_instance(self, data):
|
||||||
|
@ -185,7 +185,8 @@ class Connection(tinysql.ConnectionMixin):
|
||||||
data = data.split('#', 1)[0]
|
data = data.split('#', 1)[0]
|
||||||
|
|
||||||
query = 'SELECT * FROM instances WHERE domain = :data OR actor = :data OR inbox = :data'
|
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):
|
def get_instances(self):
|
||||||
|
@ -194,11 +195,16 @@ class Connection(tinysql.ConnectionMixin):
|
||||||
|
|
||||||
|
|
||||||
def get_request(self, domain):
|
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):
|
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):
|
def get_whitelist(self):
|
||||||
|
|
|
@ -212,6 +212,8 @@ class HttpClient(AppBase):
|
||||||
return await self.get(nodeinfo_url, loads=Nodeinfo.new_from_json) or False
|
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 def get(*args, **kwargs):
|
||||||
async with HttpClient() as client:
|
async with HttpClient() as client:
|
||||||
return await client.get(*args, **kwargs)
|
return await client.get(*args, **kwargs)
|
||||||
|
|
|
@ -6,6 +6,7 @@ import logging
|
||||||
import platform
|
import platform
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from . import __version__
|
from . import __version__
|
||||||
|
@ -384,6 +385,77 @@ def cli_inbox_remove(domain):
|
||||||
return click.echo(f'Error: Inbox does not exist: {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')
|
@cli.group('instance')
|
||||||
def cli_instance():
|
def cli_instance():
|
||||||
'Manage instance bans'
|
'Manage instance bans'
|
||||||
|
|
|
@ -192,14 +192,15 @@ class DotDict(dict):
|
||||||
|
|
||||||
class Message(DotDict):
|
class Message(DotDict):
|
||||||
@classmethod
|
@classmethod
|
||||||
def new_actor(cls, host, pubkey, description=None):
|
def new_actor(cls, host, pubkey, name=None, description=None, locked=False):
|
||||||
return cls({
|
return cls({
|
||||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||||
'id': f'https://{host}/actor',
|
'id': f'https://{host}/actor',
|
||||||
'type': 'Application',
|
'type': 'Application',
|
||||||
'preferredUsername': 'relay',
|
'preferredUsername': 'relay',
|
||||||
'name': 'ActivityRelay',
|
'name': name or 'ActivityRelay',
|
||||||
'summary': description or 'ActivityRelay bot',
|
'summary': description or 'ActivityRelay bot',
|
||||||
|
'manuallyApprovesFollowers': locked,
|
||||||
'followers': f'https://{host}/followers',
|
'followers': f'https://{host}/followers',
|
||||||
'following': f'https://{host}/following',
|
'following': f'https://{host}/following',
|
||||||
'inbox': f'https://{host}/inbox',
|
'inbox': f'https://{host}/inbox',
|
||||||
|
|
|
@ -26,6 +26,11 @@ async def handle_relay(request, s):
|
||||||
logging.verbose(f'already relayed {request.message.objectid}')
|
logging.verbose(f'already relayed {request.message.objectid}')
|
||||||
return
|
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(
|
message = Message.new_announce(
|
||||||
host = request.config.host,
|
host = request.config.host,
|
||||||
object = request.message.objectid
|
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
|
## 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):
|
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}')
|
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
|
## 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}')
|
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
|
## reject if the actor is not an instance actor
|
||||||
if person_check(request.actor, software):
|
if person_check(request.actor, software):
|
||||||
logging.verbose(f'Non-application actor tried to follow: {request.actor.id}')
|
logging.verbose(f'Non-application actor tried to follow: {request.actor.id}')
|
||||||
accept = False
|
approve = False
|
||||||
|
|
||||||
if approve:
|
if approve:
|
||||||
if not request.instance:
|
if not request.instance:
|
||||||
|
@ -89,7 +94,7 @@ async def handle_follow(request, s):
|
||||||
actor_data = request.actor,
|
actor_data = request.actor,
|
||||||
software = software,
|
software = software,
|
||||||
followid = request.message.id,
|
followid = request.message.id,
|
||||||
accept = s.get_config('require_approval')
|
accept = not s.get_config('require_approval')
|
||||||
)
|
)
|
||||||
|
|
||||||
if 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
|
followid = request.message.id
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Rejects don't seem to work right with mastodon
|
||||||
request.app.push_message(
|
request.app.push_message(
|
||||||
request.actor.shared_inbox,
|
request.actor.inbox,
|
||||||
Message.new_response(
|
Message.new_response(
|
||||||
host = request.config.host,
|
host = request.config.host,
|
||||||
actor = request.actor.id,
|
actor = request.message.actorid,
|
||||||
followid = request.message.id,
|
followid = request.message.id,
|
||||||
accept = approve
|
accept = approve
|
||||||
)
|
)
|
||||||
|
@ -136,8 +142,19 @@ async def handle_undo(request, s):
|
||||||
if request.message.object.type != 'Follow':
|
if request.message.object.type != 'Follow':
|
||||||
return await handle_forward(request)
|
return await handle_forward(request)
|
||||||
|
|
||||||
s.delete('instances', id=request.instance.id)
|
instance_follow = request.instance.followid
|
||||||
|
message_follow = request.message.object.id
|
||||||
|
|
||||||
|
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.app.push_message(
|
||||||
request.actor.shared_inbox,
|
request.actor.shared_inbox,
|
||||||
Message.new_unfollow(
|
Message.new_unfollow(
|
||||||
|
@ -163,15 +180,18 @@ async def run_processor(request):
|
||||||
return
|
return
|
||||||
|
|
||||||
with request.database.session as s:
|
with request.database.session as s:
|
||||||
|
if request.instance:
|
||||||
new_data = {}
|
new_data = {}
|
||||||
|
|
||||||
if request.instance and not request.instance.software:
|
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)
|
nodeinfo = await request.app.client.fetch_nodeinfo(request.instance.domain)
|
||||||
|
|
||||||
if nodeinfo:
|
if nodeinfo:
|
||||||
new_data['software'] = nodeinfo.sw_name
|
new_data['software'] = nodeinfo.sw_name
|
||||||
|
|
||||||
if not request.instance.actor:
|
if not request.instance.actor:
|
||||||
|
logging.verbose(f'Fetching actor for instance: {request.instance.domain}')
|
||||||
new_data['actor'] = request.signature.keyid.split('#', 1)[0]
|
new_data['actor'] = request.signature.keyid.split('#', 1)[0]
|
||||||
|
|
||||||
if not request.instance.actor_data:
|
if not request.instance.actor_data:
|
||||||
|
|
|
@ -68,10 +68,13 @@ a:hover {{ color: #8AF; }}
|
||||||
|
|
||||||
|
|
||||||
@register_route('GET', '/actor', '/inbox')
|
@register_route('GET', '/actor', '/inbox')
|
||||||
async def actor(request):
|
async def actor(request, s):
|
||||||
data = Message.new_actor(
|
data = Message.new_actor(
|
||||||
host = request.config.host,
|
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')
|
return Response.new(data, ctype='activity')
|
||||||
|
@ -135,7 +138,7 @@ async def inbox(request, s):
|
||||||
return Response.new_error(401, str(e), 'json')
|
return Response.new_error(401, str(e), 'json')
|
||||||
|
|
||||||
## reject if activity type isn't 'Follow' and the actor isn't following
|
## 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}')
|
logging.verbose(f'Rejected actor for trying to post while not following: {request.actor.id}')
|
||||||
return Response.new_error(401, 'access denied', 'json')
|
return Response.new_error(401, 'access denied', 'json')
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue