2019-02-20 22:12:29 +00:00
|
|
|
import { promises as fs } from "fs";
|
|
|
|
import crypto from "crypto";
|
|
|
|
import uuidv4 from "uuid/v4";
|
|
|
|
import request from "request";
|
2019-08-18 22:09:28 +00:00
|
|
|
import {
|
|
|
|
Activity,
|
|
|
|
ArticleObject,
|
|
|
|
ActorObject,
|
|
|
|
CreateActivity,
|
|
|
|
NoteObject
|
|
|
|
} from "./activity";
|
2019-02-20 22:12:29 +00:00
|
|
|
import { URL } from "url";
|
2019-08-17 18:50:18 +00:00
|
|
|
import { getConnection } from "typeorm";
|
|
|
|
import Actor from "../entity/Actor";
|
|
|
|
import Article from "../entity/Article";
|
2019-02-20 22:12:29 +00:00
|
|
|
|
|
|
|
const domain = process.env.DOMAIN;
|
|
|
|
|
2019-08-18 22:09:28 +00:00
|
|
|
export function createActivity(object: ArticleObject | NoteObject): CreateActivity {
|
2019-02-20 22:12:29 +00:00
|
|
|
const uuid = uuidv4();
|
|
|
|
const createObject = {
|
|
|
|
"@context": [
|
|
|
|
"https://www.w3.org/ns/activitystreams"
|
|
|
|
],
|
|
|
|
"type": "Create",
|
|
|
|
"id": `https://${domain}/ap/${uuid}`,
|
|
|
|
"actor": `https://${domain}/ap/actor`,
|
2019-08-18 22:09:28 +00:00
|
|
|
"to": object.to,
|
|
|
|
"cc": object.cc,
|
|
|
|
"object": object
|
2019-02-20 22:12:29 +00:00
|
|
|
};
|
|
|
|
return createObject;
|
|
|
|
}
|
|
|
|
|
2019-08-17 18:50:18 +00:00
|
|
|
export async function getActor(url: string, forceUpdate: boolean = false): Promise<ActorObject | null> {
|
2019-04-05 15:23:24 +00:00
|
|
|
if (!forceUpdate) {
|
2019-02-24 15:21:14 +00:00
|
|
|
try {
|
2019-08-17 18:50:18 +00:00
|
|
|
const cached = await getCachedActor(url);
|
2019-02-24 15:21:14 +00:00
|
|
|
if (cached) return cached;
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`Encountered error getting cached actor ${url}`, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const remote = await fetchActor(url);
|
2019-08-17 18:50:18 +00:00
|
|
|
if (remote) cacheActor(remote);
|
2019-02-24 15:21:14 +00:00
|
|
|
return remote;
|
|
|
|
}
|
|
|
|
|
2019-08-17 18:50:18 +00:00
|
|
|
export async function getCachedActor(url: string): Promise<ActorObject | null> {
|
|
|
|
const result = await getConnection().manager.findByIds(Actor, [url]);
|
|
|
|
if (result.length > 0) {
|
|
|
|
const actor = result[0];
|
2019-08-18 22:09:28 +00:00
|
|
|
return actor.actorObject;
|
2019-08-17 18:50:18 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2019-02-24 15:21:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 18:50:18 +00:00
|
|
|
async function cacheActor(actorObject: ActorObject) {
|
2019-02-24 15:21:14 +00:00
|
|
|
function getIconUrl(icon: string | object): string {
|
|
|
|
return icon instanceof String ? icon : (icon as any).url;
|
|
|
|
}
|
2019-09-17 18:19:02 +00:00
|
|
|
const iconURL = !actorObject.icon ? null : actorObject.icon instanceof Array ? getIconUrl(actorObject.icon[0]) : getIconUrl(actorObject.icon);
|
2019-08-17 18:50:18 +00:00
|
|
|
const actor = new Actor();
|
|
|
|
actor.id = actorObject.id;
|
|
|
|
actor.actorObject = actorObject;
|
|
|
|
actor.displayName = actorObject.name;
|
|
|
|
actor.inbox = actorObject.inbox;
|
|
|
|
actor.iconURL = iconURL;
|
2020-04-15 16:55:16 +00:00
|
|
|
if (actor.publicKey && actor.publicKey.publicKeyPem) {
|
|
|
|
actor.publicKeyPem = actorObject.publicKey.publicKeyPem;
|
|
|
|
}
|
2019-08-17 18:50:18 +00:00
|
|
|
actor.isFollower = false;
|
|
|
|
await getConnection().manager.save(actor);
|
2019-02-24 15:21:14 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 22:59:21 +00:00
|
|
|
async function fetchActor(url?: string): Promise<ActorObject | null> {
|
|
|
|
if (!url) {
|
|
|
|
return Promise.resolve(null);
|
|
|
|
}
|
2019-02-20 22:12:29 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request({
|
|
|
|
url,
|
|
|
|
headers: {
|
|
|
|
"Accept": "application/activity+json"
|
|
|
|
},
|
|
|
|
method: "GET",
|
|
|
|
json: true
|
|
|
|
}, (err, res) => {
|
|
|
|
if (err) reject(err);
|
2019-09-18 12:39:17 +00:00
|
|
|
else resolve(res.body && res.body instanceof Object ? res.body as ActorObject : null);
|
2019-02-20 22:12:29 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function signAndSend(activity: Activity, inbox: string) {
|
|
|
|
const targetDomain = new URL(inbox).hostname;
|
|
|
|
const inboxFragment = inbox.replace("https://" + targetDomain, "");
|
|
|
|
const date = new Date();
|
|
|
|
const privKey = (await fs.readFile(process.env.PRIV_KEY_PEM!)).toString();
|
|
|
|
const signer = crypto.createSign("sha256");
|
|
|
|
const stringToSign = `(request-target): post ${inboxFragment}\nhost: ${targetDomain}\ndate: ${date.toUTCString()}`;
|
|
|
|
signer.update(stringToSign);
|
|
|
|
signer.end();
|
2019-02-20 23:07:29 +00:00
|
|
|
const signature = signer.sign(privKey, "base64");
|
|
|
|
const header = `keyId="https://${domain}/ap/actor#main-key",headers="(request-target) host date",signature="${signature}"`;
|
2019-02-20 22:12:29 +00:00
|
|
|
console.log("Sending:", activity);
|
|
|
|
console.log("stringToSign:", stringToSign);
|
|
|
|
console.log("Signature: " + header);
|
|
|
|
request({
|
|
|
|
url: inbox,
|
|
|
|
headers: {
|
|
|
|
"Host": targetDomain,
|
|
|
|
"Date": date.toUTCString(),
|
|
|
|
"Signature": header,
|
|
|
|
"Accept": "application/activity+json, application/json"
|
|
|
|
},
|
|
|
|
method: "POST",
|
|
|
|
json: true,
|
|
|
|
body: activity
|
|
|
|
}, (err, res) => {
|
|
|
|
console.log("Sent message to inbox at", targetDomain);
|
|
|
|
console.log("Response status code", res.statusCode);
|
|
|
|
console.log(res.body);
|
|
|
|
if (err) console.log("Error:", err, res);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-17 18:50:18 +00:00
|
|
|
async function sendToFollowers(activity: CreateActivity) {
|
2019-09-22 22:21:58 +00:00
|
|
|
const followers = await getConnection().getRepository(Actor).find({where: {isFollower: true}});
|
2019-08-17 18:50:18 +00:00
|
|
|
const inboxes = followers.map(it => "https://" + new URL(it.inbox).host + "/inbox");
|
|
|
|
// convert to a Set to deduplicate inboxes
|
|
|
|
(new Set(inboxes)).forEach(inbox => {
|
|
|
|
console.log(`Federating ${activity.object.id} to ${inbox}`);
|
|
|
|
signAndSend(activity, inbox);
|
2019-02-20 22:12:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-17 18:50:18 +00:00
|
|
|
export default async function federate(toFederate: [string, ArticleObject][]) {
|
2019-02-20 22:12:29 +00:00
|
|
|
for (const [id, article] of toFederate) {
|
|
|
|
|
2019-08-17 18:50:18 +00:00
|
|
|
sendToFollowers(createActivity(article));
|
|
|
|
await getConnection().manager.update(Article, id, { hasFederated: true });
|
2019-02-20 22:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|