From 5688f8d67907a0bc01bc60016217c32929eef5a4 Mon Sep 17 00:00:00 2001 From: FCS Date: Sun, 30 Aug 2026 11:28:14 +0200 Subject: [PATCH] Verbesserung Redirect --- lib/webinterface/webinterface.cpp | 80 ++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/lib/webinterface/webinterface.cpp b/lib/webinterface/webinterface.cpp index 1dbe0bc..d7d9f3d 100644 --- a/lib/webinterface/webinterface.cpp +++ b/lib/webinterface/webinterface.cpp @@ -78,6 +78,51 @@ String html_escape(const String &value) return escaped; } +bool is_captive_portal_target(const String &host, const String &uri) +{ + String lowercase_host = host; + lowercase_host.toLowerCase(); + + String lowercase_uri = uri; + lowercase_uri.toLowerCase(); + + static const char *captive_hosts[] = { + "connectivitycheck.gstatic.com", + "clients3.google.com", + "google.com", + "www.google.com", + "msftconnecttest.com", + "msftncsi.com", + "captive.apple.com", + "www.apple.com", + "apple.com", + "connectivitycheck.android.com"}; + + for (const char *candidate : captive_hosts) + { + if (lowercase_host == candidate) + return true; + } + + static const char *captive_paths[] = { + "/generate_204", + "/hotspot-detect.html", + "/library/test/success.html", + "/connecttest.txt", + "/redirect", + "/ncsi.txt", + "/success.txt", + "/wap"}; + + for (const char *candidate : captive_paths) + { + if (lowercase_uri == candidate) + return true; + } + + return false; +} + void redirect_to_root(const String &message, bool success) { String location = String("/?status=") + (success ? "success" : "error") + "&msg=" + url_encode(message); @@ -85,6 +130,21 @@ void redirect_to_root(const String &message, bool success) server.send(303, "text/plain; charset=utf-8", ""); } +void handle_captive_portal_redirect() +{ + String host = server.hostHeader(); + String uri = server.uri(); + + if (is_captive_portal_target(host, uri)) + { + server.sendHeader("Location", "/", true); + server.send(302, "text/plain; charset=utf-8", ""); + return; + } + + server.send(404, "text/plain; charset=utf-8", "Not Found"); +} + void send_page(const String &message = String()) { String page; @@ -174,6 +234,9 @@ void handle_settings() return; } + const bool ap_name_changed = active_settings->access_point_name != updated.access_point_name; + const bool ap_password_changed = active_settings->access_point_password != updated.access_point_password; + active_settings->callsign = updated.callsign; active_settings->access_point_name = updated.access_point_name; active_settings->access_point_password = updated.access_point_password; @@ -186,7 +249,10 @@ void handle_settings() preferences.putInt("min", active_settings->min_speed); preferences.putInt("max", active_settings->max_speed); preferences.putUInt("active_time", active_settings->active_time_seconds); - access_point_restart_pending = true; + + if (ap_name_changed || ap_password_changed) + access_point_restart_pending = true; + redirect_to_root("Einstellungen gespeichert.", true); } @@ -283,10 +349,22 @@ bool webinterface_begin(TelemetrySettings &settings) return false; access_point_started_ms = millis(); + dns_server.start(53, "connectivitycheck.gstatic.com", WiFi.softAPIP()); + dns_server.start(53, "clients3.google.com", WiFi.softAPIP()); + dns_server.start(53, "msftconnecttest.com", WiFi.softAPIP()); + dns_server.start(53, "msftncsi.com", WiFi.softAPIP()); + dns_server.start(53, "captive.apple.com", WiFi.softAPIP()); + dns_server.start(53, "apple.com", WiFi.softAPIP()); dns_server.start(53, "*", WiFi.softAPIP()); server.on("/", HTTP_GET, handle_root); + server.on("/generate_204", HTTP_GET, handle_captive_portal_redirect); + server.on("/hotspot-detect.html", HTTP_GET, handle_captive_portal_redirect); + server.on("/connecttest.txt", HTTP_GET, handle_captive_portal_redirect); + server.on("/ncsi.txt", HTTP_GET, handle_captive_portal_redirect); + server.on("/success.txt", HTTP_GET, handle_captive_portal_redirect); server.on("/settings", HTTP_POST, handle_settings); server.on("/update", HTTP_POST, handle_update_result, handle_update_upload); + server.onNotFound(handle_captive_portal_redirect); server.begin(); Serial.print("Firmware-Version: "); Serial.println(firmware_version_string());