From ebb1141863a0bb931a70755fad6ee52112858521 Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Sun, 14 Nov 2021 03:20:21 +0100 Subject: [PATCH] Added nvs dump and dns magic --- main/displays/menus/wifisettingsmenu.cpp | 8 +++ main/globals.cpp | 6 ++ main/globals.h | 6 ++ main/main.cpp | 90 ++++++++++++++++++++++++ main/stringsettings.h | 2 + main/texts.h | 1 + main/webserver.h | 2 +- main/webserver_dumpnvs.h | 8 +-- 8 files changed, 117 insertions(+), 6 deletions(-) diff --git a/main/displays/menus/wifisettingsmenu.cpp b/main/displays/menus/wifisettingsmenu.cpp index 613de4b..09b5c27 100644 --- a/main/displays/menus/wifisettingsmenu.cpp +++ b/main/displays/menus/wifisettingsmenu.cpp @@ -10,14 +10,22 @@ #include "displays/menus/stationwifisettingsmenu.h" #include "displays/menus/accesspointwifisettingsmenu.h" #include "displays/menus/settingsmenu.h" +#include "globals.h" using namespace espgui; +class ResendDNSRequest : public virtual ActionInterface +{ +public: + void triggered() override { dns_lastIpAddress_v4 = "---"; dns_lastIpAddress_v6 = "---"; dns_lastIpAddress_v6_global = "---"; } +}; + WifiSettingsMenu::WifiSettingsMenu() { constructMenuItem, SwitchScreenAction>>(); constructMenuItem, SwitchScreenAction>>(); constructMenuItem, SwitchScreenAction>>(); + constructMenuItem, ResendDNSRequest>>(); constructMenuItem, SwitchScreenAction, StaticMenuItemIcon<&espgui::icons::back>>>(); } diff --git a/main/globals.cpp b/main/globals.cpp index 7907e66..40e4ec5 100644 --- a/main/globals.cpp +++ b/main/globals.cpp @@ -25,6 +25,12 @@ bool simplified = #endif ; +#ifdef FEATURE_DNS_NS +std::string dns_lastIpAddress_v4 = ""; +std::string dns_lastIpAddress_v6 = ""; +std::string dns_lastIpAddress_v6_global = ""; +#endif + Settings settings; StringSettings stringSettings; SettingsPersister settingsPersister; diff --git a/main/globals.h b/main/globals.h index 5c251a9..69d6512 100644 --- a/main/globals.h +++ b/main/globals.h @@ -48,6 +48,12 @@ extern char deviceName[32]; #include GLOBALS_PLUGIN #endif +#ifdef FEATURE_DNS_NS +extern std::string dns_lastIpAddress_v4; +extern std::string dns_lastIpAddress_v6; +extern std::string dns_lastIpAddress_v6_global; +#endif + extern bool simplified; extern Settings settings; diff --git a/main/main.cpp b/main/main.cpp index 2199d4f..ec91246 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -71,6 +71,11 @@ using namespace std::chrono_literals; #include "modes/defaultmode.h" #include "displays/statusdisplay.h" #include "displays/calibratedisplay.h" +#ifdef FEATURE_DNS_NS +#include "lwip/dns.h" +#include +#include +#endif namespace { std::optional lastWifiUpdate; @@ -433,6 +438,91 @@ extern "C" void app_main() lastLedstripUpdate = now; } +#endif +#ifdef FEATURE_DNS_NS + const auto staStatus = wifi_stack::get_sta_status(); + const auto randDNSName = cpputils::randomNumber(espcpputils::esp_random_device{}); + if (staStatus == wifi_stack::WiFiStaStatus::CONNECTED) + { + EVERY_N_SECONDS ( 2 ) { + // Get IPv4 + if (const auto result = wifi_stack::get_ip_info(TCPIP_ADAPTER_IF_STA); result) + { + std::string curIpAddress = wifi_stack::toString(result->ip); + if (dns_lastIpAddress_v4 != curIpAddress) + { + dns_lastIpAddress_v4 = curIpAddress; + ip_addr_t tmpIpResolved; + std::string toLookup = fmt::format("{}__{}.announce.{}.bobbycar.cloud", randDNSName, curIpAddress, OTA_USERNAME); + ESP_LOGW("BOBBY", "Trying to look up %s", toLookup.c_str()); + if (const auto err = dns_gethostbyname(toLookup.c_str(), &tmpIpResolved, NULL, NULL); err != ERR_OK && err != ERR_INPROGRESS) + { + ESP_LOGW("BOBBY", "There is a error in the matrix (dns ipv4 lookup failed) -> %d", err); + dns_lastIpAddress_v4 = "-"; + dns_lastIpAddress_v6 = "-"; + dns_lastIpAddress_v6_global = "-"; + } + } + } + else + { + ESP_LOGW("BOBBY", "get_ip_info() failed with %.*s", result.error().size(), result.error().data()); + } + + esp_ip6_addr_t tmpv6addr; + if (const auto result = esp_netif_get_ip6_linklocal(wifi_stack::esp_netifs[ESP_IF_WIFI_STA], &tmpv6addr); result == ESP_OK) + { + std::string curIpV6Address = wifi_stack::toString(tmpv6addr); + std::replace(curIpV6Address.begin(), curIpV6Address.end(), ':', '-'); + if (dns_lastIpAddress_v6 != curIpV6Address) + { + dns_lastIpAddress_v6 = curIpV6Address; + ip_addr_t tmpIpResolved; + std::string toLookup = fmt::format("{}__{}.announce6.{}.bobbycar.cloud", randDNSName, curIpV6Address, OTA_USERNAME); + ESP_LOGW("BOBBY", "Trying to look up %s", toLookup.c_str()); + if (const auto err = dns_gethostbyname(toLookup.c_str(), &tmpIpResolved, NULL, NULL); err != ERR_OK && err != ERR_INPROGRESS) + { + ESP_LOGW("BOBBY", "There is a error in the matrix (dns ipv6 local lookup failed) -> %d", err); + dns_lastIpAddress_v4 = "-"; + dns_lastIpAddress_v6 = "-"; + dns_lastIpAddress_v6_global = "-"; + } + } + } + + if (const auto result = esp_netif_get_ip6_global(wifi_stack::esp_netifs[ESP_IF_WIFI_STA], &tmpv6addr); result == ESP_OK) + { + std::string curIpV6Address = wifi_stack::toString(tmpv6addr); + if (dns_lastIpAddress_v6_global != curIpV6Address) + { + dns_lastIpAddress_v6_global = curIpV6Address; + std::replace(curIpV6Address.begin(), curIpV6Address.end(), ':', '-'); + ip_addr_t tmpIpResolved; + std::string toLookup = fmt::format("{}__{}.announce6.{}.bobbycar.cloud", randDNSName, curIpV6Address, OTA_USERNAME); + ESP_LOGW("BOBBY", "Trying to look up %s", toLookup.c_str()); + if (const auto err = dns_gethostbyname(toLookup.c_str(), &tmpIpResolved, NULL, NULL); err != ERR_OK && err != ERR_INPROGRESS) + { + ESP_LOGW("BOBBY", "There is a error in the matrix (dns ipv6 global lookup failed) -> %d", err); + dns_lastIpAddress_v4 = "-"; + dns_lastIpAddress_v6 = "-"; + dns_lastIpAddress_v6_global = "-"; + } + } + } + } + + EVERY_N_SECONDS( 120 ) { + dns_lastIpAddress_v4 = "-"; + dns_lastIpAddress_v6 = "-"; + dns_lastIpAddress_v6_global = "-"; + } + } + else + { + dns_lastIpAddress_v4 = "-"; + dns_lastIpAddress_v6 = "-"; + dns_lastIpAddress_v6_global = "-"; + } #endif } } diff --git a/main/stringsettings.h b/main/stringsettings.h index 82fac93..a75a91e 100644 --- a/main/stringsettings.h +++ b/main/stringsettings.h @@ -43,6 +43,7 @@ struct StringSettings std::array otaServers; std::string otaServerUrl; + std::string dns_key; #endif }; @@ -108,6 +109,7 @@ void StringSettings::executeForEveryCommonSetting(T &&callable) callable("otaserver", otaServerUrl); #endif + callable("dnskey", dns_key); } template diff --git a/main/texts.h b/main/texts.h index 07e7921..feaf2d3 100644 --- a/main/texts.h +++ b/main/texts.h @@ -7,6 +7,7 @@ constexpr char TEXT_BACK[] = "Back"; //AccessPointWifiSettingsMenu constexpr char TEXT_ACCESSPOINTWIFISETTINGS[] = "Access Point WiFi settings"; constexpr char TEXT_WIFIAPENABLED[] = "AP enabled"; +constexpr char TEXT_RESEND_DNS[] = "Resend DNS"; //constexpr char TEXT_BACK[] = "Back"; #ifdef TEXTS_PLUGIN diff --git a/main/webserver.h b/main/webserver.h index d1328ee..954f187 100644 --- a/main/webserver.h +++ b/main/webserver.h @@ -69,7 +69,7 @@ void initWebserver() httpd_uri_t { .uri = "/stringSettings", .method = HTTP_GET, .handler = webserver_stringSettings_handler, .user_ctx = NULL }, httpd_uri_t { .uri = "/saveStringSettings", .method = HTTP_GET, .handler = webserver_saveStringSettings_handler, .user_ctx = NULL }, #ifdef OLD_NVS - // httpd_uri_t { .uri = "/dumpnvs", .method = HTTP_GET, .handler = webserver_dump_nvs_handler, .user_ctx = NULL }, + httpd_uri_t { .uri = "/dumpnvs", .method = HTTP_GET, .handler = webserver_dump_nvs_handler, .user_ctx = NULL }, #endif }) { diff --git a/main/webserver_dumpnvs.h b/main/webserver_dumpnvs.h index 6d6643c..32c32a9 100644 --- a/main/webserver_dumpnvs.h +++ b/main/webserver_dumpnvs.h @@ -96,6 +96,7 @@ esp_err_t webserver_dump_nvs_handler(httpd_req_t *req) } std::string body; + body.reserve(1024*60); const auto profile = settingsPersister.currentlyOpenProfileIndex(); const auto switchBackProfile = profile ? int(*profile) : 0; @@ -190,18 +191,15 @@ esp_err_t webserver_dump_nvs_handler(httpd_req_t *req) } }); - // ToDo: Do something that it does not crash (std::string probabbly gets to big) - // After that, change "profile_num < 1" to "profile_num < 4", uncomment the "switchProfile(profile_num);" and reenable the url in webserver.h - // Profile settings - for (uint8_t profile_num = 0; profile_num < 1; profile_num++) { + for (uint8_t profile_num = 0; profile_num < 4; profile_num++) { #ifdef SIMPLIFIED_TRIGGER_TRIGGERONPRESET if (profile_num == SIMPLIFIED_TRIGGER_TRIGGERONPRESET) { continue; } #endif - // switchProfile(profile_num); + switchProfile(profile_num); const auto cur_profile = settingsPersister.currentlyOpenProfileIndex(); const auto cur_profile_int = profile ? int(*cur_profile) : 0;