2019-09-16 13:19:52 +00:00
|
|
|
import base64, binascii, json, os, queue, threading, urllib.parse, uuid
|
2019-09-10 16:32:41 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
2019-09-15 10:19:50 +00:00
|
|
|
from . import ap_activities, ap_actor, ap_security, ap_utils
|
2019-09-10 16:32:41 +00:00
|
|
|
|
2019-09-10 17:08:58 +00:00
|
|
|
ACTIVITY_SETTING_PREFIX = "ap-activity-"
|
2019-09-10 16:32:41 +00:00
|
|
|
|
2019-09-15 10:06:00 +00:00
|
|
|
class Server(object):
|
2019-09-16 13:23:06 +00:00
|
|
|
def __init__(self, bot, exports, username, instance):
|
2019-09-15 10:19:50 +00:00
|
|
|
self.bot = bot
|
2019-09-16 13:23:06 +00:00
|
|
|
self.exports = exports
|
2019-09-15 10:06:00 +00:00
|
|
|
self.username = username
|
|
|
|
self.instance = instance
|
2019-09-10 16:32:41 +00:00
|
|
|
|
2019-09-16 13:18:58 +00:00
|
|
|
url_for = self.exports.get_one("url-for")
|
|
|
|
|
|
|
|
key_id = self._ap_keyid_url(url_for)
|
|
|
|
private_key = ap_security.PrivateKey(self.bot.config["tls-key"], key_id)
|
|
|
|
|
|
|
|
self_id = self._ap_self_url(url_for)
|
|
|
|
our_actor = ap_actor.Actor(self_id)
|
|
|
|
|
|
|
|
del url_for
|
|
|
|
|
2019-09-16 14:49:03 +00:00
|
|
|
self._request_queue = queue.Queue()
|
|
|
|
self._request_thread = threading.Thread(target=self._request_loop,
|
|
|
|
args=(private_key, our_actor))
|
|
|
|
self._request_thread.daemon = True
|
|
|
|
self._request_thread.start()
|
|
|
|
|
|
|
|
def _request_loop(self, private_key, our_actor):
|
|
|
|
|
2019-09-16 13:18:58 +00:00
|
|
|
while True:
|
|
|
|
obj = self._request_queue.get()
|
|
|
|
if obj == "kill":
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
actor, activity = obj
|
|
|
|
actor.inbox.send(our_actor, activity, private_key)
|
|
|
|
|
|
|
|
def unload(self):
|
|
|
|
self._request_queue.put("kill")
|
|
|
|
|
2019-09-10 17:08:58 +00:00
|
|
|
def _random_id(self):
|
|
|
|
return binascii.hexlify(os.urandom(3)).decode("ascii")
|
|
|
|
|
|
|
|
def _get_activities(self):
|
|
|
|
activities = []
|
|
|
|
for setting, (content, timestamp) in self.bot.find_settings_prefix(
|
|
|
|
ACTIVITY_SETTING_PREFIX):
|
2019-09-10 17:20:22 +00:00
|
|
|
activity_id = setting.replace(ACTIVITY_SETTING_PREFIX, "", 1)
|
|
|
|
activities.append([activity_id, content, timestamp])
|
2019-09-10 17:08:58 +00:00
|
|
|
return activities
|
|
|
|
def _make_activity(self, content):
|
|
|
|
timestamp = utils.iso8601_format_now()
|
|
|
|
activity_id = self._random_id()
|
|
|
|
self.bot.set_setting("ap-activity-%s" % activity_id,
|
|
|
|
[content, timestamp])
|
|
|
|
return activity_id
|
|
|
|
|
2019-09-15 09:43:46 +00:00
|
|
|
def _toot(self, activity_id):
|
|
|
|
content, timestamp = self.bot.get_setting(
|
|
|
|
"ap-activity-%s" % activity_id)
|
2019-09-13 10:51:39 +00:00
|
|
|
url_for = self.exports.get_one("url-for")
|
2019-09-15 10:06:00 +00:00
|
|
|
self_id = self._ap_self_url(url_for)
|
2019-09-15 09:43:46 +00:00
|
|
|
activity_url = self._ap_activity_url(url_for, activity_id)
|
2019-09-13 10:51:39 +00:00
|
|
|
|
2019-09-15 09:43:46 +00:00
|
|
|
object = {
|
|
|
|
"id": activity_url,
|
|
|
|
"type": "Note",
|
|
|
|
"published": timestamp,
|
|
|
|
"attributedTo": self_id,
|
|
|
|
"content": content,
|
|
|
|
"to": "https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
}
|
|
|
|
activity = ap_activities.Create(activity_url, object)
|
2019-09-13 10:51:39 +00:00
|
|
|
|
2019-09-15 09:43:46 +00:00
|
|
|
private_key = self._private_key()
|
2019-09-13 10:51:39 +00:00
|
|
|
|
2019-09-15 09:43:46 +00:00
|
|
|
for actor_url in self._get_actors():
|
|
|
|
actor = ap_actor.Actor(actor_url)
|
|
|
|
actor.load()
|
|
|
|
actor.inbox.send(activity, private_key)
|
2019-09-13 10:51:39 +00:00
|
|
|
|
2019-09-20 09:46:49 +00:00
|
|
|
def _ap_url(self, url_for, fragment, arg):
|
|
|
|
return "https://%s" % url_for("api", fragment, args=[arg])
|
2019-09-15 10:06:00 +00:00
|
|
|
def _ap_self_url(self, url_for):
|
2019-09-20 09:46:49 +00:00
|
|
|
return self._ap_url(url_for, "ap-user", self.username)
|
2019-09-15 10:06:00 +00:00
|
|
|
def _ap_inbox_url(self, url_for):
|
2019-09-20 09:46:49 +00:00
|
|
|
return self._ap_url(url_for, "ap-inbox", self.username)
|
2019-09-15 10:06:00 +00:00
|
|
|
def _ap_outbox_url(self, url_for):
|
2019-09-20 09:46:49 +00:00
|
|
|
return self._ap_url(url_for, "ap-outbox", self.username)
|
2019-09-10 17:08:58 +00:00
|
|
|
def _ap_activity_url(self, url_for, activity_id):
|
2019-09-20 09:46:49 +00:00
|
|
|
return self._ap_url(url_for, "ap-activity", activity_id)
|
2019-09-15 10:06:00 +00:00
|
|
|
def _ap_keyid_url(self, url_for):
|
|
|
|
return "%s#key" % self._ap_self_url(url_for)
|
2019-09-20 10:39:46 +00:00
|
|
|
def _ap_uuid_url(self, url_for):
|
|
|
|
return self._ap_url(url_for, "ap-id", [str(uuid.uuid4())])
|
2019-09-10 16:32:41 +00:00
|
|
|
|
|
|
|
def ap_webfinger(self, event):
|
|
|
|
resource = event["params"].get("resource", None)
|
|
|
|
if resource.startswith("acct:"):
|
|
|
|
resource = resource.split(":", 1)[1]
|
|
|
|
|
|
|
|
if resource:
|
2019-09-15 10:06:00 +00:00
|
|
|
requested_username, requested_instance = ap_utils.split_username(
|
|
|
|
resource)
|
2019-09-10 16:32:41 +00:00
|
|
|
|
2019-09-15 10:06:00 +00:00
|
|
|
if (requested_username == self.username and
|
|
|
|
requested_instance == self.instance):
|
2019-09-10 16:32:41 +00:00
|
|
|
|
2019-09-15 10:24:59 +00:00
|
|
|
self_id = self._ap_self_url(event["url_for"])
|
2019-09-10 16:32:41 +00:00
|
|
|
|
2019-09-15 10:24:00 +00:00
|
|
|
event["response"].content_type = ap_utils.JRD_TYPE
|
2019-09-10 16:32:41 +00:00
|
|
|
event["response"].write_json({
|
|
|
|
"aliases": [self_id],
|
|
|
|
"links": [{
|
|
|
|
"href": self_id,
|
|
|
|
"rel": "self",
|
2019-09-15 10:24:00 +00:00
|
|
|
"type": ap_utils.ACTIVITY_TYPE
|
2019-09-10 16:32:41 +00:00
|
|
|
}],
|
2019-09-11 09:53:57 +00:00
|
|
|
"subject": "acct:%s" % resource
|
2019-09-10 16:32:41 +00:00
|
|
|
})
|
2019-09-10 16:34:01 +00:00
|
|
|
else:
|
|
|
|
event["response"].code = 404
|
|
|
|
else:
|
|
|
|
event["response"].code = 400
|
2019-09-10 16:32:41 +00:00
|
|
|
|
2019-09-20 09:42:08 +00:00
|
|
|
def _get_arg(self, args):
|
|
|
|
return (args or [None])[0]
|
|
|
|
|
2019-09-10 16:32:41 +00:00
|
|
|
def ap_user(self, event):
|
2019-09-20 09:42:08 +00:00
|
|
|
username = self._get_arg(event["args"])
|
2019-09-13 10:51:39 +00:00
|
|
|
|
2019-09-15 10:06:00 +00:00
|
|
|
if username and username == self.username:
|
|
|
|
self_id = self._ap_self_url(event["url_for"])
|
|
|
|
inbox = self._ap_inbox_url(event["url_for"])
|
|
|
|
outbox = self._ap_outbox_url(event["url_for"])
|
2019-09-10 16:32:41 +00:00
|
|
|
|
|
|
|
cert_filename = self.bot.config["tls-certificate"]
|
|
|
|
with open(cert_filename) as cert_file:
|
|
|
|
cert = cert_file.read().strip()
|
|
|
|
|
2019-09-15 10:24:00 +00:00
|
|
|
event["response"].content_type = ap_utils.LD_TYPE
|
2019-09-10 16:32:41 +00:00
|
|
|
event["response"].write_json({
|
2019-09-11 09:42:59 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
2019-09-11 09:54:36 +00:00
|
|
|
"id": self_id, "url": self_id,
|
2019-09-20 10:44:23 +00:00
|
|
|
"type": "Service",
|
2019-09-11 09:42:59 +00:00
|
|
|
"summary": "beep boop",
|
2019-09-15 10:06:00 +00:00
|
|
|
"preferredUsername": self.username, "name": self.username,
|
2019-09-10 16:32:41 +00:00
|
|
|
"inbox": inbox,
|
2019-09-10 17:08:58 +00:00
|
|
|
"outbox": outbox,
|
2019-09-10 16:32:41 +00:00
|
|
|
"publicKey": {
|
|
|
|
"id": "%s#key" % self_id,
|
|
|
|
"owner": self_id,
|
|
|
|
"publicKeyPem": cert
|
|
|
|
}
|
|
|
|
})
|
2019-09-10 17:08:58 +00:00
|
|
|
else:
|
|
|
|
event["response"].code = 404
|
|
|
|
|
|
|
|
def _prepare_activity(self, url_for, self_id, activity_id, content,
|
|
|
|
timestamp):
|
|
|
|
activity_url = self._ap_activity_url(url_for, activity_id)
|
2019-09-11 12:46:21 +00:00
|
|
|
context = "data:%s" % activity_id
|
2019-09-10 17:08:58 +00:00
|
|
|
return activity_url, {
|
|
|
|
"attributedTo": self_id,
|
|
|
|
"content": content,
|
2019-09-11 12:46:21 +00:00
|
|
|
"conversation": context, "context": context,
|
2019-09-10 17:08:58 +00:00
|
|
|
"id": activity_url, "url": activity_url,
|
|
|
|
"published": timestamp,
|
|
|
|
"summary": "", # content warning here
|
|
|
|
"to": "https://www.w3.org/ns/activitystreams#Public",
|
|
|
|
"type": "Note",
|
|
|
|
}
|
|
|
|
|
|
|
|
def ap_outbox(self, event):
|
2019-09-20 09:42:08 +00:00
|
|
|
username = self._get_arg(event["args"])
|
|
|
|
|
2019-09-15 10:06:00 +00:00
|
|
|
if username and username == self.username:
|
|
|
|
self_id = self._ap_self_url(event["url_for"])
|
|
|
|
outbox = self._ap_outbox_url(event["url_for"])
|
2019-09-10 17:08:58 +00:00
|
|
|
|
|
|
|
activities = []
|
2019-09-10 17:20:22 +00:00
|
|
|
for activity_id, content, timestamp in self._get_activities():
|
2019-09-10 17:21:20 +00:00
|
|
|
activity_url, activity_object = self._prepare_activity(
|
2019-09-10 17:08:58 +00:00
|
|
|
event["url_for"], self_id, activity_id, content, timestamp)
|
|
|
|
activities.append({
|
|
|
|
"actor": self_id,
|
|
|
|
"id": activity_url,
|
|
|
|
"object": activity_object,
|
|
|
|
"published": timestamp,
|
2019-09-13 10:51:39 +00:00
|
|
|
"to": "https://www.w3.org/ns/activitystreams#Public",
|
2019-09-10 17:08:58 +00:00
|
|
|
"type": "Create"
|
|
|
|
})
|
|
|
|
|
2019-09-15 10:24:00 +00:00
|
|
|
event["response"].content_type = ap_utils.LD_TYPE
|
2019-09-10 17:08:58 +00:00
|
|
|
event["response"].write_json({
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"id": outbox,
|
|
|
|
"orderedItems": activities,
|
|
|
|
"totalItems": len(activities),
|
|
|
|
"type": "OrderedCollection"
|
|
|
|
})
|
|
|
|
|
|
|
|
else:
|
|
|
|
event["response"].code = 404
|
|
|
|
|
2019-09-15 10:43:04 +00:00
|
|
|
def _private_key(self, id):
|
2019-09-15 10:47:22 +00:00
|
|
|
filename = self.bot.config["tls-key"]
|
2019-09-15 09:43:46 +00:00
|
|
|
return ap_security.PrivateKey(filename, id)
|
|
|
|
|
|
|
|
def ap_inbox(self, event):
|
|
|
|
data = json.loads(event["data"])
|
2019-09-15 10:06:00 +00:00
|
|
|
self_id = self._ap_self_url(event["url_for"])
|
2019-09-15 09:43:46 +00:00
|
|
|
|
|
|
|
if data["type"] == "Follow":
|
|
|
|
if data["object"] == self_id:
|
|
|
|
new_follower = data["actor"]
|
|
|
|
followers = set(self.bot.get_setting("fediverse-followers", []))
|
|
|
|
if not new_follower in followers:
|
|
|
|
followers.add(new_follower)
|
|
|
|
|
|
|
|
actor = ap_actor.Actor(new_follower)
|
2019-09-15 10:48:58 +00:00
|
|
|
actor.load()
|
2019-09-20 09:53:58 +00:00
|
|
|
accept = ap_activities.Accept(
|
2019-09-20 10:39:46 +00:00
|
|
|
self._ap_uuid_url(event["url_for"]), data)
|
2019-09-16 13:18:58 +00:00
|
|
|
self._request_queue.put([actor, accept])
|
2019-09-15 09:43:46 +00:00
|
|
|
|
2019-09-20 09:53:58 +00:00
|
|
|
follow = ap_activities.Follow(
|
2019-09-20 10:39:46 +00:00
|
|
|
self._ap_uuid_url(event["url_for"]), actor.url)
|
2019-09-16 13:18:58 +00:00
|
|
|
self._request_queue.put([actor, follow])
|
2019-09-15 09:43:46 +00:00
|
|
|
else:
|
|
|
|
event["response"].code = 404
|