Introduce random delays between res writes

This commit is contained in:
2024-07-26 16:08:05 +02:00
parent 90a9b31d07
commit 8b64852e34

View File

@@ -1,38 +1,45 @@
import { createServer } from 'node:http'; import { createServer } from 'node:http';
import { clearInterval, setInterval } from 'node:timers'; import { setTimeout } from 'node:timers';
const msg = ':) you are an idiot hahahahaha :)'; const msg = ':) you are an idiot hahahahaha :)';
const minDelay = 1000;
const maxDelay = 5000;
const delayDiff = maxDelay - minDelay;
const randomDelay = () => Math.floor(Math.random() * delayDiff + minDelay);
const server = createServer((req, res) => { const server = createServer((req, res) => {
const connOpenDate = new Date(); const connOpenDate = new Date();
const dateLogText = connOpenDate.toLocaleString('pl'); const dateText = connOpenDate.toLocaleString('pl');
const attacker = req.headers['x-forwarded-for']; const attacker = req.headers['x-forwarded-for'];
const host = req.headers['x-forwarded-host']; const host = req.headers['x-forwarded-host'];
const endpoint = `${req.method} ${req.url}`;
console.log(
`[${dateLogText}] ${attacker} targeted ${host} on ${req.method} ${req.url}`
);
let charIdx = 0; let charIdx = 0;
const intervalId = setInterval(() => { console.log(`[${dateText}] ${attacker} targeted ${host} on ${endpoint}`);
const hang = () => {
if (res.closed) return;
if (charIdx === msg.length) { if (charIdx === msg.length) {
charIdx = 0; charIdx = 0;
res.write('\n'); res.write('\n');
} else { } else {
res.write(msg[charIdx++]); res.write(msg[charIdx++]);
} }
}, 3000);
setTimeout(hang, randomDelay());
};
hang();
res.once('close', () => { res.once('close', () => {
const connCloseDate = new Date(); const connCloseDate = new Date();
const timeDiff = connCloseDate.getTime() - connOpenDate.getTime(); const timeDiff = connCloseDate.getTime() - connOpenDate.getTime();
const dateLogText = connCloseDate.toLocaleString('pl'); const dateText = connCloseDate.toLocaleString('pl');
const diffText = new Date(timeDiff).toISOString().substring(11, 19); const diffText = new Date(timeDiff).toISOString().substring(11, 19);
clearInterval(intervalId);
console.log( console.log(
`[${dateLogText}] ${attacker} aborted connection after ${diffText}` `[${dateText}] ${attacker} aborted connection after ${diffText}`
); );
}); });
}); });