Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
 
2
// Captive Portal - what is needed
3
// - DNS Server + Catch-all Handler class
4
// - OR: DNS-Server + device specific request handlers (/generate_204, /success.html, etc.)
5
// -> these seem to work with recent stock Android and iPhones
6
//
7
// - IP-config seems to be necessary for all Samsung phones (since they use hard-coded
8
//   DNS for connectivitycheck.gstatic.com)
9
 
10
#ifdef ESP32
11
#include <WiFi.h>
12
#elif defined(ESP8266)
13
#include <ESP8266WiFi.h>
14
#else
15
#error "Captive portal needs ESP32 or ESP8266 platform"
16
#endif
17
#include <DNSServer.h>
18
#include "ESPAsyncWebServer.h"
19
#include "AsyncFsWebServer.h"
20
 
21
 
22
// Inspiration: https://github.com/andig/vzero/blob/master/src/webserver.cpp
23
class CaptiveRequestHandler : public AsyncWebHandler
24
{
25
public:
26
    explicit CaptiveRequestHandler(String redirectTargetURL) :
27
        targetURL(buildTargetURL(redirectTargetURL))
28
    {
29
    }
30
    virtual ~CaptiveRequestHandler() {}
31
 
32
    const String targetURL;
33
 
34
    static String buildTargetURL(const String& redirect) {
35
        String s;
36
        s.reserve(64);
37
        s += "http://";
38
        s += WiFi.softAPIP().toString();
39
        s += redirect;
40
        return s;
41
    }
42
 
43
    bool canHandle(AsyncWebServerRequest *request) const override {
44
        if (request->host() != WiFi.softAPIP().toString())
45
            return true;
46
        else
47
            return false;
48
    }
49
 
50
    void handleRequest(AsyncWebServerRequest *request) override
51
    {
52
        request->redirect(targetURL);
53
        log_info("Captive handler triggered. Requested %s%s -> redirecting to %s", request->host().c_str(), request->url().c_str(), targetURL.c_str());
54
    }
55
};