aidxnFUN/app.js

73 lines
2.6 KiB
JavaScript
Raw Normal View History

2024-10-12 19:18:53 -04:00
const express = require('express');
2024-11-13 14:54:53 -05:00
const openpgp = require('openpgp');
2024-10-12 19:18:53 -04:00
const path = require('path');
2024-10-15 16:35:07 -04:00
const fs = require('fs');
2024-10-12 19:18:53 -04:00
const app = express();
2024-11-13 14:54:53 -05:00
app.use(express.urlencoded({ extended: true }));
2024-10-12 19:18:53 -04:00
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
2024-10-15 16:35:07 -04:00
app.use(express.json());
2024-10-12 19:18:53 -04:00
2024-11-13 14:54:53 -05:00
const PUBKEY = path.join(__dirname, 'src', 'pgp', 'publickey.asc');
const routes = ['/', '/about', '/contact', '/status', '/design', '/projects', '/cloud'];
2024-10-15 16:35:07 -04:00
routes.forEach(route => {
app.get(route, (req, res) => {
res.render(route === '/' ? 'index' : route.slice(1), { req });
});
2024-10-12 19:18:53 -04:00
});
2024-11-13 14:54:53 -05:00
app.get('/verify', (req, res) => {
res.render('verify', { req, verifyResult: null });
});
2024-12-08 18:48:34 -05:00
app.get('/manifesto', (req, res) => {
res.render('manifesto', { req, verifyResult: null });
});
2024-11-13 14:54:53 -05:00
app.post('/verify', async (req, res) => {
const { pgpMessage } = req.body;
try {
const pubKeyArmored = fs.readFileSync(PUBKEY, 'utf8');
const pubKey = await openpgp.readKey({ armoredKey: pubKeyArmored });
const cleartextMessage = await openpgp.readCleartextMessage({ cleartextMessage: pgpMessage });
const verifyResult = await openpgp.verify({
message: cleartextMessage,
verificationKeys: pubKey,
});
const isValid = await verifyResult.signatures[0].verified;
let resultMessage;
if (isValid) {
resultMessage = '✅ Signature is valid!';
} else {
resultMessage = '❌ Signature is invalid or message has been tampered with.';
}
res.render('verify', { req, verifyResult: resultMessage });
} catch (error) {
if (error.message.includes("Could not find signing key")) {
console.error('Verification failed: Unknown/invalid signing key');
res.render('verify', { req, verifyResult: '❌ Signature is from unknown signer or invalid.' });
} else {
console.error('Verification failed:', error);
res.render('verify', { req, verifyResult: '❌ An error occurred during verification.' });
}
}
});
2024-10-21 20:25:23 -04:00
const PORT = process.env.PORT || 5566;
2024-10-12 19:18:53 -04:00
app.listen(PORT, () => {
2024-10-15 16:35:07 -04:00
const now = new Date();
const fT = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`;
console.log(`Running at: ${fT}`);
2024-10-12 19:18:53 -04:00
console.log(`Server running on http://localhost:${PORT}`);
2024-10-15 16:35:07 -04:00
});