JayV
Published

Captive Portal for WiFi AP

WiFi access points (Espressif devices) are very cool, but how to connect without checking the IP address via serial? Captive portal.

IntermediateWork in progress13,010
Captive Portal for WiFi AP

Things used in this project

Hardware components

Arduino MKR WiFi 1010
Arduino MKR WiFi 1010
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

AP_CaptivePortal.ino

C/C++
Captive Portal Demo
/*
  AccesPoint Server with Captive Portal

  John V - Jan 2019
  Version 0.1.0 - WIP
  Build for MKR Wifi 1000 with uBlox Nina W10
  Build from the basic AP Webserver demo from Arduino website.

  Setting DNS and Gateway IUP to own AP-IPaddress
  Checking first 16 DNS requests via UDP port 53
  Send Respond on DNS requests with redirected IP-adress of own AP : your landing page
  WebServer is a simple main page with a Text-box (php-POST), entered answer is read back from the received data.
  <yourIP>/SECRETS sends the list of answers to the serial - just a test

  Tested with Samsung S7 S5mini Phone with Firefox/Chrome.
  Win10 and iExplore / Chrome gives a problem  - WIP to test others, especialy on HTML replies for connectivity check ???? - how to handle these beyond the CaptivePortal check ?
  
  Serial Debug info is everywhere- feel free to cut ut out to make a leaner code
  btw ... When connecting your phone, you wil see a spam of DNS request data comming in :), its limited to 16 DNS requests, - changed in #define DNSMAXREQUESTS XX 

 */

#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>
#include "arduino_secrets.h" 

char ssid[] = SECRET_SSID;     // your network SSID (name)
char pass[] = SECRET_PASS;     // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;              // your network key Index number (needed only for WEP)

// Define Wifi Client parameters
         
IPAddress gwip(172,128, 128, 1);         // GW fixed IP adress
IPAddress apip(172,128, 128, 100);         // AP fixed IP adress
uint8_t apChannel = 2;                  // AP wifi channel
String answerLine = "";                   // make a String to hold incoming php-data from the client
String answerList1[32];                   // store of answerList SSID
String answerList2[32];                   // store of answerList answers
int answerCounter =0;                     

int status = WL_IDLE_STATUS;
WiFiServer server(80);

// Define UDP settings for DNS 
#define UDP_PACKET_SIZE 128           // MAX UDP packaet size = 512
#define DNSHEADER_SIZE 12             // DNS Header
#define DNSANSWER_SIZE 16             // DNS Answer = standard set with Packet Compression
#define DNSMAXREQUESTS 16             // trigger first DNS requests, to redirect to own web-page
byte packetBuffer[ UDP_PACKET_SIZE];  // buffer to hold incoming and outgoing packets
byte dnsReplyHeader[DNSHEADER_SIZE] = { 
  0x00,0x00,   // ID, to be filled in #offset 0
  0x81,0x80,   // answer header Codes
  0x00,0x01,   //QDCOUNT = 1 question
  0x00,0x01,   //ANCOUNT = 1 answer
  0x00,0x00,   //NSCOUNT / ignore
  0x00,0x00    //ARCOUNT / ignore
  };
byte dnsReplyAnswer[DNSANSWER_SIZE] = {   
  0xc0,0x0c,  // pointer to pos 12 : NAME Labels
  0x00,0x01,  // TYPE
  0x00,0x01,  // CLASS
  0x00,0x00,  // TTL
  0x00,0x3c,  // TLL 1 hour
  0x00,0x04,   // RDLENGTH = 4
  0x00,0x00,  // IP adress octets to be filled #offset 12
  0x00,0x00   // IP adress octeds to be filled
  } ;
byte dnsReply[UDP_PACKET_SIZE];       // buffer to hold the send DNS repluy
IPAddress dnsclientIp;
unsigned int dnsclientPort;
unsigned int udpPort = 53;            // local port to listen for UDP packets
WiFiUDP Udp;                          // A UDP instance to let us send and receive packets over UDP
int dnsreqCount=0;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  Serial.println("Access Point Web Server with CaptivePortal");

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < "1.0.0") {
    Serial.println("Please upgrade the firmware");
  }

  // by default the local IP address of will be 192.168.4.1
  // Create open network. Change this line if you want to create an WEP network:
  Serial.print("Creating access point named: ");
  Serial.println(ssid);
  WiFi.disconnect();
  WiFi.config(apip,apip,gwip,IPAddress(255,255,255,0));
  //WiFi.setHostname(myHost);
  status = WiFi.beginAP(ssid,apChannel); // setup AP 
  if (status != WL_AP_LISTENING) {
    Serial.println("Creating access point failed");
    // don't continue
    while (true) ;  
  }
    // wait 10 seconds for connection:
  delay(10000);

  // you're connected now, so print out the status
  printWiFiStatus();

  // start the web server on port 80
  Udp.begin(udpPort);
  server.begin();


}


void loop() {
int t;
char c;
// compare the previous AP status to the current status
  IPAddress ip = WiFi.localIP();
  if (status != WiFi.status()) {
    // it has changed update the variable
    status = WiFi.status();

    if (status == WL_AP_CONNECTED) {
      // a device has connected to the AP
      Serial.println("Device connected to AP");
      dnsreqCount=0; 
      udpScan(); // scan DNS request for redirect
    }
    else {
      // a device has disconnected from the AP, and we are back in listening mode
      Serial.println("Device disconnected from AP");
    }
  }
// Wifi Server check
  WiFiClient client = server.available();   // listen for incoming clients
  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
            // the content of the HTTP response follows the header:
            client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"); // metaview
            client.println("<body style=\"background-color:black\">"); // set color CCS HTML5 style
            client.print( "<h2 style=\"font-family:verdana; color:GoldenRod\">Demo Access Point ");client.print(WiFi.SSID());client.println("</h2>");
            client.print("<p style=\"font-family:verdana; color:Goldenrod\">IP-address :"); client.print(WiFi.localIP());client.print("<br>");
            client.print("<p style=\"font-family:verdana; color:Goldenrod\">Enter your Name:<br>");
            client.println("<form method=POST action=\"checkpass.php\">");
            client.println("<input type=text name=data>");
            client.println("<input type=submit name=action value=Submit>");
            client.println("</form></p><br><br>");
            client.print("<h3 style=\"font-family:verdana; color:GoldenRod\">Click <a href=\"http://");client.print(WiFi.localIP());client.print("\">here</a> to goto landing page</h3><br>");
            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:

            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was a post
        if (currentLine.endsWith("POST /checkpass.php")) {
                Serial.println("** found POST");  
                currentLine = ""; 
                while (client.connected()) {                 // loop while the client's connected
                if (client.available()) {                    // if there's bytes to read from the client,
                   c = client.read();                        // read a byte, then
                   Serial.write(c);                          // print it out the serial monitor
                    if (c == '\n') {                          // if the byte is a newline character
                       //if (currentLine.length() == 0) break; // no lenght :  end of data request
                       currentLine = "";                      // if you got a newline, then clear currentLine:
                       }
                    else if (c != '\r') currentLine += c;     // if you got anything else but a carriage return character, add to string
  
               if (currentLine.endsWith("&action")) {         // Check read line on &action = posted PhP data
                    t = currentLine.length()-12;              // count 12 back to get answer
                    Serial.print("** found pass, length("); Serial.print(t);Serial.print("):");
                    answerLine = currentLine.substring(4, t+5);  // save answers
                    Serial.println(answerLine); 
                    answerList1[answerCounter]=WiFi.SSID();  // manage list of answers and SSID's
                    answerList2[answerCounter]=answerLine;
                    answerCounter= (answerCounter+1)%32;
                     break;
                    }
    
                 } 
            }
            delay(1000);
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
            client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"); // metaview
            client.println("<body style=\"background-color:black\">"); // set color CCS HTML5 style
            client.print( "<h2 style=\"font-family:verdana; color:GoldenRod\">Access Router ");client.print(WiFi.SSID());client.println("</h2>");
            client.print("<p style=\"font-family:verdana; color:indianred\">Thank you, analysing Post request.<br>");
            client.print("<meta http-equiv=\"refresh\" content=\"4;url=/\" />");
            client.println();  
            break;
        } // end loop POST /checkpass.php check

        if (currentLine.endsWith("GET /SECRETS")) {
            client.println("HTTP/1.1 200 OK"); 
            client.println("Content-type:text/html");
            client.println();    
            client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"); // metaview    
            client.println("<body style=\"background-color:black\">"); // set color CCS HTML5 style
            client.print( "<h2 style=\"font-family:verdana; color:GoldenRod\">Demo Access Point ");client.print(WiFi.SSID());client.println("</h2>");
            client.print("<p style=\"font-family:verdana; color:indianred\">Thank you, list is Serial send.<br>");
            client.print("<meta http-equiv=\"refresh\" content=\"4;url=/\" />");
            client.println();  
            printAnswers();
          break;
        }
/*
        if (currentLine.endsWith("GET /connecttest")) {              // HTTP redirect for browsers with connect-test ?
            // client.println("HTTP/1.1 302 found");
            // client.print("Location: http://");client.print(WiFi.localIP());client.println("/");
            client.println("HTTP/1.1 204 No Content");
            client.println();  

          break;
        }

        if (currentLine.endsWith("GET /generate_204")) {              // HTTP generate 204 ?
            // client.println("HTTP/1.1 302 found");
            // client.print("Location: http://");client.print(WiFi.localIP());client.println("/");
            client.println("HTTP/1.1 204 No Content");
            client.println();  
          break;
        }

*/
        
      } // end loop client avaialbe    
    } // end loo client connected
    
    // close the connection:
    client.stop();
    Serial.println("**client disconnected");
  }
  else udpScan();
}



void printWiFiStatus() {
  IPAddress ip;
  // print the SSID of the network you're attached to:
 
  Serial.print("Nina W10 firmware: ");Serial.println(WiFi.firmwareVersion());
  Serial.print("SSID: "); Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  ip = WiFi.localIP();
  Serial.print("IP Address  : ");  Serial.println(ip);
  // print your WiFi shield's gateway:
  ip = WiFi.gatewayIP();
  Serial.print("IP Gateway  : ");  Serial.println(ip);
  // print your WiFi shield's gateway:
  ip = WiFi.subnetMask();
  Serial.print("Subnet Mask : ");  Serial.println(ip);
  // print where to go in a browser:
  ip = WiFi.localIP();
  Serial.print("To see this page in action, open a browser to http://");  Serial.println(ip);
}



void printAnswers() {
int t;
Serial.println("answerList:\n----------");
for (t=0;t<32;++t){
  Serial.print("Pass");Serial.print(t+1);Serial.print(":");
  Serial.print(answerList1[t]);;Serial.print(":"); Serial.println(answerList2[t]);
  }
Serial.println("----------");  
}




// UIDP port 53 - DNS - Scan
void udpScan()
{
int t=0;  // generic loop counter
int r,p;  // reply and packet counters
unsigned int packetSize=0;
unsigned int replySize=0;
  packetSize = Udp.parsePacket();
  if ( (packetSize!=0) && (packetSize<UDP_PACKET_SIZE) )  //only packets with small size
    {
    // We've received a packet, read the data from it
    Udp.read(packetBuffer, packetSize); // read the packet into the buffer
    dnsclientIp = Udp.remoteIP();
    dnsclientPort = Udp.remotePort();
    if ( (dnsclientIp != apip) && (dnsreqCount<=DNSMAXREQUESTS) ) // only non-local IP and only the first few DNSMAXREQUESTS x times
    {
    // DEBUG : Serial Print received Packet
    Serial.print("DNS-packets (");Serial.print(packetSize);
    Serial.print(") from ");Serial.print(dnsclientIp);
    Serial.print(" port ");Serial.println(dnsclientPort);
      for (t=0;t<packetSize;++t){
      Serial.print(packetBuffer[t],HEX);Serial.print(":");
      }
      Serial.println(" ");
      for (t=0;t<packetSize;++t){
      Serial.print( (char) packetBuffer[t]);//Serial.print("");
      }
    Serial.println("");
    
    //Copy Packet ID and IP into DNS header and DNS answer
    dnsReplyHeader[0] = packetBuffer[0];dnsReplyHeader[1] = packetBuffer[1]; // Copy ID of Packet offset 0 in Header
    dnsReplyAnswer[12] = apip[0];dnsReplyAnswer[13] = apip[1];dnsReplyAnswer[14] = apip[2];dnsReplyAnswer[15] = apip[3]; // copy AP Ip adress offset 12 in Answer
    r=0; // set reply buffer counter
    p=12; // set packetbuffer counter @ QUESTION QNAME section
    // copy Header into reply
    for (t=0;t<DNSHEADER_SIZE;++t) dnsReply[r++]=dnsReplyHeader[t];
    // copy Qusetion into reply:  Name labels till octet=0x00
    while (packetBuffer[p]!=0) dnsReply[r++]=packetBuffer[p++];
    // copy end of question plus Qtype and Qclass 5 octets
    for(t=0;t<5;++t)  dnsReply[r++]=packetBuffer[p++];
    //copy Answer into reply
    for (t=0;t<DNSANSWER_SIZE;++t) dnsReply[r++]=dnsReplyAnswer[t];
    replySize=r;
    
    // DEBUG : Serial print DSN reply
        Serial.print("DNS-Reply (");Serial.print(replySize);
        Serial.print(") from ");Serial.print(apip);
        Serial.print(" port ");Serial.println(udpPort);
        for (t=0;t<replySize;++t){
        Serial.print(dnsReply[t],HEX);Serial.print(":");
        }
        Serial.println(" ");
        for (t=0;t<replySize;++t){
        Serial.print( (char) dnsReply[t]);//Serial.print("");
        }
    Serial.println("");  
  // Send DSN UDP packet
  Udp.beginPacket(dnsclientIp, dnsclientPort); //reply DNSquestion
  Udp.write(dnsReply, replySize);
  Udp.endPacket();
  dnsreqCount++;
    } // end loop correct IP
 } // end loop received packet
  
}

Credits

JayV

JayV

28 projects • 32 followers
Silicon crazy for profession, silicon do-it-yourselves at Home.

Comments