From a3e28d3c6677b536e5a457acc2a02b7db8a0696f Mon Sep 17 00:00:00 2001 From: cschwinne Date: Thu, 5 Sep 2019 22:45:59 +0200 Subject: [PATCH] First version of captive portal --- wled00/wled05_init.ino | 1 + wled00/wled18_server.ino | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/wled00/wled05_init.ino b/wled00/wled05_init.ino index 88d1904f..a7050cfe 100644 --- a/wled00/wled05_init.ino +++ b/wled00/wled05_init.ino @@ -180,6 +180,7 @@ void beginStrip() void initAP(){ bool set = apSSID[0]; if (!set) strcpy(apSSID,"WLED-AP"); + WiFi.softAPConfig(IPAddress(4, 3, 2, 1), IPAddress(4, 3, 2, 1), IPAddress(255,255,255,0)); WiFi.softAP(apSSID, apPass, apChannel, apHide); if (!set) apSSID[0] = 0; } diff --git a/wled00/wled18_server.ino b/wled00/wled18_server.ino index b9ceb9e8..57f8866e 100644 --- a/wled00/wled18_server.ino +++ b/wled00/wled18_server.ino @@ -2,6 +2,33 @@ * Server page definitions */ +//Is this an IP? +bool isIp(String str) { + for (size_t i = 0; i < str.length(); i++) { + int c = str.charAt(i); + if (c != '.' && (c < '0' || c > '9')) { + return false; + } + } + return true; +} + +bool captivePortal(AsyncWebServerRequest *request) +{ + String hostH; + if (!request->hasHeader("Host")) return false; + hostH = request->getHeader("Host")->value(); + + if (!isIp(hostH) && hostH.indexOf("wled.me") < 0 && hostH.indexOf(cmDNS) < 0) { + DEBUG_PRINTLN("Captive portal"); + AsyncWebServerResponse *response = request->beginResponse(302); + response->addHeader("Location", "http://local.wled.me"); + request->send(response); + return true; + } + return false; +} + void initServer() { //CORS compatiblity @@ -161,13 +188,20 @@ void initServer() } server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ + if (captivePortal(request)) return; serveIndexOrWelcome(request); }); + + /*server.on("/generate_204", HTTP_GET, [](AsyncWebServerRequest *request){ + //if (captivePortal(request)) return; + serveIndexOrWelcome(request); + });*/ //called when the url is not defined here, ajax-in; get-settings server.onNotFound([](AsyncWebServerRequest *request){ DEBUG_PRINTLN("Not-Found HTTP call:"); DEBUG_PRINTLN("URI: " + request->url()); + if (captivePortal(request)) return; //make API CORS compatible if (request->method() == HTTP_OPTIONS)