|
| 1 | +#include <ESP8266WiFi.h> |
| 2 | +#include <WiFiClient.h> |
| 3 | +#include <ESP8266WebServer.h> |
| 4 | + |
| 5 | +const char* ssid = "........"; |
| 6 | +const char* password = "........"; |
| 7 | + |
| 8 | +ESP8266WebServer server(80); |
| 9 | + |
| 10 | +//Check if header is present and correct |
| 11 | +bool is_authentified(){ |
| 12 | + Serial.println("Enter is_authentified"); |
| 13 | + if (server.hasHeader("Cookie")){ |
| 14 | + Serial.print("Found cookie: "); |
| 15 | + String cookie = server.header("Cookie"); |
| 16 | + Serial.println(cookie); |
| 17 | + if (cookie.indexOf("ESPSESSIONID=1") != -1) { |
| 18 | + Serial.println("Authentification Successful"); |
| 19 | + return true; |
| 20 | + } |
| 21 | + } |
| 22 | + Serial.println("Authentification Failed"); |
| 23 | + return false; |
| 24 | +} |
| 25 | + |
| 26 | +//login page, also called for disconnect |
| 27 | +void handleLogin(){ |
| 28 | + String msg; |
| 29 | + if (server.hasHeader("Cookie")){ |
| 30 | + Serial.print("Found cookie: "); |
| 31 | + String cookie = server.header("Cookie"); |
| 32 | + Serial.println(cookie); |
| 33 | + } |
| 34 | + if (server.hasArg("DISCONNECT")){ |
| 35 | + Serial.println("Disconnection"); |
| 36 | + String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=0\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n"; |
| 37 | + server.sendContent(header); |
| 38 | + return; |
| 39 | + } |
| 40 | + if (server.hasArg("USERNAME") && server.hasArg("PASSWORD")){ |
| 41 | + if (server.arg("USERNAME") == "admin" && server.arg("PASSWORD") == "admin" ){ |
| 42 | + String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=1\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n"; |
| 43 | + server.sendContent(header); |
| 44 | + Serial.println("Log in Successful"); |
| 45 | + return; |
| 46 | + } |
| 47 | + msg = "Wrong username/password! try again."; |
| 48 | + Serial.println("Log in Failed"); |
| 49 | + } |
| 50 | + String content = "<html><body><form action='/login' method='POST'>To log in, please use : admin/admin<br>"; |
| 51 | + content += "User:<input type='text' name='USERNAME' placeholder='user name'><br>"; |
| 52 | + content += "Password:<input type='password' name='PASSWORD' placeholder='password'><br>"; |
| 53 | + content += "<input type='submit' name='SUBMIT' value='Submit'></form>" + msg + "<br>"; |
| 54 | + content += "You also can go <a href='/inline'>here</a></body></html>"; |
| 55 | + server.send(200, "text/html", content); |
| 56 | +} |
| 57 | + |
| 58 | +//root page can be accessed only if authentification is ok |
| 59 | +void handleRoot(){ |
| 60 | + Serial.println("Enter handleRoot"); |
| 61 | + String header; |
| 62 | + if (!is_authentified()){ |
| 63 | + String header = "HTTP/1.1 301 OK\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n"; |
| 64 | + server.sendContent(header); |
| 65 | + return; |
| 66 | + } |
| 67 | + String content = "<html><body><H2>hello, you successfully connected to esp8266!</H2><br>"; |
| 68 | + if (server.hasHeader("User-Agent")){ |
| 69 | + content += "the user agent used is : " + server.header("User-Agent") + "<br><br>"; |
| 70 | + } |
| 71 | + content += "You can access this page until you <a href=\"/login?DISCONNECT=YES\">disconnect</a></body></html>"; |
| 72 | + server.send(200, "text/html", content); |
| 73 | +} |
| 74 | + |
| 75 | +//no need authentification |
| 76 | +void handleNotFound(){ |
| 77 | + String message = "File Not Found\n\n"; |
| 78 | + message += "URI: "; |
| 79 | + message += server.uri(); |
| 80 | + message += "\nMethod: "; |
| 81 | + message += (server.method() == HTTP_GET)?"GET":"POST"; |
| 82 | + message += "\nArguments: "; |
| 83 | + message += server.args(); |
| 84 | + message += "\n"; |
| 85 | + for (uint8_t i=0; i<server.args(); i++){ |
| 86 | + message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; |
| 87 | + } |
| 88 | + server.send(404, "text/plain", message); |
| 89 | +} |
| 90 | + |
| 91 | +void setup(void){ |
| 92 | + Serial.begin(115200); |
| 93 | + WiFi.begin(ssid, password); |
| 94 | + Serial.println(""); |
| 95 | + |
| 96 | + // Wait for connection |
| 97 | + while (WiFi.status() != WL_CONNECTED) { |
| 98 | + delay(500); |
| 99 | + Serial.print("."); |
| 100 | + } |
| 101 | + Serial.println(""); |
| 102 | + Serial.print("Connected to "); |
| 103 | + Serial.println(ssid); |
| 104 | + Serial.print("IP address: "); |
| 105 | + Serial.println(WiFi.localIP()); |
| 106 | + |
| 107 | + |
| 108 | + server.on("/", handleRoot); |
| 109 | + server.on("/login", handleLogin); |
| 110 | + server.on("/inline", [](){ |
| 111 | + server.send(200, "text/plain", "this works without need of authentification"); |
| 112 | + }); |
| 113 | + |
| 114 | + server.onNotFound(handleNotFound); |
| 115 | + //here the list of headers to be recorded |
| 116 | + const char * headerkeys[] = {"User-Agent","Cookie"} ; |
| 117 | + size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*); |
| 118 | + //ask server to track these headers |
| 119 | + server.collectHeaders(headerkeys, headerkeyssize ); |
| 120 | + server.begin(); |
| 121 | + Serial.println("HTTP server started"); |
| 122 | +} |
| 123 | + |
| 124 | +void loop(void){ |
| 125 | + server.handleClient(); |
| 126 | +} |
0 commit comments