forked from petkov/http_to_mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
239 lines (210 loc) · 6.11 KB
/
index.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
#!/usr/bin/env node
const mqtt = require('mqtt');
const express = require('express');
const bodyParser = require('body-parser');
const multer = require('multer');
const yargs = require('yargs');
// Prepare commandline argument parsing
var argv = yargs
.scriptName('http_to_mqtt2')
.usage('$0 <cmd> [args]')
.config('settings', function (configPath) {
return JSON.parse(fs.readFileSync(configPath, 'utf-8'))
})
.help()
.alias('help', 'h')
.argv;
// Get the settings
const settings = {
mqtt: {
url: argv.mqtt_url || process.env.MQTT_URL || process.env.MQTT_HOST || '',
user: argv.mqtt_user || process.env.MQTT_USER || '',
password: argv.mqtt_pass || process.env.MQTT_PASS || '',
clientId: argv.mqtt_clientid || process.env.MQTT_CLIENT_ID || null
},
keepalive: {
topic: argv.keep_alive_topic || process.env.KEEP_ALIVE_TOPIC || 'keep_alive',
message: argv.keep_alive_message || process.env.KEEP_ALIVE_MESSAGE || 'keep_alive'
},
debug: argv.debug || process.env.DEBUG_MODE || false,
secret: argv.secret || process.env.SECRET || process.env.AUTH_KEY || '',
http_port: argv.port || process.env.PORT || 5000
}
var app = express();
var mqttClient = getMqttClient();
/**
* Connect to the MQTT broker
*/
function getMqttClient() {
// Set authentication information for MQTT broker
const mqttOptions = {
username: settings.mqtt.user,
password: settings.mqtt.password
};
// Set client id for MQTT broker
if (settings.mqtt.clientId) {
mqttOptions.clientId = settings.mqtt.clientId
}
// Return connected MQTT client
return mqtt.connect(settings.mqtt.url, mqttOptions);
}
/**
* Simple request logger
*
* @param {*} req
* @param {*} res
* @param {*} next
*/
function logRequest(req, res, next) {
const reqIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
var message = `Request [${req.originalUrl}] from ${reqIp}`;
if (settings.debug) {
message += ` with payload [${JSON.stringify(req.body)}]`;
} else {
message += '.';
}
console.log(message);
next();
}
/**
* Authorize HTTP request if matching secret
* @param {*} req
* @param {*} res
* @param {*} next
*/
function authorizeRequest(req, res, next) {
if (settings.secret && req.body['key'] != settings.secret) {
console.error('Access denied.');
res.sendStatus(401);
}
else {
next();
}
}
/**
* Check request for single file upload
* @param {*} req
* @param {*} res
* @param {*} next
*/
function checkSingleFileUpload(req, res, next) {
if (req.query.single) {
try {
// Upload file
var upload = multer().single(req.query.single);
upload(req, res, next);
} catch (error) {
// Failed to upload file
console.error(`Failed to upload file: ${error}`);
res.status(500).send('Failed to upload file');
}
}
else {
next();
}
}
/**
* Generate MQTT message from request path
* @param {*} req
* @param {*} res
* @param {*} next
*/
function checkMessagePathQueryParameter(req, res, next) {
if (req.query.path) {
req.body.message = req.body[req.query.path];
}
next();
}
/**
* Set MQTT topic from request
* @param {*} req
* @param {*} res
* @param {*} next
*/
function checkTopicQueryParameter(req, res, next) {
if (req.query.topic) {
req.body.topic = req.query.topic;
}
next();
}
/**
* Ensure MQTT topic is set.
* @param {*} req
* @param {*} res
* @param {*} next
*/
function ensureTopicSpecified(req, res, next) {
if (!req.body.topic) {
console.error('Topic not specified');
res.status(500).send('Topic not specified');
}
else {
next();
}
}
// Setup Express
app.set('port', settings.http_port);
app.use(bodyParser.json());
/**
* Send keep-alive message to MQTT broker
*/
app.get('/keep_alive/', logRequest, function (req, res) {
try {
mqttClient.publish(settings.keepalive.topic, settings.keepalive.message);
res.sendStatus(200);
} catch (error) {
// Failed to publish keep alive message
console.error(`Failed to publish keep alive message: ${error}`);
res.status(500).send('Failed to publish keep alive message');
}
});
/**
* Publish a HTPP post request to the MQTT broker
*/
app.post('/post/', logRequest, authorizeRequest, checkSingleFileUpload, checkMessagePathQueryParameter, checkTopicQueryParameter, ensureTopicSpecified, function (req, res) {
try {
mqttClient.publish(req.body['topic'], req.body['message']);
res.sendStatus(200);
} catch (error) {
// Failed to publish message to topic
console.error(`Failed to publish message [${req.body['message']}] to topic [${req.body['topic']}]: ${error}`);
res.status(500).send('Failed to publish message to topic');
}
});
/**
* Subscripte to a MQTT topic to stay API-compatible to https://github.com/petkov/http_to_mqtt
*/
app.get('/subscribe/', logRequest, authorizeRequest, function (req, res) {
var topic = req.query.topic;
if (!topic) {
console.error('Topic not specified');
res.status(500).send('topic not specified');
}
else {
// Create a new mqttClient instance for the new subscription to avoid adding listeners to the main mqttClient.
var mqttClient = getMqttClient();
mqttClient.on('connect', function () {
mqttClient.subscribe(topic);
});
mqttClient.on('message', function (t, m) {
if (t === topic) {
res.write(m);
}
});
req.on("close", function () {
mqttClient.end();
});
req.on("end", function () {
mqttClient.end();
});
}
});
// Start listening
try {
app.listen(app.get('port'), function () {
console.log('http_to_mqtt is running on port', app.get('port'));
});
} catch (error) {
// Failed to start Express listener
console.error(`Failed to start http_to_mqtt bridge: ${error}`);
}