diff --git a/src/wesnothd_connection.cpp b/src/wesnothd_connection.cpp index 61e1db88ea6e..c02b1f5385ef 100644 --- a/src/wesnothd_connection.cpp +++ b/src/wesnothd_connection.cpp @@ -165,8 +165,10 @@ void wesnothd_connection::handle_handshake(const error_code& ec) { MPTEST_LOG; if(ec) { - if(ec == boost::asio::error::eof) { - throw std::runtime_error("Failed to complete handshake with server"); + if(ec == boost::asio::error::eof && use_tls_) { + // immediate disconnect likely means old server not supporting TLS handshake code + fallback_to_unencrypted(); + return; } LOG_NW << __func__ << " Throwing: " << ec << "\n"; throw system_error(ec); @@ -174,7 +176,8 @@ void wesnothd_connection::handle_handshake(const error_code& ec) if(use_tls_) { if(handshake_response_.num == 0xFFFFFFFFU) { - throw std::runtime_error("The server doesn't support TLS"); + fallback_to_unencrypted(); + return; } if(handshake_response_.num == 0x00000000) { @@ -203,13 +206,26 @@ void wesnothd_connection::handle_handshake(const error_code& ec) return; } - throw std::runtime_error("Invalid handshake"); + fallback_to_unencrypted(); } else { handshake_finished_.set_value(); recv(); } } +// worker thread +void wesnothd_connection::fallback_to_unencrypted() +{ + assert(use_tls_ == true); + use_tls_ = false; + + boost::asio::ip::tcp::endpoint endpoint { utils::get(socket_).remote_endpoint() }; + utils::get(socket_).close(); + + utils::get(socket_).async_connect(endpoint, + std::bind(&wesnothd_connection::handle_connect, this, std::placeholders::_1, endpoint)); +} + // main thread void wesnothd_connection::wait_for_handshake() { diff --git a/src/wesnothd_connection.hpp b/src/wesnothd_connection.hpp index 0a8365d861a9..c790df168f9f 100644 --- a/src/wesnothd_connection.hpp +++ b/src/wesnothd_connection.hpp @@ -183,6 +183,8 @@ class wesnothd_connection data_union handshake_response_; + void fallback_to_unencrypted(); + std::size_t is_write_complete(const boost::system::error_code& error, std::size_t bytes_transferred); void handle_write(const boost::system::error_code& ec, std::size_t bytes_transferred);