Bugfix: switching brokers with self signed certs. (#1007)
* Fixes BLE scanning being disabled after switching server fails. * Fixes crash caused by BLE scan results arriving while switching servers. * Adds documentation to use the server switching command.
This commit is contained in:
1 parent
05dddf42de
commit
3f13984f44
4 files changed
+40
-12
No files matched your search
@@ -57,6 +57,27 @@ Server, port, and secure_flag are only required if changing connection to anothe
|
||||
If the new connection fails the gateway will fallback to the previous connection.
|
||||
:::
|
||||
|
||||
## Switching brokers and using self signed and client certificates
|
||||
|
||||
In the `user_config.h` file it is possible to specify multiple MQTT brokers and client certificates. These are commonly self signed and are supported by defining `MQTT_SECURE_SELF_SIGNED` as true or 1.
|
||||
Additonally, support for multiple brokers and client certificates has been added. To use this, it is required that the server certificate, client certificate, and client key are provided as their own constatnt string value as demonstrated in the file.
|
||||
To add more than one broker and switch between them it is necessary to provide all of the relevant certificates/keys and add their respective variable names in the `certs_array` structure, as shown in `user_config.h`, and changing the array size to the number of different connections -1.
|
||||
|
||||
To switch between these servers with an MQTT command message, the format is as follows:
|
||||
```
|
||||
mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/config" -m
|
||||
'{
|
||||
"mqtt_user": "user",
|
||||
"mqtt_pass": "password",
|
||||
"mqtt_server": "host",
|
||||
"mqtt_port": "port",
|
||||
"mqtt_secure": "true",
|
||||
"mqtt_cert_index":0
|
||||
}'
|
||||
```
|
||||
::: tip
|
||||
The `mqtt_cert_index` value corresponds to the 0 to X index of the `certs_array` in `user_config.h`.
|
||||
:::
|
||||
|
||||
# Firmware update from MQTT (ESP only)
|
||||
|
||||
|
||||
+3
-3
@@ -198,7 +198,7 @@ vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep
|
||||
# endif
|
||||
|
||||
# ifndef MQTT_SECURE_SELF_SIGNED_CLIENT
|
||||
# define MQTT_SECURE_SELF_SIGNED_CLIENT 1
|
||||
# define MQTT_SECURE_SELF_SIGNED_CLIENT 1 // If using a self signed certificate for the broker and not using client certificates set this to false or 0
|
||||
# endif
|
||||
|
||||
# ifndef MQTT_SECURE_SELF_SIGNED_INDEX_DEFAULT
|
||||
@@ -219,9 +219,9 @@ const char* ss_client_cert PROGMEM = R"EOF("
|
||||
")EOF";
|
||||
|
||||
const char* ss_client_key PROGMEM = R"EOF("
|
||||
-----BEGIN CERTIFICATE-----
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
...
|
||||
-----END CERTIFICATE-----
|
||||
-----END RSA PRIVATE KEY-----
|
||||
")EOF";
|
||||
|
||||
struct ss_certs {
|
||||
|
||||
@@ -623,6 +623,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
|
||||
}
|
||||
|
||||
void onResult(BLEAdvertisedDevice* advertisedDevice) {
|
||||
if (!ProcessLock) {
|
||||
Log.trace(F("Creating BLE buffer" CR));
|
||||
JsonObject& BLEdata = getBTJsonObject();
|
||||
String mac_adress = advertisedDevice->getAddress().toString().c_str();
|
||||
@@ -704,6 +705,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
|
||||
Log.trace(F("Filtered mac device" CR));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
+14
-9
@@ -868,6 +868,7 @@ void setupTLS(bool self_signed, uint8_t index) {
|
||||
WiFiClientSecure* sClient = (WiFiClientSecure*)eClient;
|
||||
# if MQTT_SECURE_SELF_SIGNED
|
||||
if (self_signed) {
|
||||
Log.notice(F("Using self signed cert index %u" CR), index);
|
||||
# if defined(ESP32)
|
||||
sClient->setCACert(certs_array[index].server_cert);
|
||||
# if MQTT_SECURE_SELF_SIGNED_CLIENT
|
||||
@@ -1881,33 +1882,32 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
|
||||
}
|
||||
|
||||
if (SYSdata.containsKey("mqtt_user") && SYSdata.containsKey("mqtt_pass")) {
|
||||
# if defined(ZgatewayBT) && defined(ESP32)
|
||||
stopProcessing();
|
||||
# endif
|
||||
client.disconnect();
|
||||
bool update_server = false;
|
||||
bool secure_connect = SYSdata.get<bool>("mqtt_secure");
|
||||
void* prev_client = nullptr;
|
||||
bool use_ss_cert = SYSdata.containsKey("mqtt_ss_cert");
|
||||
bool use_ss_cert = SYSdata.containsKey("mqtt_cert_index");
|
||||
uint8_t cert_index = mqtt_ss_index;
|
||||
|
||||
if (SYSdata.containsKey("mqtt_server") && SYSdata.containsKey("mqtt_port")) {
|
||||
if (!SYSdata.containsKey("mqtt_secure")) {
|
||||
Log.warning(F("mqtt_server provided without mqtt_secure defined - ignoring command" CR));
|
||||
Log.error(F("mqtt_server provided without mqtt_secure defined - ignoring command" CR));
|
||||
return;
|
||||
}
|
||||
# if MQTT_SECURE_SELF_SIGNED
|
||||
if (use_ss_cert) {
|
||||
cert_index = SYSdata.get<uint8_t>("mqtt_ss_cert");
|
||||
cert_index = SYSdata.get<uint8_t>("mqtt_cert_index");
|
||||
if (cert_index >= sizeof(certs_array) / sizeof(ss_certs)) {
|
||||
Log.warning(F("mqtt_ss_cert index invalid - ignoring command" CR));
|
||||
Log.error(F("mqtt_cert_index invalid - ignoring command" CR));
|
||||
return;
|
||||
}
|
||||
}
|
||||
# endif
|
||||
|
||||
# if defined(ZgatewayBT) && defined(ESP32)
|
||||
stopProcessing();
|
||||
# endif
|
||||
client.disconnect();
|
||||
update_server = true;
|
||||
|
||||
if (secure_connect != mqtt_secure) {
|
||||
prev_client = eClient;
|
||||
if (!mqtt_secure) {
|
||||
@@ -1925,6 +1925,11 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
|
||||
}
|
||||
|
||||
client.setServer(SYSdata.get<const char*>("mqtt_server"), SYSdata.get<unsigned int>("mqtt_port"));
|
||||
} else {
|
||||
# if defined(ZgatewayBT) && defined(ESP32)
|
||||
stopProcessing();
|
||||
# endif
|
||||
client.disconnect();
|
||||
}
|
||||
|
||||
String prev_user = mqtt_user;
|
||||
|
||||
Reference in new issue
Block a user