forked from Indicardo/Commando
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
170 lines (145 loc) · 5.77 KB
/
index.js
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
170
require('dotenv').config();
const express = require('express');
const fs = require('fs');
const ws = require('ws');
const app = express();
const getPixels = require('get-pixels');
const multer = require('multer')
const upload = multer({ dest: `${__dirname}/uploads/` });
const VALID_COLORS = ['#BE0039', '#FF4500', '#FFA800', '#FFD635', '#00A368', '#00CC78', '#7EED56', '#00756F', '#009EAA', '#2450A4', '#3690EA', '#51E9F4', '#493AC1', '#6A5CFF', '#811E9F', '#B44AC0', '#FF3881', '#FF99AA', '#6D482F', '#9C6926', '#000000', '#898D90', '#D4D7D9', '#FFFFFF'];
let appData = {
currentMap: 'blank.png',
mapHistory: [
{ file: 'blank.png', reason: 'Init ^Indicado', date: 164900695100 }
]
};
let brandUsage = {};
let userCount = 0;
let socketId = 0;
if (fs.existsSync(`${__dirname}/data.json`)) {
appData = require(`${__dirname}/data.json`);
}
const server = app.listen(4000, () => console.log("Server is live!"));
const wsServer = new ws.Server({ server: server, path: '/api/ws' });
app.use('/maps', (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
next();
});
app.use('/maps', express.static(`${__dirname}/maps`));
app.use(express.static(`${__dirname}/static`));
app.get('/api/stats', (req, res) => {
res.json({
connectionCount: userCount,
...appData,
brands: brandUsage,
date: Date.now()
});
});
app.post('/updateorders', upload.single('image'), async (req, res) => {
if (!req.body || !req.file || !req.body.reason || !req.body.password || req.body.password !== process.env.PASSWORD) {
res.send('Invalid password!');
fs.unlinkSync(req.file.path);
return;
}
if (req.file.mimetype !== 'image/png') {
res.send('File must be a PNG!');
fs.unlinkSync(req.file.path);
return;
}
getPixels(req.file.path, 'image/png', function (err, pixels) {
if (err) {
res.send('Error reading file!');
console.log(err);
fs.unlinkSync(req.file.path);
return
}
for (var i = 0; i < 2000000; i++) {
const r = pixels.data[i * 4];
const g = pixels.data[(i * 4) + 1];
const b = pixels.data[(i * 4) + 2];
const hex = rgbToHex(r, g, b);
if (VALID_COLORS.indexOf(hex) === -1) {
res.send(`Pixel on ${i % 2000}, ${Math.floor(i / 2000)} has invalid color.`);
fs.unlinkSync(req.file.path);
return;
}
}
const file = `${Date.now()}.png`;
fs.copyFileSync(req.file.path, `${__dirname}/maps/${file}`);
fs.unlinkSync(req.file.path);
appData.currentMap = file;
appData.mapHistory.push({
file,
reason: req.body.reason,
date: Date.now()
})
wsServer.clients.forEach((client) => client.send(JSON.stringify({ type: 'map', data: file, reason: req.body.reason })));
fs.writeFileSync(`${__dirname}/data.json`, JSON.stringify(appData));
res.redirect('/');
});
});
wsServer.on('connection', (socket) => {
socket.id = socketId++;
socket.brand = 'unknown';
socket.lastActivity = Date.now() - (5 * 6 * 1000);
console.log(`[${new Date().toLocaleString()}] [+] Client connected: ${socket.id}`);
socket.on('close', () => {
console.log(`[${new Date().toLocaleString()}] [-] Client disconnected: ${socket.id}`);
});
socket.on('message', (message) => {
var data;
try {
data = JSON.parse(message);
} catch (e) {
socket.send(JSON.stringify({ type: 'error', data: 'Failed to parse message!' }));
return;
}
if (!data.type) {
socket.send(JSON.stringify({ type: 'error', data: 'Data missing type!' }));
}
switch (data.type.toLowerCase()) {
case 'brand':
const { brand } = data;
if (brand === undefined || brand.length < 1 || brand.length > 32 || !isAlphaNumeric(brand)) return;
socket.brand = data.brand;
break;
case 'getmap':
socket.send(JSON.stringify({ type: 'map', data: appData.currentMap, reason: null }));
break;
case 'ping':
socket.send(JSON.stringify({ type: 'pong' }));
break;
case 'placepixel':
const { x, y, color } = data;
if (x === undefined || y === undefined || color === undefined && x < 0 || x > 1999 || y < 0 || y > 1999 || color < 0 || color > 32) return;
socket.lastActivity = Date.now();
// console.log(`[${new Date().toLocaleString()}] Pixel placed by ${socket.id}: ${x}, ${y}: ${color}`);
break;
default:
socket.send(JSON.stringify({ type: 'error', data: 'Unknown command!' }));
break;
}
});
});
setInterval(() => {
const threshold = Date.now() - (11 * 60 * 1000); // 11 min cooldown.
userCount = Array.from(wsServer.clients).filter(c => c.lastActivity >= threshold).length;
brandUsage = Array.from(wsServer.clients).filter(c => c.lastActivity >= threshold).map(c => c.brand).reduce(function (acc, curr) {
return acc[curr] ? ++acc[curr] : acc[curr] = 1, acc
}, {});
}, 1000);
function rgbToHex(r, g, b) {
return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
}
function isAlphaNumeric(str) {
var code, i, len;
for (i = 0, len = str.length; i < len; i++) {
code = str.charCodeAt(i);
if (!(code > 47 && code < 58) && // numeric (0-9)
!(code > 64 && code < 91) && // upper alpha (A-Z)
!(code > 96 && code < 123)) { // lower alpha (a-z)
return false;
}
}
return true;
}