From fbd17685a196d09de810f06430eab93b9828b711 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Wed, 8 Jul 2020 12:10:35 -0700 Subject: [PATCH] (socket+websocket+http+redis+snake servers) expose the remote ip and remote port when a new connection is made (see #222) / only ipv4 is handled --- CMakeLists.txt | 1 + docs/CHANGELOG.md | 4 ++++ docs/usage.md | 22 ++++++++++++++-------- ixredis/ixredis/IXRedisServer.cpp | 5 ++++- ixredis/ixredis/IXRedisServer.h | 3 ++- ixsnake/ixsnake/IXSnakeServer.cpp | 9 ++++++--- ixwebsocket/IXConnectionInfo.h | 25 +++++++++++++++++++++++++ ixwebsocket/IXHttpServer.cpp | 19 +++++++++++++------ ixwebsocket/IXHttpServer.h | 7 +++++-- ixwebsocket/IXSocketServer.cpp | 6 +++++- ixwebsocket/IXSocketServer.h | 4 +++- ixwebsocket/IXWebSocketServer.cpp | 5 +++-- ixwebsocket/IXWebSocketServer.h | 6 ++++-- ixwebsocket/IXWebSocketVersion.h | 2 +- test/IXCobraToSentryBotTest.cpp | 6 ++++-- test/IXTest.cpp | 7 +++++-- test/IXWebSocketBroadcastTest.cpp | 7 +++++-- test/IXWebSocketChatTest.cpp | 8 ++++++-- test/IXWebSocketCloseTest.cpp | 6 +++++- test/IXWebSocketServerTest.cpp | 7 +++++-- test/IXWebSocketSubProtocolTest.cpp | 7 +++++-- third_party/cpp-linenoise/linenoise.hpp | 2 ++ ws/ws_broadcast_server.cpp | 7 +++++-- ws/ws_echo_server.cpp | 7 +++++-- ws/ws_proxy_server.cpp | 7 +++++-- ws/ws_transfer.cpp | 7 +++++-- 26 files changed, 147 insertions(+), 49 deletions(-) create mode 100644 ixwebsocket/IXConnectionInfo.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 522c2883..446ea507 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,7 @@ set( IXWEBSOCKET_SOURCES set( IXWEBSOCKET_HEADERS ixwebsocket/IXBench.h ixwebsocket/IXCancellationRequest.h + ixwebsocket/IXConnectionInfo.h ixwebsocket/IXConnectionState.h ixwebsocket/IXDNSLookup.h ixwebsocket/IXExponentialBackoff.h diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index e9e81774..eb43dc6a 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All changes to this project will be documented in this file. +## [9.9.0] - 2020-07-08 + +(socket+websocket+http+redis+snake servers) expose the remote ip and remote port when a new connection is made + ## [9.8.6] - 2020-07-06 (cmake) change the way zlib and openssl are searched diff --git a/docs/usage.md b/docs/usage.md index f44d8f77..b4df5ddb 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -257,28 +257,31 @@ ix::WebSocketServer server(port); server.setOnConnectionCallback( [&server](std::shared_ptr webSocket, - std::shared_ptr connectionState) + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) { + std::cout << "Remote ip: " << connectionInfo->remoteIp << std::endl; + webSocket->setOnMessageCallback( [webSocket, connectionState, &server](const ix::WebSocketMessagePtr msg) { if (msg->type == ix::WebSocketMessageType::Open) { - std::cerr << "New connection" << std::endl; + std::cout << "New connection" << std::endl; // A connection state object is available, and has a default id // You can subclass ConnectionState and pass an alternate factory // to override it. It is useful if you want to store custom // attributes per connection (authenticated bool flag, attributes, etc...) - std::cerr << "id: " << connectionState->getId() << std::endl; + std::cout << "id: " << connectionState->getId() << std::endl; // The uri the client did connect to. - std::cerr << "Uri: " << msg->openInfo.uri << std::endl; + std::cout << "Uri: " << msg->openInfo.uri << std::endl; - std::cerr << "Headers:" << std::endl; + std::cout << "Headers:" << std::endl; for (auto it : msg->openInfo.headers) { - std::cerr << it.first << ": " << it.second << std::endl; + std::cout << it.first << ": " << it.second << std::endl; } } else if (msg->type == ix::WebSocketMessageType::Message) @@ -417,11 +420,14 @@ If you want to handle how requests are processed, implement the setOnConnectionC ```cpp setOnConnectionCallback( [this](HttpRequestPtr request, - std::shared_ptr /*connectionState*/) -> HttpResponsePtr + std::shared_ptr /*connectionState*/, + std::unique_ptr connectionInfo) -> HttpResponsePtr { // Build a string for the response std::stringstream ss; - ss << request->method + ss << connectionInfo->remoteIp + << " " + << request->method << " " << request->uri; diff --git a/ixredis/ixredis/IXRedisServer.cpp b/ixredis/ixredis/IXRedisServer.cpp index 783211b8..57154cb0 100644 --- a/ixredis/ixredis/IXRedisServer.cpp +++ b/ixredis/ixredis/IXRedisServer.cpp @@ -45,8 +45,11 @@ namespace ix } void RedisServer::handleConnection(std::unique_ptr socket, - std::shared_ptr connectionState) + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) { + logInfo("New connection from remote ip " + connectionInfo->remoteIp); + _connectedClientsCount++; while (!_stopHandlingConnections) diff --git a/ixredis/ixredis/IXRedisServer.h b/ixredis/ixredis/IXRedisServer.h index a94c4da0..c981b3a3 100644 --- a/ixredis/ixredis/IXRedisServer.h +++ b/ixredis/ixredis/IXRedisServer.h @@ -44,7 +44,8 @@ namespace ix // Methods virtual void handleConnection(std::unique_ptr, - std::shared_ptr connectionState) final; + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) final; virtual size_t getConnectedClientsCount() final; bool startsWith(const std::string& str, const std::string& start); diff --git a/ixsnake/ixsnake/IXSnakeServer.cpp b/ixsnake/ixsnake/IXSnakeServer.cpp index 49b2625c..0afcb0a5 100644 --- a/ixsnake/ixsnake/IXSnakeServer.cpp +++ b/ixsnake/ixsnake/IXSnakeServer.cpp @@ -61,16 +61,19 @@ namespace snake _server.setOnConnectionCallback( [this](std::shared_ptr webSocket, - std::shared_ptr connectionState) { + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) { auto state = std::dynamic_pointer_cast(connectionState); - + auto remoteIp = connectionInfo->remoteIp; + webSocket->setOnMessageCallback( - [this, webSocket, state](const ix::WebSocketMessagePtr& msg) { + [this, webSocket, state, remoteIp](const ix::WebSocketMessagePtr& msg) { std::stringstream ss; ix::LogLevel logLevel = ix::LogLevel::Debug; if (msg->type == ix::WebSocketMessageType::Open) { ss << "New connection" << std::endl; + ss << "remote ip: " << remoteIp << std::endl; ss << "id: " << state->getId() << std::endl; ss << "Uri: " << msg->openInfo.uri << std::endl; ss << "Headers:" << std::endl; diff --git a/ixwebsocket/IXConnectionInfo.h b/ixwebsocket/IXConnectionInfo.h new file mode 100644 index 00000000..1ef54961 --- /dev/null +++ b/ixwebsocket/IXConnectionInfo.h @@ -0,0 +1,25 @@ +/* + * IXConnectionInfo.h + * Author: Benjamin Sergeant + * Copyright (c) 2020 Machine Zone, Inc. All rights reserved. + */ + +#pragma once + +#include + +namespace ix +{ + struct ConnectionInfo + { + std::string remoteIp; + int remotePort; + + ConnectionInfo(const std::string& r = std::string(), int p = 0) + : remoteIp(r) + , remotePort(p) + { + ; + } + }; +} // namespace ix diff --git a/ixwebsocket/IXHttpServer.cpp b/ixwebsocket/IXHttpServer.cpp index 7096e816..af3aeb4c 100644 --- a/ixwebsocket/IXHttpServer.cpp +++ b/ixwebsocket/IXHttpServer.cpp @@ -115,7 +115,8 @@ namespace ix } void HttpServer::handleConnection(std::unique_ptr socket, - std::shared_ptr connectionState) + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) { _connectedClientsCount++; @@ -124,7 +125,9 @@ namespace ix if (std::get<0>(ret)) { - auto response = _onConnectionCallback(std::get<2>(ret), connectionState); + auto response = _onConnectionCallback(std::get<2>(ret), + connectionState, + std::move(connectionInfo)); if (!Http::sendResponse(response, socket)) { logError("Cannot send response"); @@ -144,7 +147,8 @@ namespace ix { setOnConnectionCallback( [this](HttpRequestPtr request, - std::shared_ptr /*connectionState*/) -> HttpResponsePtr { + std::shared_ptr /*connectionState*/, + std::unique_ptr connectionInfo) -> HttpResponsePtr { std::string uri(request->uri); if (uri.empty() || uri == "/") { @@ -174,7 +178,8 @@ namespace ix // Log request std::stringstream ss; - ss << request->method << " " << request->headers["User-Agent"] << " " + ss << connectionInfo->remoteIp << ":" << connectionInfo->remotePort << " " + << request->method << " " << request->headers["User-Agent"] << " " << request->uri << " " << content.size(); logInfo(ss.str()); @@ -200,13 +205,15 @@ namespace ix setOnConnectionCallback( [this, redirectUrl](HttpRequestPtr request, - std::shared_ptr /*connectionState*/) -> HttpResponsePtr { + std::shared_ptr /*connectionState*/, + std::unique_ptr connectionInfo) -> HttpResponsePtr { WebSocketHttpHeaders headers; headers["Server"] = userAgent(); // Log request std::stringstream ss; - ss << request->method << " " << request->headers["User-Agent"] << " " + ss << connectionInfo->remoteIp << ":" << connectionInfo->remotePort << " " + << request->method << " " << request->headers["User-Agent"] << " " << request->uri; logInfo(ss.str()); diff --git a/ixwebsocket/IXHttpServer.h b/ixwebsocket/IXHttpServer.h index 96b22d3f..2735e835 100644 --- a/ixwebsocket/IXHttpServer.h +++ b/ixwebsocket/IXHttpServer.h @@ -23,7 +23,9 @@ namespace ix { public: using OnConnectionCallback = - std::function)>; + std::function, + std::unique_ptr connectionInfo)>; HttpServer(int port = SocketServer::kDefaultPort, const std::string& host = SocketServer::kDefaultHost, @@ -44,7 +46,8 @@ namespace ix // Methods virtual void handleConnection(std::unique_ptr, - std::shared_ptr connectionState) final; + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) final; virtual size_t getConnectedClientsCount() final; void setDefaultConnectionCallback(); diff --git a/ixwebsocket/IXSocketServer.cpp b/ixwebsocket/IXSocketServer.cpp index f1d34e43..62c5bb03 100644 --- a/ixwebsocket/IXSocketServer.cpp +++ b/ixwebsocket/IXSocketServer.cpp @@ -307,6 +307,10 @@ namespace ix continue; } + // FIXME error handling + char *remoteIp = inet_ntoa(client.sin_addr); + auto connectionInfo = std::make_unique(remoteIp, client.sin_port); + std::shared_ptr connectionState; if (_connectionStateFactory) { @@ -342,7 +346,7 @@ namespace ix _connectionsThreads.push_back(std::make_pair( connectionState, std::thread( - &SocketServer::handleConnection, this, std::move(socket), connectionState))); + &SocketServer::handleConnection, this, std::move(socket), connectionState, std::move(connectionInfo)))); } } diff --git a/ixwebsocket/IXSocketServer.h b/ixwebsocket/IXSocketServer.h index 8874587b..ff7a36b7 100644 --- a/ixwebsocket/IXSocketServer.h +++ b/ixwebsocket/IXSocketServer.h @@ -6,6 +6,7 @@ #pragma once +#include "IXConnectionInfo.h" #include "IXConnectionState.h" #include "IXSocketTLSOptions.h" #include @@ -102,7 +103,8 @@ namespace ix ConnectionStateFactory _connectionStateFactory; virtual void handleConnection(std::unique_ptr, - std::shared_ptr connectionState) = 0; + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) = 0; virtual size_t getConnectedClientsCount() = 0; // Returns true if all connection threads are joined diff --git a/ixwebsocket/IXWebSocketServer.cpp b/ixwebsocket/IXWebSocketServer.cpp index 86b0242a..1a568850 100644 --- a/ixwebsocket/IXWebSocketServer.cpp +++ b/ixwebsocket/IXWebSocketServer.cpp @@ -72,12 +72,13 @@ namespace ix } void WebSocketServer::handleConnection(std::unique_ptr socket, - std::shared_ptr connectionState) + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) { setThreadName("WebSocketServer::" + connectionState->getId()); auto webSocket = std::make_shared(); - _onConnectionCallback(webSocket, connectionState); + _onConnectionCallback(webSocket, connectionState, std::move(connectionInfo)); webSocket->disableAutomaticReconnection(); diff --git a/ixwebsocket/IXWebSocketServer.h b/ixwebsocket/IXWebSocketServer.h index db08f0ee..32f4693f 100644 --- a/ixwebsocket/IXWebSocketServer.h +++ b/ixwebsocket/IXWebSocketServer.h @@ -23,7 +23,8 @@ namespace ix { public: using OnConnectionCallback = - std::function, std::shared_ptr)>; + std::function, std::shared_ptr, + std::unique_ptr connectionInfo)>; WebSocketServer(int port = SocketServer::kDefaultPort, const std::string& host = SocketServer::kDefaultHost, @@ -60,7 +61,8 @@ namespace ix // Methods virtual void handleConnection(std::unique_ptr socket, - std::shared_ptr connectionState) final; + std::shared_ptr connectionState, + std::unique_ptr connectionInfo); virtual size_t getConnectedClientsCount() final; }; } // namespace ix diff --git a/ixwebsocket/IXWebSocketVersion.h b/ixwebsocket/IXWebSocketVersion.h index af2b0a34..b5a45231 100644 --- a/ixwebsocket/IXWebSocketVersion.h +++ b/ixwebsocket/IXWebSocketVersion.h @@ -6,4 +6,4 @@ #pragma once -#define IX_WEBSOCKET_VERSION "9.8.6" +#define IX_WEBSOCKET_VERSION "9.9.0" diff --git a/test/IXCobraToSentryBotTest.cpp b/test/IXCobraToSentryBotTest.cpp index 03c20d92..aa45a283 100644 --- a/test/IXCobraToSentryBotTest.cpp +++ b/test/IXCobraToSentryBotTest.cpp @@ -95,13 +95,15 @@ TEST_CASE("Cobra_to_sentry_bot", "[cobra_bots]") sentryServer.setOnConnectionCallback( [](HttpRequestPtr request, - std::shared_ptr /*connectionState*/) -> HttpResponsePtr { + std::shared_ptr /*connectionState*/, + std::unique_ptr connectionInfo) -> HttpResponsePtr { WebSocketHttpHeaders headers; headers["Server"] = userAgent(); // Log request std::stringstream ss; - ss << request->method << " " << request->headers["User-Agent"] << " " + ss << connectionInfo->remoteIp << ":" << connectionInfo->remotePort << " " + << request->method << " " << request->headers["User-Agent"] << " " << request->uri; if (request->method == "POST") diff --git a/test/IXTest.cpp b/test/IXTest.cpp index 26cfb033..405e74a7 100644 --- a/test/IXTest.cpp +++ b/test/IXTest.cpp @@ -85,12 +85,15 @@ namespace ix bool startWebSocketEchoServer(ix::WebSocketServer& server) { server.setOnConnectionCallback([&server](std::shared_ptr webSocket, - std::shared_ptr connectionState) { + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) { + auto remoteIp = connectionInfo->remoteIp; webSocket->setOnMessageCallback( - [webSocket, connectionState, &server](const ix::WebSocketMessagePtr& msg) { + [webSocket, connectionState, remoteIp, &server](const ix::WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { TLogger() << "New connection"; + TLogger() << "Remote ip: " << remoteIp; TLogger() << "Uri: " << msg->openInfo.uri; TLogger() << "Headers:"; for (auto it : msg->openInfo.headers) diff --git a/test/IXWebSocketBroadcastTest.cpp b/test/IXWebSocketBroadcastTest.cpp index 879df7a2..d9381586 100644 --- a/test/IXWebSocketBroadcastTest.cpp +++ b/test/IXWebSocketBroadcastTest.cpp @@ -191,13 +191,16 @@ namespace server.setOnConnectionCallback([&server, &connectionId]( std::shared_ptr webSocket, - std::shared_ptr connectionState) { - webSocket->setOnMessageCallback([webSocket, connectionState, &connectionId, &server]( + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) { + auto remoteIp = connectionInfo->remoteIp; + webSocket->setOnMessageCallback([webSocket, connectionState, remoteIp, &connectionId, &server]( const ix::WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { TLogger() << "New connection"; connectionState->computeId(); + TLogger() << "remote ip: " << remoteIp; TLogger() << "id: " << connectionState->getId(); TLogger() << "Uri: " << msg->openInfo.uri; TLogger() << "Headers:"; diff --git a/test/IXWebSocketChatTest.cpp b/test/IXWebSocketChatTest.cpp index 8c531079..caa3f5f7 100644 --- a/test/IXWebSocketChatTest.cpp +++ b/test/IXWebSocketChatTest.cpp @@ -194,12 +194,16 @@ namespace bool startServer(ix::WebSocketServer& server) { server.setOnConnectionCallback([&server](std::shared_ptr webSocket, - std::shared_ptr connectionState) { + std::shared_ptr connectionState, + + std::unique_ptr connectionInfo) { + auto remoteIp = connectionInfo->remoteIp; webSocket->setOnMessageCallback( - [webSocket, connectionState, &server](const ix::WebSocketMessagePtr& msg) { + [webSocket, connectionState, remoteIp, &server](const ix::WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { TLogger() << "New connection"; + TLogger() << "remote ip: " << remoteIp; TLogger() << "id: " << connectionState->getId(); TLogger() << "Uri: " << msg->openInfo.uri; TLogger() << "Headers:"; diff --git a/test/IXWebSocketCloseTest.cpp b/test/IXWebSocketCloseTest.cpp index b52ab2c5..8091a53a 100644 --- a/test/IXWebSocketCloseTest.cpp +++ b/test/IXWebSocketCloseTest.cpp @@ -171,9 +171,12 @@ namespace server.setOnConnectionCallback( [&receivedCloseCode, &receivedCloseReason, &receivedCloseRemote, &mutexWrite]( std::shared_ptr webSocket, - std::shared_ptr connectionState) { + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) { + auto remoteIp = connectionInfo->remoteIp; webSocket->setOnMessageCallback([webSocket, connectionState, + remoteIp, &receivedCloseCode, &receivedCloseReason, &receivedCloseRemote, @@ -181,6 +184,7 @@ namespace if (msg->type == ix::WebSocketMessageType::Open) { TLogger() << "New server connection"; + TLogger() << "remote ip: " << remoteIp; TLogger() << "id: " << connectionState->getId(); TLogger() << "Uri: " << msg->openInfo.uri; TLogger() << "Headers:"; diff --git a/test/IXWebSocketServerTest.cpp b/test/IXWebSocketServerTest.cpp index a53c697d..a375aa02 100644 --- a/test/IXWebSocketServerTest.cpp +++ b/test/IXWebSocketServerTest.cpp @@ -35,13 +35,16 @@ namespace ix server.setOnConnectionCallback([&server, &connectionId]( std::shared_ptr webSocket, - std::shared_ptr connectionState) { - webSocket->setOnMessageCallback([webSocket, connectionState, &connectionId, &server]( + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) { + auto remoteIp = connectionInfo->remoteIp; + webSocket->setOnMessageCallback([webSocket, connectionState, remoteIp, &connectionId, &server]( const ix::WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { TLogger() << "New connection"; connectionState->computeId(); + TLogger() << "remote ip: " << remoteIp; TLogger() << "id: " << connectionState->getId(); TLogger() << "Uri: " << msg->openInfo.uri; TLogger() << "Headers:"; diff --git a/test/IXWebSocketSubProtocolTest.cpp b/test/IXWebSocketSubProtocolTest.cpp index e4430c13..40a4037a 100644 --- a/test/IXWebSocketSubProtocolTest.cpp +++ b/test/IXWebSocketSubProtocolTest.cpp @@ -18,12 +18,15 @@ bool startServer(ix::WebSocketServer& server, std::string& subProtocols) { server.setOnConnectionCallback( [&server, &subProtocols](std::shared_ptr webSocket, - std::shared_ptr connectionState) { - webSocket->setOnMessageCallback([webSocket, connectionState, &server, &subProtocols]( + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) { + auto remoteIp = connectionInfo->remoteIp; + webSocket->setOnMessageCallback([webSocket, connectionState, remoteIp, &server, &subProtocols]( const ix::WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { TLogger() << "New connection"; + TLogger() << "remote ip: " << remoteIp; TLogger() << "id: " << connectionState->getId(); TLogger() << "Uri: " << msg->openInfo.uri; TLogger() << "Headers:"; diff --git a/third_party/cpp-linenoise/linenoise.hpp b/third_party/cpp-linenoise/linenoise.hpp index f0db9d1d..2c31ee71 100644 --- a/third_party/cpp-linenoise/linenoise.hpp +++ b/third_party/cpp-linenoise/linenoise.hpp @@ -122,6 +122,8 @@ #pragma once +#include + namespace linenoise { bool Readline(const char *prompt, std::string& line); diff --git a/ws/ws_broadcast_server.cpp b/ws/ws_broadcast_server.cpp index 84fbf265..b60eccbb 100644 --- a/ws/ws_broadcast_server.cpp +++ b/ws/ws_broadcast_server.cpp @@ -21,12 +21,15 @@ namespace ix server.setTLSOptions(tlsOptions); server.setOnConnectionCallback([&server](std::shared_ptr webSocket, - std::shared_ptr connectionState) { - webSocket->setOnMessageCallback([webSocket, connectionState, &server]( + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) { + auto remoteIp = connectionInfo->remoteIp; + webSocket->setOnMessageCallback([webSocket, connectionState, remoteIp, &server]( const WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { spdlog::info("New connection"); + spdlog::info("remote ip: {}", remoteIp); spdlog::info("id: {}", connectionState->getId()); spdlog::info("Uri: {}", msg->openInfo.uri); spdlog::info("Headers:"); diff --git a/ws/ws_echo_server.cpp b/ws/ws_echo_server.cpp index 18852fc9..68017cb0 100644 --- a/ws/ws_echo_server.cpp +++ b/ws/ws_echo_server.cpp @@ -44,12 +44,15 @@ namespace ix server.setOnConnectionCallback( [greetings](std::shared_ptr webSocket, - std::shared_ptr connectionState) { + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) { + auto remoteIp = connectionInfo->remoteIp; webSocket->setOnMessageCallback( - [webSocket, connectionState, greetings](const WebSocketMessagePtr& msg) { + [webSocket, connectionState, remoteIp, greetings](const WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { spdlog::info("New connection"); + spdlog::info("remote ip: {}", remoteIp); spdlog::info("id: {}", connectionState->getId()); spdlog::info("Uri: {}", msg->openInfo.uri); spdlog::info("Headers:"); diff --git a/ws/ws_proxy_server.cpp b/ws/ws_proxy_server.cpp index bec02006..6676fb7a 100644 --- a/ws/ws_proxy_server.cpp +++ b/ws/ws_proxy_server.cpp @@ -56,15 +56,18 @@ namespace ix server.setOnConnectionCallback([remoteUrl, verbose](std::shared_ptr webSocket, - std::shared_ptr connectionState) { + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) { auto state = std::dynamic_pointer_cast(connectionState); + auto remoteIp = connectionInfo->remoteIp; // Server connection - state->webSocket().setOnMessageCallback([webSocket, state, verbose]( + state->webSocket().setOnMessageCallback([webSocket, state, remoteIp, verbose]( const WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { spdlog::info("New connection to remote server"); + spdlog::info("remote ip: {}", remoteIp); spdlog::info("id: {}", state->getId()); spdlog::info("Uri: {}", msg->openInfo.uri); spdlog::info("Headers:"); diff --git a/ws/ws_transfer.cpp b/ws/ws_transfer.cpp index cf4984d1..51f35b75 100644 --- a/ws/ws_transfer.cpp +++ b/ws/ws_transfer.cpp @@ -20,12 +20,15 @@ namespace ix server.setTLSOptions(tlsOptions); server.setOnConnectionCallback([&server](std::shared_ptr webSocket, - std::shared_ptr connectionState) { - webSocket->setOnMessageCallback([webSocket, connectionState, &server]( + std::shared_ptr connectionState, + std::unique_ptr connectionInfo) { + auto remoteIp = connectionInfo->remoteIp; + webSocket->setOnMessageCallback([webSocket, connectionState, remoteIp, &server]( const WebSocketMessagePtr& msg) { if (msg->type == ix::WebSocketMessageType::Open) { spdlog::info("ws_transfer: New connection"); + spdlog::info("remote ip: {}", remoteIp); spdlog::info("id: {}", connectionState->getId()); spdlog::info("Uri: {}", msg->openInfo.uri); spdlog::info("Headers:");