Integrated the new SSL prefs. Seems to work.

This commit is contained in:
casey langen 2020-10-17 18:57:06 -07:00
parent 205f1697cd
commit a5c871c89f
3 changed files with 32 additions and 8 deletions

View File

@ -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);
}

View File

@ -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));
}
}
}

View File

@ -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<std::thread> thread;
mutable std::recursive_mutex mutex;
bool useTls{ false };
std::string uri, password;
std::unordered_map<std::string, Query> messageIdToQuery;
std::atomic<bool> quit{ false };