2024-10-02 16:53:07 +03:00
|
|
|
import express from 'express';
|
|
|
|
import crypto from 'crypto';
|
|
|
|
import fs from 'fs';
|
|
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
export default router;
|
2018-10-15 07:18:10 +03:00
|
|
|
|
|
|
|
function signAndSend(message, name, domain, req, res, targetDomain) {
|
2019-03-11 05:32:43 +02:00
|
|
|
// get the URI of the actor object and append 'inbox' to it
|
|
|
|
let inbox = message.object.actor+'/inbox';
|
|
|
|
let inboxFragment = inbox.replace('https://'+targetDomain,'');
|
2018-10-15 07:18:10 +03:00
|
|
|
// get the private key
|
|
|
|
let db = req.app.get('db');
|
|
|
|
let result = db.prepare('select privkey from accounts where name = ?').get(`${name}@${domain}`);
|
|
|
|
if (result === undefined) {
|
|
|
|
return res.status(404).send(`No record found for ${name}.`);
|
|
|
|
}
|
|
|
|
else {
|
2020-10-26 06:26:30 +02:00
|
|
|
// digest
|
|
|
|
const digest = crypto.createHash('sha256').update(JSON.stringify(message)).digest('base64');
|
|
|
|
|
2018-10-15 07:18:10 +03:00
|
|
|
let privkey = result.privkey;
|
|
|
|
const signer = crypto.createSign('sha256');
|
|
|
|
let d = new Date();
|
2020-10-26 06:26:30 +02:00
|
|
|
let stringToSign = `(request-target): post ${inboxFragment}\nhost: ${targetDomain}\ndate: ${d.toUTCString()}\ndigest: SHA-256=${digest}`;
|
2018-10-15 07:18:10 +03:00
|
|
|
signer.update(stringToSign);
|
|
|
|
signer.end();
|
|
|
|
const signature = signer.sign(privkey);
|
|
|
|
const signature_b64 = signature.toString('base64');
|
2020-10-26 07:37:34 +02:00
|
|
|
const algorithm = 'rsa-sha256';
|
|
|
|
let header = `keyId="https://${domain}/u/${name}",algorithm="${algorithm}",headers="(request-target) host date digest",signature="${signature_b64}"`;
|
2024-10-02 16:53:07 +03:00
|
|
|
fetch(inbox, {
|
2018-10-15 07:18:10 +03:00
|
|
|
headers: {
|
|
|
|
'Host': targetDomain,
|
|
|
|
'Date': d.toUTCString(),
|
2020-10-26 06:26:30 +02:00
|
|
|
'Signature': header,
|
2020-10-26 12:40:06 +02:00
|
|
|
'Digest': `SHA-256=${digest}`,
|
|
|
|
'Content-Type': 'application/activity+json',
|
|
|
|
'Accept': 'application/activity+json'
|
2018-10-15 07:18:10 +03:00
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
body: message
|
|
|
|
});
|
|
|
|
res.json('done');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendAcceptMessage(thebody, name, domain, req, res, targetDomain) {
|
|
|
|
const guid = crypto.randomBytes(16).toString('hex');
|
|
|
|
let message = {
|
2020-10-26 06:26:30 +02:00
|
|
|
'@context': ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'],
|
2018-10-15 07:18:10 +03:00
|
|
|
'id': `https://${domain}/${guid}`,
|
|
|
|
'type': 'Accept',
|
|
|
|
'actor': `https://${domain}/u/${name}`,
|
|
|
|
'object': thebody,
|
|
|
|
};
|
|
|
|
signAndSend(message, name, domain, req, res, targetDomain);
|
|
|
|
}
|
|
|
|
|
|
|
|
router.post('/', function (req, res) {
|
|
|
|
// pass in a name for an account, if the account doesn't exist, create it!
|
|
|
|
let domain = req.app.get('domain');
|
2018-12-17 23:48:14 +02:00
|
|
|
if (req.body.actor === undefined) {
|
|
|
|
return res.status(400).send(`No actor specified.`);
|
|
|
|
}
|
2018-10-15 07:18:10 +03:00
|
|
|
const myURL = new URL(req.body.actor);
|
|
|
|
let targetDomain = myURL.hostname;
|
2018-11-06 19:45:33 +02:00
|
|
|
fs.appendFile('./inbox.log', JSON.stringify(req.body)+'\r\n', function (err) {
|
2018-10-15 07:18:10 +03:00
|
|
|
if (err) {
|
|
|
|
return console.log(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// TODO: add "Undo" follow event
|
|
|
|
if (typeof req.body.object === 'string' && req.body.type === 'Follow') {
|
|
|
|
let name = req.body.object.replace(`https://${domain}/u/`,'');
|
|
|
|
sendAcceptMessage(req.body, name, domain, req, res, targetDomain);
|
|
|
|
// Add the user to the DB of accounts that follow the account
|
|
|
|
let db = req.app.get('db');
|
|
|
|
// get the followers JSON for the user
|
|
|
|
let result = db.prepare('select followers from accounts where name = ?').get(`${name}@${domain}`);
|
|
|
|
if (result === undefined) {
|
|
|
|
console.log(`No record found for ${name}.`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// update followers
|
|
|
|
let followers = result.followers;
|
|
|
|
if (followers) {
|
2018-12-17 23:48:14 +02:00
|
|
|
followers = JSON.parse(followers);
|
2018-10-15 07:18:10 +03:00
|
|
|
followers.push(req.body.actor);
|
|
|
|
// unique items
|
|
|
|
followers = [...new Set(followers)];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
followers = [req.body.actor];
|
|
|
|
}
|
|
|
|
let followersText = JSON.stringify(followers);
|
|
|
|
// update into DB
|
2018-10-15 22:31:18 +03:00
|
|
|
db.prepare('update accounts set followers = ? where name = ?').run(followersText, `${name}@${domain}`);
|
2018-10-15 07:18:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|