diff --git a/.gitignore b/.gitignore index 63ae6684..58273a4e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,8 @@ .vscode .pio platformio-user.ini +<<<<<<< HEAD /src/version.h +======= +src/version.h +>>>>>>> master diff --git a/lib/HanConfigAp/src/Base64.cpp b/lib/HanConfigAp/src/Base64.cpp new file mode 100644 index 00000000..5cf68987 --- /dev/null +++ b/lib/HanConfigAp/src/Base64.cpp @@ -0,0 +1,142 @@ +/* +Copyright (C) 2016 Arturo Guadalupi. All right reserved. + +This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +*/ + +#include "Base64.h" +#include +#if (defined(__AVR__)) +#include +#else +#include +#endif +const char PROGMEM _Base64AlphabetTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + +int Base64Class::encode(char *output, char *input, int inputLength) { + int i = 0, j = 0; + int encodedLength = 0; + unsigned char A3[3]; + unsigned char A4[4]; + + while(inputLength--) { + A3[i++] = *(input++); + if(i == 3) { + fromA3ToA4(A4, A3); + + for(i = 0; i < 4; i++) { + output[encodedLength++] = pgm_read_byte(&_Base64AlphabetTable[A4[i]]); + } + + i = 0; + } + } + + if(i) { + for(j = i; j < 3; j++) { + A3[j] = '\0'; + } + + fromA3ToA4(A4, A3); + + for(j = 0; j < i + 1; j++) { + output[encodedLength++] = pgm_read_byte(&_Base64AlphabetTable[A4[j]]); + } + + while((i++ < 3)) { + output[encodedLength++] = '='; + } + } + output[encodedLength] = '\0'; + return encodedLength; +} + +int Base64Class::decode(char * output, char * input, int inputLength) { + int i = 0, j = 0; + int decodedLength = 0; + unsigned char A3[3]; + unsigned char A4[4]; + + + while (inputLength--) { + if(*input == '=') { + break; + } + + A4[i++] = *(input++); + if (i == 4) { + for (i = 0; i <4; i++) { + A4[i] = lookupTable(A4[i]); + } + + fromA4ToA3(A3,A4); + + for (i = 0; i < 3; i++) { + output[decodedLength++] = A3[i]; + } + i = 0; + } + } + + if (i) { + for (j = i; j < 4; j++) { + A4[j] = '\0'; + } + + for (j = 0; j <4; j++) { + A4[j] = lookupTable(A4[j]); + } + + fromA4ToA3(A3,A4); + + for (j = 0; j < i - 1; j++) { + output[decodedLength++] = A3[j]; + } + } + output[decodedLength] = '\0'; + return decodedLength; +} + +int Base64Class::encodedLength(int plainLength) { + int n = plainLength; + return (n + 2 - ((n + 2) % 3)) / 3 * 4; +} + +int Base64Class::decodedLength(char * input, int inputLength) { + int i = 0; + int numEq = 0; + for(i = inputLength - 1; input[i] == '='; i--) { + numEq++; + } + + return ((6 * inputLength) / 8) - numEq; +} + +//Private utility functions +inline void Base64Class::fromA3ToA4(unsigned char * A4, unsigned char * A3) { + A4[0] = (A3[0] & 0xfc) >> 2; + A4[1] = ((A3[0] & 0x03) << 4) + ((A3[1] & 0xf0) >> 4); + A4[2] = ((A3[1] & 0x0f) << 2) + ((A3[2] & 0xc0) >> 6); + A4[3] = (A3[2] & 0x3f); +} + +inline void Base64Class::fromA4ToA3(unsigned char * A3, unsigned char * A4) { + A3[0] = (A4[0] << 2) + ((A4[1] & 0x30) >> 4); + A3[1] = ((A4[1] & 0xf) << 4) + ((A4[2] & 0x3c) >> 2); + A3[2] = ((A4[2] & 0x3) << 6) + A4[3]; +} + +inline unsigned char Base64Class::lookupTable(char c) { + if(c >='A' && c <='Z') return c - 'A'; + if(c >='a' && c <='z') return c - 71; + if(c >='0' && c <='9') return c + 4; + if(c == '+') return 62; + if(c == '/') return 63; + return -1; +} + +Base64Class Base64; diff --git a/lib/HanConfigAp/src/Base64.h b/lib/HanConfigAp/src/Base64.h new file mode 100644 index 00000000..7330225e --- /dev/null +++ b/lib/HanConfigAp/src/Base64.h @@ -0,0 +1,26 @@ +/* +Copyright (C) 2016 Arturo Guadalupi. All right reserved. + +This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +*/ + +#ifndef _BASE64_H +#define _BASE64_H + +class Base64Class{ + public: + int encode(char *output, char *input, int inputLength); + int decode(char * output, char * input, int inputLength); + int encodedLength(int plainLength); + int decodedLength(char * input, int inputLength); + + private: + inline void fromA3ToA4(unsigned char * A4, unsigned char * A3); + inline void fromA4ToA3(unsigned char * A3, unsigned char * A4); + inline unsigned char lookupTable(char c); +}; +extern Base64Class Base64; + +#endif // _BASE64_H diff --git a/lib/HanConfigAp/src/HanConfigAp.cpp b/lib/HanConfigAp/src/HanConfigAp.cpp index 8cfeef96..9c9fa182 100644 --- a/lib/HanConfigAp/src/HanConfigAp.cpp +++ b/lib/HanConfigAp/src/HanConfigAp.cpp @@ -73,6 +73,189 @@ bool HanConfigAp::loop() { return isActivated; } +<<<<<<< HEAD +======= +/** Handle root or redirect to captive portal */ +void HanConfigAp::handleRoot() { + println("Serving / over http..."); + + configuration *config = new configuration(); + config->load(); + + String html = String((const __FlashStringHelper*) CONFIG_HTML); + + if(config->hasConfig()) { + bool access = !config->isAuth(); + if(config->isAuth() && server.hasHeader("Authorization")) { + String expectedAuth = String(config->authUser) + ":" + String(config->authPass); + + String providedPwd = server.header("Authorization"); + providedPwd.replace("Basic ", ""); + char inputString[providedPwd.length()]; + providedPwd.toCharArray(inputString, providedPwd.length()+1); + + int inputStringLength = sizeof(inputString); + int decodedLength = Base64.decodedLength(inputString, inputStringLength); + char decodedString[decodedLength]; + Base64.decode(decodedString, inputString, inputStringLength); + print("Received auth: "); + println(decodedString); + access = String(decodedString).equals(expectedAuth); + } + + if(!access) { + server.sendHeader("WWW-Authenticate", "Basic realm=\"Secure Area\""); + server.send(401, "text/html", ""); + } else { + server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); + server.sendHeader("Pragma", "no-cache"); + server.sendHeader("Expires", "-1"); + + html.replace("${config.ssid}", config->ssid); + html.replace("${config.ssidPassword}", config->ssidPassword); + switch (config->meterType) { + case 1: + html.replace("${config.meterType0}", ""); + html.replace("${config.meterType1}", "selected"); + html.replace("${config.meterType2}", ""); + html.replace("${config.meterType3}", ""); + break; + case 2: + html.replace("${config.meterType0}", ""); + html.replace("${config.meterType1}", ""); + html.replace("${config.meterType2}", "selected"); + html.replace("${config.meterType3}", ""); + break; + case 3: + html.replace("${config.meterType0}", ""); + html.replace("${config.meterType1}", ""); + html.replace("${config.meterType2}", ""); + html.replace("${config.meterType3}", "selected"); + break; + default: + html.replace("${config.meterType0}", "selected"); + html.replace("${config.meterType1}", ""); + html.replace("${config.meterType2}", ""); + html.replace("${config.meterType3}", ""); + } + html.replace("${config.mqtt}", config->mqtt); + html.replace("${config.mqttPort}", String(config->mqttPort)); + html.replace("${config.mqttClientID}", config->mqttClientID); + html.replace("${config.mqttPublishTopic}", config->mqttPublishTopic); + html.replace("${config.mqttSubscribeTopic}", config->mqttSubscribeTopic); + html.replace("${config.mqttUser}", config->mqttUser); + html.replace("${config.mqttPass}", config->mqttPass); + html.replace("${config.authUser}", config->authUser); + html.replace("${config.authPass}", config->authPass); + } + + } else { + server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); + server.sendHeader("Pragma", "no-cache"); + server.sendHeader("Expires", "-1"); + + html.replace("${config.ssid}", ""); + html.replace("${config.ssidPassword}", ""); + html.replace("${config.meterType0}", "selected"); + html.replace("${config.meterType1}", ""); + html.replace("${config.meterType2}", ""); + html.replace("${config.meterType3}", ""); + html.replace("${config.mqtt}", ""); + html.replace("${config.mqttPort}", "1883"); + html.replace("${config.mqttClientID}", ""); + html.replace("${config.mqttPublishTopic}", ""); + html.replace("${config.mqttSubscribeTopic}", ""); + html.replace("${config.mqttUser}", ""); + html.replace("${config.mqttPass}", ""); + html.replace("${config.authUser}", ""); + html.replace("${config.authPass}", ""); + } + server.send(200, "text/html", html); +} + +void HanConfigAp::handleStyle() { + println("Serving /style.css over http..."); + + String css = String((const __FlashStringHelper*) STYLE_CSS); + + server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); + server.sendHeader("Pragma", "no-cache"); + server.sendHeader("Expires", "-1"); + server.send(200, "text/css", css); +} + +void HanConfigAp::handleSave() { + configuration *config = new configuration(); + + String temp; + + temp = server.arg("ssid"); + config->ssid = new char[temp.length() + 1]; + temp.toCharArray(config->ssid, temp.length() + 1, 0); + + temp = server.arg("ssidPassword"); + config->ssidPassword = new char[temp.length() + 1]; + temp.toCharArray(config->ssidPassword, temp.length() + 1, 0); + + config->meterType = (byte)server.arg("meterType").toInt(); + + temp = server.arg("mqtt"); + config->mqtt = new char[temp.length() + 1]; + temp.toCharArray(config->mqtt, temp.length() + 1, 0); + + config->mqttPort = (int)server.arg("mqttPort").toInt(); + + temp = server.arg("mqttClientID"); + config->mqttClientID = new char[temp.length() + 1]; + temp.toCharArray(config->mqttClientID, temp.length() + 1, 0); + + temp = server.arg("mqttPublishTopic"); + config->mqttPublishTopic = new char[temp.length() + 1]; + temp.toCharArray(config->mqttPublishTopic, temp.length() + 1, 0); + + temp = server.arg("mqttSubscribeTopic"); + config->mqttSubscribeTopic = new char[temp.length() + 1]; + temp.toCharArray(config->mqttSubscribeTopic, temp.length() + 1, 0); + + temp = server.arg("mqttUser"); + config->mqttUser = new char[temp.length() + 1]; + temp.toCharArray(config->mqttUser, temp.length() + 1, 0); + + temp = server.arg("mqttPass"); + config->mqttPass = new char[temp.length() + 1]; + temp.toCharArray(config->mqttPass, temp.length() + 1, 0); + + temp = server.arg("authUser"); + config->authUser = new char[temp.length() + 1]; + temp.toCharArray(config->authUser, temp.length() + 1, 0); + + temp = server.arg("authPass"); + config->authPass = new char[temp.length() + 1]; + temp.toCharArray(config->authPass, temp.length() + 1, 0); + + println("Saving configuration now..."); + + if (HanConfigAp::debugger) config->print(HanConfigAp::debugger); + if (config->save()) + { + println("Successfully saved. Will reboot now."); + String html = "

Successfully Saved!

Device is restarting now...

"; + server.send(200, "text/html", html); +#if defined(ESP8266) + ESP.reset(); +#elif defined(ESP32) + ESP.restart(); +#endif + } + else + { + println("Error saving configuration"); + String html = "

Error saving configuration!

"; + server.send(500, "text/html", html); + } +} + +>>>>>>> master size_t HanConfigAp::print(const char* text) { if (debugger) debugger->print(text);