-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcpserver.js
358 lines (321 loc) · 12.6 KB
/
rcpserver.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
var RCPServer = function () {
this.config = require('./config.js');
this.debug = require('./debug.js')
this.components = require('./components').setFacility(this, 'rcpserver');
if (this.config.cloud.enabled) {
require('./init.js').setInit('rcpserver');
this.app = require('express')();
this.bodyParser = require('body-parser');
this.app.use(this.bodyParser.json());
this.express = require('express');
this.http = require('http');
this.server = this.http.createServer(this.app).listen(this.config.rcpserver.port || 32400, onRCPListen);
this.server.on('error', onRCPError);
this.app.use(onPreAuth);
this.app.post('/device', onDevice);
this.app.post('/heartbeat', onHeartbeat);
this.app.post('/arduino-dead', onArduinoDead);
this.app.post('/arduino-alive', onArduinoAlive);
this.app.post('/uplink', onUplink);
this.app.post('/request-vpnkey', onRequestVPNKey);
this.app.post('/send-pubkey', onSendPubKey)
setInterval(monitorRaspys, 3000);
} else {
require('./init.js').setInit('rcpserver');
this.app = require('express')();
this.bodyParser = require('body-parser');
this.app.use(this.bodyParser.json());
this.express = require('express');
this.http = require('http');
this.server = this.http.createServer(this.app).listen(this.config.rcpserver.port || 32400, onRCPListen);
this.server.on('error', onRCPError);
this.app.post('/downlink', onDownlink);
}
}
// script for authenticating RCP packets
//const VPNKEY_EXEC = '/etc/openvpn/scripts/openvpn-db-get-pass.sh';
var rcpserver = new RCPServer();
function onRCPListen() {
require('./debug.js').log(1, 'rcpserver', 'RCP server listening on port: ' + components.getFacility('config').rcpserver.port || 32400);
require('./init.js').clearInit('rcpserver');
}
function onRCPError(error) {
require('./debug.js').log(1, 'rcpserver', 'RCP server failed: ' + error);
}
function onHeartbeat(req, res) {
var mem = require('./mem.js');
authRCP(req, res, function(error){
if (error == null){
vpnid = req.get('iot-raspyid');
accountID = vpnid.split('-')[0]
raspyID = vpnid.split('-')[1]
mem.updateRaspyIP(accountID, raspyID, req.connection.remoteAddress);
mem.clearRaspyDeadCounter(accountID, raspyID);
res.writeHead(200, { 'Content-Type' : 'text/plain'});
res.end('HB ok');
}
});
}
function onDevice(req, res) {
authRCP(req, res, function(error){
if (error == null){
vpnid = req.get('iot-raspyid');
accountID = vpnid.split('-')[0]
raspyID = vpnid.split('-')[1]
require('./mem.js').updateRaspyIP(accountID, raspyID, req.connection.remoteAddress);
require('./mem.js').clearRaspyDeadCounter(accountID, raspyID);
require('./mem.js').setRCPDeviceStatus(vpnid, srcip, req.body);
res.writeHead(200, { 'Content-Type' : 'text/plain'});
res.end('HB ok');
}
});
}
function onUplink(req, res) {
authRCP(req, res, function(error) {
if (error == null) {
var vpnid = req.get('iot-raspyid');
var accountID = vpnid.split('-')[0]
var raspyID = vpnid.split('-')[1]
var debug = require('./debug.js');
var mem = require('./mem.js');
var BFPMessage = req.body;
mem.updateRaspyIP(accountID, raspyID, req.connection.remoteAddress);
mem.clearRaspyDeadCounter(accountID, raspyID);
if (BFPMessage.header) {
switch (BFPMessage.header.code) {
case 'BFP_DEVICE_STATUS':
debug.logVPN(1, 'rcpserver', vpnid, 'Code BFP_DEVICE_STATUS received, passing to mem');
/* commented out as it was causing crashes */
//mem.setDeviceStatus(accountID, req.body);
break;
case undefined:
debug.logVPN(1, 'rcpserver', vpnid, 'Undefined Code in BFP, dropping message!');
break;
default:
debug.logVPN(1, 'rcpserver', vpnid, 'Unrecognized Code in BFP: ' + BFPMessage.header.code + ', dropping message!');
}
}
//require('./mem.js').setRCPDeviceStatus(vpnid, srcip, req.body);
res.writeHead(200, { 'Content-Type' : 'text/plain'});
res.end('HB ok');
}
});
}
/*
* Function to handle new VPNKey assignment, once it responsds with succesfully code, the VPNKey must be already set.
*/
function onRequestVPNKey(req, res) {
let debug = require('./debug.js');
let mem = require('./mem.js');
let bfp = require('./bfp.js');
let db = require('./configdb.js');
debug.log(4, 'rcpserver', 'Received RCP message: request VPNKey');
authRCP(req, res, function(error) {
if (error == null) {
var vpnid = req.get('iot-raspyid');
var accountID = vpnid.split('-')[0]
var raspyID = vpnid.split('-')[1]
debug.logVPN(4, 'rcpserver', vpnid, 'Received RCP message: request VPNKey from: ' + vpnid);
mem.getLatestVPNKey(accountID, raspyID, function(error, vpnKey, initVpnKey) {
let BFPMessage;
if (!error) {
BFPMessage = bfp.BFPVPNKeyResponse(vpnKey, true, null);
debug.logVPN(1, 'rcpserver', vpnid, 'Succesfully obtained VPNKey from mem/DB for: ' + vpnid);
//res.writeHead(200, { 'Content-Type' : 'text/plain'});
res.json(JSON.stringify(BFPMessage));
db.setVpnID(accountID, raspyID, undefined, undefined, undefined, undefined, false);
} else {
BFPMessage = bfp.BFPVPNKeyResponse(null, false, 'Could not obtain VPNKey from mem/DB');
//res.writeHead(500, { 'Content-Type' : 'text/plain'});
res.json(JSON.stringify(BFPMessage));
debug.logVPN(1, 'rcpserver', vpnid, 'Could not obtain VPNKey from mem/DB for: ' + vpnid);
}
});
} else {
res.writeHead(403, { 'Content-Type' : 'text/plain'});
res.end();
}
});
}
function onDownlink(req, res) {
authRCP(req, res, function(error) {
if (error == null) {
var vpnid = req.get('iot-raspyid');
var accountID = vpnid.split('-')[0]
var raspyID = vpnid.split('-')[1]
var debug = require('./debug.js');
var mem = require('./mem.js');
var BFPMessage = req.body;
if (BFPMessage.header) {
switch (BFPMessage.header.code) {
case 'BFP_DEVICE_COMMAND':
debug.logVPN(1, 'rcpserver', vpnid, 'Code BFP_DEVICE_COMMAND received, passing to mem');
//mem.setDeviceStatus(accountID, req.body);
break;
case undefined:
debug.logVPN(1, 'rcpserver', vpnid, 'Undefined Code in BFP, dropping message!');
break;
default:
debug.logVPN(1, 'rcpserver', vpnid, 'Unrecognized Code in BFP: ' + BFPMessage.header.code + ', dropping message!');
}
}
//require('./mem.js').setRCPDeviceStatus(vpnid, srcip, req.body);
res.writeHead(200, { 'Content-Type' : 'text/plain'});
res.end('HB ok');
}
});
}
function onArduinoDead(req, res) {
authRCP(req, res, function(error){
if (error == null) {
vpnid = req.get('iot-raspyid');
accountID = vpnid.split('-')[0];
raspyID = vpnid.split('-')[1];
var ardID = req.query.ardID;
/* removing as it was causing crashes, not needed */
//require('./mem.js').setArduinoDead(accountID, raspyID, ardID);
res.writeHead(200, { 'Content-Type' : 'text/plain'});
res.end('ok');
}
});
}
function onArduinoAlive(req, res) {
authRCP(req, res, function(error){
if (error == null) {
vpnid = req.get('iot-raspyid');
accountID = vpnid.split('-')[0];
raspyID = vpnid.split('-')[1];
var ardID = req.query.ardID;
/* removing as it was causing crashes, not needed */
//require('./mem.js').setArduinoAlive(accountID, raspyID, ardID);
res.writeHead(200, { 'Content-Type' : 'text/plain'});
res.end('ok');
}
});
}
function onSendPubKey(req, res) {
authRCP(req, res, function(error){
if (error == null) {
let vpnid = req.get('iot-raspyid');
let accountID = vpnid.split('-')[0];
let raspyID = vpnid.split('-')[1];
let BFPMessage = req.body;
let debug = require('./debug.js');
let os = require('./os.js');
debug.logVPN(4, 'rcpserver', vpnid, 'Received send-pubkey message with json body: ' + JSON.stringify(BFPMessage));
if (typeof(BFPMessage.body) != 'undefined') {
os.appendKeyToFile('/home/' + require('./config.js').os.serviceUser + '/.ssh/known_hosts', BFPMessage.body + '\n');
} else {
debug.logVPN(1, 'rcpserver', vpnid, 'send-pubkey message does not contain valid key.');
}
let serverSSHKey = os.getServerSSHPublicKey();
if (typeof(serverSSHKey) != 'undefined') {
let BFPResponse = require('./bfp.js').BFPCreateSendPublicKey(serverSSHKey);
res.json(BFPResponse);
console.log(JSON.stringify(BFPResponse))
debug.logVPN(4, 'rcpserver', vpnid, 'Succesfully obtained SSH Public Key from file');
} else {
debug.logVPN(1, 'rcpserver', vpnid, 'Could not obtain SSH Public Key from file');
res.writeHead(500, { 'Content-Type' : 'text/plain'});
res.end('Cannot obtain Server SSH Key');
}
}
});
}
function authRCP(req, res, callback) {
let srcip = req.connection.remoteAddress;
let vpnid = req.get('iot-raspyid');
let vpnkey = req.get('iot-vpnkey');
let accountID = vpnid.split('-')[0];
let raspyID = vpnid.split('-')[1];
var debug = require('./debug.js');
debug.logVPN(5, 'rcpserver', vpnid, 'Request POST URL: ' + req.originalUrl + ' from: ' + srcip + ' data: ' + JSON.stringify(req.body));
if (req.originalUrl == '/request-vpnkey') {
callback(null);
return;
}
if (!vpnid || !vpnkey) {
debug.logVPN(3, 'rcpserver', vpnid, 'Request POST did not contain auth headers from: ' + srcip);
res.writeHead(401, { 'Content-Type' : 'text/plain'});
res.end('No auth headers');
callback(1);
return;
} else {
debug.logVPN(5, 'rcpserver', vpnid, 'Request POST contained vpnid: ' + vpnid + ' and vpnkey: ' + vpnkey);
//var exec = require('child_process').exec;
let memVpnKey;
memVpnKey = require('./mem.js').getVPNKey(accountID, raspyID);
//var child = exec(VPNKEY_EXEC + ' ' + vpnid, function (error, stdout, stderr) {
if (memVpnKey != null) {
debug.logVPN(5, 'rcpserver', vpnid, 'Succesfully Accessed raspy DB for POST: ' + srcip);
if (memVpnKey.trim() == vpnkey.trim()) {
debug.logVPN(5, 'rcpserver', vpnid, 'RCP packet authenticated successfully from: ' + srcip);
//res.writeHead(200, { 'Content-Type' : 'text/plain'});
//res.end('Raspy DB accessed');
callback(null);
//require('./mem.js').setRCPDeviceStatus(vpnid, srcip, req.body);
} else {
debug.logVPN.log(3, 'rcpserver', vpnid, 'RCP packet authentication failed from: ' + srcip);
res.writeHead(401, { 'Content-Type' : 'text/plain'});
res.end('Auth failed');
callback(2)
}
} else {
debug.logVPN(1, 'rcpserver', vpnid, 'failed to obtain vpnKey from mem.');
res.writeHead(500, { 'Content-Type' : 'text/plain'});
res.end('Raspy DB cannot be accessed');
callback(3)
}
}
}
function onPreAuth(req, res, next) {
if (req.originalUrl == '/request-vpnkey') {
let mem = require('./mem.js');
let debug = require('./debug.js');
let bfp = require('./bfp.js');
let vpnid = req.get('iot-raspyid');
let vpnkey = req.get('iot-vpnkey');
let accountID = vpnid.split('-')[0];
let raspyID = vpnid.split('-')[1];
let srcip = req.connection.remoteAddress;
if (!vpnid || !vpnkey) {
debug.logVPN(3, 'rcpserver', vpnid, 'Request POST did not contain auth headers from: ' + srcip);
//res.writeHead(401, { 'Content-Type' : 'text/plain'});
let BFPMessage = bfp.BFPVPNKeyResponse(null, false, 'no auth headers');
res.json(BFPMessage);
} else {
debug.log(5, 'rcpserver', vpnid, '/request-vpnkey special URL detected. Obtaining keys from DB.')
mem.getLatestVPNKey(accountID, raspyID, function(error, vpnKey, initVpnKey) {
if (!error) {
if (initVpnKey.trim() == vpnkey.trim()) {
debug.logVPN(5, 'rcpserver', vpnid, '/request-vpnkey RCP packet authenticated successfully from: ' + srcip);
next();
} else {
require('./debug.js').log(3, 'rcpserver', '/request-vpnkey RCP packet authentication failed from: ' + srcip);
//res.writeHead(401, { 'Content-Type' : 'text/plain'});
let BFPMessage = bfp.BFPVPNKeyResponse(null, false, 'auth failed');
res.json(BFPMessage);
}
} else {
debug.logVPN(1, 'rcpserver', vpnid, '/request-vpnkey RCP packet authentication failed, unable to obtain VPN credentails from DB: ' + srcip);
}
});
}
} else {
next();
}
}
function monitorRaspys() {
var mem = require('./mem.js');
var accounts = mem.devices;
for (var accountID in accounts) {
if (accounts.hasOwnProperty(accountID)) {
for (var raspyID in accounts[accountID].raspys) {
if (accounts[accountID].raspys.hasOwnProperty(raspyID)) {
mem.increaseRaspyDeadCounter(accountID, raspyID);
}
}
}
}
}
module.exports = rcpserver;