0

I am running a webserver from an ESP8266 with the library version v2.6.3. This is most of the code:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <WiFiServer.h>
#include <WiFiServerSecure.h>
#include <WiFiUdp.h>
#include <ESP8266WebServer.h>
#include <ESP8266WebServerSecure.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266Ping.h>
#include <my_lib_COMMON.h>
#include <my_lib_Esp8266_MCU_ONLY.h>


extern "C" {
  #include<user_interface.h>
}


#define PORT 80
const String MODULE_HOSTNAME = "zzzzzzzzzz";
const char* ssid =  "xxxxxxxxxxxx";
const char* pass =  "xxxxxxxxxxx";


ESP8266WebServer server(PORT);


bool SCAN_NETWORKS = false;
bool CONNECT_TO_NETWORK = true;
bool RUN_WEBSERVER = true;
bool CHANGE_HOSTNAME = false;

void setup() 
{
  Serial.begin(9600);

  Serial.println("\n");

  if (CONNECT_TO_NETWORK)
  {
    if (CONNECT_TO_SSID_AND_OUTPUT_SERIAL(ssid, pass))
    {
      IPs_TO_SERIAL(PORT);
    }
  }

  if (RUN_WEBSERVER)
  {
    /*server.on("/list", HTTP_GET, printDirectory);
    server.on("/edit", HTTP_DELETE, handleDelete);
    server.on("/edit", HTTP_PUT, handleCreate);
    server.on("/edit", HTTP_POST, []()
    {
      returnOK();
    }, handleFileUpload);*/
    server.onNotFound(handleNotFound);

    Serial.print("Activating Webserver... ");
    server.begin();
    Serial.println("OK!");
  }
}


void loop() 
{      
  if (RUN_WEBSERVER)
  {
    server.handleClient();
  }
}

void handleNotFound()
{
  Serial.println("New client with IP:" + IP + " and MAC: " + MAC); <==== client's IP and MAC here
  Serial.println("handleNotFound");
  Serial.println("Client status:");
  if (hasSD && loadFromSdCard(server.uri()))
  {
    return;
  }
  String message = "File not Detected\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i = 0; i < server.args(); i++)
  {
    message += " NAME:" + server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  Serial.print("Sent to client:");
  Serial.println(message);

  Serial.println("Connection terminated.");
}

The htm files are store in an SD card, but for code simplicity I have removed those parts of the code.

Someone has been trying to scan the server for hours. Even though I can see what he/she has been scanning for, I would like to find some more information.

Is there a way to get the client's, who made the request, IP and MAC address and/or any other information I can get?

CameFromSpace
  • 113
  • 1
  • 4
  • 11

1 Answers1

1

server.client() returns the current WiFiClientSecure

server.client().remoteIP() returns the IPAddress of that client object

Juraj
  • 3,490
  • 4
  • 18
  • 25
  • Ah I see. It seems that server.client() is of type WiFiClient, but indeed it returns the ip with .remoteIP(). Thank you for pointing me to the right direction. I looked on the library file but couldn't find anything for MAC address. Is there another way to get it? I've seen that websites can occasionally get information like OS or browser. Is there some way to get more information from the client? – CameFromSpace Mar 23 '20 at 12:22
  • on application level the MAC address is not accessible – Juraj Mar 23 '20 at 12:42