-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbroker.js
338 lines (304 loc) · 12.2 KB
/
broker.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
var aedes;
var hashpwd = require("./hashpwd.js");
var globalVar1 = null;
var logmodule1 = null;
var globHomey = null;
var SECURE_PRIVATEKEY = '/userdata/tls-privkey.pem';
var SECURE_PUBLICKEY = '/userdata/tls-pubkey.pem';
var SECURE_CERT = '/userdata/tls-cert.pem';
class brokerMQTT {
constructor(app) {
this.brokerSettings = {};
this.certificate = {};
this.logmodule = app.logmodule;
logmodule1 = this.logmodule;
this.globalVar = app.globalVar;
globalVar1 = this.globalVar;
this.server = null;
this.serverTLS = null;
this.serverOnline = false;
this.serverRestart = false;
//this.Homey = require('homey');
this.Homey = app.homey;
globHomey = app.homey;
this.OnInit();
}
OnInit() {
this.startBroker();
}
getConnectOptions() {
var bConfigValid = false;
var bTLS = false;
var bAllowNonSecure = false;
var iPort = parseInt(this.Homey.settings.get('ip_port'));
var iPortTLS = parseInt(this.Homey.settings.get('ip_port_tls'));
var bKeyFileValid = false;
var bCertFileValid = false;
this.brokerSettings = {};
this.brokerSettings.secure = {};
this.brokerSettings.secure.enabled = false;
this.certificate = {};
if (this.Homey.settings.get('tls') == true) {
bTLS = true;
}
if (this.Homey.settings.get('allow_nonsecure') == true) {
bAllowNonSecure = true;
}
// Check if key and certificate files are created
if (bTLS === true && iPortTLS > 1000 && iPortTLS !== iPort) {
this.logmodule.writelog('debug', "Check if key and cert PEM files are available");
var fs = require('fs');
if (fs.existsSync(SECURE_PRIVATEKEY)) {
this.logmodule.writelog('debug', "Key file found");
var privstats=fs.statSync(SECURE_PRIVATEKEY);
this.logmodule.writelog('debug', "PRIVKEY size = " + privstats.size);
if (privstats.size > 256){
bKeyFileValid = true;
this.certificate.key = fs.readFileSync(SECURE_PRIVATEKEY);
}
}
if (fs.existsSync(SECURE_CERT)) {
this.logmodule.writelog('debug', "Cert file found");
var certstats = fs.statSync(SECURE_CERT);
this.logmodule.writelog('debug', "CERT size = " + certstats.size);
if (certstats.size > 256) {
bCertFileValid = true;
this.certificate.cert = fs.readFileSync(SECURE_CERT);
}
}
if (bKeyFileValid === true && bCertFileValid === true) {
this.logmodule.writelog('debug', "TLS Config seems valid");
bConfigValid = true;
}
}
if ((bTLS === false || bAllowNonSecure === true) && iPort > 1000) {
// Check if we have a combined secure and non secure port. If we do, then the secure
// config should be valid.
if (bTLS === true && bConfigValid === false) {
this.logmodule.writelog('error', "broker: TLS config is not valid.");
return false;
}
this.logmodule.writelog('debug', "PLAIN Config seems valid");
bConfigValid = true;
}
// Create the config struct
if (bConfigValid === true) {
if (bTLS === true) {
this.brokerSettings.secure.enabled = true;
this.brokerSettings.secure.port = iPortTLS;
}
if (bTLS === false || bAllowNonSecure === true) {
this.brokerSettings.port = iPort;
if (bAllowNonSecure === true) {
// add allowNonSecure here when both http and https should be allowed
this.brokerSettings.allowNonSecure = true;
}
}
return true;
}
this.logmodule.writelog('error', "broker: Config is not valid.");
return false;
}
startBroker() {
var ref = this;
if (this.serverOnline == false && this.getConnectOptions() == true) {
this.logmodule.writelog('info', "Starting broker...");
this.logmodule.writelog('info', "brokerSettings: " + JSON.stringify(this.brokerSettings));
try {
var NedbPersistence = require('aedes-persistence-nedb');
var db = new NedbPersistence({
path: '/userdata',
prefix: 'mqtt'
});
aedes = require('aedes') ();
aedes.persistance = db;
aedes.authenticate = this.authUser;
this.brokerEvents();
if (this.brokerSettings.secure.enabled) {
this.serverTLS = require('tls').createServer(this.certificate, aedes.handle);
this.serverTLS.listen(this.brokerSettings.secure.port, function() {
ref.logmodule.writelog('info', 'Aedes MQTTS server is up and running');
ref.serverOnline = true;
});
}
if (this.brokerSettings.allowNonSecure || !this.brokerSettings.secure.enabled) {
this.server = require('net').createServer(aedes.handle);
this.server.listen(this.brokerSettings.port, function() {
ref.logmodule.writelog('info', 'Aedes MQTT server is up and running');
ref.serverOnline = true;
});
}
} catch(err) {
this.logmodule.writelog('error', "startBroker(): " +err);
}
} else {
if (this.serverOnline == true) {
this.logmodule.writelog('info', "startBroker() was called, but broker is already running.");
}
}
}
stopBroker() {
var ref = this;
if (this.serverOnline !== false) {
if (this.brokerSettings.allowNonSecure || !this.brokerSettings.secure.enabled) {
ref.server.close();
ref.logmodule.writelog('info', 'Aedes MQTT server is stopped');
}
if (this.brokerSettings.secure.enabled) {
ref.serverTLS.close();
ref.logmodule.writelog('info', 'Aedes MQTTS server is stopped');
}
aedes.close();
}
}
restartBroker() {
var ref = this;
this.logmodule.writelog('info', "Restarting broker...");
if (this.serverOnline === true) {
if (this.brokerSettings.allowNonSecure || !this.brokerSettings.secure.enabled) {
ref.server.close();
ref.logmodule.writelog('info', 'Aedes MQTT server is stopped');
}
if (this.brokerSettings.secure.enabled) {
ref.serverTLS.close();
ref.logmodule.writelog('info', 'Aedes MQTTS server is stopped');
}
aedes.close();
ref.serverRestart = true;
} else {
ref.startBroker();
}
}
isBrokerRunning() {
return this.serverOnline;
}
brokerEvents() {
var ref = this;
// when a client is connected; the client is passed as a parameter
aedes.on('client', function(client) {
ref.logmodule.writelog('info', 'client connected: '+ client.id);
});
// when a client is disconnected; the client is passed as a parameter.
aedes.on('clientDisconnect', function(client) {
ref.logmodule.writelog('info', 'client disconnected: '+ client.id);
});
// when a new message is published; the packet and the client are passed as parameters.
aedes.on('publish', function(packet, client) {
ref.logmodule.writelog('info', 'Packet published '+ packet.payload);
});
// when a client is subscribed to a topic; the topic and the client are passed as parameters.
aedes.on('subscribe', function(topic, client) {
ref.logmodule.writelog('info', client.id + ' has subscribed to topic '+ JSON.stringify(topic));
});
// when a client is unsubscribed to a topic; the topic and the client are passed as parameters.
aedes.on('unsubscribe', function(topic, client) {
ref.logmodule.writelog('info', client.id + ' has unsubscribed from topic '+ JSON.stringify(topic));
});
aedes.on('closed', function() {
ref.logmodule.writelog('info', 'Server is closed');
ref.serverOnline = false;
if (ref.serverRestart) {
ref.serverRestart = false;
ref.startBroker();
}
});
aedes.on('uncaughtException', function(err) {
if (err.errno === 'EADDRINUSE') {
ref.logmodule.writelog('info', 'The port is already in use');
}
});
}
// Accepts the connection if the username and password are valid
authUser(client, username, password, callback) {
logmodule1.writelog('info',"User authentication for user " + username);
if (globHomey.settings.get('disable_auth') == false) {
if (username !== null) {
var userCheck = globalVar1.getUser(username);
if (userCheck !== null) {
var authorized = false;
if (username === userCheck.userName) {
if (globHomey.settings.get('disable_hashing') == false) {
if (hashpwd.machPasswords(password, globalVar1.getUser(username).userPassword)) {
authorized = true;
logmodule1.writelog('info', 'User '+username+' has been succesfully authenticated');
}
} else {
logmodule1.writelog('debug', "plain pwd ("+password +" / "+globalVar1.getUser(username).userPassword+")");
if (password == globalVar1.getUser(username).userPassword) {
logmodule1.writelog('debug',"plain pwd authorized");
authorized = true;
}
}
}
if (authorized) client.user = username;
callback(null, authorized);
} else {
callback(null, false);
}
} else {
callback(null, false);
}
} else {
logmodule1.writelog('info', 'User '+username+' is allowed but not checked');
callback(null,true);
}
}
// In this case the client authorized as alice can publish to /users/alice taking
// the username from the topic and verifing it is the same of the authorized user
authPublish(client, topic, payload, callback) {
// right now no check for topic ACL
callback(null);
}
// In this case the client authorized as alice can subscribe to /users/alice taking
// the username from the topic and verifing it is the same of the authorized user
authSubscribe(client, topic, callback) {
// right now now check for topic ACL
callback(null);
}
// write and read certificate info
writeX509Data(pemData) {
this.logmodule.writelog('debug', "writeX509Data called");
const ref = this;
require('fs').writeFile(SECURE_PRIVATEKEY, pemData.private, function (err) {
if (err) {
ref.logmodule.writelog('error', "Persisting private key failed: "+ err);
}
});
require('fs').writeFile(SECURE_PUBLICKEY, pemData.public, function (err) {
if (err) {
ref.logmodule.writelog('error', "Persisting public key failed: "+ err);
}
});
require('fs').writeFile(SECURE_CERT, pemData.cert, function (err) {
if (err) {
ref.logmodule.writelog('error', "Persisting cerificate failed: "+ err);
}
});
}
readX509Data() {
const ref = this;
this.logmodule.writelog('debug', "readX509Sata called");
var privKey = null;
var pubKey = null;
var cert = null;
var fs = require('fs');
if (fs.existsSync(SECURE_PRIVATEKEY)) {
privKey = fs.readFileSync(SECURE_PRIVATEKEY).toString();
ref.logmodule.writelog('debug', "privkey: "+ privKey);
}
if (fs.existsSync(SECURE_PUBLICKEY)) {
pubKey = fs.readFileSync(SECURE_PUBLICKEY).toString();
ref.logmodule.writelog('debug', "pubkey: "+ pubKey);
}
if (fs.existsSync(SECURE_CERT)) {
cert = fs.readFileSync(SECURE_CERT).toString();
ref.logmodule.writelog('debug', "cert: "+ cert);
}
var pems = {};
pems.private = privKey;
pems.public = pubKey;
pems.cert = cert;
return pems;
}
}
module.exports = brokerMQTT;