Skip to content

Commit

Permalink
Merge pull request #127 from MortezaBashsiz/dev
Browse files Browse the repository at this point in the history
New release
  • Loading branch information
MortezaBashsiz authored Nov 21, 2024
2 parents 451e182 + 805bd0f commit 6a33488
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ temp
build
.idea
/cmake-build-debug/
test
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ Config :
### Package

#### Install
Currently it is only available for debian based Linuxs and fully tested on ubuntu 24.04
You can simply down load and install the package. To download it please visit the [releases page](https://github.com/MortezaBashsiz/nipovpn/tags) and get the latest release.
Currently it is only available for debian-based Linuxes and fully tested on ubuntu 24.04
You can simply download and install the package. To download it please visit the [releases page](https://github.com/MortezaBashsiz/nipovpn/tags) and get the latest release.
```bash
[~]>$ sudo apt install ./nipovpn.deb
```
Expand Down
10 changes: 5 additions & 5 deletions core/src/agenthandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void AgentHandler::handle() {
BoolStr encryption{false, std::string("FAILED")};

encryption =
aes256Encrypt(hexStreambufToStr(readBuffer_), config_->agent().token);
aes256Encrypt(hexStreambufToStr(readBuffer_), config_->general().token);

if (encryption.ok) {
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Encryption Done]", Log::Level::DEBUG);
Expand Down Expand Up @@ -70,7 +70,7 @@ void AgentHandler::handle() {
Log::Level::DEBUG);

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

if (client_->readBuffer().size() > 0) {
if (request_->httpType() != HTTP::HttpType::connect) {
Expand All @@ -84,7 +84,7 @@ void AgentHandler::handle() {
decryption =
aes256Decrypt(decode64(boost::lexical_cast<std::string>(
response->parsedHttpResponse().body())),
config_->agent().token);
config_->general().token);
if (boost::lexical_cast<std::string>(response->parsedHttpResponse()[config_->general().chunkHeader]) == "yes") {
end_ = true;
}
Expand Down Expand Up @@ -146,7 +146,7 @@ void AgentHandler::continueRead() {
request_->genHttpRestPostReqString());
copyStringToStreambuf(newReq, readBuffer_);
client_->doWrite(readBuffer_);
client_->doRead();
client_->doHandle();
if (client_->readBuffer().size() > 0) {
if (request_->httpType() != HTTP::HttpType::connect) {
HTTP::pointer response =
Expand All @@ -159,7 +159,7 @@ void AgentHandler::continueRead() {
decryption =
aes256Decrypt(decode64(boost::lexical_cast<std::string>(
response->parsedHttpResponse().body())),
config_->agent().token);
config_->general().token);
if (boost::lexical_cast<std::string>(response->parsedHttpResponse()[config_->general().chunkHeader]) == "yes") {
end_ = true;
}
Expand Down
10 changes: 4 additions & 6 deletions core/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Config::Config(const RunMode &mode, const std::string &filePath)
threads_(0),
listenIp_("127.0.0.1"),
listenPort_(0),
general_({configYaml_["general"]["fakeUrl"].as<std::string>(),
general_({configYaml_["general"]["token"].as<std::string>(),
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>()}),
configYaml_["general"]["chunkHeader"].as<std::string>()}),
log_({configYaml_["log"]["logLevel"].as<std::string>(),
configYaml_["log"]["logFile"].as<std::string>()}),
server_({configYaml_["server"]["threads"].as<unsigned short>(),
Expand All @@ -24,7 +24,6 @@ Config::Config(const RunMode &mode, const std::string &filePath)
configYaml_["agent"]["listenPort"].as<unsigned short>(),
configYaml_["agent"]["serverIp"].as<std::string>(),
configYaml_["agent"]["serverPort"].as<unsigned short>(),
configYaml_["agent"]["token"].as<std::string>(),
configYaml_["agent"]["httpVersion"].as<std::string>(),
configYaml_["agent"]["userAgent"].as<std::string>()}) {
std::lock_guard<std::mutex> lock(configMutex_);
Expand Down Expand Up @@ -59,13 +58,13 @@ std::string Config::toString() const {
std::stringstream ss;
ss << "\nConfig :\n"
<< " General :\n"
<< " token: " << general_.token << "\n"
<< " fakeUrl: " << general_.fakeUrl << "\n"
<< " method: " << general_.method << "\n"
<< " 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 All @@ -79,7 +78,6 @@ std::string Config::toString() const {
<< " listenPort: " << agent_.listenPort << "\n"
<< " serverIp: " << agent_.serverIp << "\n"
<< " serverPort: " << agent_.serverPort << "\n"
<< " token: " << agent_.token << "\n"
<< " httpVersion: " << agent_.httpVersion << "\n"
<< " userAgent: " << agent_.userAgent << "\n";
return ss.str();
Expand Down
3 changes: 1 addition & 2 deletions core/src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ enum class RunMode { server,
class Config : private Uncopyable {
private:
struct General {
std::string token;
std::string fakeUrl;
std::string method;
unsigned int timeWait;
unsigned short timeout;
unsigned short repeatWait;
std::string chunkHeader;
unsigned short chunkSize;
};

struct Log {
Expand All @@ -40,7 +40,6 @@ class Config : private Uncopyable {
unsigned short listenPort;
std::string serverIp;
unsigned short serverPort;
std::string token;
std::string httpVersion;
std::string userAgent;
};
Expand Down
3 changes: 1 addition & 2 deletions core/src/general.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,13 @@ inline BoolStr validateConfig(int argc, const char *argv[]) {
}

try {
configYaml["general"]["token"].as<std::string>();
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;
Expand All @@ -343,7 +343,6 @@ inline BoolStr validateConfig(int argc, const char *argv[]) {
configYaml["agent"]["listenPort"].as<unsigned short>();
configYaml["agent"]["serverIp"].as<std::string>();
configYaml["agent"]["serverPort"].as<unsigned short>();
configYaml["agent"]["token"].as<std::string>();
configYaml["agent"]["httpVersion"].as<std::string>();
configYaml["agent"]["userAgent"].as<std::string>();
} catch (const std::exception &e) {
Expand Down
8 changes: 4 additions & 4 deletions core/src/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ HTTP::HTTP(const HTTP &http)
HTTP::~HTTP() {}

bool HTTP::detectType() {
std::string requestStr(hexStreambufToStr(buffer_));
std::string requestStr{hexStreambufToStr(buffer_)};
std::string tmpStr;
unsigned short pos = 0;
tmpStr = requestStr.substr(pos, 2);
Expand Down 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
6 changes: 2 additions & 4 deletions core/src/http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ class HTTP {

const std::string tlsTypeToString() const;

inline const std::string &dstIP() & { return dstIP_; }
inline const std::string &&dstIP() && { return std::move(dstIP_); }
inline const std::string &dstIP() { return dstIP_; }

inline const unsigned short &dstPort() & { return dstPort_; }
inline const unsigned short &&dstPort() && { return std::move(dstPort_); }
inline const unsigned short &dstPort() { return dstPort_; }

const std::string toString() const;
const std::string restoString() const;
Expand Down
2 changes: 1 addition & 1 deletion core/src/runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void Runner::run() {

auto tcpServer = TCPServer::create(io_context_, config_, log_);

for (auto i = 0; i < config_->threads(); ++i) {
for (auto i = 0; i < config_->threads() * 50; ++i) {
threadPool_.emplace_back([this] { workerThread(); });
}

Expand Down
14 changes: 7 additions & 7 deletions core/src/serverhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void ServerHandler::handle() {
BoolStr decryption{false, std::string("FAILED")};
decryption = aes256Decrypt(decode64(boost::lexical_cast<std::string>(
request_->parsedHttpRequest().body())),
config_->agent().token);
config_->general().token);

if (decryption.ok) {
log_->write(
Expand Down 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 All @@ -91,13 +91,13 @@ void ServerHandler::handle() {
client_->doConnect(request_->dstIP(), request_->dstPort());
}
client_->doWrite(readBuffer_);
client_->doRead();
client_->doHandle();
end_ = client_->end_;
if (client_->readBuffer().size() > 0) {
BoolStr encryption{false, std::string("FAILED")};
encryption =
aes256Encrypt(streambufToString(client_->readBuffer()),
config_->agent().token);
config_->general().token);
if (encryption.ok) {
if (end_) {
request_->chunkHeader_ = "yes";
Expand Down Expand Up @@ -163,13 +163,13 @@ void ServerHandler::handle() {

void ServerHandler::continueRead() {
std::lock_guard<std::mutex> lock(mutex_);
client_->doRead();
client_->doHandle();
end_ = client_->end_;
if (client_->readBuffer().size() > 0) {
BoolStr encryption{false, std::string("FAILED")};
encryption =
aes256Encrypt(streambufToString(client_->readBuffer()),
config_->agent().token);
config_->general().token);
if (encryption.ok) {
if (end_) {
request_->chunkHeader_ = "yes";
Expand Down
Loading

0 comments on commit 6a33488

Please sign in to comment.