1
0
Fork 0
mirror of https://github.com/Ylianst/MeshCentral.git synced 2025-02-14 12:01:52 +00:00

Made MQTT optional.

This commit is contained in:
Ylian Saint-Hilaire 2019-10-04 12:18:56 -07:00
parent 8abb6ef910
commit 7bba856984
7 changed files with 88 additions and 86 deletions

View file

@ -116,6 +116,7 @@
<Compile Include="meshrelay.js" />
<Compile Include="meshuser.js" />
<Compile Include="mpsserver.js" />
<Compile Include="mqttbroker.js" />
<Compile Include="public\scripts\agent-redir-rtc-0.1.0.js" />
<Compile Include="swarmserver.js" />
<Compile Include="multiserver.js" />

View file

@ -824,8 +824,9 @@ function CreateMeshCentralServer(config, args) {
// Create APF server to hook into webserver
obj.apfserver = require('./apfserver.js').CreateApfServer(obj, obj.db, obj.args);
// Create MQTT Broker to hook into webserver and mpsserver
obj.mqttbroker = require("./mqttbroker.js").CreateMQTTBroker(obj,obj.db,obj.args);
if (obj.config.mqtt != null) { obj.mqttbroker = require("./mqttbroker.js").CreateMQTTBroker(obj, obj.db, obj.args); }
// Start the web server and if needed, the redirection web server.
obj.webserver = require('./webserver.js').CreateWebServer(obj, obj.db, obj.args, obj.certificates);
@ -1828,6 +1829,7 @@ function mainStart() {
if (require('os').platform() == 'win32') { modules.push('node-windows'); if (sspi == true) { modules.push('node-sspi'); } } // Add Windows modules
if (ldap == true) { modules.push('ldapauth-fork'); }
if (config.letsencrypt != null) { modules.push('greenlock'); modules.push('le-store-certbot'); modules.push('le-challenge-fs'); modules.push('le-acme-core'); } // Add Greenlock Modules
if (config.mqtt != null) { modules.push('mqtt'); modules.push('aedes'); } // Add MQTT Modules
if (config.settings.mongodb != null) { modules.push('mongodb'); } // Add MongoDB, official driver.
else if (config.settings.xmongodb != null) { modules.push('mongojs'); } // Add MongoJS, old driver.
if (config.smtp != null) { modules.push('nodemailer'); } // Add SMTP support

View file

@ -159,7 +159,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
};
}
// required for TLS piping to MQTT broker
// Required for TLS piping to MQTT broker
function SerialTunnel(options) {
var obj = new require('stream').Duplex(options);
obj.forwardwrite = null;
@ -169,6 +169,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
return obj;
}
// Return's the length of an MQTT packet
function getMQTTPacketLength(chunk) {
var packet_len = 0;
if (chunk.readUInt8(0) == 16) {
@ -197,6 +198,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
return packet_len;
}
// Called when a new TLS/TCP connection is accepted
function onConnection(socket) {
connectionCount++;
if (obj.args.mpstlsoffload) {
@ -221,13 +223,12 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
//if (!socket.tag.clientCert.subject) { console.log("MPS Connection, no client cert: " + socket.remoteAddress); socket.write('HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\nMeshCentral2 MPS server.\r\nNo client certificate given.'); socket.end(); return; }
if (socket.tag.accumulator.substring(0, 3) == "GET") { if (args.mpsdebug) { console.log("MPS Connection, HTTP GET detected: " + socket.remoteAddress); } socket.write("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n<!DOCTYPE html><html><head><meta charset=\"UTF-8\"></head><body>MeshCentral2 MPS server.<br />Intel&reg; AMT computers should connect here.</body></html>"); socket.end(); return; }
// If the MQTT broker is active, look for inbound MQTT connections
if (parent.mqttbroker != null) {
var chunk = Buffer.from(socket.tag.accumulator, "binary");
var packet_len = 0;
if (chunk.readUInt8(0)==16) {
packet_len = getMQTTPacketLength(chunk);
}
if (chunk.readUInt8(0)==16 && (socket.tag.accumulator.length < packet_len )) return;// minimum MQTT detection
if (chunk.readUInt8(0) == 16) { packet_len = getMQTTPacketLength(chunk); }
if (chunk.readUInt8(0) == 16 && (socket.tag.accumulator.length < packet_len)) return; // Minimum MQTT detection
// check if it is MQTT, need more initial packet to probe
if (chunk.readUInt8(0) == 16 && ((chunk.slice(4, 8).toString() === "MQTT") || (chunk.slice(5, 9).toString() === "MQTT")
@ -240,11 +241,14 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
socket.on('data', function (b) { socket.serialtunnel.updateBuffer(Buffer.from(b, 'binary')) });
socket.serialtunnel.forwardwrite = function (b) { socket.write(b, "binary") }
socket.on("close", function () { socket.serialtunnel.emit('end'); });
//pass socket wrapper to mqtt broker
// Pass socket wrapper to the MQTT broker
parent.mqttbroker.handle(socket.serialtunnel);
socket.unshift(socket.tag.accumulator);
return;
}
}
socket.tag.first = false;
// Setup this node with certificate authentication

View file

@ -6,24 +6,20 @@
* @version v0.0.1
*/
module.exports.CreateMQTTBroker = function (parent, db, args) {
// internal objects container
// Internal objects container
var obj = {}
obj.parent = parent;
obj.db = db;
obj.args = args;
obj.aedes = require("aedes")();
// argument parsing -- tbd
// event handling and filtering
// authentication filter
obj.aedes.authenticate = function (client, username, password, callback) {
// accept all user
// TODO: add authentication handler
obj.parent.debug("mqtt", "Authentication with " + username + ":" + password);
callback(null, true);
@ -36,19 +32,20 @@ module.exports.CreateMQTTBroker = function (parent, db, args) {
callback(null);
}
// check if a client can publish a packet
// Check if a client can publish a packet
obj.aedes.authorizeSubscribe = function (client, sub, callback) {
// TODO: add subscription control here
obj.parent.debug("mqtt", "AuthorizeSubscribe");
callback(null, sub);
}
// check if a client can publish a packet
// Check if a client can publish a packet
obj.aedes.authorizeForward = function (client, packet) {
// TODO: add forwarding control
obj.parent.debug("mqtt", "AuthorizeForward");
return packet;
}
obj.handle = obj.aedes.handle;
return obj;
}

View file

@ -1,6 +1,6 @@
{
"name": "meshcentral",
"version": "0.4.1-r",
"version": "0.4.1-s",
"keywords": [
"Remote Management",
"Intel AMT",
@ -27,7 +27,6 @@
"sample-config.json"
],
"dependencies": {
"aedes": "^0.39.0",
"archiver": "^3.0.0",
"body-parser": "^1.19.0",
"cbor": "4.1.5",
@ -40,11 +39,9 @@
"ipcheck": "^0.1.0",
"meshcentral": "*",
"minimist": "^1.2.0",
"mqtt": "^3.0.0",
"multiparty": "^4.2.1",
"nedb": "^1.8.0",
"node-forge": "^0.8.4",
"otplib": "^11.0.1",
"ws": "^6.2.1",
"xmldom": "^0.1.27",
"yauzl": "^2.10.0"

File diff suppressed because one or more lines are too long

View file

@ -3323,15 +3323,16 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
try { obj.meshAgentHandler.CreateMeshAgent(obj, obj.db, ws, req, obj.args, domain); } catch (e) { console.log(e); }
});
// MQTT broker over websocket
// Setup MQTT broker over websocket
if (obj.parent.mqttbroker != null) {
obj.app.ws(url + 'mqtt.ashx', function (ws, req) {
var ser = SerialTunnel();
ws.on('message', function (b) { ser.updateBuffer(Buffer.from(b, 'binary')) });
ser.forwardwrite = function (b) { ws.send(b, "binary") }
ws.on("close", function () { ser.emit('end'); });
//pass socket wrapper to mqtt broker
obj.parent.mqttbroker.handle(ser);
})
obj.parent.mqttbroker.handle(ser); // Pass socket wrapper to MQTT broker
});
}
// Memory Tracking
if (typeof obj.args.memorytracking == 'number') {