forked from goblinbot/eos-beacon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
255 lines (195 loc) · 8.02 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* dependancies verklaren en ophalen */
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const ip = require('ip');
const fs = require('fs');
const globalSettings = require('./config.js');
/* CONFIGURATIE: vaste gegevens bij start up. */
var port = process.env.PORT || globalSettings.sys.port;
globalSettings.sys.localaddress = ip.address() +':'+ port;
globalSettings.data.localaddress = globalSettings.sys.localaddress;
var default_security_level = "Code green - All clear";
/* DYNAMIC DATA: data set to defaults on boot, that can be manipulated by users. */
var dynamicData = {
countClients : 0,
alertLevel : default_security_level,
lastBC : "bcdefault",
portalStatus : "ok"
};
/* INITIALISING THE APP */
app.use(express.static('public'));
app.use(express.static('_includes'));
http.listen(port, function(){
/* flavor text */
console.log('\n..\n..');
console.log('// '+globalSettings['cfg']['appname']+' ////////////');
console.log('# Initialising ..' );
console.log('# Loading dependancies ..');
console.log('-------------------------');
console.log('# CONNECT DEVICES//USERS TO :');
console.log(' ? External IP :\n\t- ' + globalSettings.sys.localaddress ); /*console.log(' ? External IP :\n\t- ' + globalSettings.sys['localaddress'] );*/
console.log(' ? Internal IP :\n\t- localhost:'+ port + '\n\t- ' + '127.0.0.1:'+ port );
console.log('-------------------------');
console.log('THANK YOU FOR USING '+globalSettings.cfg.appname+' INFORMATION & BROADCASTING SERVICES'); /*console.log('THANK YOU FOR USING '+globalSettings['cfg']['appname']+' INFORMATION & BROADCASTING SERVICES');*/
/* writing to eventlog.txt */
eventLogger('INIT','Beacon succesfully started.');
});
/* pathing / routing */
app.get('/', function(req, res){
res.sendFile('index.html', {"root": __dirname+'/public/'});
});
/* accessing the eventlog without making the LOGS folder public. */
app.get('/analysisMode', function(req, res){
res.sendFile('index.html', {"root": __dirname+'/LOGS/'});
});
/* Path for the system to request eventlog.txt specificly, without making the LOGS folder public. */
app.get('/systemRequestLogText', function(req, res){
res.sendFile('eventlog.txt', {"root": __dirname+'/LOGS/'});
});
/* fallback: 404 when non-valid location is requested. */
app.get('*', function(req, res){
res.sendFile('404.html', {"root": __dirname+'/public/'});
});
io.on('connection', function (socket) {
dynamicData['countClients'] = io.engine.clientsCount;
console.log('\t[ + ] '+dynamicData['countClients']+' active client(s).');
setTimeout(function(){
/* send FRONTEND data to FRONT */
socket.emit('startConfig', globalSettings.data);
},1000);
socket.on('updateSecurity', function(input){
var outString = input.replace(/[`~!@#$%^&*()_|+=?;:'",<>\{\}\[\]\\\/]/gi, '');
dynamicData['alertLevel'] = outString;
io.emit('updateDynamicData', dynamicData);
console.log('[secLVL] level => '+ input);
eventLogger('SEC','Security level change => '+ input +'. \n');
});
socket.on('updatePortalStatus', function(input){
var outString = input.replace(/[`~!@#$%\^&*()_|+=?;:'",<>\{\}\[\]\\\/]/gi, '');
dynamicData['portalStatus'] = input;
io.emit('updateDynamicData', dynamicData);
io.emit('portalfrontend');
console.log('[portal] status => '+ input);
eventLogger('portal','Portal status change => '+ input +'. \n');
});
// FORCE RESET ::
socket.on('forceReset', function() {
dynamicData['lastBC'] = "bcdefault";
io.emit('F5');
console.log('[admin] command => FORCE_RESET');
eventLogger('admin','Force reset activated. \n',1);
});
socket.on('requestDynamicData', function (){
dynamicData['countClients'] = io.engine.clientsCount;
io.emit('updateDynamicData', dynamicData);
});
socket.on('broadcastSend', function(value){
dynamicData['lastBC'] = value.file;
io.emit('updateDynamicData', dynamicData);
io.emit('broadcastReceive', value);
if (value.duration > 1) {
setTimeout(function(){
dynamicData.lastBC = 'bcdefault';
console.log('=> last-bc timer cleared.');
},value.duration);
}
console.log('[broadcast] sent => '+ value['title']);
eventLogger('broadcast','sent: '+ value['title'] +'. \n');
});
socket.on('disconnect', function(){
dynamicData['countClients'] = io.engine.clientsCount;
console.log('\t[ - ]' +dynamicData['countClients']+' active client(s).');
io.emit('updateDynamicData', dynamicData );
});
socket.on('auth', function(keycode){
var checklogincode = 0;
var loginrank = 0;
console.log('[AUTH] code received: '+keycode);
eventLogger('AUTH','auth-code received: '+ keycode +'. \n');
for (var i in globalSettings.accounts) {
if(globalSettings.accounts[i].logincode == keycode) {
checklogincode = 1;
loginrank = globalSettings.accounts[i].loginrank;
}
}
if(checklogincode == 1) {
socket.emit('authTrue', keycode, loginrank);
} else {
socket.emit('authFalse');
}
});
/* broadcast from adminpanel to index.js. Sends a "play this file!" request to every connected client. */
socket.on('broadcastAudio', function(audiofile){
console.log('[audio] => file: '+ audiofile);
io.emit('playAudioFile', audiofile);
eventLogger('AUDIO','audio file sent: '+ audiofile +'. \n');
});
/* getMedia: function to automatically read audio files into pushable buttons, caused by the adminpanel when logging in with sufficient rights. */
socket.on('getMedia', function(){
/*
getMedia has three seperate folders by default: miscAudio, aliceAudio and daveAudio.
First, beacon will check if the subfolders actually exist, then read every file and push them into an array.
Secondly, we push this array to the admin screen to generate the "play audio" buttons
*/
var miscAudio = [];
if(fs.existsSync('./public/sounds/audio-misc')) {
fs.readdir('./public/sounds/audio-misc', (err, files) => {
files.forEach(file => {
miscAudio.push(file);
});
socket.emit('sendMediaMisc', miscAudio);
});
}
/* copy of misc audio */
var aliceAudio = [];
if(fs.existsSync('./public/sounds/audio-alice')) {
fs.readdir('./public/sounds/audio-alice', (err, files) => {
files.forEach(file => {
aliceAudio.push(file);
});
socket.emit('sendMediaAlice', aliceAudio);
});
}
/* copy of misc audio */
var daveAudio = [];
if(fs.existsSync('./public/sounds/audio-dave')) {
fs.readdir('./public/sounds/audio-dave', (err, files) => {
files.forEach(file => {
daveAudio.push(file);
});
socket.emit('sendMediaDave', daveAudio);
});
}
});
});
/* eventLogger writes system logs to /LOGS/eventlog.txt. */
function eventLogger(type, message) {
/* grab the current date. */
var datum = new Date(), y = datum.getFullYear(), m = datum.getMonth();
/* declare log file location, and the result to add to said file. */
var logfile = 'LOGS/eventlog.txt';
var printresult = "";
/* missing values? Don't do anything.*/
if(!message || !type) {
return false;
} else {
/* grab the current time and format it to our european standards */
var currentTime = new Date();
var currentHours = currentTime.getHours ( );
var currentMinutes = currentTime.getMinutes ( );
currentHours = ( currentHours < 10 ? "0" : "" ) + currentHours;
currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
currentTime = currentHours + ":" + currentMinutes;
/* add to log: [TYPE] [DATE/TIME] :: [MESSAGE] */
printresult += '[ ' + type + ' ]\t';
printresult += datum.getDate() + '-' + datum.getMonth() + ', '+ currentTime;
printresult += '\t:: ';
printresult += message + '\n';
/* ADD log to txt file by appending. */
fs.appendFile(logfile, printresult, function(err) {
if(err) throw err;
});
}
}