99 lines
2.8 KiB
TypeScript
99 lines
2.8 KiB
TypeScript
import { promises as fs } from "fs";
|
|
import crypto from "crypto";
|
|
import uuidv4 from "uuid/v4";
|
|
import request from "request";
|
|
import { Database } from "sqlite3";
|
|
import { Activity, Article, Create, Actor, Follow, Accept } from "./activity";
|
|
import { URL } from "url";
|
|
|
|
const domain = process.env.DOMAIN;
|
|
|
|
function createActivity(article: Article): Create {
|
|
const uuid = uuidv4();
|
|
const createObject = {
|
|
"@context": [
|
|
"https://www.w3.org/ns/activitystreams"
|
|
],
|
|
"type": "Create",
|
|
"id": `https://${domain}/ap/${uuid}`,
|
|
"actor": `https://${domain}/ap/actor`,
|
|
"to": article.to,
|
|
"cc": article.cc,
|
|
"object": article
|
|
};
|
|
return createObject;
|
|
}
|
|
|
|
export async function fetchActor(url: string): Promise<Actor> {
|
|
return new Promise((resolve, reject) => {
|
|
request({
|
|
url,
|
|
headers: {
|
|
"Accept": "application/activity+json"
|
|
},
|
|
method: "GET",
|
|
json: true
|
|
}, (err, res) => {
|
|
if (err) reject(err);
|
|
else resolve(<Actor>res.body);
|
|
});
|
|
});
|
|
}
|
|
|
|
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();
|
|
const signature = signer.sign(privKey, "base64");
|
|
const header = `keyId="https://${domain}/ap/actor#main-key",headers="(request-target) host date",signature="${signature}"`;
|
|
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);
|
|
});
|
|
}
|
|
|
|
async function sendToFollowers(activity: Create, db: Database) {
|
|
db.each("SELECT inbox FROM followers", (err, result) => {
|
|
if (err) {
|
|
console.log("Error getting followers: ", err);
|
|
return;
|
|
}
|
|
const inbox = "https://" + new URL(result.inbox).host + "/inbox";
|
|
console.log(`Federating ${activity.object.id} to ${inbox}`);
|
|
signAndSend(activity, inbox);
|
|
});
|
|
}
|
|
|
|
export default function federate(toFederate: [string, Article][], db: Database) {
|
|
for (const [id, article] of toFederate) {
|
|
|
|
sendToFollowers(createActivity(article), db);
|
|
db.run("UPDATE articles SET has_federated = 1 WHERE id = $id", {
|
|
$id: id
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
|