-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
201 lines (177 loc) · 5.15 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
'use strict'
var storeDb = require("./storeDb.js");
var dateFormat = require("date-format");
var config = require("./config/config.json");
var storeTopics = require("./config/storeTopics.json");
var level = require("level");
var aedesPersistencelevel = require("aedes-persistence-level");
var fsWatch = require("node-watch");
var clients = require("./config/clients.json");
var users = require("./config/users.json");
var checker = require("./checker.js");
var cleanup = require("./cleanup.js");
var squel = require("squel");
/**
* Aedes INIT
*/
var aedes = require("aedes")({
persistence : aedesPersistencelevel(level(config.broker.persistence.dbPath)),
heartbeatInterval : config.broker.heartbeatInterval
});
/**
* TCP stream
*/
var tcpServer = require("net").createServer(aedes.handle);
tcpServer.listen(config.ports.tcp);
/**
* TCP TLS stream
*/
var tls = require('tls');
var tlsOptions = {
key : checker.certPrivate,
cert : checker.certPublic
};
var tlsServer = tls.createServer(tlsOptions, function (stream) {
aedes.handle(stream);
});
tlsServer.listen(config.ports.tcptls);
/**
* WS stream
*/
var websocket = require("websocket-stream");
var wsServer = websocket.createServer({
server : config.hostname,
port : config.ports.ws
}, aedes.handle);
/**
* Secure WS stream
*/
var https = require('https');
var wssServer = https.createServer(tlsOptions);
var wss = websocket.createServer({
server : wssServer
}, aedes.handle);
wssServer.listen(config.ports.wss);
/**
* Aedes functions
*/
aedes.authenticate = function (client, username, password, cb) {
if (checker.authenticate(username, password)) {
client.username = username;
return cb(null, true);
} else {
cb(null, false);
}
}
aedes.authorizePublish = function (client, packet, cb) {
if (checker.userTopicPermission(client.username, packet.topic)) {
return cb(null);
} else {
console.log("publish denied");
cb(new Error("publish denied"));
}
}
aedes.authorizeSubscribe = function (client, packet, cb) {
if (checker.userTopicPermission(client.username, packet.topic)) {
console.log("subscribed", client.id, packet.topic);
return cb(null, packet);
} else {
console.log("subscribe denied");
cb(new Error("subscribe denied"));
}
}
aedes.on("client", function (client) {
console.log("client connected:", client.id);
});
aedes.on("clientDisconnect", function (client) {
console.log("client disconnected:", client.id);
});
aedes.on("publish", function (packet, client) {
console.log("published", packet.topic, packet.payload.toString());
});
/**
* STORE Topics
*/
function storeTopic(topic, payload) {
var topicParts = topic.split("/");
var iq = squel.insert()
.into("store")
.set("client_id", topicParts[0])
.set("created", dateFormat(new Date()))
.set("source", topicParts.slice(1).join("/"))
.set("value", payload.toString())
.toParam();
storeDb.pool.query(iq.text, iq.values, function (err, result) {
if (err) {
console.error(err)
throw err;
}
console.log("DB inserted, record_id=" + result.insertId);
});
}
storeTopics.forEach(function (topicName) {
console.log("store topic registered", topicName);
aedes.subscribe(topicName, function (packet, cb) {
storeTopic(packet.topic, packet.payload);
cb();
});
});
/**
* File watchers
*/
fsWatch("config/clients.json", function (filename) {
console.log(filename, " changed.");
delete require.cache[require.resolve("./config/clients.json")];
clients = require("./config/clients.json");
});
fsWatch("config/users.json", function (filename) {
console.log(filename, " changed.");
delete require.cache[require.resolve("./config/users.json")];
users = require("./config/users.json");
});
fsWatch("config/storeTopics.json", function (filename) {
var oldTopicsList = require("./config/storeTopics.json");
console.log(filename, " changed.");
delete require.cache[require.resolve("./config/storeTopics.json")];
storeTopics = require("./config/storeTopics.json");
var newTopics = storeTopics.filter(function (x) {
return oldTopicsList.indexOf(x) < 0
});
var deletedTopics = oldTopicsList.filter(function (x) {
return storeTopics.indexOf(x) < 0
});
newTopics.forEach(function (topicName) {
console.log("store topic registered", topicName);
aedes.subscribe(topicName, callStoreTopic);
});
deletedTopics.forEach(function (topicName) {
aedes.unsubscribe(topicName, callStoreTopic);
console.log("store topic unregistered", topicName);
});
});
function callStoreTopic(packet, cb) {
storeTopic(packet.topic, packet.payload);
cb();
}
/**
* Data API
*/
var dataapi = require("./dataapi.js")({
port : config.ports.dataapi,
broker : aedes
});
dataapi.start();
/**
* Cleanup
*/
cleanup(function () {
console.log("doing cleanup");
dataapi.close();
aedes.close();
tcpServer.close();
tlsServer.close();
wsServer.close();
storeDb.close();
wssServer.close();
});
console.log("server ready");