Skip to content

Commit

Permalink
Implemented auto-fallback for servers that don't support TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
loonycyborg committed Feb 13, 2021
1 parent a38198e commit 04859de
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/wesnothd_connection.cpp
Expand Up @@ -165,16 +165,19 @@ 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);
}

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) {
Expand Down Expand Up @@ -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<raw_socket>(socket_).remote_endpoint() };
utils::get<raw_socket>(socket_).close();

utils::get<raw_socket>(socket_).async_connect(endpoint,
std::bind(&wesnothd_connection::handle_connect, this, std::placeholders::_1, endpoint));
}

// main thread
void wesnothd_connection::wait_for_handshake()
{
Expand Down
2 changes: 2 additions & 0 deletions src/wesnothd_connection.hpp
Expand Up @@ -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);

Expand Down

0 comments on commit 04859de

Please sign in to comment.