Compare commits
5
Commits
dev-v0.2.59
...
v1.1.55
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
277321306b | ||
|
|
d6e0161aa5 | ||
|
|
14b0947140 | ||
|
|
134251779c | ||
|
|
92d877be31 |
No files matched your search
@@ -16,7 +16,7 @@ permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
PACKAGE_VERSION: 1.1.54
|
||||
PACKAGE_VERSION: 1.1.55
|
||||
|
||||
jobs:
|
||||
build-android:
|
||||
|
||||
@@ -14,7 +14,7 @@ permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
PACKAGE_VERSION: 1.1.54
|
||||
PACKAGE_VERSION: 1.1.55
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
@@ -15,7 +15,7 @@ permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
PACKAGE_VERSION: 1.1.54
|
||||
PACKAGE_VERSION: 1.1.55
|
||||
|
||||
jobs:
|
||||
build-windows:
|
||||
|
||||
+78
-14
@@ -4,6 +4,8 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -93,9 +95,16 @@ bool TCPClient::enableTlsClient() {
|
||||
}
|
||||
}
|
||||
|
||||
bool TCPClient::doConnect(const std::string &dstIp, unsigned short dstPort) {
|
||||
|
||||
bool TCPClient::doConnect(const std::string &dstIP,
|
||||
unsigned short dstPort) {
|
||||
try {
|
||||
if (dstPort == 0) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPClient doConnect] invalid destination port 0",
|
||||
Log::Level::ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tlsEnabled_ && sslSocket_ && sslSocket_->lowest_layer().is_open()) {
|
||||
return true;
|
||||
}
|
||||
@@ -104,15 +113,15 @@ bool TCPClient::doConnect(const std::string &dstIp, unsigned short dstPort) {
|
||||
return true;
|
||||
}
|
||||
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPClient doConnect] [DST " + dstIp +
|
||||
":" + std::to_string(dstPort) + "]",
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPClient doConnect] [DST " +
|
||||
dstIP + ":" + std::to_string(dstPort) + "]",
|
||||
Log::Level::DEBUG);
|
||||
|
||||
boost::system::error_code error_code;
|
||||
|
||||
auto endpoint = resolver_.resolve(
|
||||
dstIp.c_str(),
|
||||
std::to_string(dstPort).c_str(),
|
||||
auto endpoints = resolver_.resolve(
|
||||
dstIP,
|
||||
std::to_string(dstPort),
|
||||
error_code);
|
||||
|
||||
if (error_code) {
|
||||
@@ -123,35 +132,90 @@ bool TCPClient::doConnect(const std::string &dstIp, unsigned short dstPort) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto timer = std::make_shared<boost::asio::steady_timer>(io_context_);
|
||||
auto finished = std::make_shared<bool>(false);
|
||||
auto connectError = std::make_shared<boost::system::error_code>();
|
||||
|
||||
auto closeSocket = [this]() {
|
||||
boost::system::error_code ignored;
|
||||
|
||||
if (tlsEnabled_ && sslSocket_) {
|
||||
sslSocket_->lowest_layer().cancel(ignored);
|
||||
sslSocket_->lowest_layer().close(ignored);
|
||||
} else {
|
||||
socket_.cancel(ignored);
|
||||
socket_.close(ignored);
|
||||
}
|
||||
};
|
||||
|
||||
timer->expires_after(std::chrono::seconds(5));
|
||||
timer->async_wait([this, finished, closeSocket](
|
||||
const boost::system::error_code &ec) {
|
||||
if (ec) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!*finished) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPClient doConnect] connect timeout",
|
||||
Log::Level::ERROR);
|
||||
closeSocket();
|
||||
}
|
||||
});
|
||||
|
||||
if (tlsEnabled_) {
|
||||
if (!sslSocket_ || closed_ || !sslSocket_->lowest_layer().is_open()) {
|
||||
sslSocket_ = std::make_unique<ssl_stream>(io_context_, sslContext_);
|
||||
closed_ = false;
|
||||
}
|
||||
|
||||
boost::asio::connect(sslSocket_->lowest_layer(), endpoint, error_code);
|
||||
boost::asio::async_connect(
|
||||
sslSocket_->lowest_layer(),
|
||||
endpoints,
|
||||
[finished, connectError, timer](
|
||||
const boost::system::error_code &ec,
|
||||
const boost::asio::ip::tcp::endpoint &) {
|
||||
*connectError = ec;
|
||||
*finished = true;
|
||||
|
||||
timer->cancel();
|
||||
});
|
||||
} else {
|
||||
if (!socket_.is_open()) {
|
||||
socket_ = tcp::socket(io_context_);
|
||||
closed_ = false;
|
||||
}
|
||||
|
||||
boost::asio::connect(socket_, endpoint, error_code);
|
||||
boost::asio::async_connect(
|
||||
socket_,
|
||||
endpoints,
|
||||
[finished, connectError, timer](
|
||||
const boost::system::error_code &ec,
|
||||
const boost::asio::ip::tcp::endpoint &) {
|
||||
*connectError = ec;
|
||||
*finished = true;
|
||||
|
||||
timer->cancel();
|
||||
});
|
||||
}
|
||||
|
||||
if (error_code) {
|
||||
while (!*finished) {
|
||||
io_context_.run_one();
|
||||
}
|
||||
|
||||
io_context_.restart();
|
||||
|
||||
if (*connectError) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPClient doConnect] connect error: " +
|
||||
error_code.message(),
|
||||
connectError->message(),
|
||||
Log::Level::ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} catch (std::exception &error) {
|
||||
log_->write("[" + to_string(uuid_) +
|
||||
"] [TCPClient doConnect] " +
|
||||
log_->write("[" + to_string(uuid_) + "] [TCPClient doConnect] " +
|
||||
error.what(),
|
||||
Log::Level::ERROR);
|
||||
return false;
|
||||
|
||||
+74
-31
@@ -1,5 +1,8 @@
|
||||
#include "tcpserver.hpp"
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
|
||||
TCPServer::TCPServer(boost::asio::io_context &io_context,
|
||||
const std::shared_ptr<Config> &config,
|
||||
const std::shared_ptr<Log> &log)
|
||||
@@ -11,7 +14,6 @@ TCPServer::TCPServer(boost::asio::io_context &io_context,
|
||||
boost::asio::ip::make_address(config->listenIp()),
|
||||
config->listenPort())) {
|
||||
acceptor_.set_option(boost::asio::socket_base::reuse_address(true));
|
||||
|
||||
startAccept();
|
||||
}
|
||||
|
||||
@@ -28,45 +30,86 @@ void TCPServer::startAccept() {
|
||||
|
||||
void TCPServer::handleAccept(TCPConnection::pointer connection,
|
||||
const boost::system::error_code &error) {
|
||||
/*
|
||||
* IMPORTANT:
|
||||
* Do not wait for TLS handshake before accepting the next client.
|
||||
* Bad/half-open TLS clients can otherwise occupy all io threads and
|
||||
* the server looks frozen until restart.
|
||||
*/
|
||||
startAccept();
|
||||
|
||||
try {
|
||||
if (!error) {
|
||||
if (config_->runMode() == RunMode::server) {
|
||||
if (config_->general().tlsEnable) {
|
||||
if (!connection->initTlsServerContext()) {
|
||||
log_->write("[TCPServer handleAccept] TLS server context init failed",
|
||||
Log::Level::ERROR);
|
||||
connection->socketShutdown();
|
||||
startAccept();
|
||||
return;
|
||||
}
|
||||
|
||||
connection->tlsSocket().lowest_layer() =
|
||||
std::move(connection->socket());
|
||||
|
||||
if (!connection->doHandshakeServer()) {
|
||||
log_->write("[TCPServer handleAccept] TLS server handshake failed",
|
||||
Log::Level::ERROR);
|
||||
connection->socketShutdown();
|
||||
startAccept();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
connection->startServer();
|
||||
} else {
|
||||
connection->startAgent();
|
||||
}
|
||||
} else {
|
||||
if (error) {
|
||||
log_->write("[TCPServer handleAccept] Error: " + error.message(),
|
||||
Log::Level::ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
if (config_->runMode() != RunMode::server) {
|
||||
connection->startAgent();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!config_->general().tlsEnable) {
|
||||
connection->startServer();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!connection->initTlsServerContext()) {
|
||||
log_->write("[TCPServer handleAccept] TLS server context init failed",
|
||||
Log::Level::ERROR);
|
||||
connection->socketShutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
connection->tlsSocket().lowest_layer() = std::move(connection->socket());
|
||||
|
||||
auto done = std::make_shared<std::atomic_bool>(false);
|
||||
auto timer = std::make_shared<boost::asio::steady_timer>(io_context_);
|
||||
|
||||
timer->expires_after(std::chrono::seconds(5));
|
||||
timer->async_wait([this, connection, done](const boost::system::error_code &ec) {
|
||||
if (ec) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!done->exchange(true)) {
|
||||
log_->write("[TCPServer handleAccept] TLS server handshake timeout",
|
||||
Log::Level::DEBUG);
|
||||
connection->socketShutdown();
|
||||
}
|
||||
});
|
||||
|
||||
connection->tlsSocket().async_handshake(
|
||||
boost::asio::ssl::stream_base::server,
|
||||
[this, connection, timer, done](const boost::system::error_code &ec) {
|
||||
timer->cancel();
|
||||
|
||||
if (done->exchange(true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ec) {
|
||||
/*
|
||||
* stream truncated is common when random scanners or
|
||||
* non-Nipo clients hit port 443. Do not treat it as fatal.
|
||||
*/
|
||||
log_->write("[TCPServer handleAccept] TLS server handshake failed: " + ec.message(),
|
||||
Log::Level::DEBUG);
|
||||
connection->socketShutdown();
|
||||
return;
|
||||
}
|
||||
|
||||
connection->startServer();
|
||||
});
|
||||
|
||||
} catch (const std::exception &ex) {
|
||||
log_->write("[TCPServer handleAccept] Exception: " + std::string(ex.what()),
|
||||
Log::Level::ERROR);
|
||||
connection->socketShutdown();
|
||||
} catch (...) {
|
||||
log_->write("[TCPServer handleAccept] Unknown exception",
|
||||
Log::Level::ERROR);
|
||||
connection->socketShutdown();
|
||||
}
|
||||
|
||||
startAccept();
|
||||
}
|
||||
Reference in new issue
Block a user