-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
278 lines (242 loc) · 7.97 KB
/
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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*******************************
*
* Server.js
*
* Authenticate users and allow data transport between
* server and client.js
*
* Pass MySql queries to database and return data.
*
********************************/
/*******************************
*
* Requirements
*
********************************/
// Require FS module to check for configuration files
var fs = require( "fs" );
// Require Math
var MATH = require('mathjs');
// Require crypto
// var crypto = require('crypto');
// Require Mysql Module
var mysql = require("mysql");
var express = require( "express" );
// Check configuration files here
fs.exists( "public/config.js", function( exists ) {
if ( exists ) {
// Report to console of success
console.log( "Configuration files found for front-end" );
} else {
// Report
console.err( "Configuration file not found for frontend, please read the README for proper configuration" );
process.exit( 1 );
}
});
fs.exists( "private/config.json", function( exists ) {
if ( exists ) {
// Report to console of success
console.log( "Configuration files found for back-end" );
} else {
// Report
console.err( "Configuration file not found for backend, please read the README for proper configuration" );
process.exit( 1 );
}
});
/*******************
*
* Retrieving Configuration
*
*******************/
// Load configuration file
var config = require( "./private/config.json" );
var params = require( "./private/params.json" );
/*******************
*
* MySQL connection
*
*******************/
// /Insert custom mysql connection information here
var dbConfig = {
host: config.host,
user: config.user,
password: config.password,
database: config.database
}
var db;
function handleDisconnect() {
db = mysql.createConnection(dbConfig);
// Custom error handler
db.connect(function(err) {
if(err) {
console.log("Error when connecting to DB:", err);
setTimeout(handleDisconnect, 2000);
}
});
db.on( "error", function( err ) {
console.log("Mysql Error:: " + err.code);
if(err.code === "PROTOCOL_CONNECTION_LOST") {
handleDisconnect();
} else {
console.log("Error MySql: ", err);
}
});
}
handleDisconnect();
/*******************
*
* Express and Socket.io
*
*******************/
// Require Express and cast to app variable
//var express = require( "express" ); Added to require block
var app = express();
app.use( express.static( __dirname + '/public' ) );
console.log( "Express is returning public pages" );
// Require Socket.io
// config.port comes from config.json object
var io = require( 'socket.io' ).listen( app.listen( config.port ) );
// IO settings
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // Reduce Logging
console.log( "SocketIO is listening to port::" + config.port );
/******************
*
* Cast usable variables
* TODO: Drop restrictions into config or param JSON
*
*******************/
var badQueries = params.bad,
ignoreLimit = params.noLimit,
userList = [],
roomList = [],
history = [],
socketData = [];
console.log( "Now Ready" );
/*************************
*
* Listening to sockets
* TODO: Drop sockets into sharable sessions
*
**************************/
io.sockets.on( 'connection', function( socket ) {
// TODO: Authenticate users
socket.on( 'register', function( data ) {
console.log( data );
if( data.pass == config.password) {
registerSocket( data.user );
socket.emit( 'rules', { bad: badQueries, noLimit: ignoreLimit });
var rooms = io.sockets.manager.rooms;
socket.emit( 'sessions', { rooms: rooms });
console.log( io.sockets.manager.rooms );
} else {
socket.emit( 'register', { reply: false } );
}
// TODO: Present optional sessions for sockets to join
});
socket.on( 'disconnect', function() {
// Depreciateing in favor method
// socket.broadcast.emit( 'sessionUpdate', { action: 'remove', room: socket.room } );
sessionManagement( socket.room );
});
/***************************
*
* Function
*
* *************************/
function sessionManagement( room ) {
setTimeout(function() {
checkRoom = "/"+room;
var rooms = Object.keys(io.sockets.manager.rooms);
console.log(rooms);
if( rooms.indexOf(checkRoom) > -1 && roomList.indexOf(checkRoom) == -1 ) {
socket.broadcast.emit( 'sessionUpdate', { action: 'add', room: room } );
roomList.push(room);
} else if( rooms.indexOf(checkRoom) == -1 && roomList.indexOf(checkRoom) > -1 ) {
socket.broadcast.emit( 'sessionUpdate', { action: 'remove', room: room } );
roomList.splice(roomList.indexOf(room),1);
}
}, 1000);
}
function registerSocket( user ) {
var key = Math.random().toString(36).substring(7);
userList[user] = { key: key };
socket.emit( 'register', { reply: true, user: user, key: key } );
socket.room = user;
socket.join( user );
sessionManagement( socket.room );
// Depreciating in favor of method
//socket.broadcast.emit( 'sessionUpdate', { action: 'add', room: socket.room } );
}
/***************************
*
* Logged in socket listeners
*
****************************/
socket.on( 'chat', function( data ) {
if( userList[data.auth.user] != null && userList[data.auth.user].key == data.auth.key ) {
console.log( data );
io.sockets.emit('chat', { data: data.auth.user + ": " + data.query } );
}
});
socket.on( 'history', function( data ) {
if( userList[data.auth.user] != null && userList[data.auth.user].key == data.auth.key ) {
socket.emit( 'history', { data: history } );
}
});
socket.on( 'query', function( data ) {
history.push(data.query);
console.log( "Query" );
console.log( data );
var activeSocket = data.session;
var auth = data.auth;
data = data.query;
data = data.replace(/;/g,'');
var queryC = data.toLowerCase();
var pass = true;
if( auth.key !== userList[auth.user].key ) {
pass = false;
}
badQueries.forEach( function( call ) {
if(queryC.indexOf( call ) != -1) {
console.log("Caught Illegal call: " + data );
pass = false;
}
});
var limit = true;
if(queryC.indexOf( 'limit' ) == -1) {
ignoreLimit.forEach( function( call ) {
if(queryC.indexOf( call ) != -1 ) {
limit = false;
}
});
if( limit ) data += ' LIMIT 500;';
}
if(pass) {
db.query( data, function( err, reply ) {
if(err) {
socket.emit('reply', { 0: { reply: 'Failed Query -- Error :' + err} });
}
if(reply) {
socketData[activeSocket] = reply;
io.sockets.in(activeSocket).emit('reply', reply );
}
});
} else {
socket.emit('reply', { 0: { reply: 'Failed Query -- Illegal Syntax' } });
}
});
socket.on( 'retrieve', function( data ) {
if( userList[data.auth.user] != null && userList[data.auth.user].key == data.auth.key ) {
console.log( data );
activeSocket = data.data;
console.log( activeSocket);
socket.leave(socket.room);
socket.join(activeSocket);
console.log( socketData[activeSocket] );
if(socketData[activeSocket] != null ) socket.emit( 'reply', socketData[activeSocket] );
}
} );
});