Files
nipovpn/core/src/agenthandler.cpp
T

175 lines
8.6 KiB
C++
Raw Normal View History

2024-01-12 15:00:12 +01:00
#include "agenthandler.hpp"
2024-08-06 12:31:48 +02:00
// Constructor for the AgentHandler class
2024-08-08 15:48:35 +02:00
AgentHandler::AgentHandler(boost::asio::streambuf &readBuffer,
boost::asio::streambuf &writeBuffer,
const std::shared_ptr<Config> &config,
const std::shared_ptr<Log> &log,
const TCPClient::pointer &client,
2024-08-30 15:02:13 +02:00
const std::string &clientConnStr,
boost::uuids::uuid uuid)
2024-08-29 00:13:37 +03:30
: 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
2024-08-30 15:02:13 +02:00
request_(HTTP::create(config, log, readBuffer, uuid)),
clientConnStr_(clientConnStr),// Initialize client connection string
uuid_(uuid) {}
2024-01-12 15:00:12 +01:00
2024-08-06 12:31:48 +02:00
// Destructor for the AgentHandler class
2024-08-08 15:48:35 +02:00
AgentHandler::~AgentHandler() {}
2024-01-12 15:00:12 +01:00
2024-08-06 12:31:48 +02:00
// Main handler function for processing requests
2024-08-08 15:48:35 +02:00
void AgentHandler::handle() {
2024-08-29 00:13:37 +03:30
// Initialize encryption status
BoolStr encryption{false, std::string("FAILED")};
2024-08-06 12:31:48 +02:00
2024-08-29 00:13:37 +03:30
// Encrypt the request data from the read buffer
encryption =
aes256Encrypt(hexStreambufToStr(readBuffer_), config_->agent().token);
2024-08-06 12:31:48 +02:00
2024-08-29 00:13:37 +03:30
if (encryption.ok) {
// Log successful token validation
2024-08-30 15:02:13 +02:00
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Encryption Done]", Log::Level::DEBUG);
2024-08-06 12:31:48 +02:00
2024-08-29 00:13:37 +03:30
// Generate a new HTTP POST request string with the encrypted message
std::string newReq(
request_->genHttpPostReqString(encode64(encryption.message)));
2024-08-06 12:31:48 +02:00
2024-08-29 00:13:37 +03:30
// Check if the request type is valid
if (request_->detectType()) {
// Log the HTTP request details
2024-08-30 15:02:13 +02:00
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Request] : " + request_->toString(),
2024-08-29 00:13:37 +03:30
Log::Level::DEBUG);
2024-08-06 12:31:48 +02:00
2024-08-29 00:13:37 +03:30
// Log the client connention string and HTTP request target
if (request_->parsedHttpRequest().target().length() > 0) {
2024-08-30 15:02:13 +02:00
log_->write("[" + to_string(uuid_) + "] [CONNECT] [SRC " + clientConnStr_ + "]" + " [DST " +
2024-08-29 00:13:37 +03:30
boost::lexical_cast<std::string>(
request_->parsedHttpRequest().target()) +
"]",
Log::Level::INFO);
2024-08-08 15:48:35 +02:00
}
2024-08-06 12:31:48 +02:00
2024-08-30 15:02:13 +02:00
// If the client socket is not open or the request type is HTTP or CONNECT
2024-08-29 00:13:37 +03:30
if (!client_->socket().is_open() ||
request_->httpType() == HTTP::HttpType::http ||
request_->httpType() == HTTP::HttpType::connect) {
// Connect the TCP client to the server
boost::system::error_code ec;
;
if (!client_->doConnect(config_->agent().serverIp,
config_->agent().serverPort)) {
2024-08-30 15:02:13 +02:00
log_->write(std::string("[" + to_string(uuid_) + "] [CONNECT] [ERROR] [To Server] [SRC ") +
2024-08-29 00:13:37 +03:30
clientConnStr_ + "] [DST " +
config_->agent().serverIp + ":" +
std::to_string(config_->agent().serverPort) + "]",
Log::Level::INFO);
}
// Check for connection errors
if (ec) {
2024-08-30 15:02:13 +02:00
log_->write(std::string("[" + to_string(uuid_) + "] [AgentHandler handle] Connection error: ") +
2024-08-29 00:13:37 +03:30
ec.message(),
Log::Level::ERROR);
return;
}
}
// Copy the new request to the read buffer
copyStringToStreambuf(newReq, readBuffer_);
// Log the request to be sent to the server
2024-08-30 15:02:13 +02:00
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Request To Server] : \n" + newReq,
2024-08-29 00:13:37 +03:30
Log::Level::DEBUG);
// Write the request to the client socket and initiate a read
// operation
client_->doWrite(readBuffer_);
client_->doRead();
// Check if there is data available in the read buffer
if (client_->readBuffer().size() > 0) {
// If the HTTP request type is not CONNECT
if (request_->httpType() != HTTP::HttpType::connect) {
// Create an HTTP response handler
HTTP::pointer response =
2024-08-30 15:02:13 +02:00
HTTP::create(config_, log_, client_->readBuffer(), uuid_);
2024-08-29 00:13:37 +03:30
// Parse the HTTP response
if (response->parseHttpResp()) {
// Log the response details
log_->write(
2024-08-30 15:02:13 +02:00
"[" + to_string(uuid_) + "] [AgentHandler handle] [Response] : " + response->restoString(),
2024-08-29 00:13:37 +03:30
Log::Level::DEBUG);
// Decrypt the response body
BoolStr decryption{false, std::string("FAILED")};
decryption =
aes256Decrypt(decode64(boost::lexical_cast<std::string>(
response->parsedHttpResponse().body())),
config_->agent().token);
// Check if decryption was successful
if (decryption.ok) {
// Copy the decrypted message to the write buffer
copyStringToStreambuf(decryption.message, writeBuffer_);
2024-08-30 15:02:13 +02:00
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Decryption Done]", Log::Level::DEBUG);
2024-08-29 00:13:37 +03:30
} else {
// Log decryption failure and close the socket
2024-08-30 15:02:13 +02:00
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Decryption Failed] : [ " +
2024-08-29 00:13:37 +03:30
decryption.message + "] ",
Log::Level::DEBUG);
2024-08-30 15:02:13 +02:00
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Decryption Failed] : " +
2024-08-29 00:13:37 +03:30
request_->toString(),
Log::Level::INFO);
client_->socket().close();
}
} else {
// Log if the response is not an HTTP response
log_->write(
"[AgentHandler handle] [NOT HTTP Response] "
"[Response] : " +
streambufToString(client_->readBuffer()),
Log::Level::DEBUG);
}
} else {
// Log the response to a CONNECT request
2024-08-30 15:02:13 +02:00
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Response to connect] : \n" +
2024-08-29 00:13:37 +03:30
streambufToString(client_->readBuffer()),
Log::Level::DEBUG);
// Move the response from the read buffer to the write buffer
moveStreambuf(client_->readBuffer(), writeBuffer_);
}
} else {
// Close the socket if no data is available
client_->socket().close();
return;
}
} else {
// Log if the request is not a valid HTTP request
2024-08-30 15:02:13 +02:00
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [NOT HTTP Request] [Request] : " +
2024-08-29 00:13:37 +03:30
streambufToString(readBuffer_),
Log::Level::DEBUG);
// Close the socket if no data is available
client_->socket().close();
return;
2024-07-19 15:38:07 +02:00
}
2024-08-29 00:13:37 +03:30
} else {
// Log encryption failure and close the socket
2024-08-30 15:02:13 +02:00
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Encryption Failed] : [ " +
2024-08-29 00:13:37 +03:30
encryption.message + "] ",
Log::Level::DEBUG);
log_->write(
"[AgentHandler handle] [Encryption Failed] : " +
client_->socket().remote_endpoint().address().to_string() + ":" +
std::to_string(client_->socket().remote_endpoint().port()) + "] ",
Log::Level::INFO);
2024-08-08 15:48:35 +02:00
// Close the socket if no data is available
client_->socket().close();
return;
2024-07-01 20:15:31 +02:00
}
2024-08-06 09:55:54 +02:00
}