2022-05-06 07:04:51 +00:00
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
from cachetools import LRUCache
|
2022-05-06 07:04:51 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
from .database import RELAY_SOFTWARE
|
2022-11-29 22:41:04 +00:00
|
|
|
from .misc import Message
|
2022-11-26 23:56:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
cache = LRUCache(1024)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
|
2022-12-04 09:40:40 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
async def handle_relay(request, s):
|
2022-11-26 23:56:34 +00:00
|
|
|
if request.message.objectid in cache:
|
2022-11-18 21:41:14 +00:00
|
|
|
logging.verbose(f'already relayed {request.message.objectid}')
|
2022-05-06 07:04:51 +00:00
|
|
|
return
|
|
|
|
|
2022-12-13 15:14:27 +00:00
|
|
|
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
|
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
message = Message.new_announce(
|
2022-11-18 21:41:14 +00:00
|
|
|
host = request.config.host,
|
|
|
|
object = request.message.objectid
|
2022-11-07 10:30:13 +00:00
|
|
|
)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
cache[request.message.objectid] = message.id
|
2022-05-06 07:04:51 +00:00
|
|
|
logging.debug(f'>> relay: {message}')
|
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
inboxes = s.distill_inboxes(request.message)
|
2022-11-20 10:50:14 +00:00
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
for inbox in inboxes:
|
|
|
|
request.app.push_message(inbox, message)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
async def handle_forward(request, s):
|
2022-11-26 23:56:34 +00:00
|
|
|
if request.message.id in cache:
|
2022-11-18 21:41:14 +00:00
|
|
|
logging.verbose(f'already forwarded {request.message.id}')
|
2022-05-06 07:04:51 +00:00
|
|
|
return
|
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
message = Message.new_announce(
|
2022-11-18 21:41:14 +00:00
|
|
|
host = request.config.host,
|
|
|
|
object = request.message
|
2022-11-07 10:30:13 +00:00
|
|
|
)
|
2022-11-06 06:11:36 +00:00
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
cache[request.message.id] = message.id
|
|
|
|
logging.debug(f'>> forward: {message}')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
inboxes = s.distill_inboxes(request.message)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
for inbox in inboxes:
|
|
|
|
request.app.push_message(inbox, message)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
async def handle_follow(request, s):
|
|
|
|
approve = True
|
|
|
|
|
2022-11-26 23:56:34 +00:00
|
|
|
nodeinfo = await request.app.client.fetch_nodeinfo(request.actor.domain)
|
2022-12-04 09:16:50 +00:00
|
|
|
software = nodeinfo.sw_name if nodeinfo else None
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
## 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}')
|
2022-12-13 15:14:27 +00:00
|
|
|
approve = False
|
2022-12-04 09:40:40 +00:00
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
## reject if software used by actor is banned
|
2022-12-13 15:14:27 +00:00
|
|
|
if s.get_ban('software', software):
|
2022-12-13 13:27:09 +00:00
|
|
|
logging.verbose(f'Rejected follow from actor for using specific software: actor={request.actor.id}, software={software}')
|
2022-12-13 15:14:27 +00:00
|
|
|
approve = False
|
2022-11-18 21:41:14 +00:00
|
|
|
|
2022-12-04 09:40:40 +00:00
|
|
|
## reject if the actor is not an instance actor
|
|
|
|
if person_check(request.actor, software):
|
2022-12-13 13:27:09 +00:00
|
|
|
logging.verbose(f'Non-application actor tried to follow: {request.actor.id}')
|
2022-12-13 15:14:27 +00:00
|
|
|
approve = False
|
2022-12-13 13:27:09 +00:00
|
|
|
|
|
|
|
if approve:
|
|
|
|
if not request.instance:
|
|
|
|
s.put_instance(
|
|
|
|
domain = request.actor.domain,
|
2022-12-04 09:40:40 +00:00
|
|
|
actor = request.actor.id,
|
2022-12-13 13:27:09 +00:00
|
|
|
inbox = request.actor.shared_inbox,
|
|
|
|
actor_data = request.actor,
|
|
|
|
software = software,
|
2022-12-04 09:40:40 +00:00
|
|
|
followid = request.message.id,
|
2022-12-13 13:27:09 +00:00
|
|
|
accept = s.get_config('require_approval')
|
2022-12-04 09:40:40 +00:00
|
|
|
)
|
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
if s.get_config('require_approval'):
|
|
|
|
return
|
2022-12-04 09:40:40 +00:00
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
else:
|
|
|
|
s.put_instance(
|
|
|
|
domain = request.actor.domain,
|
|
|
|
followid = request.message.id
|
|
|
|
)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-12-14 00:44:08 +00:00
|
|
|
# Rejects don't seem to work right with mastodon
|
2022-12-04 09:40:40 +00:00
|
|
|
request.app.push_message(
|
2022-12-13 15:14:27 +00:00
|
|
|
request.actor.inbox,
|
2022-11-26 23:56:34 +00:00
|
|
|
Message.new_response(
|
2022-11-18 21:41:14 +00:00
|
|
|
host = request.config.host,
|
2022-12-13 15:14:27 +00:00
|
|
|
actor = request.message.actorid,
|
2022-11-18 21:41:14 +00:00
|
|
|
followid = request.message.id,
|
2022-12-13 13:27:09 +00:00
|
|
|
accept = approve
|
2022-11-07 10:30:13 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
## Don't send a follow if the the follow has been rejected
|
|
|
|
if not approve:
|
|
|
|
return
|
|
|
|
|
|
|
|
## Make sure two relays aren't continuously following each other
|
|
|
|
if software in RELAY_SOFTWARE and not request.instance:
|
|
|
|
return
|
|
|
|
|
2022-11-07 10:30:13 +00:00
|
|
|
# Are Akkoma and Pleroma the only two that expect a follow back?
|
|
|
|
# Ignoring only Mastodon for now
|
2022-11-18 21:41:14 +00:00
|
|
|
if software != 'mastodon':
|
2022-12-04 09:40:40 +00:00
|
|
|
request.app.push_message(
|
2022-11-18 21:41:14 +00:00
|
|
|
request.actor.shared_inbox,
|
2022-11-26 23:56:34 +00:00
|
|
|
Message.new_follow(
|
2022-11-18 21:41:14 +00:00
|
|
|
host = request.config.host,
|
|
|
|
actor = request.actor.id
|
2022-11-07 10:30:13 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
async def handle_undo(request, s):
|
2022-11-06 00:15:40 +00:00
|
|
|
## If the object is not a Follow, forward it
|
2022-11-18 21:41:14 +00:00
|
|
|
if request.message.object.type != 'Follow':
|
|
|
|
return await handle_forward(request)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-12-13 15:14:27 +00:00
|
|
|
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}')
|
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
s.delete('instances', id=request.instance.id)
|
2022-12-13 15:14:27 +00:00
|
|
|
logging.verbose(f'Removed inbox: {request.instance.inbox}')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-12-13 15:14:27 +00:00
|
|
|
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
|
|
|
|
)
|
2022-11-26 23:56:34 +00:00
|
|
|
)
|
2022-11-07 10:30:13 +00:00
|
|
|
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
processors = {
|
|
|
|
'Announce': handle_relay,
|
|
|
|
'Create': handle_relay,
|
|
|
|
'Delete': handle_forward,
|
|
|
|
'Follow': handle_follow,
|
|
|
|
'Undo': handle_undo,
|
|
|
|
'Update': handle_forward,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-18 21:41:14 +00:00
|
|
|
async def run_processor(request):
|
|
|
|
if request.message.type not in processors:
|
2022-08-12 06:36:08 +00:00
|
|
|
return
|
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
with request.database.session as s:
|
|
|
|
new_data = {}
|
|
|
|
|
|
|
|
if request.instance and not request.instance.software:
|
2022-12-13 15:14:27 +00:00
|
|
|
logging.verbose(f'Fetching nodeinfo for instance: {request.instance.domain}')
|
2022-12-13 13:27:09 +00:00
|
|
|
nodeinfo = await request.app.client.fetch_nodeinfo(request.instance.domain)
|
|
|
|
|
|
|
|
if nodeinfo:
|
|
|
|
new_data['software'] = nodeinfo.sw_name
|
|
|
|
|
|
|
|
if not request.instance.actor:
|
2022-12-13 15:14:27 +00:00
|
|
|
logging.verbose(f'Fetching actor for instance: {request.instance.domain}')
|
2022-12-13 13:27:09 +00:00
|
|
|
new_data['actor'] = request.signature.keyid.split('#', 1)[0]
|
|
|
|
|
|
|
|
if not request.instance.actor_data:
|
|
|
|
new_data['actor_data'] = request.actor
|
2022-11-20 10:22:57 +00:00
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
if new_data:
|
|
|
|
s.put_instance(request.actor.domain, **new_data)
|
2022-11-20 10:22:57 +00:00
|
|
|
|
2022-12-13 13:27:09 +00:00
|
|
|
logging.verbose(f'New "{request.message.type}" from actor: {request.actor.id}')
|
|
|
|
return await processors[request.message.type](request, s)
|