Skip to content

Commit

Permalink
aegea: Try wss first and fall back to ws on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceManiac committed Dec 21, 2024
1 parent beb81b1 commit 6e93525
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
6 changes: 4 additions & 2 deletions source/aegea/include/aegea.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,14 @@ class ArchipelagoClient
private:
enum
{
Connecting,
Idle,
ConnectingExact,
ConnectingAutoWss,
WaitingForRoomInfo,
WaitingForDataPackage,
WaitingForConnected,
Active,
} status = Connecting;
} status = Idle;

std::string game, address, slot, password;
std::set<std::string, std::less<>> tags;
Expand Down
37 changes: 31 additions & 6 deletions source/aegea/src/aegea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,20 @@ ArchipelagoClient::ArchipelagoClient(std::string_view game, std::string_view add
, slot(slot)
, password(password)
{
// TODO: if address has no protocol, first try wss:// then fall back to ws://
std::string fullAddress = "ws://";
fullAddress += address;
socket = WebSocket::connect(fullAddress.c_str());
if (address.find("://") == std::string_view::npos)
{
// Address has no protocol. First try wss:// then fall back to ws://
std::string fullAddress = "wss://";
fullAddress += address;
socket = WebSocket::connect(fullAddress.c_str());
status = ConnectingAutoWss;
}
else
{
std::string fullAddress { address };
socket = WebSocket::connect(fullAddress.c_str());
status = ConnectingExact;
}
}

ArchipelagoClient::~ArchipelagoClient()
Expand Down Expand Up @@ -177,12 +187,27 @@ static void decode(ArchipelagoClient::Item* item, const jt::Json& json)

void ArchipelagoClient::update()
{
if (!socket || !socket->error_message().empty())
if (!socket)
{
return;
}
if (!socket->error_message().empty())
{
if (status == ConnectingAutoWss)
{
debug_printf("Auto-TLS failed, trying insecure: %.*s\n", (int)socket->error_message().size(), socket->error_message().data());
// wss://X failed, try ws://X
std::string fullAddress = "ws://" + address;
socket = WebSocket::connect(fullAddress.c_str());
status = ConnectingExact; // Nothing left to try
}
else
{
return;
}
}

if (socket->is_connected() && status == Connecting)
if (socket->is_connected() && (status == ConnectingAutoWss || status == ConnectingExact))
{
status = WaitingForRoomInfo;
}
Expand Down

0 comments on commit 6e93525

Please sign in to comment.