Compare commits
5
Commits
main
...
v.0.1.1.39
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
323d227ce8 | ||
|
|
fb6804b8fb | ||
|
|
4a95a19d81 | ||
|
|
8158ac4ddc | ||
|
|
c68f588040 |
No files matched your search
@@ -86,7 +86,7 @@ void AgentHandler::handle() {
|
||||
request_->httpType() == HTTP::HttpType::connect ? "open" : "request";
|
||||
|
||||
std::ostringstream outer;
|
||||
outer << config_->randomMethod() + " /" + config_->randomEndPoint() + " HTTP/1.1\r\n"
|
||||
outer << config_->randomMethod() + " /" + config_->randomEndPoint() + " HTTP/" + config_->agent().httpVersion + "\r\n"
|
||||
<< "Host: " << relayHostHeader(config_) << "\r\n"
|
||||
<< "User-Agent: Mozilla/5.0\r\n"
|
||||
<< "Accept: */*\r\n"
|
||||
@@ -94,7 +94,9 @@ void AgentHandler::handle() {
|
||||
<< "X-Nipo-Session: " << to_string(uuid_) << "\r\n"
|
||||
<< "X-Nipo-Action: " << action << "\r\n"
|
||||
<< "Content-Length: " << innerRequest.size() << "\r\n"
|
||||
<< "Connection: close\r\n"
|
||||
<< "Connection: "
|
||||
<< (config_->general().connectionReuse ? "keep-alive" : "close")
|
||||
<< "\r\n"
|
||||
<< "\r\n"
|
||||
<< innerRequest;
|
||||
|
||||
|
||||
+3
-1
@@ -13,7 +13,8 @@ Config::Config(const RunMode &mode, const std::string &filePath)
|
||||
configYaml_["general"]["methods"].as<std::vector<std::string>>(),
|
||||
configYaml_["general"]["endPoints"].as<std::vector<std::string>>(),
|
||||
configYaml_["general"]["timeout"].as<unsigned short>(),
|
||||
configYaml_["general"]["tunnelEnable"].as<bool>(false)}),
|
||||
configYaml_["general"]["tunnelEnable"].as<bool>(false),
|
||||
configYaml_["general"]["connectionReuse"].as<bool>(false)}),
|
||||
log_({configYaml_["log"]["logLevel"].as<std::string>(),
|
||||
configYaml_["log"]["logFile"].as<std::string>()}),
|
||||
server_({configYaml_["server"]["threads"].as<unsigned short>(),
|
||||
@@ -99,6 +100,7 @@ std::string Config::toString() const {
|
||||
ss << "\n"
|
||||
<< " timeout: " << general_.timeout << "\n"
|
||||
<< " tunnelEnable: " << std::boolalpha << general_.tunnelEnable << "\n"
|
||||
<< " connectionReuse: " << std::boolalpha << general_.connectionReuse << "\n"
|
||||
|
||||
<< " Log :\n"
|
||||
<< " logLevel: " << log_.level << "\n"
|
||||
|
||||
@@ -25,6 +25,7 @@ private:
|
||||
std::vector<std::string> endPoints;
|
||||
unsigned short timeout;
|
||||
bool tunnelEnable;
|
||||
bool connectionReuse;
|
||||
};
|
||||
|
||||
struct Log {
|
||||
|
||||
@@ -330,6 +330,7 @@ inline BoolStr validateConfig(int argc, const char *argv[]) {
|
||||
configYaml["general"]["endPoints"].as<std::vector<std::string>>();
|
||||
configYaml["general"]["timeout"].as<unsigned short>();
|
||||
configYaml["general"]["tunnelEnable"].as<bool>();
|
||||
configYaml["general"]["connectionReuse"].as<bool>();
|
||||
} catch (const std::exception &e) {
|
||||
result.message = std::string("Error in 'general' block: ") + e.what() + "\n";
|
||||
return result;
|
||||
|
||||
+1
-1
@@ -177,7 +177,7 @@ const std::string HTTP::genHttpRestPostReqString() const {
|
||||
}
|
||||
|
||||
const std::string HTTP::genHttpOkResString(const std::string &body) const {
|
||||
return std::string("HTTP/1.1 200 OK\r\n") +
|
||||
return std::string("HTTP/" + config_->agent().httpVersion + " 200 OK\r\n") +
|
||||
"Content-Type: application/x-www-form-urlencoded\r\n" +
|
||||
"Content-Length: " + std::to_string(body.length()) + "\r\n" +
|
||||
"Connection: keep-alive\r\n" + "Cache-Control: no-cache\r\n" +
|
||||
|
||||
+45
-18
@@ -90,7 +90,7 @@ void ServerHandler::makePlainHttpResponse(const std::string &body,
|
||||
const std::string &connection) {
|
||||
std::ostringstream outer;
|
||||
|
||||
outer << "HTTP/1.1 " << status << "\r\n"
|
||||
outer << "HTTP/" << config_->agent().httpVersion << status << "\r\n"
|
||||
<< "Content-Type: application/octet-stream\r\n"
|
||||
<< "Content-Length: " << body.size() << "\r\n"
|
||||
<< "Connection: " << connection << "\r\n"
|
||||
@@ -198,7 +198,10 @@ void ServerHandler::handle() {
|
||||
}
|
||||
|
||||
if (!targetClient || !targetClient->isOpen()) {
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse(
|
||||
"",
|
||||
"200 OK",
|
||||
config_->general().connectionReuse ? "keep-alive" : "close");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -206,7 +209,10 @@ void ServerHandler::handle() {
|
||||
copyStringToStreambuf(hexToASCII(decryption.message), tunnelBuf);
|
||||
targetClient->doWrite(tunnelBuf);
|
||||
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse(
|
||||
"",
|
||||
"200 OK",
|
||||
config_->general().connectionReuse ? "keep-alive" : "close");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -223,7 +229,10 @@ void ServerHandler::handle() {
|
||||
}
|
||||
|
||||
if (!targetClient || !targetClient->isOpen()) {
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse(
|
||||
"",
|
||||
"200 OK",
|
||||
config_->general().connectionReuse ? "keep-alive" : "close");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -231,7 +240,10 @@ void ServerHandler::handle() {
|
||||
std::size_t available = targetClient->socket().available(ec);
|
||||
|
||||
if (ec || available == 0) {
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse(
|
||||
"",
|
||||
"200 OK",
|
||||
config_->general().connectionReuse ? "keep-alive" : "close");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -241,7 +253,10 @@ void ServerHandler::handle() {
|
||||
ec);
|
||||
|
||||
if (ec || n == 0) {
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse(
|
||||
"",
|
||||
"200 OK",
|
||||
config_->general().connectionReuse ? "keep-alive" : "close");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -262,7 +277,10 @@ void ServerHandler::handle() {
|
||||
sessions_.erase(it);
|
||||
}
|
||||
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse(
|
||||
"",
|
||||
"200 OK",
|
||||
config_->general().connectionReuse ? "keep-alive" : "close");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -310,7 +328,7 @@ void ServerHandler::handle() {
|
||||
Log::Level::INFO);
|
||||
|
||||
makeEncryptedHttpResponse(
|
||||
"HTTP/1.1 502 Bad Gateway\r\n\r\n",
|
||||
"HTTP/" + config_->agent().httpVersion + "502 Bad Gateway\r\n\r\n",
|
||||
"502 Bad Gateway",
|
||||
"close");
|
||||
|
||||
@@ -327,7 +345,7 @@ void ServerHandler::handle() {
|
||||
Log::Level::INFO);
|
||||
|
||||
makeEncryptedHttpResponse(
|
||||
"HTTP/1.1 200 Connection Established\r\n\r\n",
|
||||
"HTTP/" + config_->agent().httpVersion + " 200 Connection Established\r\n\r\n",
|
||||
"200 OK",
|
||||
"keep-alive");
|
||||
|
||||
@@ -353,7 +371,7 @@ void ServerHandler::handle() {
|
||||
Log::Level::INFO);
|
||||
|
||||
makeEncryptedHttpResponse(
|
||||
"HTTP/1.1 502 Bad Gateway\r\n\r\n",
|
||||
"HTTP/" + config_->agent().httpVersion + " 502 Bad Gateway\r\n\r\n",
|
||||
"502 Bad Gateway",
|
||||
"close");
|
||||
|
||||
@@ -381,7 +399,7 @@ void ServerHandler::handle() {
|
||||
}
|
||||
|
||||
makeEncryptedHttpResponse(
|
||||
"HTTP/1.1 200 Connection Established\r\n\r\n",
|
||||
"HTTP/" + config_->agent().httpVersion + " 200 Connection Established\r\n\r\n",
|
||||
"200 OK",
|
||||
"close");
|
||||
|
||||
@@ -394,11 +412,13 @@ void ServerHandler::handle() {
|
||||
case HTTP::HttpType::https: {
|
||||
copyStringToStreambuf(innerRequest, readBuffer_);
|
||||
|
||||
if (!client_->socket().is_open()) {
|
||||
if (!client_->doConnect(inner->dstIP(), inner->dstPort())) {
|
||||
client_->socket().close();
|
||||
makeEncryptedHttpResponse("", "502 Bad Gateway", "close");
|
||||
return;
|
||||
if (!config_->general().connectionReuse) {
|
||||
if (!client_->socket().is_open()) {
|
||||
if (!client_->doConnect(inner->dstIP(), inner->dstPort())) {
|
||||
client_->socket().close();
|
||||
makeEncryptedHttpResponse("", "502 Bad Gateway", "close");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -407,14 +427,21 @@ void ServerHandler::handle() {
|
||||
|
||||
if (client_->readBuffer().size() == 0) {
|
||||
client_->socket().close();
|
||||
makeEncryptedHttpResponse("", "200 OK", "close");
|
||||
makeEncryptedHttpResponse(
|
||||
"",
|
||||
"200 OK",
|
||||
config_->general().connectionReuse ? "keep-alive" : "close");
|
||||
return;
|
||||
}
|
||||
|
||||
makeEncryptedHttpResponse(
|
||||
streambufToString(client_->readBuffer()),
|
||||
"200 OK",
|
||||
"close");
|
||||
config_->general().connectionReuse ? "keep-alive" : "close");
|
||||
|
||||
if (!config_->general().connectionReuse) {
|
||||
client_->socketShutdown();
|
||||
}
|
||||
|
||||
connect_ = false;
|
||||
end_ = false;
|
||||
|
||||
@@ -167,14 +167,14 @@ bool TCPClient::doHandshakeClient() {
|
||||
if (SSL_set_alpn_protos(sslSocket_->native_handle(),
|
||||
alpn, sizeof(alpn)) != 0) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TLS] Failed to set ALPN http/1.1",
|
||||
"] [TLS] Failed to set ALPN http/" + config_->agent().httpVersion,
|
||||
Log::Level::ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TLS] Using SNI/ALPN host: " + sniHost +
|
||||
" [ALPN http/1.1]",
|
||||
" [ALPN http/" + config_->agent().httpVersion + "]",
|
||||
Log::Level::DEBUG);
|
||||
}
|
||||
}
|
||||
@@ -193,7 +193,7 @@ bool TCPClient::doHandshakeClient() {
|
||||
"] [TLS] Negotiated ALPN: " + negotiated,
|
||||
Log::Level::DEBUG);
|
||||
|
||||
if (negotiated != "http/1.1") {
|
||||
if (negotiated != "http/" + config_->agent().httpVersion) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TLS] Unsupported negotiated ALPN: " + negotiated,
|
||||
Log::Level::ERROR);
|
||||
|
||||
+35
-25
@@ -332,26 +332,21 @@ void TCPConnection::handleReadServer(const boost::system::error_code &error, siz
|
||||
|
||||
resetTimeout();
|
||||
|
||||
auto writeHandler =
|
||||
[self = shared_from_this()](
|
||||
const boost::system::error_code &ec,
|
||||
std::size_t) {
|
||||
self->cancelTimeout();
|
||||
auto writeHandler = [self = shared_from_this()](const boost::system::error_code &ec, std::size_t) {
|
||||
self->cancelTimeout();
|
||||
|
||||
if (ec) {
|
||||
self->socketShutdown();
|
||||
return;
|
||||
}
|
||||
if (ec) {
|
||||
self->socketShutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
if (self->connect_ && self->config_->general().tunnelEnable) {
|
||||
self->enableDirectTunnelMode();
|
||||
self->relayAgentToTarget();
|
||||
self->relayTargetToAgent();
|
||||
return;
|
||||
}
|
||||
if (self->config_->general().connectionReuse) {
|
||||
self->doReadServer();
|
||||
return;
|
||||
}
|
||||
|
||||
self->socketShutdown();
|
||||
};
|
||||
self->socketShutdown();
|
||||
};
|
||||
|
||||
if (config_->server().tlsEnable) {
|
||||
boost::asio::async_write(
|
||||
@@ -388,12 +383,22 @@ std::string TCPConnection::makeRelayHostHeader() const {
|
||||
}
|
||||
|
||||
std::string TCPConnection::postTunnelAction(const std::string &action, const std::string &rawBody) {
|
||||
TCPClient::pointer c = TCPClient::create(io_context_, config_, log_);
|
||||
c->uuid_ = uuid_;
|
||||
if (!tunnelRelayClient_) {
|
||||
tunnelRelayClient_ = TCPClient::create(io_context_, config_, log_);
|
||||
tunnelRelayClient_->uuid_ = uuid_;
|
||||
}
|
||||
|
||||
if (config_->agent().tlsEnable && !c->enableTlsClient()) return "";
|
||||
if (!c->doConnect(config_->agent().serverIp, config_->agent().serverPort)) return "";
|
||||
if (c->tlsEnabled() && !c->doHandshakeClient()) return "";
|
||||
TCPClient::pointer c = tunnelRelayClient_;
|
||||
|
||||
if (config_->agent().tlsEnable && !c->tlsEnabled()) {
|
||||
if (!c->enableTlsClient()) return "";
|
||||
}
|
||||
|
||||
if (!c->isOpen()) {
|
||||
if (!c->doConnect(config_->agent().serverIp, config_->agent().serverPort)) return "";
|
||||
|
||||
if (c->tlsEnabled() && !c->doHandshakeClient()) return "";
|
||||
}
|
||||
|
||||
BoolStr enc = aes256Encrypt(
|
||||
hexArrToStr(reinterpret_cast<const unsigned char *>(rawBody.data()), rawBody.size()),
|
||||
@@ -404,7 +409,7 @@ std::string TCPConnection::postTunnelAction(const std::string &action, const std
|
||||
const std::string encodedBody = encode64(enc.message);
|
||||
|
||||
std::ostringstream req;
|
||||
req << config_->randomMethod() + " /" + config_->randomEndPoint() + " HTTP/1.1\r\n"
|
||||
req << config_->randomMethod() + " /" + config_->randomEndPoint() + " HTTP/" + config_->agent().httpVersion + "\r\n"
|
||||
<< "Host: " << makeRelayHostHeader() << "\r\n"
|
||||
<< "User-Agent: Mozilla/5.0\r\n"
|
||||
<< "Accept: */*\r\n"
|
||||
@@ -412,7 +417,9 @@ std::string TCPConnection::postTunnelAction(const std::string &action, const std
|
||||
<< "X-Nipo-Session: " << to_string(uuid_) << "\r\n"
|
||||
<< "X-Nipo-Action: " << action << "\r\n"
|
||||
<< "Content-Length: " << encodedBody.size() << "\r\n"
|
||||
<< "Connection: close\r\n"
|
||||
<< "Connection: "
|
||||
<< (config_->general().connectionReuse ? "keep-alive" : "close")
|
||||
<< "\r\n"
|
||||
<< "\r\n"
|
||||
<< encodedBody;
|
||||
|
||||
@@ -424,7 +431,10 @@ std::string TCPConnection::postTunnelAction(const std::string &action, const std
|
||||
|
||||
const std::string response = streambufToString(c->readBuffer());
|
||||
|
||||
c->socketShutdown();
|
||||
if (!config_->general().connectionReuse || action == "close") {
|
||||
c->socketShutdown();
|
||||
tunnelRelayClient_.reset();
|
||||
}
|
||||
|
||||
const auto pos = response.find("\r\n\r\n");
|
||||
if (pos == std::string::npos) return "";
|
||||
|
||||
@@ -34,6 +34,7 @@ public:
|
||||
boost::asio::ip::tcp::socket &socket();
|
||||
inline TCPClient::pointer client() { return client_; }
|
||||
ssl_stream &tlsSocket();
|
||||
TCPClient::pointer tunnelRelayClient_;
|
||||
|
||||
bool initTlsServerContext();
|
||||
bool doHandshakeServer();
|
||||
|
||||
@@ -33,6 +33,10 @@ general:
|
||||
# Degines the connection mode between agent and server. If enabled then the traffic
|
||||
# will be TCP/TLS tunnel like.
|
||||
tunnelEnable: false
|
||||
# tunnelEnable: bool
|
||||
# Degines the connection mode between agent and server. If enabled then the traffic
|
||||
# will be TCP/TLS tunnel like.
|
||||
connectionReuse: true
|
||||
|
||||
|
||||
# This block is to define log directives
|
||||
|
||||
Reference in new issue
Block a user