forked from octoblu/meshblu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
442 lines (352 loc) · 12.7 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* Setup command line parsing and options
* See: https://github.com/visionmedia/commander.js
*/
var _ = require('lodash');
var app = require('commander');
var tokenthrottle = require("tokenthrottle");
var restify = require('restify');
var socketio = require('socket.io');
var mqtt = require('mqtt');
var config = require('./config');
var securityImpl = require('./lib/getSecurityImpl');
var redis = require('./lib/redis');
var whoAmI = require('./lib/whoAmI');
var logEvent = require('./lib/logEvent');
var updateDevice = require('./lib/updateDevice');
var setupRestfulRoutes = require('./lib/setupHttpRoutes');
var setupCoapRoutes = require('./lib/setupCoapRoutes');
var setupMqttClient = require('./lib/setupMqttClient');
var socketLogic = require('./lib/socketLogic');
var fs = require('fs');
var setupGatewayConfig = require('./lib/setupGatewayConfig');
// sudo NODE_ENV=production forever start server.js --environment production
app
.option('-e, --environment', 'Set the environment (defaults to development)')
.parse(process.argv);
// console.log(app.environment || "running in development mode");
// if(!app.environment) app.environment = 'development';
if(app.args[0]){
app.environment = app.args[0];
} else {
app.environment = 'development';
}
// Create a throttle with 10 access limit per second.
// https://github.com/brycebaril/node-tokenthrottle
// var throttle = require("tokenthrottle")({
// rate: 10, // replenish actions at 10 per second
// burst: 20, // allow a maximum burst of 20 actions per second
// window: 60000, // set the throttle window to a minute
// overrides: {
// "127.0.0.1": {rate: 0}, // No limit for localhost
// "Joe Smith": {rate: 10}, // token "Joe Smith" gets 10 actions per second (Note defaults apply here, does not inherit)
// "2da0f39": {rate: 1000, burst: 2000, window: 1000}, // Allow a lot more actions to this token.
// }
// });
config.rateLimits = config.rateLimits || {};
// rate per second
var throttles = {
connection : tokenthrottle({rate: config.rateLimits.connection || 3}),
message : tokenthrottle({rate: config.rateLimits.message || 10}),
data : tokenthrottle({rate: config.rateLimits.data || 10}),
query : tokenthrottle({rate: config.rateLimits.query || 2}),
whoami : tokenthrottle({rate: config.rateLimits.whoami || 10}),
unthrottledIps : config.rateLimits.unthrottledIps || []
};
// Instantiate our two servers (http & https)
var server = restify.createServer();
server.pre(restify.pre.sanitizePath());
if(config.tls){
// Setup some https server options
if(app.environment == 'development'){
var https_options = {
certificate: fs.readFileSync("../skynet_certs/server.crt"),
key: fs.readFileSync("../skynet_certs/server.key")
};
} else {
var https_options = {
certificate: fs.readFileSync(config.tls.cert),
key: fs.readFileSync(config.tls.key),
};
}
var https_server = restify.createServer(https_options);
https_server.pre(restify.pre.sanitizePath());
}
// Setup websockets
var io, ios;
io = socketio.listen(server);
if(config.redis){
io.configure(function() {
return io.set("store", redis.createIoStore());
});
}
if(config.tls){
ios = socketio.listen(https_server);
// TODO: Figure out why secure socket.io doesn't log to REDIS
// if(config.redis){
// ios.configure(function() {
// return ios.set("store", redis.createIoStore());
// });
// };
}
restify.CORS.ALLOW_HEADERS.push('skynet_auth_uuid');
restify.CORS.ALLOW_HEADERS.push('skynet_auth_token');
restify.CORS.ALLOW_HEADERS.push('accept');
restify.CORS.ALLOW_HEADERS.push('sid');
restify.CORS.ALLOW_HEADERS.push('lang');
restify.CORS.ALLOW_HEADERS.push('origin');
restify.CORS.ALLOW_HEADERS.push('withcredentials');
restify.CORS.ALLOW_HEADERS.push('x-requested-with');
// server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.use(restify.CORS({ headers: [ 'skynet_auth_uuid', 'skynet_auth_token' ], origins: ['*'] }));
server.use(restify.fullResponse());
// Add throttling to HTTP API requests
// server.use(restify.throttle({
// burst: 100,
// rate: 50,
// ip: true, // throttle based on source ip address
// overrides: {
// '127.0.0.1': {
// rate: 0, // unlimited
// burst: 0
// }
// }
process.on("uncaughtException", function(error) {
return console.log(error.stack);
});
function cloneMessage(msg, device, fromUuid){
var clonedMsg = _.clone(msg);
clonedMsg.devices = device; //strip other devices from message
delete clonedMsg.protocol;
delete clonedMsg.api;
clonedMsg.fromUuid = msg.fromUuid; // add from device object to message for logging
return clonedMsg;
}
function sendToSocket(device, msg, callback){
var socketServer = device.secure ? ios : io;
if(socketServer){
if(callback){
socketServer.sockets.socket(device.socketid).emit('message', msg, function(results){
console.log('results', results);
try{
callback(results);
} catch (e){
console.log(e);
}
});
}else{
socketServer.sockets.in(device.uuid).emit('message', msg);
}
}
}
function handleUpdate(fromDevice, data, fn){
whoAmI(data.uuid, false, function(check){
if(check.error){
return fn(check);
}
if(securityImpl.canUpdate(fromDevice, check)){
updateDevice(data.uuid, data, function(results){
console.log('update results', results);
results.fromUuid = fromDevice;
logEvent(401, results);
try{
fn(results);
} catch (e){
console.log(e);
}
});
}else{
fn({error: {message: 'unauthorized', code: 401} });
}
});
}
function wrapMqttMessage(topic, data){
return JSON.stringify({topic: topic, data: data});
}
function sendMessage(fromDevice, data, fn){
var fromUuid;
if(fromDevice){
fromUuid = fromDevice.uuid;
}
console.log("sendMessage() from", fromUuid, data);
if(fromUuid){
data.fromUuid = fromUuid;
}
if(data.token){
//never forward token to another client
delete data.token;
}
console.log('devices: ' + data.devices);
console.log('message: ' + JSON.stringify(data));
//console.log('protocol: ' + data.protocol); <- dont think this makes sense
var devices = data.devices;
if(devices == "all" || devices == "*"){
if(fromUuid){
io.sockets.in(fromUuid + '_bc').emit('message', data);
if(config.tls){
ios.sockets.in(fromUuid + '_bc').emit('message', data);
}
mqttclient.publish(fromUuid + '_bc', wrapMqttMessage('message', data), {qos:qos});
}
logEvent(300, data);
} else {
if(devices){
if( typeof devices === 'string' ) {
devices = [ devices ];
}
devices.forEach( function(device) {
if (device.length > 35){
//check devices are valid
whoAmI(device, false, function(check){
var clonedMsg = cloneMessage(data, device, fromUuid);
console.log('device check:', check);
if(!check.error){
if(securityImpl.canSend(fromDevice, check)){
//to phone, but not from same phone
if(check.phoneNumber && (clonedMsg.fromPhone !== check.phoneNumber)){
// SMS handler
console.log("Sending SMS to", check.phoneNumber);
require('./lib/sendSms')(device, JSON.stringify(clonedMsg.payload), function(sms){
console.log('Sent SMS!', device, check.phoneNumber);
});
}
if(check.protocol === "mqtt"){
// MQTT handler
console.log('sending mqtt', device);
mqttclient.publish(device, JSON.stringify(clonedMsg), {qos:qos});
}
else{
// Websocket handler
if(fn && devices.length === 1 ){
console.log('sending with callback to: ', device);
sendToSocket(check, clonedMsg, fn);
}else{
sendToSocket(check, clonedMsg, null);
}
}
if(check.type == 'octobluMobile'){
// Push notification handler
console.log("Sending Push Notification to", check.uuid);
require('./lib/sendPushNotification')(check, JSON.stringify(clonedMsg.payload), function(push){
console.log('Sent Push Notification!', device);
});
}
}else{
clonedMsg.UNAUTHORIZED=true; //for logging
console.log('unauthorized send attempt from', fromUuid, 'to', device);
}
}else{
clonedMsg.INVALID_DEVICE=true; //for logging
console.log('send attempt on invalid device from', fromUuid, 'to', device);
}
var logMsg = _.clone(clonedMsg);
logMsg.toUuid = check; // add to device object to message for logging
logEvent(300, logMsg);
});
}
});
}
}
}
var skynet = {
handleUpdate: handleUpdate,
sendMessage: sendMessage,
gatewayConfig : setupGatewayConfig(io, ios),
throttles: throttles,
io: io,
ios: ios
};
function checkConnection(socket, secure){
var ip = socket.handshake.address.address;
//console.log(ip);
if(_.contains(throttles.unthrottledIps, ip)){
socketLogic(socket, secure, skynet);
}else{
throttles.connection.rateLimit(ip, function (err, limited) {
if(limited){
socket.emit('notReady',{error: 'rate limit exceeded ' + ip});
socket.disconnect();
}else{
console.log('io connected');
socketLogic(socket, secure, skynet);
}
});
}
}
io.sockets.on('connection', function (socket) {
checkConnection(socket, false);
});
if(config.tls){
ios.sockets.on('connection', function (socket) {
checkConnection(socket, true);
});
}
var qos = 0;
// create mqtt connection
try {
// var mqttclient = mqtt.createClient(1883, 'mqtt.skynet.im', mqttsettings);
var mqttConfig = config.mqtt || {};
var mqttsettings = {
keepalive: 1000, // seconds
protocolId: 'MQIsdp',
protocolVersion: 3,
clientId: 'skynet',
username: 'skynet',
password: mqttConfig.skynetPass
};
//console.log('attempting mqtt connection', mqttsettings);
var mqttclient = mqtt.createClient(mqttConfig.port || 1883, mqttConfig.host || 'localhost', mqttsettings);
// var mqttclient = mqtt.createClient(1883, '127.0.0.1', mqttsettings);
console.log('Skynet connected to MQTT broker');
setupMqttClient(skynet, mqttclient);
} catch(err){
console.log('No MQTT server found.', err);
}
// Redirect www subdomain to root domain for https cert
// if(config.tls){
// server.get(/^\/.*/, function(req, res, next) {
// if (req.headers.host.match(/^www/) !== null) {
// // return res.redirect("https://" + req.headers.host.replace(/www\./i, "") + req.url);
// res.send(302, "https://" + req.headers.host.replace(/www\./i, "") + req.url);
// } else {
// return next;
// }
// });
// };
// Integrate coap
var coap = require('coap'),
coapRouter = require('./lib/coapRouter'),
coapServer = coap.createServer(),
coapConfig = config.coap || {};
setupCoapRoutes(coapRouter, skynet);
coapServer.on('request', coapRouter.process);
// Now, setup both servers in one step
setupRestfulRoutes(server, skynet);
if(config.tls){
setupRestfulRoutes(https_server);
}
console.log("\n SSSSS kk tt ");
console.log("SS kk kk yy yy nn nnn eee tt ");
console.log(" SSSSS kkkkk yy yy nnn nn ee e tttt ");
console.log(" SS kk kk yyyyyy nn nn eeeee tt ");
console.log(" SSSSS kk kk yy nn nn eeeee tttt ");
console.log(" yyyyy ");
console.log('\nSkynet %s environment loaded... ', app.environment);
// Start our restful servers to listen on the appropriate ports
coapPort = coapConfig.port || 5683;
coapHost = coapConfig.host || 'localhost';
// Passing in null for the host responds to any request on server
// coapServer.listen(coapPort, coapHost, function () {
// coapServer.listen(coapPort, null, function () {
coapServer.listen(coapPort, function () {
console.log('coap listening at coap://' + coapHost + ':' + coapPort);
});
server.listen(process.env.PORT || config.port, function() {
console.log('%s listening at %s', server.name, server.url);
});
if(config.tls){
https_server.listen(process.env.SSLPORT || config.tls.sslPort, function() {
console.log('%s listening at %s', https_server.name, https_server.url);
});
}