-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
90 lines (81 loc) · 2.59 KB
/
Copy pathserver.js
File metadata and controls
90 lines (81 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const http = require('http');
const fs = require('fs');
const crypto = require('crypto');
const path = 'messages.txt';
const port = 8888;
const CD = 60e3;
let cooldown = {};
let messages = [];
let lid = Math.random();
getmessages();
const svr = http.createServer(async (req, res) => {
res.setHeader('access-control-allow-origin', '*');
let un = username(req.socket.remoteAddress + req.headers['user-agent']);
let body = [];
if (req.method == 'POST') {
await new Promise((y, n) => {
req.on('data', x => {
body.push(x);
});
req.on('end', () => {
body = Buffer.concat(body);
y();
});
req.on('close', n);
req.on('error', n);
setTimeout(n, 10e3);
})
}
let url = req.url;
if (url == '/send' && req.method == 'POST') {
if (cooldown[un] && Date.now() - cooldown[un] < CD) {
console.log(un, 'cooldown reached');
return res.writeHead(429, '429 Too Many Requests').end('429 Too Many Requests');
}
body = un + ': ' + body.toString().replace('\n', '');
console.log(un, 'sent', body);
messages.push(body);
cooldown[un] = Date.now();
lid = Math.random();
res.writeHead(200).end('true');
} else if (url == '/get' && req.method == 'POST') {
let cd = Date.now() - cooldown[un] < CD;
if (body == lid && !cd) return res.writeHead(200).end();
if (body != lid && !cd) console.log(un, 'got messages');
res.writeHead(200).end((lid + cd ? 1 : 0) + ',' + un + '\n' + messages.slice(-50).join('\n') +
(cd ?
'\nCooldown reached, ' +
Math.floor((CD - Date.now() + cooldown[un]) / 1e3) + ' seconds left' : ''));
} else {
console.log(un, 'tried', req.url);
res.writeHead(404, '404 Not Found').end('404 Not Found');
}
});
svr.listen(port, () => {
console.log('Server listening on port', port);
});
function getmessages() {
try {
fs.accessSync(path);
} catch (e) {
fs.writeFileSync(path, '', 'utf8');
console.error('Created message file');
}
messages = fs.readFileSync(path, 'utf8').split('\n');
console.log('Got messages');
}
function setmessages() {
fs.writeFileSync(path, messages.join('\n'), 'utf8');
console.log('Saved messages');
}
process.on('uncaughtException', e => console.error(e));
process.on('unhandledRejection', e => console.error(e));
process.on('SIGINT', () => { setmessages(); process.exit(0) });
process.on('beforeExit', setmessages);
setInterval(setmessages, 36e5);
function username(ip) {
let h = crypto.createHash('sha256', {});
h.update(ip);
h = h.digest('base64');
return h.slice(0, 4).replaceAll('/', '_').replaceAll('+', '-');
}