Home > Mobile >  How to access IP address of HTTP requests using uWS?
How to access IP address of HTTP requests using uWS?

Time:10-27

I am using uWebSockets to do a project. What I need to do is get the sender IP address out of incoming HTTP Requests. In the documentation I can see IP address can be taken out from the WebSockets. Do anybody have an idea to cast uWS to WebSockets to get the data or is there an other way to get it?

#include <iostream>

#include "App.h"

int main() {

  /* Overly simple hello world app */

  uWS::App().get("/*", [](auto *res, auto *req) {

// **this is the place i need to access the ip address of the incoming HttpRequest**

    res->end("Hello world!");
  }).listen(3000, [](auto *listen_socket) {

    if (listen_socket) {
      std::cout << "Listening on port " << 3000 << std::endl;
    }
  }).

run();
  std::cout << "Failed to listen on port 3000" << std::endl;
} 

Link of the library: https://unetworking.github.io/uWebSockets.js/generated/interfaces/WebSocketBehavior.html

CodePudding user response:

According to the documentation, the remote address is an attribute of the response. Ergo:

std::string_view remote_ip = res->getRemoteAddressAsText();
  • Related