Skip to content

Commit

Permalink
Merge pull request #124 from MortezaBashsiz/morteza/issue_119
Browse files Browse the repository at this point in the history
Morteza/issue 119
  • Loading branch information
MortezaBashsiz authored Nov 20, 2024
2 parents 4c052e4 + 1fbbdd5 commit 06365ac
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 54 deletions.
6 changes: 3 additions & 3 deletions core/src/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ 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 + "\r\n";
"Content-Type: application/x-www-form-urlencoded\r\n" + "\r\n" + body + "COMP\r\n\r\n";
}

const std::string HTTP::genHttpRestPostReqString() const {
Expand All @@ -149,7 +149,7 @@ const std::string HTTP::genHttpRestPostReqString() const {
"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";
"Rest: yes\r\n" + "COMP\r\n\r\n";
}

const std::string HTTP::genHttpOkResString(const std::string &body) const {
Expand All @@ -158,7 +158,7 @@ const std::string HTTP::genHttpOkResString(const std::string &body) const {
"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 + "\r\n";
"Pragma: no-cache\r\n" + "\r\n" + body + "COMP\r\n\r\n";
}

void HTTP::setIPPort() {
Expand Down
4 changes: 2 additions & 2 deletions core/src/serverhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ void ServerHandler::handle() {
}
if (client_->socket().is_open()) {
std::string message(
"HTTP/1.1 200 Connection established\r\n\r\n");
"HTTP/1.1 200 Connection established COMP\r\n\r\n");
os << message;
} else {
std::string message("HTTP/1.1 500 Connection failed\r\n\r\n");
std::string message("HTTP/1.1 500 Connection failed COMP\r\n\r\n");
os << message;
}
moveStreambuf(tempBuff, writeBuffer_);
Expand Down
144 changes: 98 additions & 46 deletions core/src/tcpclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,80 @@ void TCPClient::doWrite(boost::asio::streambuf &buffer) {
}
}

void TCPClient::doRead() {
void TCPClient::doReadAgent() {
boost::system::error_code error;
std::lock_guard<std::mutex> lock(mutex_);
boost::asio::streambuf tempBuff;

try {
if (!socket_.is_open()) {
log_->write("[" + to_string(uuid_) + "] [TCPClient doReadAgent] Socket is not OPEN",
Log::Level::DEBUG);
return;
}

resetTimeout();
boost::asio::read_until(socket_, tempBuff, "COMP\r\n\r\n",
error);
cancelTimeout();
if (error == boost::asio::error::eof) {
log_->write("[" + to_string(uuid_) + "] [TCPClient doReadAgent] [EOF] Connection closed by peer.",
Log::Level::TRACE);
socketShutdown();
return;
} else if (error) {
log_->write(
std::string("[" + to_string(uuid_) + "] [TCPClient doReadAgent] [error] ") + error.message(),
Log::Level::ERROR);
socketShutdown();
return;
}

if (tempBuff.size() > 0) {
copyStreambuf(tempBuff, buffer_);
try {

log_->write("[" + to_string(uuid_) + "] [TCPClient doReadAgent] [SRC " +
socket_.remote_endpoint().address().to_string() + ":" +
std::to_string(socket_.remote_endpoint().port()) +
"] [Bytes " + std::to_string(tempBuff.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(tempBuff.size()) +
"] ",
Log::Level::TRACE);
} catch (std::exception &error) {

log_->write(
std::string("[" + to_string(uuid_) + "] [TCPClient doReadAgent] [catch log] ") + error.what(),
Log::Level::DEBUG);
}
} else {
socketShutdown();
return;
}
} catch (std::exception &error) {

log_->write(std::string("[" + to_string(uuid_) + "] [TCPClient doReadAgent] [catch read] ") + error.what(),
Log::Level::DEBUG);
socketShutdown();
return;
}
}

void TCPClient::doReadServer() {
boost::system::error_code error;
end_ = false;
std::lock_guard<std::mutex> lock(mutex_);
boost::asio::streambuf tempBuff;
boost::asio::steady_timer timer(io_context_);
bool isTlsRecord{false};

try {
if (!socket_.is_open()) {
log_->write("[" + to_string(uuid_) + "] [TCPClient doRead] Socket is not OPEN",
log_->write("[" + to_string(uuid_) + "] [TCPClient doReadServer] Socket is not OPEN",
Log::Level::DEBUG);
return;
}
Expand All @@ -108,45 +173,38 @@ void TCPClient::doRead() {
return;
}

if (config_->runMode() == RunMode::agent) {
resetTimeout();
boost::asio::read(socket_, tempBuff, boost::asio::transfer_at_least(1),
resetTimeout();
boost::asio::read(socket_, tempBuff, boost::asio::transfer_exactly(2),
error);
std::string bufStr{hexStreambufToStr(tempBuff)};
if (bufStr == "1403" || bufStr == "1503" || bufStr == "1603" || bufStr == "1703") {
isTlsRecord = true;
boost::asio::streambuf tempTempBuff;
boost::asio::read(socket_, tempBuff, boost::asio::transfer_exactly(1),
error);
cancelTimeout();
} else {
resetTimeout();
boost::asio::read(socket_, tempBuff, boost::asio::transfer_exactly(2),
boost::asio::read(socket_, tempTempBuff, boost::asio::transfer_exactly(2),
error);
int readSize{hexToInt(hexStreambufToStr(tempTempBuff))};
moveStreambuf(tempTempBuff, tempBuff);
boost::asio::read(socket_, tempBuff, boost::asio::transfer_exactly(readSize),
error);
std::string bufStr{hexStreambufToStr(tempBuff)};
if (bufStr == "1403" || bufStr == "1503" || bufStr == "1603" || bufStr == "1703") {
isTlsRecord = true;
boost::asio::streambuf tempTempBuff;
boost::asio::read(socket_, tempBuff, boost::asio::transfer_exactly(1),
error);
boost::asio::read(socket_, tempTempBuff, boost::asio::transfer_exactly(2),
error);
int readSize{hexToInt(hexStreambufToStr(tempTempBuff))};
moveStreambuf(tempTempBuff, tempBuff);
boost::asio::read(socket_, tempBuff, boost::asio::transfer_exactly(readSize),
error);
}
cancelTimeout();
}
cancelTimeout();


if (error == boost::asio::error::eof) {
log_->write("[" + to_string(uuid_) + "] [TCPClient doRead] [EOF] Connection closed by peer.",
log_->write("[" + to_string(uuid_) + "] [TCPClient doReadServer] [EOF] Connection closed by peer.",
Log::Level::TRACE);
socketShutdown();
return;
} else if (error) {
log_->write(
std::string("[" + to_string(uuid_) + "] [TCPClient doRead] [error] ") + error.message(),
std::string("[" + to_string(uuid_) + "] [TCPClient doReadServer] [error] ") + error.message(),
Log::Level::ERROR);
socketShutdown();
return;
}

boost::asio::steady_timer timer(io_context_);
if (!isTlsRecord) {
for (auto i = 0; i <= config_->general().repeatWait; i++) {
while (true) {
Expand All @@ -156,13 +214,13 @@ void TCPClient::doRead() {
boost::asio::transfer_at_least(1), error);
cancelTimeout();
if (error == boost::asio::error::eof) {
log_->write("[" + to_string(uuid_) + "] [TCPClient doRead] [EOF] Connection closed by peer.",
log_->write("[" + to_string(uuid_) + "] [TCPClient doReadServer] [EOF] Connection closed by peer.",
Log::Level::TRACE);
socketShutdown();
return;
} else if (error) {
log_->write(
std::string("[" + to_string(uuid_) + "] [TCPClient doRead] [error] ") + error.message(),
std::string("[" + to_string(uuid_) + "] [TCPClient doReadServer] [error] ") + error.message(),
Log::Level::ERROR);
socketShutdown();
return;
Expand All @@ -173,19 +231,18 @@ void TCPClient::doRead() {
}
}

if (config_->runMode() == RunMode::server) {
timer.expires_after(std::chrono::milliseconds(config_->general().timeWait));
timer.wait();
if (socket_.available() == 0) {
end_ = true;
}

timer.expires_after(std::chrono::milliseconds(config_->general().timeWait));
timer.wait();
if (socket_.available() == 0) {
end_ = true;
}

if (tempBuff.size() > 0) {
copyStreambuf(tempBuff, buffer_);
try {

log_->write("[" + to_string(uuid_) + "] [TCPClient doRead] [SRC " +
log_->write("[" + to_string(uuid_) + "] [TCPClient doReadServer] [SRC " +
socket_.remote_endpoint().address().to_string() + ":" +
std::to_string(socket_.remote_endpoint().port()) +
"] [Bytes " + std::to_string(tempBuff.size()) + "] ",
Expand All @@ -199,7 +256,7 @@ void TCPClient::doRead() {
} catch (std::exception &error) {

log_->write(
std::string("[" + to_string(uuid_) + "] [TCPClient doRead] [catch log] ") + error.what(),
std::string("[" + to_string(uuid_) + "] [TCPClient doReadServer] [catch log] ") + error.what(),
Log::Level::DEBUG);
}
} else {
Expand All @@ -208,7 +265,7 @@ void TCPClient::doRead() {
}
} catch (std::exception &error) {

log_->write(std::string("[" + to_string(uuid_) + "] [TCPClient doRead] [catch read] ") + error.what(),
log_->write(std::string("[" + to_string(uuid_) + "] [TCPClient doReadServer] [catch read] ") + error.what(),
Log::Level::DEBUG);
socketShutdown();
return;
Expand All @@ -218,15 +275,10 @@ void TCPClient::doRead() {
void TCPClient::doHandle() {
buffer_.consume(buffer_.size());
readBuffer_.consume(readBuffer_.size());
doRead();
std::string bufStr{hexStreambufToStr(buffer_)};
std::string tmpStr;
unsigned short pos = 0;
tmpStr = bufStr.substr(pos, 4);
if ((tmpStr == "1403" && tmpStr == "1503" && tmpStr == "1603" && tmpStr == "1703") || (tmpStr != "1403" && tmpStr != "1503" && tmpStr != "1603" && tmpStr != "1703")) {
while (socket_.available() > 0 && buffer_.size() < config_->general().chunkSize) {
doRead();
}
if (config_->runMode() == RunMode::server) {
doReadServer();
} else {
doReadAgent();
}
copyStreambuf(buffer_, readBuffer_);
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/tcpclient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class TCPClient : public boost::enable_shared_from_this<TCPClient> {
}
bool doConnect(const std::string &dstIP, const unsigned short &dstPort);
void doWrite(boost::asio::streambuf &buffer);
void doRead();
void doReadAgent();
void doReadServer();
void doHandle();
void socketShutdown();
boost::uuids::uuid uuid_;
Expand Down
4 changes: 2 additions & 2 deletions nipovpn/etc/nipovpn/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ general:
# timeWait: unsigned int(0-4,294,967,295)
# Defines the time wait between each repeat to read from socket.
# This directive will be useful when you expect to read very long stream from socket
timeWait: 5
timeWait: 0
# timeout: unsigned short
# Defines the timeout for I/O Operation in seconds. 0 indicates no timeout.
# Useful to automatically close stalled connections.
timeout: 10
# repeatWait: unsigned short(1-65,635)
# Defines the loop count which will try to repeat read from socket.
# Same as timeWait
repeatWait: 10
repeatWait: 1
# chunkHeader: String
# Defines the chunk header that you want to inform Agent/Server if it is end or not.
chunkHeader: "END"
Expand Down

0 comments on commit 06365ac

Please sign in to comment.