apblog/index.js

89 lines
4.3 KiB
JavaScript

const fs = require("fs");
const uuidv4 = require("uuid/v4");
const crypto = require("crypto");
const request = require("request");
const express = require("express");
const app = express();
const baseURL = process.env.BASE_URL || "new.shadowfacts.net";
// const alreadyPosted = loadAlreadyPosted();
// function loadAlreadyPosted() {
// const buffer = fs.readFileSync("data/already-posted.json");
// const data = buffer.toString();
// return JSON.parse(data);
// }
// function exitHandler() {
// const data = JSON.stringify(alreadyPosted);
// fs.writeFileSync("data/already-posted.json", data);
// }
// process.on("exit", exitHandler);
// process.on("SIGINT", exitHandler);
app.use(require("morgan")("dev"));
app.get("/ap", (req, res) => {
res.end("Hello, world!");
});
app.get("/ap/actor", (req, res) => {
if (req.accepts(["application/activity+json", "application/ld+json"])) {
res.type("application/activity+json");
res.end(`{"@context":["https://www.w3.org/ns/activitystreams","https://w3id.org/security/v1"],"type":"Person","id":"https://new.shadowfacts.net/ap/actor","preferredUsername":"shadowfacts","name":"shadowfacts' blog","icon":{"type":"Image","mediaType":"image/png","url":"https://new.shadowfacts.net/shadowfacts.png"},"inbox":"https://new.shadowfacts.net/ap/inbox","publicKey":{"id":"https://new.shadowfacts.net/ap/actor#main-key","owner":"https://new.shadowfacts.net/ap/actor","publicKeyPem":"-----BEGIN PUBLIC KEY-----\\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxsePhOEq6EqqgGe+5vKa\\nti8cnvLoP3eJDTHENxX4M9epMIeo48cI214wkAERoNufZLtYeGOHO6YJcVydVSYN\\nUd4lQxgaPwAlh0DB0BUrXR1yfcSAhHS86XBSUq86O3fY5V+vEdZiN5PfUCG5rg5t\\n5V/TrcbVQngXqDmwVZ/OcJ+5vUo+B62Mul7EmiNkZHKzp1VZGVtZ6nZ/4TxsdeSN\\nXT8Vb0f/GJQOQLzW6dV0CBupts6x7o+9oRiYiAH+cXLFyQdTQ+1TrqMSMscdfX9I\\nKos5Ih7oKdj6wQt0pDEOGx090c9JzQC25RzxRk7jc1Jt1UzDi/a1U/BbucxIvPr3\\nPQIDAQAB\\n-----END PUBLIC KEY-----"}}`);
} else {
res.redirect("/");
}
});
const port = process.env.PORT || 8083;
app.listen(port, () => {
console.log(`Listening on ${port}`);
});
function createMessage(object) {
const uuid = uuidv4();
return {
"@context": "https://www.w3.org/ns/activitystreams",
"id": `https://${baseURL}/ap/${uuid}`,
"type": "Create",
"actor": `https://${baseURL}/ap/actor`,
"object": object
};
}
function signAndSendMessage(message, targetDomain, inbox) {
const inboxFragment = inbox.replace("https://" + targetDomain, "");
const privKey = fs.readFileSync("/Users/shadowfacts/Desktop/private.pem").toString();
const signer = crypto.createSign("sha256");
const date = new Date();
let stringToSign = `(request-target): post ${inboxFragment}\nhost: ${targetDomain}\ndate: ${date.toUTCString()}`;
signer.update(stringToSign);
signer.end();
const signature = signer.sign(privKey);
const base64Signature = signature.toString("base64");
const header = `keyId="https://${baseURL}/ap/actor#main-key",headers="(request-target) host date",signature="${base64Signature}"`;
request({
url: inbox,
headers: {
"Host": targetDomain,
"Date": date.toUTCString(),
"Signature": header,
"Accept": "application/activity+json"
},
method: "POST",
json: true,
body: message
}, (err, res) => {
console.log("Sent message to an inbox at", targetDomain);
if (err) console.log("Error:", error, res);
console.log("Response status code: ", res.statusCode)
});
}
// const message = createMessage({"@context":["https://www.w3.org/ns/activitystreams"],"type":"Article","id":"https://new.shadowfacts.net/meta/2019/reincarnation.json","published":"2019-01-02T23:02:42.000Z","inReplyTo":null,"url":"https://new.shadowfacts.net/meta/2019/reincarnation/","attributedTo":"https://new.shadowfacts.net/ap/actor","to":["https://www.w3.org/ns/activitystreams#Public"],"cc":[],"name":"Reincarnation","content":"<figure>\n <img src=\"https://theinfosphere.org/images/9/93/Reincarnation_infobox.png\" alt=\"Futurama Galactic Entity image\" />\n <figcaption>A wise man once said that nothing really dies, it just comes back in a new form. Then he died.</figcaption>\n</figure>\n<p>Stand by for <em><strong>reincarnation</strong></em>.</p>\n<!-- excerpt-end -->\n<p><code>// TODO: write the actual blog post</code></p>\n"});
// console.log(message);
// signAndSendMessage(message, "social.shadowfacts.net", "https://social.shadowfacts.net/users/shadowfacts/inbox");