Initial websocket rewrite with curl ws

This commit is contained in:
ouwou committed 2025-02-19 02:30:38 -05:00
1 parent a7d1d6b3d9
commit 4e9f713975
9 files changed
+249 -88

No files matched your search

+3 -28
View File
@@ -4,7 +4,7 @@ project(abaddon)
set(ABADDON_RESOURCE_DIR "${CMAKE_INSTALL_PREFIX}/share/abaddon" CACHE PATH "Fallback directory for resources on Linux")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
option(USE_LIBHANDY "Enable features that require libhandy (default)" ON)
@@ -19,15 +19,7 @@ find_package(CURL)
find_package(ZLIB REQUIRED)
find_package(SQLite3 REQUIRED)
find_package(gtkmm REQUIRED)
set(USE_TLS TRUE)
set(USE_OPEN_SSL TRUE)
find_package(IXWebSocket QUIET)
if (NOT IXWebSocket_FOUND)
message("ixwebsocket was not found and will be included as a submodule")
add_subdirectory(subprojects/ixwebsocket)
include_directories(IXWEBSOCKET_INCLUDE_DIRS)
endif ()
find_package(OpenSSL REQUIRED)
if (MINGW OR WIN32)
link_libraries(ws2_32)
@@ -82,24 +74,6 @@ else ()
target_sources(abaddon PRIVATE src/notifications/notifier_fallback.cpp)
endif ()
if (IXWebSocket_LIBRARIES)
target_link_libraries(abaddon ${IXWebSocket_LIBRARIES})
find_library(MBEDTLS_X509_LIBRARY mbedx509)
find_library(MBEDTLS_TLS_LIBRARY mbedtls)
find_library(MBEDTLS_CRYPTO_LIBRARY mbedcrypto)
if (MBEDTLS_TLS_LIBRARY)
target_link_libraries(abaddon ${MBEDTLS_TLS_LIBRARY})
endif ()
if (MBEDTLS_X509_LIBRARY)
target_link_libraries(abaddon ${MBEDTLS_X509_LIBRARY})
endif ()
if (MBEDTLS_CRYPTO_LIBRARY)
target_link_libraries(abaddon ${MBEDTLS_CRYPTO_LIBRARY})
endif ()
else ()
target_link_libraries(abaddon $<BUILD_INTERFACE:ixwebsocket>)
endif ()
find_package(Threads)
if (Threads_FOUND)
target_link_libraries(abaddon Threads::Threads)
@@ -120,6 +94,7 @@ target_link_libraries(abaddon ${NLOHMANN_JSON_LIBRARIES})
target_link_libraries(abaddon ${CMAKE_DL_LIBS})
target_link_libraries(abaddon CURL::libcurl)
target_link_libraries(abaddon OpenSSL::SSL OpenSSL::Crypto)
include(CheckAtomic)
if (NOT HAVE_CXX_ATOMICS_WITHOUT_LIB OR NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB)
+3 -3
View File
@@ -2768,7 +2768,7 @@ EPremiumType DiscordClient::GetSelfPremiumType() const {
void DiscordClient::HeartbeatThread() {
while (m_client_connected) {
if (!m_heartbeat_acked) {
printf("wow! a heartbeat wasn't acked! how could this happen?");
spdlog::get("discord")->warn("Heartbeat not acked");
}
m_heartbeat_acked = false;
@@ -2855,8 +2855,8 @@ void DiscordClient::SetSuperPropertiesFromIdentity(const IdentifyMessage &identi
void DiscordClient::HandleSocketOpen() {
}
void DiscordClient::HandleSocketClose(const ix::WebSocketCloseInfo &info) {
auto close_code = static_cast<GatewayCloseCode>(info.code);
void DiscordClient::HandleSocketClose(const Websocket::CloseInfo &info) {
auto close_code = static_cast<GatewayCloseCode>(info.Code);
auto cb = [this, close_code]() {
m_heartbeat_waiter.kill();
if (m_heartbeat_thread.joinable()) m_heartbeat_thread.join();
+1 -1
View File
@@ -328,7 +328,7 @@ private:
void SetSuperPropertiesFromIdentity(const IdentifyMessage &identity);
void HandleSocketOpen();
void HandleSocketClose(const ix::WebSocketCloseInfo &info);
void HandleSocketClose(const Websocket::CloseInfo &info);
static bool CheckCode(const http::response_type &r);
static bool CheckCode(const http::response_type &r, int expected);
+4 -4
View File
@@ -414,11 +414,11 @@ void DiscordVoiceClient::OnWebsocketOpen() {
SetState(State::EstablishingConnection);
}
void DiscordVoiceClient::OnWebsocketClose(const ix::WebSocketCloseInfo &info) {
if (info.remote) {
m_log->debug("Websocket closed (remote): {} ({})", info.code, info.reason);
void DiscordVoiceClient::OnWebsocketClose(const Websocket::CloseInfo &info) {
if (info.Remote) {
m_log->debug("Websocket closed (remote): {} ({})", info.Code, info.Reason);
} else {
m_log->debug("Websocket closed (local): {} ({})", info.code, info.reason);
m_log->debug("Websocket closed (local): {} ({})", info.Code, info.Reason);
}
}
+1 -1
View File
@@ -242,7 +242,7 @@ private:
void SelectProtocol(const char *ip, uint16_t port);
void OnWebsocketOpen();
void OnWebsocketClose(const ix::WebSocketCloseInfo &info);
void OnWebsocketClose(const Websocket::CloseInfo &info);
void OnWebsocketMessage(const std::string &str);
void HeartbeatThread();
+179 -33
View File
@@ -1,23 +1,22 @@
#include "websocket.hpp"
#include <utility>
#include <gtkmm/main.h>
#include <spdlog/sinks/stdout_color_sinks.h>
Websocket::Websocket(const std::string &id)
: m_close_info { 1000, "Normal", false } {
Websocket::Websocket(const std::string &id) {
if (m_log = spdlog::get(id); !m_log) {
m_log = spdlog::stdout_color_mt(id);
}
m_websocket = curl_easy_init();
curl_easy_setopt(m_websocket, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(m_websocket, CURLOPT_CONNECT_ONLY, 2L);
m_open_dispatcher.connect([this]() {
m_signal_open.emit();
});
m_close_dispatcher.connect([this]() {
Stop();
m_signal_close.emit(m_close_info);
});
}
@@ -25,17 +24,18 @@ Websocket::Websocket(const std::string &id)
void Websocket::StartConnection(const std::string &url) {
m_log->debug("Starting connection to {}", url);
m_websocket = std::make_unique<ix::WebSocket>();
m_sent_close = false;
m_state = State::Connecting;
m_websocket->disableAutomaticReconnection();
m_websocket->setUrl(url);
m_websocket->setOnMessageCallback([this](auto &&msg) { OnMessage(std::forward<decltype(msg)>(msg)); });
m_websocket->setExtraHeaders(ix::WebSocketHttpHeaders { { "User-Agent", m_agent }, { "Origin", "https://discord.com" } }); // idk if this actually works
m_websocket->start();
std::scoped_lock lock(m_mutex);
curl_easy_setopt(m_websocket, CURLOPT_URL, url.c_str());
m_thread = std::thread(&Websocket::Task, this);
}
void Websocket::SetUserAgent(std::string agent) {
m_agent = std::move(agent);
std::scoped_lock lock(m_mutex);
curl_easy_setopt(m_websocket, CURLOPT_USERAGENT, agent.c_str());
}
bool Websocket::GetPrintMessages() const noexcept {
@@ -48,12 +48,36 @@ void Websocket::SetPrintMessages(bool show) noexcept {
void Websocket::Stop() {
m_log->debug("Stopping with default close code");
Stop(ix::WebSocketCloseConstants::kNormalClosureCode);
Stop(1000);
}
void Websocket::Stop(uint16_t code) {
if (m_state == State::Closing) {
m_log->debug("Attempting to stop while in Closing state");
return;
}
if (m_state == State::Closed) {
m_log->debug("Attempting to stop while in Closed state");
return;
}
m_log->debug("Stopping with close code {}", code);
m_websocket->stop(code);
m_state = State::Closing;
{
std::vector<uint8_t> payload = { static_cast<uint8_t>(code >> 8), static_cast<uint8_t>(code & 0xFF) };
m_sent_close = true;
{
std::scoped_lock lock(m_mutex);
size_t sent;
curl_ws_send(m_websocket, payload.data(), payload.size(), &sent, 0, CURLWS_CLOSE);
}
std::unique_lock lock(m_close_mutex);
m_close_cv.wait(lock);
}
m_log->trace("Socket::stop complete");
while (Gtk::Main::events_pending()) {
Gtk::Main::iteration();
@@ -62,38 +86,160 @@ void Websocket::Stop(uint16_t code) {
}
void Websocket::Send(const std::string &str) {
if (m_state != State::Open) {
m_log->warn("Attempt to send on non-open state");
return;
}
if (m_print_messages)
m_log->trace("Send: {}", str);
m_websocket->sendText(str);
std::scoped_lock lock(m_mutex);
size_t offset = 0;
while (true) {
size_t sent;
auto code = curl_ws_send(m_websocket, str.data() + offset, str.size() - offset, &sent, 0, CURLWS_TEXT);
offset += sent;
if (code == CURLE_OK) {
if (offset >= str.size()) {
break;
}
} else if (code == CURLE_AGAIN) {
continue;
} else {
m_log->error("Websocket::Send: {}", curl_easy_strerror(code));
break;
}
}
}
void Websocket::Send(const nlohmann::json &j) {
Send(j.dump());
}
void Websocket::OnMessage(const ix::WebSocketMessagePtr &msg) {
switch (msg->type) {
case ix::WebSocketMessageType::Open: {
m_log->debug("Received open frame, dispatching");
m_open_dispatcher.emit();
} break;
case ix::WebSocketMessageType::Close: {
const auto remote = msg->closeInfo.remote ? " Remote" : "";
m_log->debug("Received close frame, dispatching. {} ({}){}", msg->closeInfo.code, msg->closeInfo.reason, remote);
m_close_info = msg->closeInfo;
void Websocket::OnMessage(const WebSocketMessage &message) {
switch (message.Type) {
case WebSocketMessage::MessageType::binary:
case WebSocketMessage::MessageType::text: {
m_log->trace("Received {} bytes", message.Data.size());
std::string payload = std::string(message.Data.begin(), message.Data.end());
m_signal_message.emit(payload);
break;
}
case WebSocketMessage::MessageType::close: {
m_close_info = message.CloseInfo;
m_close_info.Remote = !m_sent_close;
m_log->debug(
"Received close frame{}, dispatching. {} ({})",
m_close_info.Remote ? " (Remote)" : "",
message.CloseInfo.Code,
message.CloseInfo.Reason);
m_close_cv.notify_all();
m_close_dispatcher.emit();
} break;
case ix::WebSocketMessageType::Message: {
m_signal_message.emit(msg->str);
} break;
case ix::WebSocketMessageType::Error: {
m_log->error("Websocket error: Status: {} Reason: {}", msg->errorInfo.http_status, msg->errorInfo.reason);
} break;
break;
}
default:
break;
}
}
std::expected<Websocket::WebSocketMessage, Websocket::Error> Websocket::ReceiveMessage() {
WebSocketMessage message;
curl_socket_t socket;
{
std::scoped_lock lock(m_mutex);
curl_easy_getinfo(m_websocket, CURLINFO_ACTIVESOCKET, &socket);
}
while (m_state == State::Open || m_state == State::Closing) {
if (m_state == State::Closing && !m_sent_close) {
// if were closing and we didnt send the close then server already did
return std::unexpected(Error::closing);
}
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(socket, &rfds);
timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
if (select(socket + 1, &rfds, nullptr, nullptr, &tv) == -1) {
return std::unexpected(Websocket::Error::select_error);
}
size_t rlen;
const curl_ws_frame *meta;
char buffer[256];
std::scoped_lock lock(m_mutex);
auto code = curl_ws_recv(m_websocket, buffer, sizeof(buffer), &rlen, &meta);
if (code != CURLE_OK) {
if (code == CURLE_AGAIN) {
continue;
} else {
return std::unexpected(Websocket::Error::recv_error);
}
}
if (meta->flags & CURLWS_PING) {
message.Type = WebSocketMessage::MessageType::ping;
} else if (meta->flags & CURLWS_TEXT) {
message.Type = WebSocketMessage::MessageType::text;
} else if (meta->flags & CURLWS_BINARY) {
message.Type = WebSocketMessage::MessageType::binary;
} else if (meta->flags & CURLWS_CLOSE) {
message.Type = WebSocketMessage::MessageType::close;
message.CloseInfo.Code = (static_cast<uint16_t>(buffer[0]) << 8) | static_cast<uint8_t>(buffer[1]);
message.CloseInfo.Reason = std::string(buffer + 2, rlen - 2);
if (m_sent_close) {
m_state = State::Closed;
} else {
m_state = State::Closing;
}
}
message.Data.insert(message.Data.end(), buffer, buffer + rlen);
if (meta->bytesleft == 0 && (meta->flags & CURLWS_CONT) == 0) {
break;
}
}
return message;
}
void Websocket::Task() {
CURLcode res;
{
std::scoped_lock lock(m_mutex);
res = curl_easy_perform(m_websocket);
}
if (res != CURLE_OK) {
m_state = State::Closed;
m_log->error("Error: {}", curl_easy_strerror(res));
} else {
m_state = State::Open;
m_open_dispatcher.emit();
while (true) {
auto msg = ReceiveMessage();
if (msg.has_value()) {
OnMessage(msg.value());
}
}
}
std::scoped_lock lock(m_mutex);
curl_easy_cleanup(m_websocket);
}
Websocket::type_signal_open Websocket::signal_open() {
return m_signal_open;
}
+51 -10
View File
@@ -1,12 +1,10 @@
#pragma once
#include <ixwebsocket/IXNetSystem.h>
#include <ixwebsocket/IXWebSocket.h>
#include <expected>
#include <string>
#include <functional>
#include <glibmm.h>
#include <nlohmann/json.hpp>
#include <sigc++/sigc++.h>
#include <spdlog/spdlog.h>
#include <curl/curl.h>
class Websocket {
public:
@@ -23,15 +21,57 @@ public:
void Stop();
void Stop(uint16_t code);
private:
void OnMessage(const ix::WebSocketMessagePtr &msg);
enum class Error {
recv_error,
select_error,
closing,
};
std::unique_ptr<ix::WebSocket> m_websocket;
std::string m_agent;
enum class State {
Closed,
Connecting,
Open,
Closing,
};
struct CloseInfo {
bool Remote;
uint16_t Code;
std::string Reason;
};
private:
struct WebSocketMessage {
enum class MessageType {
ping,
text,
binary,
close,
};
CloseInfo CloseInfo {};
std::vector<uint8_t> Data;
MessageType Type;
};
State m_state = State::Closed;
void OnMessage(const WebSocketMessage &message);
std::expected<WebSocketMessage, Error> ReceiveMessage();
void Task();
mutable std::mutex m_mutex;
std::thread m_thread;
CURL *m_websocket = nullptr;
std::atomic<bool> m_sent_close = false;
mutable std::mutex m_close_mutex;
std::condition_variable m_close_cv;
public:
using type_signal_open = sigc::signal<void>;
using type_signal_close = sigc::signal<void, ix::WebSocketCloseInfo>;
using type_signal_close = sigc::signal<void, CloseInfo>;
using type_signal_message = sigc::signal<void, std::string>;
type_signal_open signal_open();
@@ -45,9 +85,10 @@ private:
bool m_print_messages = true;
CloseInfo m_close_info;
Glib::Dispatcher m_open_dispatcher;
Glib::Dispatcher m_close_dispatcher;
ix::WebSocketCloseInfo m_close_info;
std::shared_ptr<spdlog::logger> m_log;
};
+6 -7
View File
@@ -10,7 +10,6 @@
#include "abaddon.hpp"
#include "http.hpp"
// clang-format on
RemoteAuthClient::RemoteAuthClient()
@@ -280,16 +279,16 @@ void RemoteAuthClient::OnWebsocketOpen() {
m_log->info("Websocket opened");
}
void RemoteAuthClient::OnWebsocketClose(const ix::WebSocketCloseInfo &info) {
if (info.remote) {
m_log->debug("Websocket closed (remote): {} ({})", info.code, info.reason);
void RemoteAuthClient::OnWebsocketClose(const Websocket::CloseInfo &info) {
if (info.Remote) {
m_log->debug("Websocket closed (remote): {} ({})", info.Code, info.Reason);
if (m_connected) {
m_signal_error.emit("Error. Websocket closed (remote): " + std::to_string(info.code) + " (" + info.reason + ")");
m_signal_error.emit("Error. Websocket closed (remote): " + std::to_string(info.Code) + " (" + info.Reason + ")");
}
} else {
m_log->debug("Websocket closed (local): {} ({})", info.code, info.reason);
m_log->debug("Websocket closed (local): {} ({})", info.Code, info.Reason);
if (m_connected) {
m_signal_error.emit("Error. Websocket closed (local): " + std::to_string(info.code) + " (" + info.reason + ")");
m_signal_error.emit("Error. Websocket closed (local): " + std::to_string(info.Code) + " (" + info.Reason + ")");
}
}
}
+1 -1
View File
@@ -47,7 +47,7 @@ private:
std::vector<uint8_t> Decrypt(const unsigned char *in, size_t inlen) const;
void OnWebsocketOpen();
void OnWebsocketClose(const ix::WebSocketCloseInfo &info);
void OnWebsocketClose(const Websocket::CloseInfo &info);
void OnWebsocketMessage(const std::string &str);
void HeartbeatThread();