Verbesserung Redirect

This commit is contained in:
FCS committed 2026-08-30 11:28:14 +02:00
1 parent d8558b330a
commit 5688f8d679
1 file changed
+78
+78
View File
@@ -78,6 +78,51 @@ String html_escape(const String &value)
return escaped; 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) void redirect_to_root(const String &message, bool success)
{ {
String location = String("/?status=") + (success ? "success" : "error") + "&msg=" + url_encode(message); 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", ""); 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()) void send_page(const String &message = String())
{ {
String page; String page;
@@ -174,6 +234,9 @@ void handle_settings()
return; 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->callsign = updated.callsign;
active_settings->access_point_name = updated.access_point_name; active_settings->access_point_name = updated.access_point_name;
active_settings->access_point_password = updated.access_point_password; 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("min", active_settings->min_speed);
preferences.putInt("max", active_settings->max_speed); preferences.putInt("max", active_settings->max_speed);
preferences.putUInt("active_time", active_settings->active_time_seconds); preferences.putUInt("active_time", active_settings->active_time_seconds);
if (ap_name_changed || ap_password_changed)
access_point_restart_pending = true; access_point_restart_pending = true;
redirect_to_root("Einstellungen gespeichert.", true); redirect_to_root("Einstellungen gespeichert.", true);
} }
@@ -283,10 +349,22 @@ bool webinterface_begin(TelemetrySettings &settings)
return false; return false;
access_point_started_ms = millis(); 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()); dns_server.start(53, "*", WiFi.softAPIP());
server.on("/", HTTP_GET, handle_root); 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("/settings", HTTP_POST, handle_settings);
server.on("/update", HTTP_POST, handle_update_result, handle_update_upload); server.on("/update", HTTP_POST, handle_update_result, handle_update_upload);
server.onNotFound(handle_captive_portal_redirect);
server.begin(); server.begin();
Serial.print("Firmware-Version: "); Serial.print("Firmware-Version: ");
Serial.println(firmware_version_string()); Serial.println(firmware_version_string());