-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollabRoute.js
240 lines (213 loc) · 7.87 KB
/
collabRoute.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
var fs = require('fs'),
express = require('express'),
https = require('https'),
mysql = require('mysql'),
queues = require('mysql-queues'), //syncronous multiple query and transaction support library
crypto = require('crypto'), //md5 for creating token
nodemailer = require('nodemailer'),
login = require("./login.js"),
registration = require("./registration.js"),
travelList = require("./travelList.js"),
coordinates = require("./coordinates.js"),
chat = require("./chat.js"),
request = require('request'),
//load config data from external JSON file
confFile = fs.readFileSync('/home/raffaele/collabRoute/collabRoute.json', 'utf8'),
conf = JSON.parse(confFile),
//load 2048 bit SSL/TSL key and his relative signed certificate
collabKey = fs.readFileSync(conf.keyPath),
collabCert = fs.readFileSync(conf.certPath),
option = {
key: collabKey,
cert: collabCert
},
PORT = conf.serverPort,
HOST = conf.serverHostname,
CHATPORT = conf.serverChatPort,
//chat variables
chatHttp = require('http'),
app = express();
app.configure(function() {
app.use(express.urlencoded());
app.use(express.json());
app.use(app.router);
});
var chatApp = express();
chatApp.configure(function() {
chatApp.use(express.urlencoded());
chatApp.use(express.json());
chatApp.use(chatApp.router);
});
var connection = mysql.createConnection({
host: conf.dbHostname,
port: conf.dbPort,
user: conf.dbUser,
password: conf.dbPassword,
database: conf.dbName
});
queues(connection, true); //true on debug support
connection.connect();
var mailConfig = {
service :conf.service,
auth: {
user: conf.mailUser,
pass: conf.mailPass
}
},
transport = nodemailer.createTransport("SMTP", mailConfig),
server = https.createServer(option, app).listen(PORT, HOST);
eventLog('CollabRoute Server is running on ' + HOST + ':' + PORT);
var chatServer = chatHttp.createServer(chatApp).listen(CHATPORT, HOST);
eventLog('CollabRoute ChatServer is running on ' + HOST + ':' + CHATPORT);
var io = require('socket.io').listen(chatServer);
chat.chatHandler(io, eventLog, connection);
app.get('/auth/:mail/:pass', function(req, res) {
login.doLogin(res, req, crypto, connection, eventLog, transport);
});
app.get('/recovery/:mail', function(req, res){
res.type('application/json');
registration.checkRecoveryRequest(req, res, connection, eventLog, transport);
});
app.put('/recovery/update', function(req, res){
res.type('application/json');
registration.confirmRecoverySetNewPasswd(req, res, connection, eventLog);
});
app.post('/add/user/', function(req, res) {
registration.checkSendRegister(req, res, connection, eventLog, transport);
});
app.put('/confirm/user', function(req, res) {
registration.confirmRegistration(req, res, connection, eventLog);
});
app.get('/travels/', function(req, res) {
res.type('application/json');
checkHeaderToken(req, connection, function(returnValue) {
if (!returnValue) {
res.json({type: 'adm_mbr_list', result: 'AUTH_FAILED'});
return;
}
travelList.sendTravelUsersList(req, res, connection, eventLog);
});
});
app.post('/add/travel/', function(req, res) {
res.type('application/json');
//need to create a separate connection for locking tables in race condition
var lockConnection = mysql.createConnection({
host: conf.dbHostname,
port: conf.dbPort,
user: conf.dbUser,
password: conf.dbPassword,
database: conf.dbName
});
queues(lockConnection, true);
lockConnection.connect();
checkHeaderToken(req, lockConnection, function(returnValue) {
if (!returnValue) {
res.json({type: 'add_new_travel', result: 'AUTH_FAILED'});
return;
}
travelList.addNewTravel(req, res, lockConnection, eventLog);
setTimeout(function() {
lockConnection.destroy();
}, 5000); //necessary delay, waiting to complete transaction
});
});
app.post('/add/routes/', function(req, res) {
res.type('application/json');
//need to create a separate connection for locking tables in race condition
var lockConnection = mysql.createConnection({
host: conf.dbHostname,
port: conf.dbPort,
user: conf.dbUser,
password: conf.dbPassword,
database: conf.dbName
});
queues(lockConnection, true);
lockConnection.connect();
checkHeaderToken(req, lockConnection, function(returnValue) {
if (!returnValue) {
res.json({type: 'add_new_routes', result: 'AUTH_FAILED'});
return;
}
travelList.addNewRoute(req, res, lockConnection, eventLog, chat);
setTimeout(function() {
lockConnection.destroy();
}, 5000); //necessary delay, waiting to complete transaction
});
});
app.delete('/delete/routes/:routeId/:travelId', function(req, res) {
res.type('application/json');
checkHeaderToken(req, connection, function(returnValue) {
if (!returnValue) {
res.json({type: 'delete_route', result: 'AUTH_FAILED'});
return;
}
travelList.deleteRoute(req, res, connection, chat, eventLog);
});
});
app.delete('/delete/travel/:travelId', function(req, res) {
res.type('application/json');
checkHeaderToken(req, connection, function(returnValue) {
if (!returnValue) {
res.json({type: 'leave_delete_travel', result: 'AUTH_FAILED'});
return;
}
travelList.deleteTravel(req, res, connection, chat, eventLog);
});
});
app.put('/update/coordinates/', function(req, res) {
res.type('application/json');
checkHeaderToken(req, connection, function(returnValue) {
if (!returnValue) {
res.json({type: 'leave_delete_travel', result: 'AUTH_FAILED'});
return;
}
coordinates.updateCoordinates(req, res, connection, eventLog, request, conf.APIKey, chat);
});
});
app.get('/routes/:id', function(req, res) {
res.type('application/json');
checkHeaderToken(req, connection, function(returnValue) {
if (!returnValue) {
res.json({type: 'routes_list', result: 'AUTH_FAILED'});
return;
}
travelList.getRoutes(req, res, connection, eventLog);
});
});
function eventLog(event) {
var currentdate = new Date();
var datetime = "[" + currentdate.getDate() + "/"
+ (currentdate.getMonth() + 1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds() + "]";
console.log(datetime + ' ' + event);
}
function checkHeaderToken(req, connection, callback) { //callback function return synchronously the result of the query
if (!req.headers.hasOwnProperty("token") || !req.headers.hasOwnProperty("id")) {
callback(false);
return;
}
var ip = req.connection.remoteAddress;
var id = req.headers.id;
var token = req.headers.token;
connection.query("SELECT token FROM user WHERE id =" + connection.escape(id), function(err, result) {
if (err) {
eventLog('[ Database error on header check from ' + ip + ' id: ' + id + ' ]');
callback(false);
return;
}
if (result.length === 0 || result[0].token !== token) {
eventLog('[ Authentication failed from ' + ip + ' id: ' + id + ' using token: ' + token + ' ]');
callback(false);
return;
}
callback(true);
});
}