Added dns forward to configuration web page.

This commit is contained in:
Holger Weber
2026-02-22 23:01:38 +01:00
parent 870456c17b
commit f862672a81

View File

@@ -1,14 +1,24 @@
#include "EggDuino.h" #include "EggDuino.h"
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <DNSServer.h>
#include <string.h> #include <string.h>
namespace namespace
{ {
const char *kConfigPath = "/config.json"; const char *kConfigPath = "/config.json";
const size_t kConfigJsonCapacity = 4096; const size_t kConfigJsonCapacity = 4096;
const byte kDnsPort = 53;
WebServer server(80); WebServer server(80);
DNSServer dnsServer;
bool configStoreReady = false; bool configStoreReady = false;
bool apModeActive = false;
void redirectToRoot()
{
server.sendHeader("Location", String("http://") + WiFi.softAPIP().toString() + "/", true);
server.send(302, "text/plain", "");
}
bool isIntType(const ConfigParameter &param) bool isIntType(const ConfigParameter &param)
{ {
@@ -253,6 +263,11 @@ async function pollLogs() {
void handleNotFound() void handleNotFound()
{ {
if (apModeActive)
{
redirectToRoot();
return;
}
if (server.uri().startsWith("/api/")) if (server.uri().startsWith("/api/"))
{ {
server.send(404, "text/plain", "API endpoint not found"); server.send(404, "text/plain", "API endpoint not found");
@@ -483,6 +498,8 @@ void startWebInterface()
{ {
initConfigStore(); initConfigStore();
bool staConnected = false; bool staConnected = false;
apModeActive = false;
dnsServer.stop();
if (!g_sWifiSsid.isEmpty()) if (!g_sWifiSsid.isEmpty())
{ {
@@ -510,6 +527,8 @@ void startWebInterface()
WiFi.mode(WIFI_AP); WiFi.mode(WIFI_AP);
if (WiFi.softAP("EggDuino")) if (WiFi.softAP("EggDuino"))
{ {
apModeActive = true;
dnsServer.start(kDnsPort, "*", WiFi.softAPIP());
Serial.println(String("AP aktiv: EggDuino / http://") + WiFi.softAPIP().toString()); Serial.println(String("AP aktiv: EggDuino / http://") + WiFi.softAPIP().toString());
} }
else else
@@ -522,11 +541,21 @@ void startWebInterface()
server.on("/api/config", HTTP_GET, handleGetConfig); server.on("/api/config", HTTP_GET, handleGetConfig);
server.on("/api/config", HTTP_POST, handlePostConfig); server.on("/api/config", HTTP_POST, handlePostConfig);
server.on("/api/logs", HTTP_GET, handleGetLogs); server.on("/api/logs", HTTP_GET, handleGetLogs);
server.on("/generate_204", HTTP_GET, redirectToRoot);
server.on("/gen_204", HTTP_GET, redirectToRoot);
server.on("/hotspot-detect.html", HTTP_GET, redirectToRoot);
server.on("/connecttest.txt", HTTP_GET, redirectToRoot);
server.on("/ncsi.txt", HTTP_GET, redirectToRoot);
server.on("/fwlink", HTTP_GET, redirectToRoot);
server.onNotFound(handleNotFound); server.onNotFound(handleNotFound);
server.begin(); server.begin();
} }
void handleWebInterface() void handleWebInterface()
{ {
if (apModeActive)
{
dnsServer.processNextRequest();
}
server.handleClient(); server.handleClient();
} }