diff --git a/.github/workflows/android-main.yml b/.github/workflows/android-main.yml index faaefcb..2bd3e5a 100644 --- a/.github/workflows/android-main.yml +++ b/.github/workflows/android-main.yml @@ -16,7 +16,7 @@ permissions: contents: write env: - PACKAGE_VERSION: 1.1.55 + PACKAGE_VERSION: 1.1.54 jobs: build-android: diff --git a/.github/workflows/debian-main.yml b/.github/workflows/debian-main.yml index d7ca0d1..a93349f 100644 --- a/.github/workflows/debian-main.yml +++ b/.github/workflows/debian-main.yml @@ -14,7 +14,7 @@ permissions: contents: write env: - PACKAGE_VERSION: 1.1.55 + PACKAGE_VERSION: 1.1.54 jobs: build: diff --git a/.github/workflows/windows-main.yml b/.github/workflows/windows-main.yml index 6edb5d9..58b2139 100644 --- a/.github/workflows/windows-main.yml +++ b/.github/workflows/windows-main.yml @@ -15,7 +15,7 @@ permissions: contents: write env: - PACKAGE_VERSION: 1.1.55 + PACKAGE_VERSION: 1.1.54 jobs: build-windows: diff --git a/core/src/tcpclient.cpp b/core/src/tcpclient.cpp index f7b6925..ae3f124 100644 --- a/core/src/tcpclient.cpp +++ b/core/src/tcpclient.cpp @@ -4,8 +4,6 @@ #include #include -#include -#include #include #include #include @@ -95,16 +93,9 @@ bool TCPClient::enableTlsClient() { } } -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; - } +bool TCPClient::doConnect(const std::string &dstIp, unsigned short dstPort) { + try { if (tlsEnabled_ && sslSocket_ && sslSocket_->lowest_layer().is_open()) { return true; } @@ -113,15 +104,15 @@ bool TCPClient::doConnect(const std::string &dstIP, 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 endpoints = resolver_.resolve( - dstIP, - std::to_string(dstPort), + auto endpoint = resolver_.resolve( + dstIp.c_str(), + std::to_string(dstPort).c_str(), error_code); if (error_code) { @@ -132,90 +123,35 @@ bool TCPClient::doConnect(const std::string &dstIP, return false; } - auto timer = std::make_shared(io_context_); - auto finished = std::make_shared(false); - auto connectError = std::make_shared(); - - 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(io_context_, sslContext_); closed_ = false; } - 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(); - }); + boost::asio::connect(sslSocket_->lowest_layer(), endpoint, error_code); } else { if (!socket_.is_open()) { socket_ = tcp::socket(io_context_); closed_ = false; } - 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(); - }); + boost::asio::connect(socket_, endpoint, error_code); } - while (!*finished) { - io_context_.run_one(); - } - - io_context_.restart(); - - if (*connectError) { + if (error_code) { log_->write("[" + to_string(uuid_) + "] [TCPClient doConnect] connect error: " + - connectError->message(), + error_code.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; diff --git a/core/src/tcpserver.cpp b/core/src/tcpserver.cpp index e48e3ff..c578905 100644 --- a/core/src/tcpserver.cpp +++ b/core/src/tcpserver.cpp @@ -1,8 +1,5 @@ #include "tcpserver.hpp" -#include -#include - TCPServer::TCPServer(boost::asio::io_context &io_context, const std::shared_ptr &config, const std::shared_ptr &log) @@ -14,6 +11,7 @@ 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(); } @@ -30,86 +28,45 @@ 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 (!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 { 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(false); - auto timer = std::make_shared(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(); } \ No newline at end of file