oatpp / oatpp-websocket

oatpp-websocket submodule.
https://oatpp.io/
Apache License 2.0
78 stars 32 forks source link

Add possibility to get the remote ip address of the socket. #4

Closed lganzzzo closed 4 years ago

lganzzzo commented 5 years ago

Is there a way to get the remote ip address of the socket?

I want to try to log every new socket connection

Thanks Conor

@c-lgrant

lganzzzo commented 4 years ago

Starting from release 0.19.11 Peer IP and port can be obtained via the connection stream context.

Enable extended connection info in server connection provider:

oatpp::network::server::SimpleTCPConnectionProvider::createShared(8000, true /* enable extended info */)

List all connection properties available:

 ENDPOINT("GET", "/", root, 
          REQUEST(std::shared_ptr<IncomingRequest>, request)) 
 {
   auto* context = request->getConnection()->getInputStreamContext();
   for(const auto& pair : context->getProperties().getAll()) {
     OATPP_LOGD("property", "'%s' - '%s'", pair.first.getData(), pair.second.getData());
   }
   return createResponse(Status::CODE_200, "Hello World!!!");
 }

Cheers, Leonid

bigo2050 commented 1 year ago

how to get client ip ?

Saadharta commented 1 year ago

@bigo2050 You can get an IP addres using the connection's input stream properties:

#ifdef(DEBUG)
 /* IP display debug endpoint */
    ENDPOINT("GET", "get/user/ip/and/port", getUserIpAndPort,
        REQUEST(std::shared_ptr<IncomingRequest>, request) // Map request object to endpoint method
    ) {
        const oatpp::data::stream::Context::Properties connectionProperties 
            = request->getConnection()->getInputStreamContext().getProperties();
        std::cout
            << "Incoming connection from "
            << connectionProperties.get("peer_address")->c_str()
            << ":"
            << connectionProperties.get("peer_port")->c_str()
            << std::endl;
        return oatpp::websocket::Handshaker::serversideHandshake(request->getHeaders(), myWebsocketConnectionHandler);
    }
#endif

Kind regards,

Saadharta