Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge dev to main for new release #116

Merged
merged 14 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions .github/workflows/debian-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- dev
paths:
- 'core/src/**'
- '.github/workflows/**'

jobs:
build:
Expand All @@ -29,17 +30,17 @@ jobs:
cd ..
mkdir -p nipovpn/usr/bin/
cp build/core/nipovpn nipovpn/usr/bin/nipovpn
dpkg-deb --build nipovpn/ build/nipovpn-stage.deb
dpkg-deb --build nipovpn/ build/nipovpn_staging_v1-0-${{ github.run_number }}.deb

- name: Upload the Debian nipovpn as an artifact
uses: actions/upload-artifact@v3
with:
name: nipovpn
path: build/nipovpn-stage.deb
path: build/nipovpn_staging_v1-0-${{ github.run_number }}.deb

- name: Show nipovpn information
run: |
dpkg -I build/nipovpn-stage.deb
dpkg -I build/nipovpn_staging_v1-0-${{ github.run_number }}.deb

release:
needs: build
Expand All @@ -59,7 +60,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
with:
tag_name: 'v1.0.${{ github.run_number }}'
release_name: Release ${{ github.run_number }}
release_name: Staging v1.0.${{ github.run_number }}
draft: false
prerelease: true

Expand All @@ -69,6 +70,6 @@ jobs:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./nipovpn-stage.deb
asset_name: nipovpn-stage.deb
asset_path: ./nipovpn_staging_v1-0-${{ github.run_number }}.deb
asset_name: nipovpn_staging_v1-0-${{ github.run_number }}.deb
asset_content_type: application/octet-stream
12 changes: 6 additions & 6 deletions .github/workflows/debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ jobs:
cd ..
mkdir -p nipovpn/usr/bin/
cp build/core/nipovpn nipovpn/usr/bin/nipovpn
dpkg-deb --build nipovpn/ build/nipovpn.deb
dpkg-deb --build nipovpn/ build/nipovpn_v1-0-${{ github.run_number }}.deb

- name: Upload the Debian nipovpn as an artifact
uses: actions/upload-artifact@v3
with:
name: nipovpn
path: build/nipovpn.deb
path: build/nipovpn_v1-0-${{ github.run_number }}.deb

- name: Show nipovpn information
run: |
dpkg -I build/nipovpn.deb
dpkg -I build/nipovpn_v1-0-${{ github.run_number }}.deb

release:
needs: build
Expand All @@ -59,7 +59,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
with:
tag_name: 'v1.0.${{ github.run_number }}'
release_name: Release ${{ github.run_number }}
release_name: Version v1.0.${{ github.run_number }}
draft: false
prerelease: false

Expand All @@ -69,6 +69,6 @@ jobs:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./nipovpn.deb
asset_name: nipovpn.deb
asset_path: ./nipovpn_v1-0-${{ github.run_number }}.deb
asset_name: nipovpn_v1-0-${{ github.run_number }}.deb
asset_content_type: application/octet-stream
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
Loading
Loading