diff --git a/src/RelayWebsocket.cpp b/src/RelayWebsocket.cpp index b6dc1d3..6503b14 100644 --- a/src/RelayWebsocket.cpp +++ b/src/RelayWebsocket.cpp @@ -92,21 +92,20 @@ void RelayServer::runWebsocket(ThreadPool::Thread &thr) { }); hubGroup->onConnection([&](uWS::WebSocket *ws, uWS::HttpRequest req) { - std::string addr = ws->getAddress().address; uint64_t connId = nextConnectionId++; + Connection *c = new Connection(ws, connId); + c->ipAddr = ws->getAddressBytes(); + ws->setUserData((void*)c); + connIdToConnection.emplace(connId, c); + bool compEnabled, compSlidingWindow; ws->getCompressionState(compEnabled, compSlidingWindow); - LI << "[" << connId << "] Connect from " << addr + LI << "[" << connId << "] Connect from " << renderIP(c->ipAddr) << " compression=" << (compEnabled ? 'Y' : 'N') << " sliding=" << (compSlidingWindow ? 'Y' : 'N') ; - Connection *c = new Connection(ws, connId); - c->ipAddr = addr; - ws->setUserData((void*)c); - connIdToConnection.emplace(connId, c); - if (cfg().relay__enableTcpKeepalive) { int optval = 1; if (setsockopt(ws->getFd(), SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval))) { @@ -122,7 +121,7 @@ void RelayServer::runWebsocket(ThreadPool::Thread &thr) { auto upComp = renderPercent(1.0 - (double)c->stats.bytesUpCompressed / c->stats.bytesUp); auto downComp = renderPercent(1.0 - (double)c->stats.bytesDownCompressed / c->stats.bytesDown); - LI << "[" << connId << "] Disconnect from " << c->ipAddr + LI << "[" << connId << "] Disconnect from " << renderIP(c->ipAddr) << " UP: " << renderSize(c->stats.bytesUp) << " (" << upComp << " compressed)" << " DN: " << renderSize(c->stats.bytesDown) << " (" << downComp << " compressed)" ; @@ -139,7 +138,7 @@ void RelayServer::runWebsocket(ThreadPool::Thread &thr) { c.stats.bytesDown += length; c.stats.bytesDownCompressed += compressedSize; - tpIngester.dispatch(c.connId, MsgIngester{MsgIngester::ClientMessage{c.connId, std::string(message, length), ws->getAddressBytes()}}); + tpIngester.dispatch(c.connId, MsgIngester{MsgIngester::ClientMessage{c.connId, c.ipAddr, std::string(message, length)}}); }); diff --git a/src/global.h b/src/global.h index 222d363..da202a5 100644 --- a/src/global.h +++ b/src/global.h @@ -10,3 +10,6 @@ using namespace phmap; quadrable::Quadrable getQdbInstance(lmdb::txn &txn); quadrable::Quadrable getQdbInstance(); + + +std::string renderIP(std::string_view ipBytes); diff --git a/src/misc.cpp b/src/misc.cpp new file mode 100644 index 0000000..90267a8 --- /dev/null +++ b/src/misc.cpp @@ -0,0 +1,17 @@ +#include + +#include "golpe.h" + +std::string renderIP(std::string_view ipBytes) { + char buf[128]; + + if (ipBytes.size() == 4) { + inet_ntop(AF_INET, ipBytes.data(), buf, sizeof(buf)); + } else if (ipBytes.size() == 16) { + inet_ntop(AF_INET6, ipBytes.data(), buf, sizeof(buf)); + } else { + throw herr("invalid size of ipBytes, unable to render IP"); + } + + return std::string(buf); +}