Compare commits
4
Commits
dev
...
v.10.1.1.11
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
046edf6687 | ||
|
|
11b2787746 | ||
|
|
0cd2b91412 | ||
|
|
92b348b7ad |
No files matched your search
+36
-49
@@ -27,8 +27,7 @@ void AgentHandler::handle() {
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
if (!request_->detectType()) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [AgentHandler handle] [NOT HTTP Request] [Request] : " +
|
||||
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [NOT HTTP Request] [Request] : " +
|
||||
streambufToString(readBuffer_),
|
||||
Log::Level::DEBUG);
|
||||
client_->socketShutdown();
|
||||
@@ -36,70 +35,58 @@ void AgentHandler::handle() {
|
||||
}
|
||||
|
||||
if (request_->parsedHttpRequest().target().length() > 0) {
|
||||
log_->write("[" + to_string(uuid_) + "] [FORWARD] [SRC " + clientConnStr_ +
|
||||
"] [DST " +
|
||||
std::string(request_->parsedHttpRequest().target()) +
|
||||
"]",
|
||||
log_->write("[" + to_string(uuid_) + "] [FORWARD] [SRC " + clientConnStr_ + "] [DST " +
|
||||
std::string(request_->parsedHttpRequest().target()) + "]",
|
||||
Log::Level::INFO);
|
||||
}
|
||||
|
||||
if (config_->agent().tlsEnable && !client_->tlsEnabled()) {
|
||||
if (!client_->enableTlsClient()) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TLS] [ERROR] [Init Client Context] [SRC " +
|
||||
clientConnStr_ + "] [DST " + config_->agent().serverIp +
|
||||
":" + std::to_string(config_->agent().serverPort) + "]",
|
||||
Log::Level::ERROR);
|
||||
log_->write("[" + to_string(uuid_) + "] [TLS] [ERROR] [Init Client Context]", Log::Level::ERROR);
|
||||
client_->socketShutdown();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!client_->isOpen()) {
|
||||
connect_ = true;
|
||||
|
||||
if (!client_->doConnect(config_->agent().serverIp,
|
||||
config_->agent().serverPort)) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [CONNECT] [ERROR] [To Server] [SRC " + clientConnStr_ +
|
||||
"] [DST " + config_->agent().serverIp + ":" +
|
||||
std::to_string(config_->agent().serverPort) + "]",
|
||||
Log::Level::INFO);
|
||||
if (!client_->doConnect(config_->agent().serverIp, config_->agent().serverPort)) {
|
||||
log_->write("[" + to_string(uuid_) + "] [CONNECT] [ERROR] [To Server]", Log::Level::INFO);
|
||||
client_->socketShutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
if (client_->tlsEnabled()) {
|
||||
if (!client_->doHandshakeClient()) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TLS] [ERROR] [Client Handshake] [SRC " +
|
||||
clientConnStr_ + "] [DST " + config_->agent().serverIp +
|
||||
":" + std::to_string(config_->agent().serverPort) + "]",
|
||||
Log::Level::ERROR);
|
||||
log_->write("[" + to_string(uuid_) + "] [TLS] [ERROR] [Client Handshake]", Log::Level::ERROR);
|
||||
client_->socketShutdown();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BoolStr encryption{false, std::string("FAILED")};
|
||||
encryption = aes256Encrypt(hexStreambufToStr(readBuffer_), config_->general().token);
|
||||
const std::string plainInnerRequest = hexStreambufToStr(readBuffer_);
|
||||
|
||||
if (encryption.ok) {
|
||||
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Encryption Done]", Log::Level::DEBUG);
|
||||
} else {
|
||||
BoolStr encryption = aes256Encrypt(plainInnerRequest, config_->general().token);
|
||||
if (!encryption.ok) {
|
||||
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Encryption Failed] : [ " +
|
||||
encryption.message + "] ",
|
||||
Log::Level::DEBUG);
|
||||
client_->socketShutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string innerRequest = encode64(encryption.message);
|
||||
const std::string encodedBody = encode64(encryption.message);
|
||||
|
||||
std::string hostHeader = config_->general().fakeUrl;
|
||||
auto pos = hostHeader.find("://");
|
||||
if (pos != std::string::npos) hostHeader = hostHeader.substr(pos + 3);
|
||||
if (pos != std::string::npos) {
|
||||
hostHeader = hostHeader.substr(pos + 3);
|
||||
}
|
||||
|
||||
pos = hostHeader.find('/');
|
||||
if (pos != std::string::npos) hostHeader = hostHeader.substr(0, pos);
|
||||
if (pos != std::string::npos) {
|
||||
hostHeader = hostHeader.substr(0, pos);
|
||||
}
|
||||
|
||||
std::ostringstream outer;
|
||||
outer << "POST /relay HTTP/1.1\r\n"
|
||||
@@ -107,13 +94,15 @@ void AgentHandler::handle() {
|
||||
<< "User-Agent: Mozilla/5.0\r\n"
|
||||
<< "Accept: */*\r\n"
|
||||
<< "Content-Type: application/octet-stream\r\n"
|
||||
<< "Content-Length: " << innerRequest.size() << "\r\n"
|
||||
<< "Content-Length: " << encodedBody.size() << "\r\n"
|
||||
<< "Connection: keep-alive\r\n"
|
||||
<< "\r\n"
|
||||
<< innerRequest;
|
||||
<< encodedBody;
|
||||
|
||||
copyStringToStreambuf(outer.str(), readBuffer_);
|
||||
client_->doWrite(readBuffer_);
|
||||
boost::asio::streambuf outerRequestBuffer;
|
||||
copyStringToStreambuf(outer.str(), outerRequestBuffer);
|
||||
|
||||
client_->doWrite(outerRequestBuffer);
|
||||
client_->doReadAgent();
|
||||
|
||||
if (client_->readBuffer().size() == 0) {
|
||||
@@ -123,37 +112,35 @@ void AgentHandler::handle() {
|
||||
|
||||
const std::string outerResponse = streambufToString(client_->readBuffer());
|
||||
const auto bodyPos = outerResponse.find("\r\n\r\n");
|
||||
|
||||
if (bodyPos == std::string::npos) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [AgentHandler handle] [NOT HTTP Response] [Response] : " +
|
||||
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [NOT HTTP Response] [Response] : " +
|
||||
outerResponse,
|
||||
Log::Level::DEBUG);
|
||||
client_->socketShutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
BoolStr decryption{false, std::string("FAILED")};
|
||||
decryption = aes256Decrypt(outerResponse.substr(bodyPos + 4), config_->general().token);
|
||||
if (decryption.ok) {
|
||||
copyStringToStreambuf(decryption.message, writeBuffer_);
|
||||
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Decryption Done]", Log::Level::DEBUG);
|
||||
} else {
|
||||
const std::string encodedResponseBody = outerResponse.substr(bodyPos + 4);
|
||||
const std::string encryptedResponseBody = decode64(encodedResponseBody);
|
||||
|
||||
BoolStr decryption = aes256Decrypt(encryptedResponseBody, config_->general().token);
|
||||
if (!decryption.ok) {
|
||||
log_->write("[" + to_string(uuid_) + "] [AgentHandler handle] [Decryption Failed] : [ " +
|
||||
decryption.message + "] ",
|
||||
Log::Level::DEBUG);
|
||||
client_->socketShutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const std::string innerResponse = decryption.message;
|
||||
copyStringToStreambuf(decryption.message, writeBuffer_);
|
||||
|
||||
if (request_->httpType() == HTTP::HttpType::connect) {
|
||||
copyStringToStreambuf(innerResponse, writeBuffer_);
|
||||
connect_ = true;
|
||||
end_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
copyStringToStreambuf(innerResponse, writeBuffer_);
|
||||
connect_ = false;
|
||||
end_ = false;
|
||||
}
|
||||
@@ -365,4 +365,56 @@ inline BoolStr validateConfig(int argc, const char *argv[]) {
|
||||
result.ok = true;
|
||||
result.message = "OK";
|
||||
return result;
|
||||
}
|
||||
|
||||
inline std::string toLowerCopy(std::string s) {
|
||||
std::transform(s.begin(), s.end(), s.begin(),
|
||||
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
||||
return s;
|
||||
}
|
||||
|
||||
inline std::string extractHeaders(const std::string &msg) {
|
||||
auto pos = msg.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) return {};
|
||||
return msg.substr(0, pos + 4);
|
||||
}
|
||||
|
||||
inline std::string extractBody(const std::string &msg) {
|
||||
auto pos = msg.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) return {};
|
||||
return msg.substr(pos + 4);
|
||||
}
|
||||
|
||||
inline bool parseContentLength(const std::string &headers, std::size_t &value) {
|
||||
std::istringstream iss(headers);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||||
auto lower = toLowerCopy(line);
|
||||
if (lower.rfind("content-length:", 0) == 0) {
|
||||
auto raw = line.substr(std::string("Content-Length:").size());
|
||||
raw.erase(0, raw.find_first_not_of(" \t"));
|
||||
try {
|
||||
value = static_cast<std::size_t>(std::stoull(raw));
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool isChunked(const std::string &headers) {
|
||||
std::istringstream iss(headers);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||||
auto lower = toLowerCopy(line);
|
||||
if (lower.rfind("transfer-encoding:", 0) == 0 &&
|
||||
lower.find("chunked") != std::string::npos) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
+21
-13
@@ -163,9 +163,24 @@ const std::string HTTP::genHttpPostReqString(const std::string &body) const {
|
||||
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" +
|
||||
"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 + "COMP\r\n\r\n";
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n" +
|
||||
"\r\n" +
|
||||
body;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
const std::string HTTP::genHttpRestPostReqString() const {
|
||||
@@ -174,17 +189,10 @@ const std::string HTTP::genHttpRestPostReqString() const {
|
||||
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" + "COMP\r\n\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 + "COMP\r\n\r\n";
|
||||
"Accept: */*\r\n"
|
||||
"Connection: keep-alive\r\n"
|
||||
"Rest: yes\r\n"
|
||||
"\r\n";
|
||||
}
|
||||
|
||||
void HTTP::setIPPort() {
|
||||
|
||||
+97
-83
@@ -25,9 +25,7 @@ void ServerHandler::handle() {
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
if (!request_->detectType()) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [ServerHandler handle] [NOT HTTP Request From Agent] "
|
||||
"[Request] : " +
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [NOT HTTP Request From Agent] [Request] : " +
|
||||
streambufToString(readBuffer_),
|
||||
Log::Level::DEBUG);
|
||||
client_->socket().close();
|
||||
@@ -35,6 +33,7 @@ void ServerHandler::handle() {
|
||||
}
|
||||
|
||||
const auto &outerReq = request_->parsedHttpRequest();
|
||||
|
||||
const std::string method = std::string(outerReq.method_string());
|
||||
const std::string target = std::string(outerReq.target());
|
||||
|
||||
@@ -42,29 +41,47 @@ void ServerHandler::handle() {
|
||||
copyStringToStreambuf(
|
||||
"HTTP/1.1 404 Not Found\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"Connection: close\r\n\r\n",
|
||||
"Connection: close\r\n"
|
||||
"\r\n",
|
||||
writeBuffer_);
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string encodedOuterBody = std::string(outerReq.body());
|
||||
const std::string encryptedOuterBody = decode64(encodedOuterBody);
|
||||
|
||||
BoolStr decryption{false, std::string("FAILED")};
|
||||
decryption = aes256Decrypt(decode64(std::string(outerReq.body())), config_->general().token);
|
||||
|
||||
if (decryption.ok) {
|
||||
copyStringToStreambuf(decryption.message, writeBuffer_);
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Decryption Done]", Log::Level::DEBUG);
|
||||
} else {
|
||||
BoolStr decryption = aes256Decrypt(encryptedOuterBody, config_->general().token);
|
||||
if (!decryption.ok) {
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Decryption Failed] : [ " +
|
||||
decryption.message + "] ",
|
||||
Log::Level::DEBUG);
|
||||
|
||||
const std::string plainError =
|
||||
"HTTP/1.1 400 Bad Request\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n";
|
||||
|
||||
BoolStr encryptedError = aes256Encrypt(plainError, config_->general().token);
|
||||
const std::string encodedError = encode64(encryptedError.message);
|
||||
|
||||
std::ostringstream outer;
|
||||
outer << "HTTP/1.1 400 Bad Request\r\n"
|
||||
<< "Content-Type: application/octet-stream\r\n"
|
||||
<< "Content-Length: " << encodedError.size() << "\r\n"
|
||||
<< "Connection: close\r\n"
|
||||
<< "\r\n"
|
||||
<< encodedError;
|
||||
|
||||
copyStringToStreambuf(outer.str(), writeBuffer_);
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string innerRequest = hexToASCII(decryption.message);
|
||||
const std::size_t headerEnd = innerRequest.find("\r\n\r\n");
|
||||
|
||||
if (headerEnd == std::string::npos) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [ServerHandler handle] [INNER REQUEST MISSING HEADER END]",
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [INNER REQUEST MISSING HEADER END]",
|
||||
Log::Level::DEBUG);
|
||||
client_->socket().close();
|
||||
return;
|
||||
@@ -73,13 +90,13 @@ void ServerHandler::handle() {
|
||||
const std::string innerHeaders = innerRequest.substr(0, headerEnd + 4);
|
||||
const std::string pendingTunnelData = innerRequest.substr(headerEnd + 4);
|
||||
|
||||
copyStringToStreambuf(innerHeaders, readBuffer_);
|
||||
HTTP::pointer inner = HTTP::create(config_, log_, readBuffer_, uuid_);
|
||||
boost::asio::streambuf innerHeaderBuffer;
|
||||
copyStringToStreambuf(innerHeaders, innerHeaderBuffer);
|
||||
|
||||
HTTP::pointer inner = HTTP::create(config_, log_, innerHeaderBuffer, uuid_);
|
||||
if (!inner->detectType()) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [ServerHandler handle] [NOT INNER HTTP Request] [Request] : " +
|
||||
streambufToString(readBuffer_),
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [NOT INNER HTTP Request] [Request] : " +
|
||||
innerHeaders,
|
||||
Log::Level::DEBUG);
|
||||
client_->socket().close();
|
||||
return;
|
||||
@@ -87,84 +104,79 @@ void ServerHandler::handle() {
|
||||
|
||||
switch (inner->httpType()) {
|
||||
case HTTP::HttpType::connect: {
|
||||
if (client_->doConnect(inner->dstIP(), inner->dstPort())) {
|
||||
log_->write("[" + to_string(uuid_) + "] [CONNECT] [SRC " + clientConnStr_ +
|
||||
"] [DST " + inner->dstIP() + ":" +
|
||||
std::to_string(inner->dstPort()) + "]",
|
||||
Log::Level::INFO);
|
||||
|
||||
if (!pendingTunnelData.empty()) {
|
||||
boost::asio::streambuf pendingBuf;
|
||||
copyStringToStreambuf(pendingTunnelData, pendingBuf);
|
||||
client_->doWrite(pendingBuf);
|
||||
}
|
||||
|
||||
const std::string connectEstablished =
|
||||
"HTTP/1.1 200 Connection Established\r\n\r\n";
|
||||
|
||||
BoolStr encryption{false, std::string("FAILED")};
|
||||
encryption = aes256Encrypt(connectEstablished, config_->general().token);
|
||||
|
||||
if (encryption.ok) {
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Encryption Done]", Log::Level::DEBUG);
|
||||
} else {
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Encryption Failed] : [ " +
|
||||
encryption.message + "] ",
|
||||
Log::Level::DEBUG);
|
||||
}
|
||||
|
||||
std::ostringstream outer;
|
||||
outer << "HTTP/1.1 200 OK\r\n"
|
||||
<< "Content-Type: application/octet-stream\r\n"
|
||||
<< "Content-Length: " << encryption.message.size() << "\r\n"
|
||||
<< "Connection: keep-alive\r\n"
|
||||
<< "\r\n"
|
||||
<< encryption.message;
|
||||
|
||||
copyStringToStreambuf(outer.str(), writeBuffer_);
|
||||
connect_ = true;
|
||||
end_ = false;
|
||||
return;
|
||||
} else {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [CONNECT] [ERROR] [Resolving Host] [SRC " +
|
||||
if (!client_->doConnect(inner->dstIP(), inner->dstPort())) {
|
||||
log_->write("[" + to_string(uuid_) + "] [CONNECT] [ERROR] [Resolving Host] [SRC " +
|
||||
clientConnStr_ + "] [DST " + inner->dstIP() + ":" +
|
||||
std::to_string(inner->dstPort()) + "]",
|
||||
Log::Level::INFO);
|
||||
|
||||
const std::string connectFailed =
|
||||
"HTTP/1.1 502 Bad Gateway\r\n\r\n";
|
||||
"HTTP/1.1 502 Bad Gateway\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n";
|
||||
|
||||
BoolStr encryption{false, std::string("FAILED")};
|
||||
encryption = aes256Encrypt(connectFailed, config_->general().token);
|
||||
|
||||
if (encryption.ok) {
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Encryption Done]", Log::Level::DEBUG);
|
||||
} else {
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Encryption Failed] : [ " +
|
||||
encryption.message + "] ",
|
||||
Log::Level::DEBUG);
|
||||
}
|
||||
BoolStr encryption = aes256Encrypt(connectFailed, config_->general().token);
|
||||
const std::string encodedResponse = encode64(encryption.message);
|
||||
|
||||
std::ostringstream outer;
|
||||
outer << "HTTP/1.1 502 Bad Gateway\r\n"
|
||||
<< "Content-Type: application/octet-stream\r\n"
|
||||
<< "Content-Length: " << encryption.message.size() << "\r\n"
|
||||
<< "Content-Length: " << encodedResponse.size() << "\r\n"
|
||||
<< "Connection: close\r\n"
|
||||
<< "\r\n"
|
||||
<< encryption.message;
|
||||
<< encodedResponse;
|
||||
|
||||
copyStringToStreambuf(outer.str(), writeBuffer_);
|
||||
|
||||
connect_ = false;
|
||||
end_ = true;
|
||||
return;
|
||||
}
|
||||
|
||||
log_->write("[" + to_string(uuid_) + "] [CONNECT] [SRC " + clientConnStr_ + "] [DST " +
|
||||
inner->dstIP() + ":" + std::to_string(inner->dstPort()) + "]",
|
||||
Log::Level::INFO);
|
||||
|
||||
if (!pendingTunnelData.empty()) {
|
||||
boost::asio::streambuf pendingBuf;
|
||||
copyStringToStreambuf(pendingTunnelData, pendingBuf);
|
||||
client_->doWrite(pendingBuf);
|
||||
}
|
||||
|
||||
const std::string connectEstablished =
|
||||
"HTTP/1.1 200 Connection Established\r\n"
|
||||
"\r\n";
|
||||
|
||||
BoolStr encryption = aes256Encrypt(connectEstablished, config_->general().token);
|
||||
if (!encryption.ok) {
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Encryption Failed] : [ " +
|
||||
encryption.message + "] ",
|
||||
Log::Level::DEBUG);
|
||||
client_->socket().close();
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string encodedResponse = encode64(encryption.message);
|
||||
|
||||
std::ostringstream outer;
|
||||
outer << "HTTP/1.1 200 OK\r\n"
|
||||
<< "Content-Type: application/octet-stream\r\n"
|
||||
<< "Content-Length: " << encodedResponse.size() << "\r\n"
|
||||
<< "Connection: keep-alive\r\n"
|
||||
<< "\r\n"
|
||||
<< encodedResponse;
|
||||
|
||||
copyStringToStreambuf(outer.str(), writeBuffer_);
|
||||
|
||||
connect_ = true;
|
||||
end_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
case HTTP::HttpType::http:
|
||||
case HTTP::HttpType::https: {
|
||||
|
||||
copyStringToStreambuf(innerRequest, readBuffer_);
|
||||
boost::asio::streambuf upstreamRequest;
|
||||
copyStringToStreambuf(innerRequest, upstreamRequest);
|
||||
|
||||
if (!client_->socket().is_open()) {
|
||||
if (!client_->doConnect(inner->dstIP(), inner->dstPort())) {
|
||||
@@ -173,7 +185,7 @@ void ServerHandler::handle() {
|
||||
}
|
||||
}
|
||||
|
||||
client_->doWrite(readBuffer_);
|
||||
client_->doWrite(upstreamRequest);
|
||||
client_->doReadServer();
|
||||
|
||||
if (client_->readBuffer().size() == 0) {
|
||||
@@ -181,27 +193,29 @@ void ServerHandler::handle() {
|
||||
return;
|
||||
}
|
||||
|
||||
BoolStr encryption{false, std::string("FAILED")};
|
||||
encryption = aes256Encrypt(streambufToString(client_->readBuffer()), config_->general().token);
|
||||
const std::string upstreamResponse = streambufToString(client_->readBuffer());
|
||||
|
||||
if (encryption.ok) {
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Encryption Done]", Log::Level::DEBUG);
|
||||
} else {
|
||||
BoolStr encryption = aes256Encrypt(upstreamResponse, config_->general().token);
|
||||
if (!encryption.ok) {
|
||||
log_->write("[" + to_string(uuid_) + "] [ServerHandler handle] [Encryption Failed] : [ " +
|
||||
encryption.message + "] ",
|
||||
Log::Level::DEBUG);
|
||||
client_->socket().close();
|
||||
return;
|
||||
}
|
||||
const std::string innerResponse = encryption.message;
|
||||
|
||||
const std::string encodedResponse = encode64(encryption.message);
|
||||
|
||||
std::ostringstream outer;
|
||||
outer << "HTTP/1.1 200 OK\r\n"
|
||||
<< "Content-Type: application/octet-stream\r\n"
|
||||
<< "Content-Length: " << innerResponse.size() << "\r\n"
|
||||
<< "Content-Length: " << encodedResponse.size() << "\r\n"
|
||||
<< "Connection: keep-alive\r\n"
|
||||
<< "\r\n"
|
||||
<< innerResponse;
|
||||
<< encodedResponse;
|
||||
|
||||
copyStringToStreambuf(outer.str(), writeBuffer_);
|
||||
|
||||
connect_ = false;
|
||||
end_ = false;
|
||||
return;
|
||||
|
||||
+60
-112
@@ -6,118 +6,6 @@
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
|
||||
namespace {
|
||||
|
||||
std::string toLowerCopy(std::string s) {
|
||||
std::transform(s.begin(), s.end(), s.begin(),
|
||||
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string extractHeaders(const std::string &msg) {
|
||||
auto pos = msg.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) return {};
|
||||
return msg.substr(0, pos + 4);
|
||||
}
|
||||
|
||||
std::string extractBody(const std::string &msg) {
|
||||
auto pos = msg.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) return {};
|
||||
return msg.substr(pos + 4);
|
||||
}
|
||||
|
||||
bool parseContentLength(const std::string &headers, std::size_t &value) {
|
||||
std::istringstream iss(headers);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||||
auto lower = toLowerCopy(line);
|
||||
if (lower.rfind("content-length:", 0) == 0) {
|
||||
auto raw = line.substr(std::string("Content-Length:").size());
|
||||
raw.erase(0, raw.find_first_not_of(" \t"));
|
||||
try {
|
||||
value = static_cast<std::size_t>(std::stoull(raw));
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isChunked(const std::string &headers) {
|
||||
std::istringstream iss(headers);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||||
auto lower = toLowerCopy(line);
|
||||
if (lower.rfind("transfer-encoding:", 0) == 0 &&
|
||||
lower.find("chunked") != std::string::npos) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template<typename SyncReadStream>
|
||||
bool readHttpMessage(SyncReadStream &stream,
|
||||
boost::asio::streambuf &out,
|
||||
boost::system::error_code &ec) {
|
||||
out.consume(out.size());
|
||||
|
||||
boost::asio::read_until(stream, out, "\r\n\r\n", ec);
|
||||
if (ec) return false;
|
||||
|
||||
std::string current = streambufToString(out);
|
||||
std::string headers = extractHeaders(current);
|
||||
std::string body = extractBody(current);
|
||||
|
||||
std::size_t contentLength = 0;
|
||||
if (parseContentLength(headers, contentLength)) {
|
||||
if (body.size() < contentLength) {
|
||||
boost::asio::read(stream, out,
|
||||
boost::asio::transfer_exactly(contentLength - body.size()),
|
||||
ec);
|
||||
if (ec) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isChunked(headers)) {
|
||||
for (;;) {
|
||||
current = streambufToString(out);
|
||||
auto bodyPos = current.find("\r\n\r\n");
|
||||
if (bodyPos == std::string::npos) return false;
|
||||
|
||||
std::string bodyPart = current.substr(bodyPos + 4);
|
||||
auto lastChunkPos = bodyPart.find("\r\n0\r\n\r\n");
|
||||
if (lastChunkPos != std::string::npos || bodyPart == "0\r\n\r\n") {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::array<char, 4096> tmp{};
|
||||
std::size_t n = stream.read_some(boost::asio::buffer(tmp), ec);
|
||||
if (ec) return false;
|
||||
std::ostream os(&out);
|
||||
os.write(tmp.data(), static_cast<std::streamsize>(n));
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
std::array<char, 4096> tmp{};
|
||||
std::size_t n = stream.read_some(boost::asio::buffer(tmp), ec);
|
||||
if (ec == boost::asio::error::eof) {
|
||||
ec.clear();
|
||||
return true;
|
||||
}
|
||||
if (ec) return false;
|
||||
std::ostream os(&out);
|
||||
os.write(tmp.data(), static_cast<std::streamsize>(n));
|
||||
}
|
||||
}
|
||||
|
||||
}// namespace
|
||||
|
||||
TCPClient::TCPClient(boost::asio::io_context &io_context,
|
||||
const std::shared_ptr<Config> &config,
|
||||
@@ -134,10 +22,68 @@ TCPClient::TCPClient(boost::asio::io_context &io_context,
|
||||
end_ = false;
|
||||
}
|
||||
|
||||
|
||||
TCPClient::tcp::socket &TCPClient::socket() { return socket_; }
|
||||
|
||||
TCPClient::ssl_stream &TCPClient::sslSocket() { return *sslSocket_; }
|
||||
|
||||
template<typename SyncReadStream>
|
||||
bool readHttpMessage(SyncReadStream &stream,
|
||||
boost::asio::streambuf &out,
|
||||
boost::system::error_code &ec) {
|
||||
out.consume(out.size());
|
||||
|
||||
boost::asio::read_until(stream, out, "\r\n\r\n", ec);
|
||||
if (ec) return false;
|
||||
|
||||
std::string current = streambufToString(out);
|
||||
std::string headers = extractHeaders(current);
|
||||
std::string body = extractBody(current);
|
||||
|
||||
std::size_t contentLength = 0;
|
||||
if (parseContentLength(headers, contentLength)) {
|
||||
if (body.size() < contentLength) {
|
||||
boost::asio::read(stream, out,
|
||||
boost::asio::transfer_exactly(contentLength - body.size()),
|
||||
ec);
|
||||
if (ec) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isChunked(headers)) {
|
||||
for (;;) {
|
||||
current = streambufToString(out);
|
||||
auto bodyPos = current.find("\r\n\r\n");
|
||||
if (bodyPos == std::string::npos) return false;
|
||||
|
||||
std::string bodyPart = current.substr(bodyPos + 4);
|
||||
auto lastChunkPos = bodyPart.find("\r\n0\r\n\r\n");
|
||||
if (lastChunkPos != std::string::npos || bodyPart == "0\r\n\r\n") {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::array<char, 4096> tmp{};
|
||||
std::size_t n = stream.read_some(boost::asio::buffer(tmp), ec);
|
||||
if (ec) return false;
|
||||
std::ostream os(&out);
|
||||
os.write(tmp.data(), static_cast<std::streamsize>(n));
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
std::array<char, 4096> tmp{};
|
||||
std::size_t n = stream.read_some(boost::asio::buffer(tmp), ec);
|
||||
if (ec == boost::asio::error::eof) {
|
||||
ec.clear();
|
||||
return true;
|
||||
}
|
||||
if (ec) return false;
|
||||
std::ostream os(&out);
|
||||
os.write(tmp.data(), static_cast<std::streamsize>(n));
|
||||
}
|
||||
}
|
||||
|
||||
bool TCPClient::tlsEnabled() const { return tlsEnabled_; }
|
||||
|
||||
bool TCPClient::isOpen() const {
|
||||
@@ -357,6 +303,7 @@ void TCPClient::doWrite(boost::asio::streambuf &buffer) {
|
||||
resetTimeout();
|
||||
|
||||
if (tlsEnabled_ && sslSocket_) {
|
||||
OUT(streambufToString(writeBuffer_));
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPClient doWrite] [DST " +
|
||||
sslSocket_->lowest_layer().remote_endpoint().address().to_string() +
|
||||
":" +
|
||||
@@ -365,6 +312,7 @@ void TCPClient::doWrite(boost::asio::streambuf &buffer) {
|
||||
Log::Level::DEBUG);
|
||||
boost::asio::write(*sslSocket_, writeBuffer_, error);
|
||||
} else {
|
||||
OUT(streambufToString(writeBuffer_));
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPClient doWrite] [DST " +
|
||||
socket_.remote_endpoint().address().to_string() + ":" +
|
||||
std::to_string(socket_.remote_endpoint().port()) +
|
||||
|
||||
+788
-364
File diff suppressed because it is too large.
Load diff
+35
-41
@@ -18,94 +18,88 @@
|
||||
class TCPConnection : public boost::enable_shared_from_this<TCPConnection> {
|
||||
public:
|
||||
using pointer = boost::shared_ptr<TCPConnection>;
|
||||
using ssl_stream =
|
||||
boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
|
||||
using ssl_stream = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
|
||||
|
||||
static pointer create(boost::asio::io_context &io_context,
|
||||
static pointer create(boost::asio::io_context &ioContext,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log,
|
||||
TCPClient::pointer client) {
|
||||
return pointer(new TCPConnection(io_context, config, log, client));
|
||||
}
|
||||
TCPClient::pointer client);
|
||||
|
||||
boost::asio::ip::tcp::socket &socket();
|
||||
|
||||
inline TCPClient::pointer client() { return client_; }
|
||||
|
||||
ssl_stream &tlsSocket();
|
||||
|
||||
bool initTlsServerContext();
|
||||
TCPClient::pointer client() const { return client_; }
|
||||
|
||||
bool initTlsServerContext();
|
||||
bool doHandshakeServer();
|
||||
|
||||
inline void writeBuffer(boost::asio::streambuf &buffer) {
|
||||
moveStreambuf(buffer, writeBuffer_);
|
||||
}
|
||||
void writeBuffer(boost::asio::streambuf &buffer);
|
||||
const boost::asio::streambuf &readBuffer() const & { return readBuffer_; }
|
||||
|
||||
inline const boost::asio::streambuf &readBuffer() & { return readBuffer_; }
|
||||
|
||||
inline const boost::asio::streambuf &&readBuffer() && {
|
||||
return std::move(readBuffer_);
|
||||
}
|
||||
|
||||
inline boost::uuids::uuid uuid() { return uuid_; }
|
||||
boost::uuids::uuid uuid() const { return uuid_; }
|
||||
|
||||
void startAgent();
|
||||
|
||||
void startServer();
|
||||
|
||||
void doReadAgent();
|
||||
|
||||
void doReadServer();
|
||||
|
||||
void handleReadAgent(const boost::system::error_code &error,
|
||||
size_t bytes_transferred);
|
||||
std::size_t bytesTransferred);
|
||||
|
||||
void handleReadServer(const boost::system::error_code &error,
|
||||
size_t bytes_transferred);
|
||||
|
||||
void socketShutdown();
|
||||
std::size_t bytesTransferred);
|
||||
|
||||
void enableTunnelMode();
|
||||
|
||||
void relayClientToRemote();
|
||||
|
||||
void relayRemoteToClient();
|
||||
|
||||
void socketShutdown();
|
||||
|
||||
private:
|
||||
explicit TCPConnection(boost::asio::io_context &io_context,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log,
|
||||
TCPClient::pointer client);
|
||||
TCPConnection(boost::asio::io_context &ioContext,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log,
|
||||
TCPClient::pointer client);
|
||||
|
||||
void resetTimeout();
|
||||
|
||||
void cancelTimeout();
|
||||
|
||||
void onTimeout(const boost::system::error_code &error);
|
||||
|
||||
void clearTunnelBuffers();
|
||||
|
||||
private:
|
||||
boost::asio::ip::tcp::socket socket_;
|
||||
boost::asio::ssl::context sslContext_;
|
||||
std::unique_ptr<ssl_stream> tlsSocket_;
|
||||
|
||||
const std::shared_ptr<Config> &config_;
|
||||
const std::shared_ptr<Log> &log_;
|
||||
boost::asio::io_context &io_context_;
|
||||
std::shared_ptr<Config> config_;
|
||||
std::shared_ptr<Log> log_;
|
||||
boost::asio::io_context &ioContext_;
|
||||
TCPClient::pointer client_;
|
||||
|
||||
boost::asio::streambuf readBuffer_;
|
||||
boost::asio::streambuf writeBuffer_;
|
||||
|
||||
boost::asio::streambuf downstreamHttpBuffer_;
|
||||
boost::asio::streambuf upstreamHttpBuffer_;
|
||||
|
||||
boost::asio::steady_timer timeout_;
|
||||
boost::asio::strand<boost::asio::io_context::executor_type> strand_;
|
||||
|
||||
boost::uuids::uuid uuid_;
|
||||
std::mutex mutex_;
|
||||
|
||||
bool end_;
|
||||
bool connect_;
|
||||
bool tunnelMode_;
|
||||
bool end_{false};
|
||||
bool connect_{false};
|
||||
bool tunnelMode_{false};
|
||||
|
||||
std::array<char, 16384> downstreamBuf_;
|
||||
std::array<char, 16384> upstreamBuf_;
|
||||
bool tunnelRequestHeaderSent_{false};
|
||||
bool tunnelResponseHeaderSent_{false};
|
||||
bool tunnelRequestHeaderReceived_{false};
|
||||
bool tunnelResponseHeaderReceived_{false};
|
||||
|
||||
std::array<char, 16384> downstreamBuf_{};
|
||||
std::array<char, 16384> upstreamBuf_{};
|
||||
};
|
||||
@@ -5,7 +5,7 @@ general:
|
||||
token: "af445adb-2434-4975-9445-2c1b2231"
|
||||
# fakeUrl: string
|
||||
# Defines the fake url and endpoint between agent and server
|
||||
fakeUrl: "http://nipo.ciron.net"
|
||||
fakeUrl: "https://nipo.ciron.net"
|
||||
# method: "HEAD|GET|POST|PUT|DELETE"
|
||||
# Defines with which method
|
||||
# you like to send requests from agent to server
|
||||
@@ -34,7 +34,7 @@ server:
|
||||
# listenIp: which must be IPv4 which is used on server
|
||||
listenIp: "0.0.0.0"
|
||||
# listenPort: the port number which will be used on server
|
||||
listenPort: 8081
|
||||
listenPort: 1081
|
||||
tlsEnable: false
|
||||
tlsVerifyPeer: false
|
||||
tlsCertFile: "/etc/nipovpn/server.crt"
|
||||
@@ -52,7 +52,7 @@ agent:
|
||||
# serverIp: IPv4 of the nipoServer
|
||||
serverIp: "127.0.0.10"
|
||||
# serverPort: port number of the nipoServer
|
||||
serverPort: 8081
|
||||
serverPort: 1081
|
||||
# httpVersion: This version will be used between agent and server
|
||||
httpVersion: "1.1"
|
||||
# userAgent: This is the http user agent which will be used between agent and server
|
||||
|
||||
Reference in new issue
Block a user