Skip to content

Commit

Permalink
Merge pull request #115 from MortezaBashsiz/morteza/chunk
Browse files Browse the repository at this point in the history
Morteza/chunk
  • Loading branch information
MortezaBashsiz authored Oct 14, 2024
2 parents e080630 + d8e5b55 commit e75bc11
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 117 deletions.
91 changes: 63 additions & 28 deletions core/src/agenthandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ AgentHandler::AgentHandler(boost::asio::streambuf &readBuffer,
writeBuffer_(writeBuffer),
request_(HTTP::create(config, log, readBuffer, uuid)),
clientConnStr_(clientConnStr),
uuid_(uuid) {}
uuid_(uuid) {
end_ = false;
connect_ = false;
}

AgentHandler::~AgentHandler() {}

Expand All @@ -28,28 +31,24 @@ void AgentHandler::handle() {

if (encryption.ok) {
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Encryption Done]", Log::Level::DEBUG);

std::string newReq(
request_->genHttpPostReqString(encode64(encryption.message)));

if (request_->detectType()) {
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Request] : " + request_->toString(),
Log::Level::DEBUG);

if (request_->parsedHttpRequest().target().length() > 0) {
log_->write("[" + to_string(uuid_) + "] [CONNECT] [SRC " + clientConnStr_ + "]" + " [DST " +
boost::lexical_cast<std::string>(
request_->parsedHttpRequest().target()) +
"]",
Log::Level::INFO);
}

if (!client_->socket().is_open() ||
request_->httpType() == HTTP::HttpType::http ||
request_->httpType() == HTTP::HttpType::connect) {
connect_ = true;
boost::system::error_code ec;
;

if (!client_->doConnect(config_->agent().serverIp,
config_->agent().serverPort)) {
log_->write(std::string("[" + to_string(uuid_) + "] [CONNECT] [ERROR] [To Server] [SRC ") +
Expand All @@ -58,7 +57,6 @@ void AgentHandler::handle() {
std::to_string(config_->agent().serverPort) + "]",
Log::Level::INFO);
}

if (ec) {
log_->write(std::string("[" + to_string(uuid_) + "] [AgentHandler handle] Connection error: ") +
ec.message(),
Expand All @@ -68,43 +66,32 @@ void AgentHandler::handle() {
}

copyStringToStreambuf(newReq, readBuffer_);

log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Request To Server] : \n" + newReq,
Log::Level::DEBUG);


client_->doWrite(readBuffer_);
client_->doRead();


if (client_->readBuffer().size() > 0) {

if (request_->httpType() != HTTP::HttpType::connect) {

HTTP::pointer response =
HTTP::create(config_, log_, client_->readBuffer(), uuid_);


if (response->parseHttpResp()) {

log_->write(
"[" + to_string(uuid_) + "] [AgentHandler handle] [Response] : " + response->restoString(),
Log::Level::DEBUG);


BoolStr decryption{false, std::string("FAILED")};
decryption =
aes256Decrypt(decode64(boost::lexical_cast<std::string>(
response->parsedHttpResponse().body())),
config_->agent().token);


if (boost::lexical_cast<std::string>(response->parsedHttpResponse()[config_->general().chunkHeader]) == "yes") {
end_ = true;
}
if (decryption.ok) {

copyStringToStreambuf(decryption.message, writeBuffer_);
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Decryption Done]", Log::Level::DEBUG);
} else {

log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Decryption Failed] : [ " +
decryption.message + "] ",
Log::Level::DEBUG);
Expand All @@ -114,29 +101,24 @@ void AgentHandler::handle() {
client_->socket().close();
}
} else {

log_->write(
"[AgentHandler handle] [NOT HTTP Response] "
"[Response] : " +
streambufToString(client_->readBuffer()),
Log::Level::DEBUG);
}
} else {

connect_ = true;
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Response to connect] : \n" +
streambufToString(client_->readBuffer()),
Log::Level::DEBUG);


moveStreambuf(client_->readBuffer(), writeBuffer_);
}
} else {

client_->socket().close();
return;
}
} else {

log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [NOT HTTP Request] [Request] : " +
streambufToString(readBuffer_),
Log::Level::DEBUG);
Expand All @@ -145,7 +127,6 @@ void AgentHandler::handle() {
return;
}
} else {

log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Encryption Failed] : [ " +
encryption.message + "] ",
Log::Level::DEBUG);
Expand All @@ -154,7 +135,61 @@ void AgentHandler::handle() {
client_->socket().remote_endpoint().address().to_string() + ":" +
std::to_string(client_->socket().remote_endpoint().port()) + "] ",
Log::Level::INFO);
client_->socket().close();
return;
}
}

void AgentHandler::continueRead() {
std::lock_guard<std::mutex> lock(mutex_);
std::string newReq(
request_->genHttpRestPostReqString());
copyStringToStreambuf(newReq, readBuffer_);
client_->doWrite(readBuffer_);
client_->doRead();
if (client_->readBuffer().size() > 0) {
if (request_->httpType() != HTTP::HttpType::connect) {
HTTP::pointer response =
HTTP::create(config_, log_, client_->readBuffer(), uuid_);
if (response->parseHttpResp()) {
log_->write(
"[" + to_string(uuid_) + "] [AgentHandler continueRead handle] [Response] : " + response->restoString(),
Log::Level::DEBUG);
BoolStr decryption{false, std::string("FAILED")};
decryption =
aes256Decrypt(decode64(boost::lexical_cast<std::string>(
response->parsedHttpResponse().body())),
config_->agent().token);
if (boost::lexical_cast<std::string>(response->parsedHttpResponse()[config_->general().chunkHeader]) == "yes") {
end_ = true;
}
if (decryption.ok) {
copyStringToStreambuf(decryption.message, writeBuffer_);
log_->write("[" + to_string(uuid_) + "] [AgentHandler continueRead handle] [Decryption Done]", Log::Level::DEBUG);
} else {
log_->write("[" + to_string(uuid_) + "] [AgentHandler continueRead handle] [Decryption Failed] : [ " +
decryption.message + "] ",
Log::Level::DEBUG);
log_->write("[" + to_string(uuid_) + "] [AgentHandler continueRead handle] [Decryption Failed] : " +
request_->toString(),
Log::Level::INFO);
client_->socket().close();
}
} else {
log_->write(
"[AgentHandler continueRead handle] [NOT HTTP Response] "
"[Response] : " +
streambufToString(client_->readBuffer()),
Log::Level::DEBUG);
}
} else {
connect_ = true;
log_->write("[" + to_string(uuid_) + "] [AgentHandler continueRead handle] [Response to connect] : \n" +
streambufToString(client_->readBuffer()),
Log::Level::DEBUG);
moveStreambuf(client_->readBuffer(), writeBuffer_);
}
} else {
client_->socket().close();
return;
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/agenthandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ class AgentHandler : private Uncopyable {
~AgentHandler();

void handle();
void continueRead();

inline const HTTP::pointer &request() & { return request_; }

inline const HTTP::pointer &&request() && { return std::move(request_); }

bool end_, connect_;

private:
AgentHandler(boost::asio::streambuf &readBuffer,
boost::asio::streambuf &writeBuffer,
Expand Down
6 changes: 5 additions & 1 deletion core/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Config::Config(const RunMode &mode, const std::string &filePath)
configYaml_["general"]["method"].as<std::string>(),
configYaml_["general"]["timeWait"].as<unsigned int>(),
configYaml_["general"]["timeout"].as<unsigned short>(),
configYaml_["general"]["repeatWait"].as<unsigned short>()}),
configYaml_["general"]["repeatWait"].as<unsigned short>(),
configYaml_["general"]["chunkHeader"].as<std::string>(),
configYaml_["general"]["chunkSize"].as<unsigned short>()}),
log_({configYaml_["log"]["logLevel"].as<std::string>(),
configYaml_["log"]["logFile"].as<std::string>()}),
server_({configYaml_["server"]["threads"].as<unsigned short>(),
Expand Down Expand Up @@ -62,6 +64,8 @@ std::string Config::toString() const {
<< " timeWait: " << general_.timeWait << "\n"
<< " timeout: " << general_.timeout << "\n"
<< " repeatWait: " << general_.repeatWait << "\n"
<< " chunkHeader: " << general_.chunkHeader << "\n"
<< " chunkSize: " << general_.chunkSize << "\n"
<< " Log :\n"
<< " logLevel: " << log_.level << "\n"
<< " logFile: " << log_.file << "\n"
Expand Down
2 changes: 2 additions & 0 deletions core/src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Config : private Uncopyable {
unsigned int timeWait;
unsigned short timeout;
unsigned short repeatWait;
std::string chunkHeader;
unsigned short chunkSize;
};

struct Log {
Expand Down
13 changes: 13 additions & 0 deletions core/src/general.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,19 @@ inline BoolStr validateConfig(int argc, const char *argv[]) {
return result;
}

try {
configYaml["general"]["fakeUrl"].as<std::string>();
configYaml["general"]["method"].as<std::string>();
configYaml["general"]["timeWait"].as<unsigned int>();
configYaml["general"]["timeout"].as<unsigned short>();
configYaml["general"]["repeatWait"].as<unsigned short>();
configYaml["general"]["chunkHeader"].as<std::string>();
configYaml["general"]["chunkSize"].as<unsigned short>();
} catch (const std::exception &e) {
result.message = std::string("Error in 'general' block: ") + e.what() + "\n";
return result;
}

try {
configYaml["log"]["logFile"].as<std::string>();
configYaml["log"]["logLevel"].as<std::string>();
Expand Down
23 changes: 19 additions & 4 deletions core/src/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ HTTP::HTTP(const std::shared_ptr<Config> &config,
parsedHttpRequest_(),
httpType_(HTTP::HttpType::https),
parsedTlsRequest_{"", "", TlsTypes::TLSHandshake},
uuid_(uuid) {}
uuid_(uuid) {
chunkHeader_ = "no";
}

HTTP::HTTP(const HTTP &http)
: config_(http.config_),
log_(http.log_),
buffer_(http.buffer_),
parsedHttpRequest_(http.parsedHttpRequest_),
parsedTlsRequest_(http.parsedTlsRequest_),
uuid_(http.uuid_) {}
uuid_(http.uuid_) {
chunkHeader_ = "no";
}

HTTP::~HTTP() {}

Expand Down Expand Up @@ -135,15 +139,26 @@ const std::string HTTP::genHttpPostReqString(const std::string &body) const {
"User-Agent: " + config_->agent().userAgent + "\r\n" +
"Accept: */*\r\n" + "Connection: keep-alive\r\n" +
"Content-Length: " + std::to_string(body.length()) + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" + "\r\n" + body;
"Content-Type: application/x-www-form-urlencoded\r\n" + "\r\n" + body + "\r\n";
}

const std::string HTTP::genHttpRestPostReqString() const {
return std::string(config_->general().method + " " +
config_->general().fakeUrl + " HTTP/" +
config_->agent().httpVersion + "\r\n") +
"Host: " + config_->general().fakeUrl + "\r\n" +
"User-Agent: " + config_->agent().userAgent + "\r\n" +
"Accept: */*\r\n" + "Connection: keep-alive\r\n" +
"Rest: yes\r\n";
}

const std::string HTTP::genHttpOkResString(const std::string &body) const {
return std::string("HTTP/1.1 200 OK\r\n") +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: " + std::to_string(body.length()) + "\r\n" +
config_->general().chunkHeader + ": " + chunkHeader_ + "\r\n" +
"Connection: keep-alive\r\n" + "Cache-Control: no-cache\r\n" +
"Pragma: no-cache\r\n" + "\r\n" + body;
"Pragma: no-cache\r\n" + "\r\n" + body + "\r\n";
}

void HTTP::setIPPort() {
Expand Down
3 changes: 3 additions & 0 deletions core/src/http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class HTTP {

const std::string genHttpPostReqString(const std::string &body) const;

const std::string genHttpRestPostReqString() const;

const std::string genHttpOkResString(const std::string &body) const;

inline const boost::beast::http::request<boost::beast::http::string_body> &
Expand All @@ -76,6 +78,7 @@ class HTTP {

const std::string toString() const;
const std::string restoString() const;
std::string chunkHeader_;

private:
explicit HTTP(const std::shared_ptr<Config> &config,
Expand Down
Loading

0 comments on commit e75bc11

Please sign in to comment.