From d67e83a1524c94687f344227aae99f6250000bb9 Mon Sep 17 00:00:00 2001 From: Wolfram Webers Date: Tue, 18 Sep 2018 15:43:54 +0200 Subject: [PATCH] OTA activate OTA password will be stored as hash --- platformio.ini | 1 + src/NidayandHelper.cpp | 2 +- src/NidayandHelper.h | 4 +- src/motor-on-roller-blind-ws.cpp | 94 ++++++++++++++++++++------------ 4 files changed, 63 insertions(+), 38 deletions(-) diff --git a/platformio.ini b/platformio.ini index ca762ab..75ce7e1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -21,3 +21,4 @@ lib_deps = WifiManager ArduinoJson WebSockets + CryptoLegacy diff --git a/src/NidayandHelper.cpp b/src/NidayandHelper.cpp index 9b6383e..1ed0f99 100644 --- a/src/NidayandHelper.cpp +++ b/src/NidayandHelper.cpp @@ -28,7 +28,7 @@ boolean NidayandHelper::loadconfig(){ // use configFile.readString instead. configFile.readBytes(buf.get(), size); - StaticJsonBuffer<200> jsonBuffer; + StaticJsonBuffer<300> jsonBuffer; this->_config = jsonBuffer.parseObject(buf.get()); if (!this->_config.success()) { diff --git a/src/NidayandHelper.h b/src/NidayandHelper.h index 1aaad62..2ef80cc 100644 --- a/src/NidayandHelper.h +++ b/src/NidayandHelper.h @@ -1,9 +1,9 @@ #ifndef NidayandHelper_h #define NidayandHelper_h -#include "Arduino.h" +#include #include -#include "FS.h" +#include #include #include #include diff --git a/src/motor-on-roller-blind-ws.cpp b/src/motor-on-roller-blind-ws.cpp index 91b5749..51645f8 100644 --- a/src/motor-on-roller-blind-ws.cpp +++ b/src/motor-on-roller-blind-ws.cpp @@ -1,22 +1,25 @@ #include #include #include +#include +#include #include #include #include #include -#include "FS.h" +#include #include #include +#include #include -#include #include "NidayandHelper.h" #include "index_html.h" //--------------- CHANGE PARAMETERS ------------------ //Configure Default Settings for Access Point logon -String APid = "BlindsConnectAP"; //Name of access point -String APpw = "nidayand"; //Hardcoded password for access point +String APid = "WindowBlinds"; // Name of access point +String APpw = "welcome"; // Hardcoded password for access point +//#define RESET_CONFIG //---------------------------------------------------- @@ -36,6 +39,11 @@ char mqtt_pwd[40]; //WIFI config: MQTT server password (optional) String outputTopic; //MQTT topic for sending messages String inputTopic; //MQTT topic for listening boolean mqttActive = true; + +MD5Builder ota_md5; +char ota_hash[40]; +char ota_cleartxt[40] = "welcome"; + char config_name[40]; //WIFI config: Bonjour name of device char config_rotation[40] = "false"; //WIFI config: Detault rotation is CCW @@ -54,6 +62,7 @@ const float MAX_SPEED = 650.0; ESP8266WebServer server(80); // TCP server at port 80 will respond to HTTP requests WebSocketsServer webSocket = WebSocketsServer(81); // WebSockets will respond on port 81 +ESP8266HTTPUpdateServer updateServer = ESP8266HTTPUpdateServer(); void handleReleaseUp(); void handleReleaseDown(); @@ -78,6 +87,7 @@ bool loadConfig() { strcpy(mqtt_uid, json["mqtt_uid"]); strcpy(mqtt_pwd, json["mqtt_pwd"]); strcpy(config_rotation, json["config_rotation"]); + strcpy(ota_hash, json["ota_pwd"]); return true; } @@ -87,7 +97,7 @@ bool loadConfig() { on SPIFFS */ bool saveConfig() { - StaticJsonBuffer<200> jsonBuffer; + StaticJsonBuffer<300> jsonBuffer; JsonObject& json = jsonBuffer.createObject(); json["currentPosition"] = small_stepper.currentPosition(); json["maxPosition"] = maxPosition; @@ -98,6 +108,12 @@ bool saveConfig() { json["mqtt_pwd"] = mqtt_pwd; json["config_rotation"] = config_rotation; + ota_md5.begin(); + ota_md5.add(ota_cleartxt); + ota_md5.calculate(); + ota_md5.getChars(ota_hash); + json["ota_pwd"] = ota_hash; + return helper.saveconfig(json); } @@ -279,6 +295,7 @@ void setup(void) //Define customer parameters for WIFI Manager WiFiManagerParameter custom_config_name("Name", "Bonjour name", config_name, 40); WiFiManagerParameter custom_rotation("Rotation", "Clockwise rotation", config_rotation, 40); + WiFiManagerParameter custom_ota_pwd("OTA", "OTA password", ota_cleartxt, 40); WiFiManagerParameter custom_text("

Optional MQTT server parameters:

"); WiFiManagerParameter custom_mqtt_server("server", "MQTT server", mqtt_server, 40); WiFiManagerParameter custom_mqtt_port("port", "MQTT port", mqtt_port, 6); @@ -288,14 +305,17 @@ void setup(void) //Setup WIFI Manager WiFiManager wifiManager; - //reset settings - for testing - //clean FS, for testing - //helper.resetsettings(wifiManager); + // reset settings - for testing + // clean FS, for testing +#ifdef RESET_CONFIG + helper.resetsettings(wifiManager); +#endif wifiManager.setSaveConfigCallback(saveConfigCallback); //add all your parameters here wifiManager.addParameter(&custom_config_name); wifiManager.addParameter(&custom_rotation); + wifiManager.addParameter(&custom_ota_pwd); wifiManager.addParameter(&custom_text); wifiManager.addParameter(&custom_mqtt_server); wifiManager.addParameter(&custom_mqtt_port); @@ -323,6 +343,7 @@ void setup(void) strcpy(mqtt_uid, custom_mqtt_uid.getValue()); strcpy(mqtt_pwd, custom_mqtt_pwd.getValue()); strcpy(config_rotation, custom_rotation.getValue()); + strcpy(ota_cleartxt, custom_ota_pwd.getValue()); //Save the data saveConfig(); @@ -337,6 +358,10 @@ void setup(void) if (!loadDataSuccess) { small_stepper.setCurrentPosition(0); maxPosition = 2000000; + ota_md5.begin(); + ota_md5.add(ota_cleartxt); + ota_md5.calculate(); + ota_md5.getChars(ota_hash); } /* @@ -344,6 +369,9 @@ void setup(void) */ if (MDNS.begin(config_name)) { Serial.println("MDNS responder started"); + + updateServer.setup(&server); + MDNS.addService("http", "tcp", 80); MDNS.addService("ws", "tcp", 81); @@ -390,33 +418,29 @@ void setup(void) INDEX_HTML.replace("{NAME}",String(config_name)); - //Setup OTA - //helper.ota_setup(config_name); - { - // Authentication to avoid unauthorized updates - //ArduinoOTA.setPassword(OTA_PWD); - - ArduinoOTA.setHostname(config_name); - - ArduinoOTA.onStart([]() { - Serial.println("Start"); - }); - ArduinoOTA.onEnd([]() { - Serial.println("\nEnd"); - }); - ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { - Serial.printf("Progress: %u%%\r", (progress / (total / 100))); - }); - ArduinoOTA.onError([](ota_error_t error) { - Serial.printf("Error[%u]: ", error); - if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); - else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); - else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); - else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); - else if (error == OTA_END_ERROR) Serial.println("End Failed"); - }); - ArduinoOTA.begin(); - } + // Setup OTA + // Authentication to avoid unauthorized updates + ArduinoOTA.setPasswordHash(ota_hash); + ArduinoOTA.setHostname(config_name); + + ArduinoOTA.onStart([]() { + Serial.println("Start"); + }); + ArduinoOTA.onEnd([]() { + Serial.println("\nEnd"); + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + }); + ArduinoOTA.onError([](ota_error_t error) { + Serial.printf("Error[%u]: ", error); + if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); + else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); + else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); + else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); + else if (error == OTA_END_ERROR) Serial.println("End Failed"); + }); + ArduinoOTA.begin(); // Setting up pins for switches to PULLUP mode to have a defined state pinMode(blinds_up_pin, INPUT_PULLUP);