Added nvs dump and dns magic

This commit is contained in:
CommanderRedYT
2021-11-14 03:20:21 +01:00
parent 34135f01b1
commit ebb1141863
8 changed files with 117 additions and 6 deletions

View File

@ -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<makeComponent<MenuItem, StaticText<TEXT_GENERICWIFISETTINGS>, SwitchScreenAction<GenericWifiSettingsMenu>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_STATIONWIFISETTINGS>, SwitchScreenAction<StationWifiSettingsMenu>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_ACCESSPOINTWIFISETTINGS>, SwitchScreenAction<AccessPointWifiSettingsMenu>>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_RESEND_DNS>, ResendDNSRequest>>();
constructMenuItem<makeComponent<MenuItem, StaticText<TEXT_BACK>, SwitchScreenAction<SettingsMenu>, StaticMenuItemIcon<&espgui::icons::back>>>();
}

View File

@ -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;

View File

@ -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;

View File

@ -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 <randomutils.h>
#include <esprandom.h>
#endif
namespace {
std::optional<espchrono::millis_clock::time_point> 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<uint16_t>(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
}
}

View File

@ -43,6 +43,7 @@ struct StringSettings
std::array<ConfiguredOtaServer, 5> 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<typename T>

View File

@ -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

View File

@ -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
})
{

View File

@ -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;