2022-11-20 10:50:14 +00:00
|
|
|
import asyncio
|
2022-05-06 07:04:51 +00:00
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
import traceback
|
|
|
|
|
2022-11-16 15:41:00 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2022-11-07 12:54:32 +00:00
|
|
|
from . import __version__, misc
|
2022-05-06 07:04:51 +00:00
|
|
|
from .http_debug import STATS
|
2022-11-17 21:30:56 +00:00
|
|
|
from .misc import DotDict, Message, Response, WKNodeinfo
|
2022-05-06 07:04:51 +00:00
|
|
|
from .processors import run_processor
|
|
|
|
|
|
|
|
|
2022-11-07 12:54:32 +00:00
|
|
|
routes = []
|
2022-11-16 15:41:00 +00:00
|
|
|
version = __version__
|
|
|
|
|
2022-11-07 12:54:32 +00:00
|
|
|
|
2022-11-16 15:41:00 +00:00
|
|
|
if Path(__file__).parent.parent.joinpath('.git').exists():
|
|
|
|
try:
|
|
|
|
commit_label = subprocess.check_output(["git", "rev-parse", "HEAD"]).strip().decode('ascii')
|
|
|
|
version = f'{__version__} {commit_label}'
|
2022-11-09 10:58:35 +00:00
|
|
|
|
2022-11-16 15:41:00 +00:00
|
|
|
except:
|
|
|
|
pass
|
2022-11-09 10:58:35 +00:00
|
|
|
|
2022-11-07 12:54:32 +00:00
|
|
|
|
|
|
|
def register_route(method, path):
|
|
|
|
def wrapper(func):
|
|
|
|
routes.append([method, path, func])
|
|
|
|
return func
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
@register_route('GET', '/')
|
2022-05-06 07:04:51 +00:00
|
|
|
async def home(request):
|
2022-11-18 21:41:14 +00:00
|
|
|
targets = '<br>'.join(request.database.hostnames)
|
|
|
|
note = request.config.note
|
|
|
|
count = len(request.database.hostnames)
|
|
|
|
host = request.config.host
|
2022-11-07 12:54:32 +00:00
|
|
|
|
|
|
|
text = f"""
|
2022-05-06 07:04:51 +00:00
|
|
|
<html><head>
|
|
|
|
<title>ActivityPub Relay at {host}</title>
|
|
|
|
<style>
|
|
|
|
p {{ color: #FFFFFF; font-family: monospace, arial; font-size: 100%; }}
|
|
|
|
body {{ background-color: #000000; }}
|
|
|
|
a {{ color: #26F; }}
|
|
|
|
a:visited {{ color: #46C; }}
|
|
|
|
a:hover {{ color: #8AF; }}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<p>This is an Activity Relay for fediverse instances.</p>
|
|
|
|
<p>{note}</p>
|
|
|
|
<p>You may subscribe to this relay with the address: <a href="https://{host}/actor">https://{host}/actor</a></p>
|
|
|
|
<p>To host your own relay, you may download the code at this address: <a href="https://git.pleroma.social/pleroma/relay">https://git.pleroma.social/pleroma/relay</a></p>
|
|
|
|
<br><p>List of {count} registered instances:<br>{targets}</p>
|
2022-11-07 12:54:32 +00:00
|
|
|
</body></html>"""
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-09 10:58:35 +00:00
|
|
|
return Response.new(text, ctype='html')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
|
2022-11-07 12:54:32 +00:00
|
|
|
@register_route('GET', '/inbox')
|
|
|
|
@register_route('GET', '/actor')
|
2022-05-06 07:04:51 +00:00
|
|
|
async def actor(request):
|
2022-11-07 10:30:13 +00:00
|
|
|
data = Message.new_actor(
|
2022-11-18 21:41:14 +00:00
|
|
|
host = request.config.host,
|
|
|
|
pubkey = request.database.pubkey
|
2022-11-07 10:30:13 +00:00
|
|
|
)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-09 10:58:35 +00:00
|
|
|
return Response.new(data, ctype='activity')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
|
2022-11-07 12:54:32 +00:00
|
|
|
@register_route('POST', '/inbox')
|
|
|
|
@register_route('POST', '/actor')
|
2022-05-06 07:04:51 +00:00
|
|
|
async def inbox(request):
|
2022-11-18 21:41:14 +00:00
|
|
|
config = request.config
|
|
|
|
database = request.database
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
## reject if missing signature header
|
2022-11-18 21:41:14 +00:00
|
|
|
if not request.signature:
|
2022-05-06 07:04:51 +00:00
|
|
|
logging.verbose('Actor missing signature header')
|
|
|
|
raise HTTPUnauthorized(body='missing signature')
|
|
|
|
|
|
|
|
try:
|
2022-11-18 21:41:14 +00:00
|
|
|
request['message'] = await request.json(loads=Message.new_from_json)
|
|
|
|
|
|
|
|
## reject if there is no message
|
|
|
|
if not request.message:
|
|
|
|
logging.verbose('empty message')
|
|
|
|
return Response.new_error(400, 'missing message', 'json')
|
2022-11-07 10:30:13 +00:00
|
|
|
|
2022-11-16 14:12:23 +00:00
|
|
|
## reject if there is no actor in the message
|
2022-11-18 21:41:14 +00:00
|
|
|
if 'actor' not in request.message:
|
|
|
|
logging.verbose('actor not in message')
|
2022-11-16 14:12:23 +00:00
|
|
|
return Response.new_error(400, 'no actor in message', 'json')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
except:
|
2022-11-18 21:41:14 +00:00
|
|
|
## this code should hopefully never get called
|
2022-05-06 07:04:51 +00:00
|
|
|
traceback.print_exc()
|
|
|
|
logging.verbose('Failed to parse inbox message')
|
2022-11-09 10:58:35 +00:00
|
|
|
return Response.new_error(400, 'failed to parse message', 'json')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-18 21:41:14 +00:00
|
|
|
request['actor'] = await misc.request(request.signature.keyid)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
## reject if actor is empty
|
2022-11-18 21:41:14 +00:00
|
|
|
if not request.actor:
|
2022-11-20 10:12:11 +00:00
|
|
|
## ld signatures aren't handled atm, so just ignore it
|
|
|
|
if data.type == 'Delete':
|
|
|
|
logging.verbose(f'Instance sent a delete which cannot be handled')
|
|
|
|
return Response.new(status=202)
|
|
|
|
|
|
|
|
logging.verbose(f'Failed to fetch actor: {request.signature.keyid}')
|
2022-11-09 10:58:35 +00:00
|
|
|
return Response.new_error(400, 'failed to fetch actor', 'json')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-20 10:22:57 +00:00
|
|
|
request['instance'] = request.database.get_inbox(request['actor'].inbox)
|
|
|
|
|
2022-05-06 07:04:51 +00:00
|
|
|
## reject if the actor isn't whitelisted while the whiltelist is enabled
|
2022-11-18 21:41:14 +00:00
|
|
|
if config.whitelist_enabled and not config.is_whitelisted(request.actor.domain):
|
|
|
|
logging.verbose(f'Rejected actor for not being in the whitelist: {request.actor.id}')
|
2022-11-09 10:58:35 +00:00
|
|
|
return Response.new_error(403, 'access denied', 'json')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
## reject if actor is banned
|
2022-11-18 21:41:14 +00:00
|
|
|
if request.config.is_banned(request.actor.domain):
|
2022-11-17 21:30:56 +00:00
|
|
|
logging.verbose(f'Ignored request from banned actor: {actor.id}')
|
2022-11-09 10:58:35 +00:00
|
|
|
return Response.new_error(403, 'access denied', 'json')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
## reject if the signature is invalid
|
2022-11-18 21:41:14 +00:00
|
|
|
if not (await misc.validate_signature(request.actor, request.signature, request)):
|
2022-11-17 21:30:56 +00:00
|
|
|
logging.verbose(f'signature validation failed for: {actor.id}')
|
2022-11-09 10:58:35 +00:00
|
|
|
return Response.new_error(401, 'signature check failed', 'json')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
## reject if activity type isn't 'Follow' and the actor isn't following
|
2022-11-18 21:41:14 +00:00
|
|
|
if request.message.type != 'Follow' and not database.get_inbox(request.actor.domain):
|
|
|
|
logging.verbose(f'Rejected actor for trying to post while not following: {request.actor.id}')
|
2022-11-09 10:58:35 +00:00
|
|
|
return Response.new_error(401, 'access denied', 'json')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-18 21:41:14 +00:00
|
|
|
logging.debug(f">> payload {request.message.to_json(4)}")
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-20 10:50:14 +00:00
|
|
|
asyncio.ensure_future(run_processor(request))
|
2022-11-09 10:58:35 +00:00
|
|
|
return Response.new(status=202)
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
|
2022-11-07 12:54:32 +00:00
|
|
|
@register_route('GET', '/.well-known/webfinger')
|
2022-05-06 07:04:51 +00:00
|
|
|
async def webfinger(request):
|
2022-11-18 16:50:12 +00:00
|
|
|
try:
|
|
|
|
subject = request.query['resource']
|
|
|
|
|
|
|
|
except KeyError:
|
|
|
|
return Response.new_error(400, 'missing \'resource\' query key', 'json')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
2022-11-18 21:41:14 +00:00
|
|
|
if subject != f'acct:relay@{request.config.host}':
|
2022-11-09 10:58:35 +00:00
|
|
|
return Response.new_error(404, 'user not found', 'json')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
data = {
|
|
|
|
'subject': subject,
|
2022-11-18 21:41:14 +00:00
|
|
|
'aliases': [request.config.actor],
|
2022-05-06 07:04:51 +00:00
|
|
|
'links': [
|
2022-11-18 21:41:14 +00:00
|
|
|
{'href': request.config.actor, 'rel': 'self', 'type': 'application/activity+json'},
|
|
|
|
{'href': request.config.actor, 'rel': 'self', 'type': 'application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"'}
|
2022-05-06 07:04:51 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:58:35 +00:00
|
|
|
return Response.new(data, ctype='json')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
|
2022-11-07 13:18:25 +00:00
|
|
|
@register_route('GET', '/nodeinfo/{version:\d.\d\.json}')
|
2022-05-06 07:04:51 +00:00
|
|
|
async def nodeinfo_2_0(request):
|
2022-11-09 11:11:16 +00:00
|
|
|
niversion = request.match_info['version'][:3]
|
2022-05-06 07:04:51 +00:00
|
|
|
data = {
|
2022-11-18 21:41:14 +00:00
|
|
|
'openRegistrations': not request.config.whitelist_enabled,
|
2022-05-06 07:04:51 +00:00
|
|
|
'protocols': ['activitypub'],
|
|
|
|
'services': {
|
|
|
|
'inbound': [],
|
|
|
|
'outbound': []
|
|
|
|
},
|
|
|
|
'software': {
|
|
|
|
'name': 'activityrelay',
|
2022-06-06 12:37:08 +00:00
|
|
|
'version': version
|
2022-05-06 07:04:51 +00:00
|
|
|
},
|
|
|
|
'usage': {
|
|
|
|
'localPosts': 0,
|
|
|
|
'users': {
|
|
|
|
'total': 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'metadata': {
|
2022-11-18 21:41:14 +00:00
|
|
|
'peers': request.database.hostnames
|
2022-05-06 07:04:51 +00:00
|
|
|
},
|
2022-11-09 11:11:16 +00:00
|
|
|
'version': niversion
|
2022-05-06 07:04:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 13:18:25 +00:00
|
|
|
if version == '2.1':
|
|
|
|
data['software']['repository'] = 'https://git.pleroma.social/pleroma/relay'
|
|
|
|
|
2022-11-09 10:58:35 +00:00
|
|
|
return Response.new(data, ctype='json')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
|
2022-11-07 12:54:32 +00:00
|
|
|
@register_route('GET', '/.well-known/nodeinfo')
|
2022-05-06 07:04:51 +00:00
|
|
|
async def nodeinfo_wellknown(request):
|
2022-11-07 13:18:25 +00:00
|
|
|
data = WKNodeinfo.new(
|
2022-11-18 21:41:14 +00:00
|
|
|
v20 = f'https://{request.config.host}/nodeinfo/2.0.json',
|
|
|
|
v21 = f'https://{request.config.host}/nodeinfo/2.1.json'
|
2022-11-07 13:18:25 +00:00
|
|
|
)
|
|
|
|
|
2022-11-09 10:58:35 +00:00
|
|
|
return Response.new(data, ctype='json')
|
2022-05-06 07:04:51 +00:00
|
|
|
|
|
|
|
|
2022-11-07 12:54:32 +00:00
|
|
|
@register_route('GET', '/stats')
|
2022-05-06 07:04:51 +00:00
|
|
|
async def stats(request):
|
2022-11-09 10:58:35 +00:00
|
|
|
return Response.new(STATS, ctype='json')
|