matrix-bot/auth-fetch.js
Ashley Graves c94fbfec4e fix
2024-10-15 05:53:07 +00:00

95 lines
2 KiB
JavaScript

import { exec as ossl_exec } from "openssl-wrapper";
import express from 'express';
import fs from 'fs';
import env from 'dotenv';
env.config();
const password = "penis";
function openssl(func, args) {
return new Promise((resolve, reject) => {
ossl_exec(func, args, function (err, buffer) {
if (err)
reject(err);
resolve(buffer);
});
})
}
const domain = process.env.AP_FETCH_DOMAIN;
if (!fs.existsSync("data/server-crt.pem")) {
console.log("Generating certificate...");
await openssl('req', {
new: true,
newkey: 'rsa:4096',
days: 365,
nodes: true,
x509: true,
subj: '/C=',
keyout: "data/server-key.pem",
out: "data/server-crt.pem"
});
console.log("Certificate generated!");
}
const app = express();
const pubkey = fs.readFileSync('data/server-crt.pem', 'utf8');
const notice = fs.readFileSync('auth-fetch-notice.txt', 'utf8');
const userId = process.env.USER_ID;
const username = userId.slice(1, userId.indexOf(":"));
const actor = {
'@context': [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1'
],
'id': 'https://' + domain + '/actor',
'type': 'Person',
'preferredUsername': username,
'inbox': 'https://' + domain + '/inbox',
'publicKey': {
'id': 'https://' + domain + '/actor#main-key',
'owner': 'https://' + domain + '/actor',
'publicKeyPem': pubkey
}
}
const webfinger = {
'subject': 'acct:' + userId.replace(":", "@"),
'links': [
{
'rel': 'self',
'type': 'application/activity+json',
'href': 'https://' + domain + '/actor'
}
]
}
app.get('/', (_, res) => {
res.setHeader('content-type', 'text/plain');
res.write(notice);
res.end();
});
app.get('/actor', (_, res) => {
res.writeHead(200, { 'Content-Type': 'application/activity+json' });
res.write(JSON.stringify(actor));
res.end();
});
app.get('/.well-known/webfinger', (_, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(webfinger));
res.end();
});
export default function (port) {
app.listen(port);
};