From a3b50043bfbf2cbe64eef4e97dce2e9fcf1bb6af Mon Sep 17 00:00:00 2001 From: Morteza Bashsiz Date: Fri, 30 Aug 2024 15:02:13 +0200 Subject: [PATCH] implement uuid for better log analyz --- core/src/agenthandler.cpp | 39 +++++++------- core/src/agenthandler.hpp | 10 ++-- core/src/general.hpp | 3 ++ core/src/http.cpp | 14 ++--- core/src/http.hpp | 9 ++-- core/src/runner.hpp | 2 +- core/src/serverhandler.cpp | 38 ++++++------- core/src/serverhandler.hpp | 10 ++-- core/src/tcpclient.cpp | 28 +++++----- core/src/tcpclient.hpp | 2 + core/src/tcpconnection.cpp | 95 +++++++++++++++++---------------- core/src/tcpconnection.hpp | 4 ++ core/src/tcpserver.cpp | 3 +- nipovpn/etc/nipovpn/config.yaml | 2 +- 14 files changed, 144 insertions(+), 115 deletions(-) diff --git a/core/src/agenthandler.cpp b/core/src/agenthandler.cpp index 42571c4..d142521 100644 --- a/core/src/agenthandler.cpp +++ b/core/src/agenthandler.cpp @@ -6,15 +6,16 @@ AgentHandler::AgentHandler(boost::asio::streambuf &readBuffer, const std::shared_ptr &config, const std::shared_ptr &log, const TCPClient::pointer &client, - const std::string &clientConnStr) + const std::string &clientConnStr, + boost::uuids::uuid uuid) : config_(config), // Initialize configuration log_(log), // Initialize logging client_(client), // Initialize TCP client readBuffer_(readBuffer), // Reference to the read buffer writeBuffer_(writeBuffer),// Reference to the write buffer - request_(HTTP::create(config, log, readBuffer)), - clientConnStr_(clientConnStr)// Initialize client connection string -{} + request_(HTTP::create(config, log, readBuffer, uuid)), + clientConnStr_(clientConnStr),// Initialize client connection string + uuid_(uuid) {} // Destructor for the AgentHandler class AgentHandler::~AgentHandler() {} @@ -30,7 +31,7 @@ void AgentHandler::handle() { if (encryption.ok) { // Log successful token validation - log_->write("[AgentHandler handle] [Token Valid]", Log::Level::DEBUG); + log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Encryption Done]", Log::Level::DEBUG); // Generate a new HTTP POST request string with the encrypted message std::string newReq( @@ -39,20 +40,19 @@ void AgentHandler::handle() { // Check if the request type is valid if (request_->detectType()) { // Log the HTTP request details - log_->write("[AgentHandler handle] [Request] : " + request_->toString(), + log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Request] : " + request_->toString(), Log::Level::DEBUG); // Log the client connention string and HTTP request target if (request_->parsedHttpRequest().target().length() > 0) { - log_->write("[CONNECT] [SRC " + clientConnStr_ + "]" + " [DST " + + log_->write("[" + to_string(uuid_) + "] [CONNECT] [SRC " + clientConnStr_ + "]" + " [DST " + boost::lexical_cast( request_->parsedHttpRequest().target()) + "]", Log::Level::INFO); } - // If the client socket is not open or the request type is HTTP or - // CONNECT + // If the client socket is not open or the request type is HTTP or CONNECT if (!client_->socket().is_open() || request_->httpType() == HTTP::HttpType::http || request_->httpType() == HTTP::HttpType::connect) { @@ -62,7 +62,7 @@ void AgentHandler::handle() { if (!client_->doConnect(config_->agent().serverIp, config_->agent().serverPort)) { - log_->write(std::string("[CONNECT] [ERROR] [To Server] [SRC ") + + log_->write(std::string("[" + to_string(uuid_) + "] [CONNECT] [ERROR] [To Server] [SRC ") + clientConnStr_ + "] [DST " + config_->agent().serverIp + ":" + std::to_string(config_->agent().serverPort) + "]", @@ -71,7 +71,7 @@ void AgentHandler::handle() { // Check for connection errors if (ec) { - log_->write(std::string("[AgentHandler handle] Connection error: ") + + log_->write(std::string("[" + to_string(uuid_) + "] [AgentHandler handle] Connection error: ") + ec.message(), Log::Level::ERROR); return; @@ -82,7 +82,7 @@ void AgentHandler::handle() { copyStringToStreambuf(newReq, readBuffer_); // Log the request to be sent to the server - log_->write("[AgentHandler handle] [Request To Server] : \n" + newReq, + log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Request To Server] : \n" + newReq, Log::Level::DEBUG); // Write the request to the client socket and initiate a read @@ -96,13 +96,13 @@ void AgentHandler::handle() { if (request_->httpType() != HTTP::HttpType::connect) { // Create an HTTP response handler HTTP::pointer response = - HTTP::create(config_, log_, client_->readBuffer()); + HTTP::create(config_, log_, client_->readBuffer(), uuid_); // Parse the HTTP response if (response->parseHttpResp()) { // Log the response details log_->write( - "[AgentHandler handle] [Response] : " + response->restoString(), + "[" + to_string(uuid_) + "] [AgentHandler handle] [Response] : " + response->restoString(), Log::Level::DEBUG); // Decrypt the response body @@ -116,12 +116,13 @@ void AgentHandler::handle() { if (decryption.ok) { // Copy the decrypted message to the write buffer copyStringToStreambuf(decryption.message, writeBuffer_); + log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Decryption Done]", Log::Level::DEBUG); } else { // Log decryption failure and close the socket - log_->write("[AgentHandler handle] [Decryption Failed] : [ " + + log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Decryption Failed] : [ " + decryption.message + "] ", Log::Level::DEBUG); - log_->write("[AgentHandler handle] [Decryption Failed] : " + + log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Decryption Failed] : " + request_->toString(), Log::Level::INFO); client_->socket().close(); @@ -136,7 +137,7 @@ void AgentHandler::handle() { } } else { // Log the response to a CONNECT request - log_->write("[AgentHandler handle] [Response to connect] : \n" + + log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Response to connect] : \n" + streambufToString(client_->readBuffer()), Log::Level::DEBUG); @@ -150,7 +151,7 @@ void AgentHandler::handle() { } } else { // Log if the request is not a valid HTTP request - log_->write("[AgentHandler handle] [NOT HTTP Request] [Request] : " + + log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [NOT HTTP Request] [Request] : " + streambufToString(readBuffer_), Log::Level::DEBUG); // Close the socket if no data is available @@ -159,7 +160,7 @@ void AgentHandler::handle() { } } else { // Log encryption failure and close the socket - log_->write("[AgentHandler handle] [Encryption Failed] : [ " + + log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Encryption Failed] : [ " + encryption.message + "] ", Log::Level::DEBUG); log_->write( diff --git a/core/src/agenthandler.hpp b/core/src/agenthandler.hpp index 3984a3d..0b1b9ec 100644 --- a/core/src/agenthandler.hpp +++ b/core/src/agenthandler.hpp @@ -38,9 +38,10 @@ public: const std::shared_ptr &config, const std::shared_ptr &log, const TCPClient::pointer &client, - const std::string &clientConnStr) { + const std::string &clientConnStr, + boost::uuids::uuid uuid) { return pointer(new AgentHandler(readBuffer, writeBuffer, config, log, - client, clientConnStr)); + client, clientConnStr, uuid)); } ~AgentHandler();///< Destructor for AgentHandler @@ -83,7 +84,8 @@ private: const std::shared_ptr &config, const std::shared_ptr &log, const TCPClient::pointer &client, - const std::string &clientConnStr); + const std::string &clientConnStr, + boost::uuids::uuid uuid); const std::shared_ptr &config_;///< Shared configuration object const std::shared_ptr &log_; ///< Shared logging object @@ -93,6 +95,8 @@ private: HTTP::pointer request_;///< HTTP request object const std::string &clientConnStr_;///< socket client connection string "ip:port" + + boost::uuids::uuid uuid_; }; #endif /* AGENTHADLER_HPP */ diff --git a/core/src/general.hpp b/core/src/general.hpp index 04c6f56..8c1757d 100644 --- a/core/src/general.hpp +++ b/core/src/general.hpp @@ -13,6 +13,9 @@ #include #include #include +#include +#include +#include #include #include #include diff --git a/core/src/http.cpp b/core/src/http.cpp index 9f4fa25..f736018 100644 --- a/core/src/http.cpp +++ b/core/src/http.cpp @@ -1,20 +1,22 @@ #include "http.hpp" HTTP::HTTP(const std::shared_ptr &config, - const std::shared_ptr &log, boost::asio::streambuf &buffer) + const std::shared_ptr &log, boost::asio::streambuf &buffer, boost::uuids::uuid uuid) : config_(config), log_(log), buffer_(buffer), parsedHttpRequest_(), httpType_(HTTP::HttpType::https), - parsedTlsRequest_{"", "", TlsTypes::TLSHandshake} {} + parsedTlsRequest_{"", "", TlsTypes::TLSHandshake}, + uuid_(uuid) {} HTTP::HTTP(const HTTP &http) : config_(http.config_), log_(http.log_), buffer_(http.buffer_), parsedHttpRequest_(http.parsedHttpRequest_), - parsedTlsRequest_(http.parsedTlsRequest_) {} + parsedTlsRequest_(http.parsedTlsRequest_), + uuid_(http.uuid_) {} HTTP::~HTTP() {} @@ -45,7 +47,7 @@ bool HTTP::parseHttp() { boost::beast::error_code error; parser.put(boost::asio::buffer(requestStr), error); if (error) { - log_->write(std::string("[HTTP parseHttp] ") + error.what(), + log_->write(std::string("[" + to_string(uuid_) + "] [HTTP parseHttp] ") + error.what(), Log::Level::DEBUG); return false; } else { @@ -65,7 +67,7 @@ bool HTTP::parseHttpResp() { boost::beast::error_code error; parser_.put(boost::asio::buffer(requestStr), error); if (error) { - log_->write(std::string("[HTTP parseHttpResp] ") + error.what(), + log_->write(std::string("[" + to_string(uuid_) + "] [HTTP parseHttpResp] ") + error.what(), Log::Level::DEBUG); return false; } else { @@ -170,7 +172,7 @@ void HTTP::setIPPort() { else dstPort_ = 80; } else { - log_->write("[HTTP setIPPort] wrong request", Log::Level::DEBUG); + log_->write("[" + to_string(uuid_) + "] [HTTP setIPPort] wrong request", Log::Level::DEBUG); } break; case HTTP::HttpType::connect: diff --git a/core/src/http.hpp b/core/src/http.hpp index dd276f8..5fbba5c 100644 --- a/core/src/http.hpp +++ b/core/src/http.hpp @@ -24,8 +24,9 @@ public: static pointer create(const std::shared_ptr &config, const std::shared_ptr &log, - boost::asio::streambuf &buffer) { - return pointer(new HTTP(config, log, buffer)); + boost::asio::streambuf &buffer, + boost::uuids::uuid uuid) { + return pointer(new HTTP(config, log, buffer, uuid)); } /* @@ -123,7 +124,8 @@ private: */ explicit HTTP(const std::shared_ptr &config, const std::shared_ptr &log, - boost::asio::streambuf &buffer); + boost::asio::streambuf &buffer, + boost::uuids::uuid uuid); /* * This function returns map of dst IP and Port for request @@ -142,6 +144,7 @@ private: std::string dstIP_; unsigned short dstPort_; TlsRequest parsedTlsRequest_; + boost::uuids::uuid uuid_; }; #endif /* HTTP_HPP */ \ No newline at end of file diff --git a/core/src/runner.hpp b/core/src/runner.hpp index 8bbdfd9..4e68471 100644 --- a/core/src/runner.hpp +++ b/core/src/runner.hpp @@ -70,7 +70,7 @@ private: std::atomic running_; // Thread pool to manage worker threads. - std::vector threadPool_; + std::vector threadPool_; // Add strand to manage concurrency boost::asio::strand strand_; diff --git a/core/src/serverhandler.cpp b/core/src/serverhandler.cpp index 17c1c15..aa832ae 100644 --- a/core/src/serverhandler.cpp +++ b/core/src/serverhandler.cpp @@ -13,15 +13,16 @@ ServerHandler::ServerHandler(boost::asio::streambuf &readBuffer, const std::shared_ptr &config, const std::shared_ptr &log, const TCPClient::pointer &client, - const std::string &clientConnStr) + const std::string &clientConnStr, + boost::uuids::uuid uuid) : config_(config), log_(log), client_(client), readBuffer_(readBuffer), writeBuffer_(writeBuffer), - request_(HTTP::create(config, log, readBuffer)), - clientConnStr_(clientConnStr)// Initialize client connection string -{} + request_(HTTP::create(config, log, readBuffer, uuid)), + clientConnStr_(clientConnStr), + uuid_(uuid) {} /* * Destructor for cleanup. No specific resources to release. @@ -38,7 +39,7 @@ void ServerHandler::handle() { if (request_->detectType()) { // Log the received request from the agent. log_->write( - "[ServerHandler handle] [Request From Agent] : " + request_->toString(), + "[" + to_string(uuid_) + "] [ServerHandler handle] [Request From Agent] : " + request_->toString(), Log::Level::DEBUG); // Decrypt the request body using AES256. @@ -50,7 +51,7 @@ void ServerHandler::handle() { if (decryption.ok) { // Log successful decryption and process the request. log_->write( - "[ServerHandler handle] [Token Valid] : " + request_->toString(), + "[" + to_string(uuid_) + "] [ServerHandler handle] [Token Valid] : " + request_->toString(), Log::Level::DEBUG); auto tempHexArr = strToHexArr(decryption.message); std::string tempHexArrStr(tempHexArr.begin(), tempHexArr.end()); @@ -59,7 +60,7 @@ void ServerHandler::handle() { // Re-check the request type and process accordingly. if (request_->detectType()) { log_->write( - "[ServerHandler handle] [Request] : " + request_->toString(), + "[" + to_string(uuid_) + "] [ServerHandler handle] [Request] : " + request_->toString(), Log::Level::DEBUG); switch (request_->httpType()) { case HTTP::HttpType::connect: { @@ -67,13 +68,13 @@ void ServerHandler::handle() { boost::asio::streambuf tempBuff; std::iostream os(&tempBuff); if (client_->doConnect(request_->dstIP(), request_->dstPort())) { - log_->write("[CONNECT] [SRC " + clientConnStr_ + "] [DST " + + log_->write("[" + to_string(uuid_) + "] [CONNECT] [SRC " + clientConnStr_ + "] [DST " + request_->dstIP() + ":" + std::to_string(request_->dstPort()) + "]", Log::Level::INFO); } else { log_->write( - std::string("[CONNECT] [ERROR] [Resolving Host] [SRC ") + + std::string("[" + to_string(uuid_) + "] [CONNECT] [ERROR] [Resolving Host] [SRC ") + clientConnStr_ + "] [DST " + request_->dstIP() + ":" + std::to_string(request_->dstPort()) + "]", Log::Level::INFO); @@ -93,13 +94,13 @@ void ServerHandler::handle() { // Handle HTTP or HTTPS requests. if (request_->httpType() == HTTP::HttpType::http) { if (client_->doConnect(request_->dstIP(), request_->dstPort())) { - log_->write("[CONNECT] [SRC " + clientConnStr_ + "] [DST " + + log_->write("[" + to_string(uuid_) + "] [CONNECT] [SRC " + clientConnStr_ + "] [DST " + request_->dstIP() + ":" + std::to_string(request_->dstPort()) + "]", Log::Level::INFO); } else { log_->write( - std::string("[CONNECT] [ERROR] [Resolving Host] [SRC ") + + std::string("[" + to_string(uuid_) + "] [CONNECT] [ERROR] [Resolving Host] [SRC ") + clientConnStr_ + "] [DST " + request_->dstIP() + ":" + std::to_string(request_->dstPort()) + "]", Log::Level::INFO); @@ -131,11 +132,12 @@ void ServerHandler::handle() { } else { // Log encryption failure and close the connection. log_->write( - "[ServerHandler handle] [Encryption " - "Failed] : [ " + + "[" + to_string(uuid_) + + "] [ServerHandler handle] [Encryption " + "Failed] : [ " + decryption.message + "] ", Log::Level::DEBUG); - log_->write("[ServerHandler handle] [Encryption Failed] : " + + log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Encryption Failed] : " + request_->toString(), Log::Level::INFO); client_->socket().close(); @@ -149,7 +151,7 @@ void ServerHandler::handle() { } } else { // Log non-HTTP requests if detected. - log_->write("[ServerHandler handle] [NOT HTTP Request] [Request] : " + + log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [NOT HTTP Request] [Request] : " + streambufToString(readBuffer_), Log::Level::DEBUG); // Close the socket if no data is available @@ -158,10 +160,10 @@ void ServerHandler::handle() { } } else { // Log decryption failure and close the connection. - log_->write("[ServerHandler handle] [Decryption Failed] : [ " + + log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Decryption Failed] : [ " + decryption.message + "] ", Log::Level::DEBUG); - log_->write("[ServerHandler handle] [Decryption Failed] : " + + log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Decryption Failed] : " + request_->toString(), Log::Level::INFO); // Close the socket if no data is available @@ -171,7 +173,7 @@ void ServerHandler::handle() { } else { // Log if the request is not from an agent. log_->write( - "[ServerHandler handle] [NOT HTTP Request From Agent] [Request] : " + + "[" + to_string(uuid_) + "] [ServerHandler handle] [NOT HTTP Request From Agent] [Request] : " + streambufToString(readBuffer_), Log::Level::DEBUG); // Close the socket if no data is available diff --git a/core/src/serverhandler.hpp b/core/src/serverhandler.hpp index e4bd2c6..f954066 100644 --- a/core/src/serverhandler.hpp +++ b/core/src/serverhandler.hpp @@ -36,9 +36,10 @@ public: const std::shared_ptr &config, const std::shared_ptr &log, const TCPClient::pointer &client, - const std::string &clientConnStr) { + const std::string &clientConnStr, + boost::uuids::uuid uuid) { return pointer(new ServerHandler(readBuffer, writeBuffer, config, log, - client, clientConnStr)); + client, clientConnStr, uuid)); } /* @@ -79,7 +80,8 @@ private: const std::shared_ptr &config, const std::shared_ptr &log, const TCPClient::pointer &client, - const std::string &clientConnStr); + const std::string &clientConnStr, + boost::uuids::uuid uuid); const std::shared_ptr &config_; // Reference to the configuration object. @@ -90,6 +92,8 @@ private: HTTP::pointer request_;// Shared pointer to the HTTP request object. const std::string &clientConnStr_;///< socket client connection string "ip:port" + + boost::uuids::uuid uuid_; }; #endif /* SERVERHANDLER_HPP */ diff --git a/core/src/tcpclient.cpp b/core/src/tcpclient.cpp index b0669a6..09043d1 100644 --- a/core/src/tcpclient.cpp +++ b/core/src/tcpclient.cpp @@ -51,7 +51,7 @@ bool TCPClient::doConnect(const std::string &dstIP, const unsigned short &dstPort) { try { // Log connection attempt - log_->write("[TCPClient doConnect] [DST " + dstIP + ":" + + log_->write("[" + to_string(uuid_) + "] [TCPClient doConnect] [DST " + dstIP + ":" + std::to_string(dstPort) + "]", Log::Level::DEBUG); @@ -68,7 +68,7 @@ bool TCPClient::doConnect(const std::string &dstIP, } } catch (std::exception &error) { // Log connection errors - log_->write(std::string("[TCPClient doConnect] ") + error.what(), + log_->write(std::string("[" + to_string(uuid_) + "] [TCPClient doConnect] ") + error.what(), Log::Level::ERROR); return false; } @@ -84,12 +84,12 @@ void TCPClient::doWrite(boost::asio::streambuf &buffer) { try { moveStreambuf(buffer, writeBuffer_); // Log details of the write operation - log_->write("[TCPClient doWrite] [DST " + + log_->write("[" + to_string(uuid_) + "] [TCPClient doWrite] [DST " + socket_.remote_endpoint().address().to_string() + ":" + std::to_string(socket_.remote_endpoint().port()) + "] " + "[Bytes " + std::to_string(writeBuffer_.size()) + "] ", Log::Level::DEBUG); - log_->write("[Write to] [DST " + + log_->write("[" + to_string(uuid_) + "] [Write to] [DST " + socket_.remote_endpoint().address().to_string() + ":" + std::to_string(socket_.remote_endpoint().port()) + "] " + "[Bytes " + std::to_string(writeBuffer_.size()) + "] ", @@ -99,12 +99,12 @@ void TCPClient::doWrite(boost::asio::streambuf &buffer) { if (writeBuffer_.size() > 0) { boost::asio::write(socket_, writeBuffer_, error); if (error) { - log_->write(std::string("[TCPClient doWrite] [error] ") + error.message(), + log_->write(std::string("[" + to_string(uuid_) + "] [TCPClient doWrite] [error] ") + error.message(), Log::Level::DEBUG); socket_.close();// Close socket on write error } } else { - log_->write("[TCPClient doWrite] [ZERO Bytes] [DST " + + log_->write("[" + to_string(uuid_) + "] [TCPClient doWrite] [ZERO Bytes] [DST " + socket_.remote_endpoint().address().to_string() + ":" + std::to_string(socket_.remote_endpoint().port()) + "] " + "[Bytes " + std::to_string(writeBuffer_.size()) + "] ", @@ -113,7 +113,7 @@ void TCPClient::doWrite(boost::asio::streambuf &buffer) { } } catch (std::exception &error) { // Log exceptions during the write operation - log_->write(std::string("[TCPClient doWrite] [catch] ") + error.what(), + log_->write(std::string("[" + to_string(uuid_) + "] [TCPClient doWrite] [catch] ") + error.what(), Log::Level::DEBUG); socket_.close();// Ensure socket closure on exception } @@ -137,7 +137,7 @@ void TCPClient::doRead() { for (auto i = 0; i <= config_->general().repeatWait; i++) { while (true) { if (!socket_.is_open()) { - log_->write("[TCPClient doRead] Socket is not OPEN", + log_->write("[" + to_string(uuid_) + "] [TCPClient doRead] Socket is not OPEN", Log::Level::DEBUG); socket_.close(); return; @@ -146,13 +146,13 @@ void TCPClient::doRead() { boost::asio::read(socket_, readBuffer_, boost::asio::transfer_exactly(1), error); if (error == boost::asio::error::eof) { - log_->write("[TCPClient doRead] [EOF] Connection closed by peer.", + log_->write("[" + to_string(uuid_) + "] [TCPClient doRead] [EOF] Connection closed by peer.", Log::Level::TRACE); socket_.close(); return;// Exit after closing the socket } else if (error) { log_->write( - std::string("[TCPClient doRead] [error] ") + error.message(), + std::string("[" + to_string(uuid_) + "] [TCPClient doRead] [error] ") + error.message(), Log::Level::ERROR); socket_.close(); return;// Exit after closing the socket @@ -166,12 +166,12 @@ void TCPClient::doRead() { if (readBuffer_.size() > 0) { try { // Log the successful read operation - log_->write("[TCPClient doRead] [SRC " + + log_->write("[" + to_string(uuid_) + "] [TCPClient doRead] [SRC " + socket_.remote_endpoint().address().to_string() + ":" + std::to_string(socket_.remote_endpoint().port()) + "] [Bytes " + std::to_string(readBuffer_.size()) + "] ", Log::Level::DEBUG); - log_->write("[Read from] [SRC " + + log_->write("[" + to_string(uuid_) + "] [Read from] [SRC " + socket_.remote_endpoint().address().to_string() + ":" + std::to_string(socket_.remote_endpoint().port()) + "] " + "[Bytes " + std::to_string(readBuffer_.size()) + @@ -180,7 +180,7 @@ void TCPClient::doRead() { } catch (std::exception &error) { // Log exceptions during logging log_->write( - std::string("[TCPClient doRead] [catch log] ") + error.what(), + std::string("[" + to_string(uuid_) + "] [TCPClient doRead] [catch log] ") + error.what(), Log::Level::DEBUG); } } else { @@ -189,7 +189,7 @@ void TCPClient::doRead() { } } catch (std::exception &error) { // Log exceptions during the read operation - log_->write(std::string("[TCPClient doRead] [catch read] ") + error.what(), + log_->write(std::string("[" + to_string(uuid_) + "] [TCPClient doRead] [catch read] ") + error.what(), Log::Level::DEBUG); socket_.close();// Ensure socket closure on error return; // Exit after closing the socket diff --git a/core/src/tcpclient.hpp b/core/src/tcpclient.hpp index da1a0c4..a346ce0 100644 --- a/core/src/tcpclient.hpp +++ b/core/src/tcpclient.hpp @@ -90,6 +90,8 @@ public: */ void doRead(); + boost::uuids::uuid uuid_; + private: /* * Private constructor for TCPClient. diff --git a/core/src/tcpconnection.cpp b/core/src/tcpconnection.cpp index 787549d..238359d 100644 --- a/core/src/tcpconnection.cpp +++ b/core/src/tcpconnection.cpp @@ -13,7 +13,9 @@ TCPConnection::TCPConnection(boost::asio::io_context &io_context, io_context_(io_context),// Store the I/O context client_(client), // Store the TCP client pointer timer_(io_context), // Initialize the deadline timer with the I/O context - strand_(boost::asio::make_strand(io_context_)) {} + strand_(boost::asio::make_strand(io_context_)) { + uuid_ = boost::uuids::random_generator()(); +} boost::asio::ip::tcp::socket &TCPConnection::socket() { return socket_;// Return the TCP socket @@ -25,7 +27,8 @@ void TCPConnection::start() { void TCPConnection::doRead() { try { - readBuffer_.consume(readBuffer_.size());// Clear the read buffer + readBuffer_.consume(readBuffer_.size()); // Clear the read buffer + writeBuffer_.consume(writeBuffer_.size());// Clear the write buffer boost::asio::async_read( socket_, readBuffer_, boost::asio::transfer_exactly(1), boost::asio::bind_executor( @@ -36,7 +39,7 @@ void TCPConnection::doRead() { bytes_transferred)));// Start an asynchronous // read operation } catch (std::exception &error) { - log_->write(std::string("[TCPConnection doRead] [catch] ") + error.what(), + log_->write(std::string("[" + to_string(uuid_) + "] [TCPConnection doRead] [catch] ") + error.what(), Log::Level::ERROR);// Log any exceptions socket_.close(); } @@ -47,19 +50,18 @@ void TCPConnection::handleRead(const boost::system::error_code &error, size_t) { if (error) { if (error == boost::asio::error::eof) { log_->write( - "[TCPConnection handleRead] [EOF] Connection " - "closed by peer.", + "[" + to_string(uuid_) + "] [TCPConnection handleRead] [EOF] Connection closed by peer.", Log::Level::TRACE); socket_.close();// Close the socket on error or EOF return; } else { log_->write( - std::string("[TCPConnection handleRead] ") + error.message(), + std::string("[" + to_string(uuid_) + "] [TCPConnection handleRead] ") + error.message(), Log::Level::TRACE); } } - if (socket_.available() > 0) {// Check if there is data available to read + if (socket_.available() > 0) { boost::asio::deadline_timer timer{io_context_}; for (auto i = 0; i <= config_->general().repeatWait; i++) { while (true) { @@ -70,13 +72,12 @@ void TCPConnection::handleRead(const boost::system::error_code &error, size_t) { boost::asio::transfer_exactly(1), read_error); if (read_error == boost::asio::error::eof) { log_->write( - "[TCPConnection handleRead] [EOF] " - "Connection closed by peer.", + "[" + to_string(uuid_) + "] [TCPConnection handleRead] [EOF] Connection closed by peer.", Log::Level::DEBUG); socket_.close();// Close the socket on EOF return; } else if (read_error) { - log_->write(std::string("[TCPConnection handleRead] [error] ") + + log_->write(std::string("[" + to_string(uuid_) + "] [TCPConnection handleRead] [error] ") + read_error.message(), Log::Level::ERROR); socket_.close();// Close the socket on any other errors @@ -87,43 +88,45 @@ void TCPConnection::handleRead(const boost::system::error_code &error, size_t) { boost::posix_time::milliseconds(config_->general().timeWait)); timer.wait();// Wait for the specified timeout duration } - } - // Log the successful read operation - log_->write("[TCPConnection handleRead] [SRC " + - socket_.remote_endpoint().address().to_string() + ":" + - std::to_string(socket_.remote_endpoint().port()) + - "] [Bytes " + std::to_string(readBuffer_.size()) + "] ", - Log::Level::DEBUG); - log_->write("[Read from] [SRC " + - socket_.remote_endpoint().address().to_string() + ":" + - std::to_string(socket_.remote_endpoint().port()) + "] " + - "[Bytes " + std::to_string(readBuffer_.size()) + "] ", - Log::Level::TRACE); - // Handle the request based on the running mode - if (config_->runMode() == RunMode::agent) { - AgentHandler::pointer agentHandler_ = AgentHandler::create( - readBuffer_, writeBuffer_, config_, log_, client_, - socket_.remote_endpoint().address().to_string() + ":" + - std::to_string(socket_.remote_endpoint().port())); - agentHandler_->handle();// Process the request as an agent - } else if (config_->runMode() == RunMode::server) { - ServerHandler::pointer serverHandler_ = ServerHandler::create( - readBuffer_, writeBuffer_, config_, log_, client_, - socket_.remote_endpoint().address().to_string() + ":" + - std::to_string(socket_.remote_endpoint().port())); - serverHandler_->handle();// Process the request as a server - } + // Log the successful read operation + log_->write("[" + to_string(uuid_) + "] [TCPConnection handleRead] [SRC " + + socket_.remote_endpoint().address().to_string() + ":" + + std::to_string(socket_.remote_endpoint().port()) + + "] [Bytes " + std::to_string(readBuffer_.size()) + "] ", + Log::Level::DEBUG); + log_->write("[" + to_string(uuid_) + "] [Read from] [SRC " + + socket_.remote_endpoint().address().to_string() + ":" + + std::to_string(socket_.remote_endpoint().port()) + "] " + + "[Bytes " + std::to_string(readBuffer_.size()) + "] ", + Log::Level::TRACE); + // Handle the request based on the running mode + if (config_->runMode() == RunMode::agent) { + AgentHandler::pointer agentHandler_ = AgentHandler::create( + readBuffer_, writeBuffer_, config_, log_, client_, + socket_.remote_endpoint().address().to_string() + ":" + + std::to_string(socket_.remote_endpoint().port()), + uuid_); + agentHandler_->handle();// Process the request as an agent + } else if (config_->runMode() == RunMode::server) { + ServerHandler::pointer serverHandler_ = ServerHandler::create( + readBuffer_, writeBuffer_, config_, log_, client_, + socket_.remote_endpoint().address().to_string() + ":" + + std::to_string(socket_.remote_endpoint().port()), + uuid_); + serverHandler_->handle();// Process the request as a server + } - if (writeBuffer_.size() > 0) { - doWrite();// Write to the socket if there is data in the write - // buffer - } else { - socket_.close();// Close the socket if no data to write + if (writeBuffer_.size() > 0) { + doWrite();// Write to the socket if there is data in the write + // buffer + } else { + socket_.close();// Close the socket if no data to write + } } } catch (std::exception &error) { log_->write( - std::string("[TCPConnection handleRead] [catch read] ") + error.what(), + std::string("[" + to_string(uuid_) + "] [TCPConnection handleRead] [catch read] ") + error.what(), Log::Level::DEBUG); socket_.close();// Ensure socket closure on exception } @@ -131,12 +134,12 @@ void TCPConnection::handleRead(const boost::system::error_code &error, size_t) { void TCPConnection::doWrite() { try { - log_->write("[TCPConnection doWrite] [DST " + + log_->write("[" + to_string(uuid_) + "] [TCPConnection doWrite] [DST " + socket_.remote_endpoint().address().to_string() + ":" + std::to_string(socket_.remote_endpoint().port()) + "] [Bytes " + std::to_string(writeBuffer_.size()) + "] ", Log::Level::DEBUG); - log_->write("[Write to] [DST " + + log_->write("[" + to_string(uuid_) + "] [Write to] [DST " + socket_.remote_endpoint().address().to_string() + ":" + std::to_string(socket_.remote_endpoint().port()) + "] [Bytes " + std::to_string(writeBuffer_.size()) + "] ", @@ -146,7 +149,7 @@ void TCPConnection::doWrite() { error);// Write data to the socket if (error) { log_->write( - std::string("[TCPConnection doWrite] [error] ") + error.message(), + std::string("[" + to_string(uuid_) + "] [TCPConnection doWrite] [error] ") + error.message(), Log::Level::ERROR);// Log any errors during the write operation socket_.close(); // Close the socket on error return; @@ -154,7 +157,7 @@ void TCPConnection::doWrite() { doRead();// Continue reading after writing } catch (std::exception &error) { log_->write( - std::string("[TCPConnection doWrite] [catch] ") + error.what(), + std::string("[" + to_string(uuid_) + "] [TCPConnection doWrite] [catch] ") + error.what(), Log::Level::ERROR);// Log any exceptions during the write operation socket_.close(); // Ensure socket closure on exception } diff --git a/core/src/tcpconnection.hpp b/core/src/tcpconnection.hpp index 44623af..81d5818 100644 --- a/core/src/tcpconnection.hpp +++ b/core/src/tcpconnection.hpp @@ -44,6 +44,8 @@ public: return std::move(readBuffer_); } + inline boost::uuids::uuid uuid() { return uuid_; } + // Starts the reading process void start(); @@ -74,6 +76,8 @@ private: boost::asio::deadline_timer timer_;// Timer used for managing timeouts boost::asio::strand strand_; + + boost::uuids::uuid uuid_; }; #endif /* TCPCONNECTION_HPP */ diff --git a/core/src/tcpserver.cpp b/core/src/tcpserver.cpp index 9981a28..352c23d 100644 --- a/core/src/tcpserver.cpp +++ b/core/src/tcpserver.cpp @@ -23,6 +23,7 @@ TCPServer::TCPServer(boost::asio::io_context &io_context, void TCPServer::startAccept() { // Create a new TCP connection auto connection = TCPConnection::create(io_context_, config_, log_, client_); + client_->uuid_ = connection->uuid(); // Set socket option to allow address reuse acceptor_.set_option(boost::asio::socket_base::reuse_address(true)); @@ -45,7 +46,7 @@ void TCPServer::handleAccept(TCPConnection::pointer connection, connection->start(); } else { // Log error details - log_->write("[TCPServer handleAccept] " + std::string(error.message()), + log_->write("TCPServer handleAccept] " + std::string(error.message()), Log::Level::ERROR); } diff --git a/nipovpn/etc/nipovpn/config.yaml b/nipovpn/etc/nipovpn/config.yaml index 2085d3f..2af85cd 100644 --- a/nipovpn/etc/nipovpn/config.yaml +++ b/nipovpn/etc/nipovpn/config.yaml @@ -18,7 +18,7 @@ general: # This block is to define log directives log: # logLevel: "INFO|TRACE|DEBUG" - logLevel: "INFO" + logLevel: "DEBUG" # logFile: "/var/log/nipo/nipo.log" # Path of log file logFile: "/var/log/nipovpn/nipovpn.log"