-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_secure.js
More file actions
169 lines (160 loc) · 5.06 KB
/
Copy pathserver_secure.js
File metadata and controls
169 lines (160 loc) · 5.06 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const https = require('https');
const fs = require('fs');
const crypto = require('crypto');
const { exec } = require('child_process');
const path = 'messages.txt';
const port = 8888;
const ssl = {
key: fs.readFileSync(
'/etc/letsencrypt/live/c.meowguardon.top/privkey.pem',),
cert: fs.readFileSync(
'/etc/letsencrypt/live/c.meowguardon.top/fullchain.pem',),
};
const SECRET = fs.readFileSync('./.secret').toString().trim();
const badwords = fs.readFileSync('./badwords.txt').toString().split('\n')
.sort((a, b) => b.length - a.length);
const CD = 20e3;
let cooldown = {};
let messages = [];
let lid = Math.random();
getmessages();
const svr = https.createServer(ssl, 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');
}
cooldown[un] = Date.now();
body = body.toString().replace('\n', '');
body = body.replace(/[^A-Za-z0-9:, ]/g, '').trim() + ' ';
let bwbody = body.replace(/[^A-Za-z0-9]/g, '')
let bw = badwords.map(x => [x, bwbody.includes(x)]).filter(x => x[1]).map(x => x[0]);
if (bw.length > 0) {
console.log(un, 'tried to say banned words: ', bw.join(', '));
bw.forEach(x => {
let s = 0;
let j = 0;
for (let i = 0; i < body.length; i++) {
let l = /[A-Za-z0-9]/.test(body[i]);
if (l && body[i].toLowerCase() == x[j]) {
if (!s) s = i;
j++;
if (j == x.length) {
body = body.slice(0, s) + '*'.repeat(j) + body.slice(j + s);
j = s = 0;
}
} else if (l) j = s = 0;
}
});
}
body = body.trim();
if (body.length < 3 || body.length > 100) {
console.log(un, 'length too long / small');
return res
.writeHead(429, '429 Too Many Requests')
.end('429 Too Many Requests');
}
body = un + ': ' + body;
console.log(un, 'sent', body);
messages.push(body);
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 if (url == '/pull' && req.method == 'POST') {
const signature = `sha256=${crypto
.createHmac("sha256", SECRET)
.update(body)
.digest("hex")}`;
if (req.headers["x-hub-signature-256"] !== signature) {
res.writeHead(401, { "Content-Type": "text/plain" });
return res.end("Invalid signature");
}
console.log("Received push event. Pulling changes...");
exec("cd ~/meowguard/site/ && git pull && sleep 5 && pm2 restart server_secure.js", (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${stderr}`);
res.writeHead(500, { "Content-Type": "text/plain" });
return res.end("Error executing commands");
}
console.log(stdout);
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Updated and restarted successfully");
});
}
else {
console.log(un, 'tried', req.url);
res.writeHead(404, '404 Not Found').end('404 Not Found');
}
});
svr.listen(port, () => {
console.log(`Secure HTTPS server listening on port ${port}`);
});
function getmessages() {
try {
fs.accessSync(path);
} catch (e) {
fs.writeFileSync(path, '', 'utf8');
console.error('Created message file');
}
try {
messages = fs.readFileSync(path, 'utf8').split('\n');
console.log('Got messages');
} catch (readErr) {
console.error('Error reading messages file:', readErr);
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('+', '-');
}
// let aliases = {
// O6u1: 'meow',
// OXqw: 'zast'
// };