From ee95931f00bf6c7ecf08d19f8461c21ef998896f Mon Sep 17 00:00:00 2001 From: richonguzman Date: Tue, 13 Aug 2024 14:48:08 -0400 Subject: [PATCH] web user and password --- data/igate_conf.json | 4 +++ data_embed/index.html | 69 +++++++++++++++++++++++++++++++++++++++++++ data_embed/script.js | 35 ++++++++++++++++++++++ src/configuration.cpp | 16 ++++++++-- src/configuration.h | 8 +++++ src/web_utils.cpp | 12 ++++++++ src/wifi_utils.cpp | 4 ++- 7 files changed, 145 insertions(+), 3 deletions(-) diff --git a/data/igate_conf.json b/data/igate_conf.json index e1c8243d..8917186a 100644 --- a/data/igate_conf.json +++ b/data/igate_conf.json @@ -75,6 +75,10 @@ "username": "", "password": "" }, + "webadmin": { + "username": "admin", + "password": "" + }, "other": { "rememberStationTime": 30, "lowPowerMode": false, diff --git a/data_embed/index.html b/data_embed/index.html index 9e6be99b..bdf0760c 100644 --- a/data_embed/index.html +++ b/data_embed/index.html @@ -1541,6 +1541,75 @@

+
+
+
+ + + + Admin +
+ Set your username and password to allow + access to the web interface. +
+
+
+
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+
+
diff --git a/data_embed/script.js b/data_embed/script.js index 6876fbc3..093d1ac9 100644 --- a/data_embed/script.js +++ b/data_embed/script.js @@ -201,6 +201,11 @@ function loadSettings(settings) { document.getElementById("ota.username").value = settings.ota.username; document.getElementById("ota.password").value = settings.ota.password; + // Webadmin + document.getElementById("webadmin.active").checked = settings.webadmin.active; + document.getElementById("webadmin.username").value = settings.webadmin.username; + document.getElementById("webadmin.password").value = settings.webadmin.password; + // Experimental document.getElementById("other.backupDigiMode").checked = settings.other.backupDigiMode; @@ -276,6 +281,20 @@ function toggleFields() { externalVoltagePinInput.disabled = !sendExternalVoltageCheckbox.checked; voltageDividerR1.disabled = !sendExternalVoltageCheckbox.checked; voltageDividerR2.disabled = !sendExternalVoltageCheckbox.checked; + + const WebadminCheckbox = document.querySelector( + 'input[name="webadmin.active"]' + ); + + const WebadminUsername = document.querySelector( + 'input[name="webadmin.username"]' + ); + + const WebadminPassword = document.querySelector( + 'input[name="webadmin.password"]' + ); + WebadminUsername.disabled = !WebadminCheckbox.checked; + WebadminPassword.disabled = !WebadminCheckbox.checked; } const sendExternalVoltageCheckbox = document.querySelector( @@ -299,6 +318,22 @@ sendExternalVoltageCheckbox.addEventListener("change", function () { voltageDividerR2.disabled = !this.checked; }); +const WebadminCheckbox = document.querySelector( + 'input[name="webadmin.active"]' +); + +const WebadminUsername = document.querySelector( + 'input[name="webadmin.username"]' +); + +const WebadminPassword = document.querySelector( + 'input[name="webadmin.password"]' +); +WebadminCheckbox.addEventListener("change", function () { + WebadminUsername.disabled = !this.checked; + WebadminPassword.disabled = !this.checked; +}); + document.querySelector(".new button").addEventListener("click", function () { const networksContainer = document.querySelector(".list-networks"); diff --git a/src/configuration.cpp b/src/configuration.cpp index 607d86b2..83d8441e 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -97,12 +97,16 @@ void Configuration::writeFile() { data["other"]["rememberStationTime"] = rememberStationTime; - data["other"]["backupDigiMode"] = backupDigiMode; + data["other"]["backupDigiMode"] = backupDigiMode; data["other"]["lowPowerMode"] = lowPowerMode; data["other"]["lowVoltageCutOff"] = lowVoltageCutOff; - data["personalNote"] = personalNote; + data["personalNote"] = personalNote; + + data["webadmin"]["active"] = webadmin.active; + data["webadmin"]["username"] = webadmin.username; + data["webadmin"]["password"] = webadmin.password; serializeJson(data, configFile); @@ -188,6 +192,10 @@ bool Configuration::readFile() { personalNote = data["personalNote"].as(); + webadmin.active = data["webadmin"]["active"].as(); + webadmin.username = data["webadmin"]["username"].as(); + webadmin.password = data["webadmin"]["password"].as(); + int stationMode = data["stationMode"].as(); // deprecated but need to specify config version if (stationMode == 0) { @@ -362,6 +370,10 @@ void Configuration::init() { personalNote = ""; + webadmin.active = false; + webadmin.username = "admin"; + webadmin.password = ""; + Serial.println("All is Written!"); } diff --git a/src/configuration.h b/src/configuration.h index dde2794b..551e03a0 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -106,6 +106,13 @@ class OTA { String password; }; +class WEBADMIN { +public: + bool active; + String username; + String password; +}; + class Configuration { public: bool reload; // ? @@ -129,6 +136,7 @@ class Configuration { SYSLOG syslog; TNC tnc; OTA ota; + WEBADMIN webadmin; void init(); void writeFile(); diff --git a/src/web_utils.cpp b/src/web_utils.cpp index cff2b5cf..96b86e37 100644 --- a/src/web_utils.cpp +++ b/src/web_utils.cpp @@ -49,6 +49,9 @@ namespace WEB_Utils { } void handleHome(AsyncWebServerRequest *request) { + if(Config.webadmin.active && !request->authenticate(Config.webadmin.username.c_str(), Config.webadmin.password.c_str())) + return request->requestAuthentication(); + AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", (const uint8_t*)web_index_html, web_index_html_len); response->addHeader("Content-Encoding", "gzip"); request->send(response); @@ -61,6 +64,9 @@ namespace WEB_Utils { } void handleReadConfiguration(AsyncWebServerRequest *request) { + if(Config.webadmin.active && !request->authenticate(Config.webadmin.username.c_str(), Config.webadmin.password.c_str())) + return request->requestAuthentication(); + File file = SPIFFS.open("/igate_conf.json"); String fileContent; @@ -193,6 +199,12 @@ namespace WEB_Utils { Config.personalNote = request->getParam("personalNote", true)->value(); + Config.webadmin.active = request->hasParam("webadmin.active", true); + if (Config.webadmin.active) { + Config.webadmin.username = request->getParam("webadmin.username", true)->value(); + Config.webadmin.password = request->getParam("webadmin.password", true)->value(); + } + Config.writeFile(); AsyncWebServerResponse *response = request->beginResponse(302, "text/html", ""); diff --git a/src/wifi_utils.cpp b/src/wifi_utils.cpp index 2cad7d2f..e6be2749 100644 --- a/src/wifi_utils.cpp +++ b/src/wifi_utils.cpp @@ -110,7 +110,9 @@ namespace WIFI_Utils { #endif if (WiFi.status() == WL_CONNECTED) { Serial.print("Connected as "); - Serial.println(WiFi.localIP()); + Serial.print(WiFi.localIP()); + Serial.print(" / MAC Address: "); + Serial.println(WiFi.macAddress()); show_display("", " Connected!!", "" , " loading ...", 1000); } else if (WiFi.status() != WL_CONNECTED) { startAP = true;