From a5c871c89f19055d756b7f37dab646171ec286bd Mon Sep 17 00:00:00 2001 From: casey langen Date: Sat, 17 Oct 2020 18:57:06 -0700 Subject: [PATCH] Integrated the new SSL prefs. Seems to work. --- src/musikcore/library/RemoteLibrary.cpp | 7 +++++-- src/musikcore/net/WebSocketClient.cpp | 25 ++++++++++++++++++++----- src/musikcore/net/WebSocketClient.h | 8 +++++++- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/musikcore/library/RemoteLibrary.cpp b/src/musikcore/library/RemoteLibrary.cpp index 7c431e760..6b052edb1 100644 --- a/src/musikcore/library/RemoteLibrary.cpp +++ b/src/musikcore/library/RemoteLibrary.cpp @@ -408,8 +408,10 @@ std::string RemoteLibrary::GetTrackUri(musik::core::sdk::ITrack* track, const st auto host = prefs->GetString(core::prefs::keys::RemoteLibraryHostname, "127.0.0.1"); auto port = (short) prefs->GetInt(core::prefs::keys::RemoteLibraryHttpPort, 7905); auto password = prefs->GetString(core::prefs::keys::RemoteLibraryPassword, ""); + auto useTls = prefs->GetBool(core::prefs::keys::RemoteLibraryHttpTls, false); - const std::string uri = "http://" + host + ":" + std::to_string(port) + "/audio/id/" + std::to_string(track->GetId()); + const std::string scheme = useTls ? "https://" : "http://"; + const std::string uri = scheme + host + ":" + std::to_string(port) + "/audio/id/" + std::to_string(track->GetId()); nlohmann::json path = { { "uri", uri }, { "originalUri", std::string(buffer) }, @@ -431,5 +433,6 @@ void RemoteLibrary::ReloadConnectionFromPreferences() { auto host = prefs->GetString(core::prefs::keys::RemoteLibraryHostname, "127.0.0.1"); auto port = (short) prefs->GetInt(core::prefs::keys::RemoteLibraryWssPort, 7905); auto password = prefs->GetString(core::prefs::keys::RemoteLibraryPassword, ""); - this->wsc.Connect(host, port, password); + auto useTls = prefs->GetBool(core::prefs::keys::RemoteLibraryWssTls, false); + this->wsc.Connect(host, port, password, useTls); } \ No newline at end of file diff --git a/src/musikcore/net/WebSocketClient.cpp b/src/musikcore/net/WebSocketClient.cpp index b5e95811a..171aee248 100644 --- a/src/musikcore/net/WebSocketClient.cpp +++ b/src/musikcore/net/WebSocketClient.cpp @@ -193,6 +193,9 @@ std::string WebSocketClient::EnqueueQuery(Query query) { query->Invalidate(); /* mark it as failed */ return ""; } + if (!query) { + return ""; + } auto messageId = generateMessageId(); messageIdToQuery[messageId] = query; if (this->state == State::Connected) { @@ -203,13 +206,20 @@ std::string WebSocketClient::EnqueueQuery(Query query) { return messageId; } -void WebSocketClient::Connect(const std::string& host, short port, const std::string& password) { +void WebSocketClient::Connect( + const std::string& host, + short port, + const std::string& password, + bool useTls) +{ auto newUri = "ws://" + host + ":" + std::to_string(port); if (newUri != this->uri || password != this->password || + useTls != this->useTls || this->state != State::Connected) { this->Disconnect(); + this->useTls = useTls; this->uri = newUri; this->password = password; if (this->uri.size()) { @@ -238,7 +248,10 @@ void WebSocketClient::Reconnect() { } if (uri.size()) { - websocketpp::lib::error_code ec; + auto mode = this->useTls + ? RawWebSocketClient::Mode::TLS + : RawWebSocketClient::Mode::PlainText; + rawClient->SetMode(mode); rawClient->SetPongTimeout(timeout); rawClient->Connect(uri); rawClient->Run(); @@ -280,9 +293,11 @@ void WebSocketClient::SendPendingQueries() { for (auto& kv : this->messageIdToQuery) { auto messageId = kv.first; auto query = kv.second; - this->rawClient->Send( - this->connection, - createSendRawQueryRequest(query->SerializeQuery(), messageId)); + if (query) { + this->rawClient->Send( + this->connection, + createSendRawQueryRequest(query->SerializeQuery(), messageId)); + } } } diff --git a/src/musikcore/net/WebSocketClient.h b/src/musikcore/net/WebSocketClient.h index b496c7b59..482ccd86f 100644 --- a/src/musikcore/net/WebSocketClient.h +++ b/src/musikcore/net/WebSocketClient.h @@ -89,7 +89,12 @@ namespace musik { namespace core { namespace net { WebSocketClient(const WebSocketClient&) = delete; virtual ~WebSocketClient(); - void Connect(const std::string& host, short port, const std::string& password); + void Connect( + const std::string& host, + short port, + const std::string& password, + bool useTls); + void Reconnect(); void Disconnect(); @@ -110,6 +115,7 @@ namespace musik { namespace core { namespace net { boost::asio::io_service io; std::shared_ptr thread; mutable std::recursive_mutex mutex; + bool useTls{ false }; std::string uri, password; std::unordered_map messageIdToQuery; std::atomic quit{ false };