1

I am using Crow (C++ server library) and trying to get the client's IP address.

I found this answer, Getting the client IP address: REMOTE_ADDR, HTTP_X_FORWARDED_FOR, what else could be useful?, so I have tried:

CROW_ROUTE(app, "/mine")([](const crow::request& req, crow::response& res)
{
    std::string ip_address = req.get_header_value("REMOTE_ADDR");

    res.write(req.get_header_value("HTTP_X_FORWARDED_FOR"));
    res.write(req.get_header_value("HTTP_CLIENT_IP"));
    res.write(req.get_header_value("HTTP_X_FORWARDED"));
    res.write(req.get_header_value("HTTP_X_CLUSTER_CLIENT_IP"));
    res.write(req.get_header_value("HTTP_FORWARDED_FOR"));
    res.write(req.get_header_value("HTTP_FORWARDED"));

    for( auto head : req.headers )
    {
        res.write(head.first);
        res.write(" = ");
        res.write(head.second);
        res.write( "<br/>" );
    }
    res.end();
});

But all of those header fields are blank. Is there another way to get the IP address or is my browser just not sending the required information?

Halcyon
  • 1,376
  • 1
  • 15
  • 22
  • 1
    One usually get it from the connections socket. It seems Crow is a little to minimal to provide access to that. Perhaps you could [add an issue](https://github.com/ipkn/crow/issues) requesting a way to get the clients address? – Some programmer dude Apr 16 '18 at 07:06

1 Answers1

0

This worked for me:

std::string str_IP_and_PortNumber("");
auto itr = req.headers.find("Host");
if (itr != req.headers.end())
{
  str_IP_and_PortNumber = itr->second;
}

Actually, not sure if this is what you need - this gets the IP and port number of the server, as seen by the client. In case you need something different, just view the req.headers tree in the debug view window and find the string key of your interest.