Files
hanger/index.mjs

52 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-07-24 16:40:40 +02:00
import { createServer } from 'node:http';
import { setTimeout } from 'node:timers';
2024-07-24 16:40:40 +02:00
2025-09-27 10:38:03 +02:00
const msg = 'Relax, take it easy! For there is nothing that we can do.';
const minDelay = 3000;
2025-09-27 10:38:03 +02:00
const maxDelay = 5000;
const delayDiff = maxDelay - minDelay;
const randomDelay = () => Math.floor(Math.random() * delayDiff + minDelay);
2024-07-25 23:04:36 +02:00
2024-07-24 16:40:40 +02:00
const server = createServer((req, res) => {
const connOpenDate = new Date();
const dateText = connOpenDate.toLocaleString('pl');
const scannerIP = req.headers['x-forwarded-for'];
const userAgent = req.headers['user-agent'];
const host = req.headers['x-forwarded-host'];
const endpoint = `${req.method} ${req.url}`;
2024-07-24 19:19:39 +02:00
console.log(
`[${dateText}] ${scannerIP} (${userAgent}) targeted ${host} on ${endpoint}`
);
2024-07-24 16:40:40 +02:00
2025-11-28 13:22:12 +01:00
let charIdx = 0;
const hang = () => {
if (res.closed) return;
else if (charIdx === msg.length) res.end('\n');
else res.write(msg[charIdx++]);
setTimeout(hang, randomDelay());
};
hang();
2024-07-24 16:40:40 +02:00
res.once('close', () => {
const connCloseDate = new Date();
const timeDiff = connCloseDate.getTime() - connOpenDate.getTime();
const dateText = connCloseDate.toLocaleString('pl');
const diffText = new Date(timeDiff).toISOString().substring(14, 19);
const hangResult =
charIdx === msg.length ? 'received the message' : 'aborted connection';
2024-07-24 19:15:07 +02:00
console.log(`[${dateText}] ${scannerIP} ${hangResult} after ${diffText}`);
});
2024-07-24 16:40:40 +02:00
});
server.listen(3000);
process.on('SIGTERM', () => {
server.close();
});