forked from ragnos/eBot-CSGO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket_server.js
194 lines (167 loc) · 7.26 KB
/
websocket_server.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var http = require('http');
var formidable = require('formidable');
var archiver = require('archiver');
var fs = require('fs');
var dgram = require('dgram');
var clientUDP = dgram.createSocket("udp4");
var udp_ip = process.argv[2];
var udp_port = process.argv[3];
var DEMO_PATH = __dirname + "/demos/";
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
var server = http.createServer(function(request, response) {
switch (request.url) {
case '/upload':
var form = new formidable.IncomingForm({uploadDir: DEMO_PATH});
form.parse(request, function(err, fields, files) {
if (files.file) {
if (files.file.name.endsWith(".dem")) {
if (files.file.type == "application/octet-stream") {
console.log("Recieved file");
if (fs.existsSync(DEMO_PATH + files.file.name + ".zip"))
fs.unlinkSync(DEMO_PATH + files.file.name + ".zip");
if (fs.existsSync(files.file.path))
fs.renameSync(files.file.path, DEMO_PATH + files.file.name);
var output = fs.createWriteStream(DEMO_PATH + files.file.name + ".zip");
var archive = archiver('zip');
archive.pipe(output);
var demo = DEMO_PATH + files.file.name;
archive.append(fs.createReadStream(demo), {name: files.file.name});
archive.finalize();
if (fs.existsSync(DEMO_PATH + files.file.name) && fs.existsSync(DEMO_PATH + files.file.name + ".zip"))
fs.unlinkSync(DEMO_PATH + files.file.name);
}
} else {
console.error("bad file uploaded " + files.file);
}
}
response.writeHead(200, {'content-type': 'text/plain'});
response.end();
});
break;
default:
if (request.method == 'POST') {
var body = '';
request.on('data', function(data) {
body += data;
});
request.on('end', function() {
var data = {};
try {
data = JSON.parse(body);
if (data.message === "ping") {
return;
}
} catch (e) {
}
if (request.url == "/alive") {
io.sockets.in('alive').emit('aliveHandler', {data: body});
} else if (request.url == "/rcon") {
io.sockets.in('rcon-' + data.id).emit('rconHandler', body);
} else if (request.url == "/logger") {
io.sockets.in('logger-' + data.id).emit('loggerHandler', body);
io.sockets.in('loggersGlobal').emit('loggerGlobalHandler', body);
} else if (request.url == "/match") {
io.sockets.in('matchs').emit('matchsHandler', body);
} else if (request.url == "livemap") {
io.sockets.in('livemap-' + data.id).emit('livemapHandler', body);
} else {
message = JSON.parse(body);
}
});
}
response.writeHead(404);
response.end();
break;
}
});
fs.exists(DEMO_PATH, function(exists) {
if (!exists) {
fs.mkdir(DEMO_PATH);
}
});
server.listen(udp_port, function() {
console.log((new Date()) + ' Server is listening on port ' + udp_port);
});
var io = require('socket.io').listen(server);
io.set('log level', 0);
io.sockets.on('connection', function(socket) {
socket.taggedLogger = false;
socket.on('identify', function(data) {
if (data.type === "alive") {
socket.join("alive");
var dgram = new Buffer("__aliveCheck__");
clientUDP.send(dgram, 0, dgram.length, udp_port, udp_ip);
} else if (data.type === "logger") {
if (data.match_id) {
socket.join("logger-" + data.match_id);
} else {
socket.join("loggersGlobal");
}
socket.join("loggers");
socket.taggedLogger = true;
// Send an UDP packet to enable logger forwading
var dgram = new Buffer("__true__");
clientUDP.send(dgram, 0, dgram.length, udp_port, udp_ip);
} else if ((data.type === "rcon") && data.match_id) {
socket.join("rcon-" + data.match_id);
} else if ((data.type === "livemap") && data.match_id) {
socket.join("livemap-" + data.match_id);
} else if (data.type === "matchs") {
socket.join("matchs");
} else if (data.type === "relay") {
socket.join("relay");
}
});
socket.on('disconnect', function(data) {
if (socket.taggedLogger) {
if (io.sockets.clients('loggers').length == 1) {
var dgram = new Buffer("__false__");
clientUDP.send(dgram, 0, dgram.length, udp_port, udp_ip);
}
}
});
socket.on('rconSend', function(data) {
var dgram = new Buffer(data);
clientUDP.send(dgram, 0, dgram.length, udp_port, udp_ip);
});
socket.on('matchCommandSend', function(data) {
var dgram = new Buffer(data);
clientUDP.send(dgram, 0, dgram.length, udp_port, udp_ip);
});
});
var dgram = require('dgram');
var udpServer = dgram.createSocket('udp4');
udpServer.on('listening', function() {
var address = udpServer.address();
console.log('UDP Server listening on ' + address.address + ":" + address.port);
});
udpServer.on('message', function(message, remote) {
var messageObject = JSON.parse(message);
var body = messageObject.data;
var data = {};
try {
var data = JSON.parse(body);
if (data.message == "ping") {
return;
}
} catch (e) {
}
if (messageObject.scope == "alive") {
io.sockets.in('alive').emit('aliveHandler', {data: body});
io.sockets.in('relay').emit('relay', {channel: 'alive', 'method': 'aliveHandler', content: body});
} else if (messageObject.scope == "rcon") {
io.sockets.in('rcon-' + data.id).emit('rconHandler', body);
} else if (messageObject.scope == "logger") {
io.sockets.in('logger-' + data.id).emit('loggerHandler', body);
io.sockets.in('loggersGlobal').emit('loggerGlobalHandler', body);
} else if (messageObject.scope == "match") {
io.sockets.in('matchs').emit('matchsHandler', body);
io.sockets.in('relay').emit('relay', {channel: 'matchs', 'method': 'matchsHandler', content: body});
} else if (messageObject.scope == "livemap") {
io.sockets.in('livemap-' + data.id).emit('livemapHandler', body);
io.sockets.in('relay').emit('relay', {channel: 'livemap-' + data.id, 'method': 'livemapHandler', content: body});
}
});
udpServer.bind(parseInt(udp_port) + 1, udp_ip);