From 47233e5ad688c3462418b24280f759b03999fada Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Wed, 29 Dec 2021 15:12:29 +0100 Subject: [PATCH] Moved dns announce related stuff into newsettings --- config_feedc0de.cmake | 2 +- main/dnsannounce.cpp | 140 ++++++++++++++++++++++-------------------- main/dnsannounce.h | 9 +++ main/globals.cpp | 6 -- main/globals.h | 6 -- main/newsettings.h | 6 +- main/stringsettings.h | 6 -- 7 files changed, 88 insertions(+), 87 deletions(-) diff --git a/config_feedc0de.cmake b/config_feedc0de.cmake index b22ed9c..6b00242 100644 --- a/config_feedc0de.cmake +++ b/config_feedc0de.cmake @@ -96,7 +96,7 @@ set(BOBBYCAR_BUILDFLAGS -DLEDSTRIP_ANIMATION_DEFAULT=0 -DLEDS_PER_METER=144 -DOLD_NVS -# -DFEATURE_DNS_NS + -DFEATURE_DNS_NS # -DSWITCH_BLINK # -DFEATURE_ESPNOW ) diff --git a/main/dnsannounce.cpp b/main/dnsannounce.cpp index 78b99c7..76020cd 100644 --- a/main/dnsannounce.cpp +++ b/main/dnsannounce.cpp @@ -10,80 +10,49 @@ #include "cpputils.h" #include "lwip/dns.h" #include "globals.h" +#include "newsettings.h" + +std::string dns_lastIpAddress_v4; +std::string dns_lastIpAddress_v6; +std::string dns_lastIpAddress_v6_global; void init_dns_announce() { - } void handle_dns_announce() { - 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 (curIpAddress != "0.0.0.0") - { - 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_LOGI("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_LOGI("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 (!configs.dns_announce_enabled.value) + return; - if (const auto result = esp_netif_get_ip6_global(wifi_stack::esp_netifs[ESP_IF_WIFI_STA], &tmpv6addr); result == ESP_OK) + if (wifi_stack::get_sta_status() != wifi_stack::WiFiStaStatus::CONNECTED) + { + dns_lastIpAddress_v4 = "-"; + dns_lastIpAddress_v6 = "-"; + dns_lastIpAddress_v6_global = "-"; + + return; + } + + const auto randDNSName = cpputils::randomNumber(espcpputils::esp_random_device{}); + + 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 (curIpAddress != "0.0.0.0") { - std::string curIpV6Address = wifi_stack::toString(tmpv6addr); - if (dns_lastIpAddress_v6_global != curIpV6Address) + if (dns_lastIpAddress_v4 != curIpAddress) { - dns_lastIpAddress_v6_global = curIpV6Address; - std::replace(curIpV6Address.begin(), curIpV6Address.end(), ':', '-'); + dns_lastIpAddress_v4 = curIpAddress; ip_addr_t tmpIpResolved; - std::string toLookup = fmt::format("{}global__{}.{}.announce6.bobbycar.cloud", randDNSName, curIpV6Address, OTA_USERNAME); + std::string toLookup = fmt::format("{}__{}.{}.announce.bobbycar.cloud", randDNSName, curIpAddress, OTA_USERNAME); ESP_LOGI("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); + 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 = "-"; @@ -91,15 +60,54 @@ void handle_dns_announce() } } } + else + { + ESP_LOGW("BOBBY", "get_ip_info() failed with %.*s", result.error().size(), result.error().data()); + } - EVERY_N_SECONDS( 120 ) { - dns_lastIpAddress_v4 = "-"; - dns_lastIpAddress_v6 = "-"; - dns_lastIpAddress_v6_global = "-"; + 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_LOGI("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("{}global__{}.{}.announce6.bobbycar.cloud", randDNSName, curIpV6Address, OTA_USERNAME); + ESP_LOGI("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 = "-"; + } + } } } - else - { + + EVERY_N_SECONDS( 120 ) { dns_lastIpAddress_v4 = "-"; dns_lastIpAddress_v6 = "-"; dns_lastIpAddress_v6_global = "-"; diff --git a/main/dnsannounce.h b/main/dnsannounce.h index f42b211..96c5ea7 100644 --- a/main/dnsannounce.h +++ b/main/dnsannounce.h @@ -1,4 +1,13 @@ #pragma once +// system includes +#include + +#ifdef FEATURE_DNS_NS +extern std::string dns_lastIpAddress_v4; +extern std::string dns_lastIpAddress_v6; +extern std::string dns_lastIpAddress_v6_global; + void init_dns_announce(); void handle_dns_announce(); +#endif diff --git a/main/globals.cpp b/main/globals.cpp index a90cf92..2ba0529 100644 --- a/main/globals.cpp +++ b/main/globals.cpp @@ -24,12 +24,6 @@ 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 12dd799..21c00db 100644 --- a/main/globals.h +++ b/main/globals.h @@ -48,12 +48,6 @@ extern bool isLocked; #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/newsettings.h b/main/newsettings.h index c85f23f..13a6257 100644 --- a/main/newsettings.h +++ b/main/newsettings.h @@ -111,7 +111,8 @@ public: ConfigWrapper cloudUrl {std::string{}, DoReset, StringOr, "cloudUrl" }; ConfigWrapper udpCloudHost {std::string{}, DoReset, {}, "udpCloudHost" }; ConfigWrapper otaUrl {std::string{}, DoReset, StringOr, "otaUrl" }; - + ConfigWrapper dns_announce_enabled{true, DoReset, {}, "dnsAnnounceEnab" }; + ConfigWrapper dns_announce_key {std::string{}, DoReset, {}, "dnsAnnounceKey" }; ConfigWrapper webserverPassword {std::string{}, DoReset, {}, "websPassword" }; #define NEW_SETTINGS(x) \ @@ -252,7 +253,8 @@ public: x(cloudUrl) \ x(udpCloudHost) \ x(otaUrl) \ - \ + x(dns_announce_enabled) \ + x(dns_announce_key) \ // x(webserverPassword) template diff --git a/main/stringsettings.h b/main/stringsettings.h index 68d5d68..94fce02 100644 --- a/main/stringsettings.h +++ b/main/stringsettings.h @@ -25,9 +25,6 @@ struct StringSettings std::array otaServers; std::string otaServerUrl; #endif -#ifdef FEATURE_DNS_NS - std::string dns_key; -#endif #ifdef FEATURE_OTA std::string otaServerBranch; #endif @@ -64,9 +61,6 @@ void StringSettings::executeForEveryCommonSetting(T &&callable) callable("otaserver", otaServerUrl); callable("otaBranch", otaServerBranch); #endif -#ifdef FEATURE_DNS_NS - callable("dnskey", dns_key); -#endif } template