Skip to content

Commit

Permalink
Don't modify the WS endpoint when changing other settings
Browse files Browse the repository at this point in the history
This caused issues by accidentally changing the value of the websockets
endpoint when modifying another value in settings. Should be fine now.
  • Loading branch information
MartinBriza committed Jan 14, 2024
1 parent ca7a630 commit 60de3cd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
31 changes: 21 additions & 10 deletions modules/Lith/Core/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ QCoro::Task<> Settings::migrateCredentialEncryption() {
m_host = (co_await getSecureValue("host", false)).value_or(QString());
emit hostChanged();
}

if (m_settings.contains("port")) {
m_settings.remove("port");
portSet(m_port, true);
Expand All @@ -181,6 +182,7 @@ QCoro::Task<> Settings::migrateCredentialEncryption() {
}
}
}

if (m_settings.contains("websocketsEndpoint")) {
m_settings.remove("websocketsEndpoint");
websocketsEndpointSet(m_websocketsEndpoint, true);
Expand All @@ -191,6 +193,7 @@ QCoro::Task<> Settings::migrateCredentialEncryption() {
emit websocketsEndpointChanged();
}
}

if (m_settings.contains("passphrase")) {
auto passphrase = m_settings.value("passphrase").toString();
m_settings.remove("passphrase");
Expand All @@ -202,6 +205,7 @@ QCoro::Task<> Settings::migrateCredentialEncryption() {
useEmptyPassphraseSet(storedPassphrase->isEmpty());
}
}

emit networkSettingsChanged();
} else if (credentialEncryptionAvailable()) {
auto encryptedHost = co_await getSecureValue("host", false);
Expand All @@ -211,6 +215,7 @@ QCoro::Task<> Settings::migrateCredentialEncryption() {
emit hostChanged();
co_await deleteSecureValue("host", false);
}

auto encryptedPort = co_await getSecureValue("port", false);
if (encryptedPort.has_value()) {
bool ok = false;
Expand All @@ -222,13 +227,15 @@ QCoro::Task<> Settings::migrateCredentialEncryption() {
}
co_await deleteSecureValue("port", false);
}

auto encryptedEndpoint = co_await getSecureValue("websocketsEndpoint", false);
if (encryptedEndpoint.has_value()) {
m_websocketsEndpoint = encryptedEndpoint.value();
m_settings.setValue("websocketsEndpoint", m_websocketsEndpoint);
emit websocketsEndpointChanged();
co_await deleteSecureValue("websocketsEndpoint", false);
}

auto encryptedPassphrase = co_await getSecureValue("passphrase", false);
if (encryptedPassphrase.has_value()) {
m_settings.setValue("passphrase", encryptedPassphrase.value());
Expand All @@ -241,6 +248,7 @@ QCoro::Task<> Settings::migrateCredentialEncryption() {
useEmptyPassphraseSet(plainPassphrase.value().isEmpty());
}
}

emit networkSettingsChanged();
}
m_settings.sync();
Expand All @@ -251,6 +259,7 @@ QCoro::Task<> Settings::migrateCredentialEncryption() {
hasPassphraseSet(true);
useEmptyPassphraseSet(plainPassphrase.value().isEmpty());
}
emit networkSettingsChanged();
}
co_return;
}
Expand Down Expand Up @@ -312,6 +321,10 @@ void Settings::useEmptyPassphraseSet(bool useEmptyPassphrase) {
}

bool Settings::websocketsEndpointSet(QString newWebsocketsEndpoint, bool force) {
if (newWebsocketsEndpoint.isEmpty()) {
Lith::instance()->log(Logger::Unexpected, "Attempted to set an empty websocket endpoint, replacing with \"/\"");
newWebsocketsEndpoint = QStringLiteral("/");
}
if (!force && m_websocketsEndpoint == newWebsocketsEndpoint) {
return false;
}
Expand Down Expand Up @@ -450,20 +463,18 @@ QCoro::Task<bool> Settings::setSensitiveValue(QString key, QString value) {
co_return false;
}
if (m_encryptedCredentials) {
if (m_encryptedCredentials) {
#ifndef Q_OS_WASM
QThread* callerThread = QThread::currentThread();
QThread* settingsThread = Settings::instance()->thread();
if (callerThread != settingsThread) {
co_await QCoro::moveToThread(settingsThread);
}
QThread* callerThread = QThread::currentThread();
QThread* settingsThread = Settings::instance()->thread();
if (callerThread != settingsThread) {
co_await QCoro::moveToThread(settingsThread);
}
#endif // Q_OS_WASM
auto result = co_await setSecureValue(key, value, false);
auto result = co_await setSecureValue(key, value, false);
#ifndef Q_OS_WASM
co_await QCoro::moveToThread(callerThread);
co_await QCoro::moveToThread(callerThread);
#endif // Q_OS_WASM
co_return result;
}
co_return result;
}
co_return setPlainValue(key, value);
}
Expand Down
15 changes: 11 additions & 4 deletions modules/Lith/Core/sockethelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ void SocketHelper::onConnected() {

void SocketHelper::connectToWebsocket(const QString& hostname, const QString& endpoint, int port, bool encrypted) {
reset();
lith()->log(
Logger::Network, QString("Connecting to: %1://%2:%3/%4").arg(encrypted ? "wss" : "ws").arg(hostname).arg(port).arg(endpoint)
);

bool endpointHasSlash = endpoint.startsWith('/');
QString url(QString("%1://%2:%3%4%5")
.arg(encrypted ? "wss" : "ws")
.arg(hostname)
.arg(port)
.arg(endpointHasSlash ? QStringLiteral("") : QStringLiteral("/"))
.arg(endpoint));

lith()->log(Logger::Network, QString("Connecting to: %1").arg(url));
m_webSocket = new QWebSocket("weechat", QWebSocketProtocol::VersionLatest, this);

connect(m_webSocket, &QWebSocket::connected, this, &SocketHelper::onConnected);
Expand All @@ -73,7 +80,7 @@ void SocketHelper::connectToWebsocket(const QString& hostname, const QString& en
connect(m_webSocket, &QWebSocket::sslErrors, this, &SocketHelper::onSslErrors, Qt::DirectConnection);
#endif // __EMSCRIPTEN__

m_webSocket->open(QString("%1://%2:%3/%4").arg(encrypted ? "wss" : "ws").arg(hostname).arg(port).arg(endpoint));
m_webSocket->open(url);
}

#ifndef __EMSCRIPTEN__
Expand Down
2 changes: 1 addition & 1 deletion modules/Lith/Core/weechat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void Weechat::restart() {
#ifndef __EMSCRIPTEN__
if (!Lith::settingsGet()->useWebsocketsGet() && !host.isEmpty()) {
m_connection->connectToTcpSocket(host, port, ssl);
} else if (!host.isEmpty() && !websocketEndpoint.isEmpty()) {
} else if (!host.isEmpty()) {
#endif // __EMSCRIPTEN__
m_connection->connectToWebsocket(host, websocketEndpoint, port, ssl);
#ifndef __EMSCRIPTEN__
Expand Down
8 changes: 5 additions & 3 deletions modules/Lith/UI/SettingsNetwork.qml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ ScrollView {
if (typeof Lith.settings.useWebsockets !== "undefined") {
useWebsocketsCheckbox.checked = Lith.settings.useWebsockets
}
websocketsEndpointInput.text = Lith.settings.websocketsEndpoint
if (typeof Lith.settings.websocketsEndpoint !== "undefined") {
websocketsEndpointInput.text = Lith.settings.websocketsEndpoint
}
showInternalDataCheckbox.checked = Lith.settings.showInternalData
enableLoggingCheckbox.checked = Lith.settings.enableLogging
enableReplayRecordingCheckbox.checked = Lith.settings.enableReplayRecording
Expand Down Expand Up @@ -203,14 +205,14 @@ ScrollView {
Fields.Boolean {
id: useWebsocketsCheckbox
visible: typeof Lith.settings.useWebsockets !== "undefined"
checked: visible ? Lith.settings.useWebsockets : false
checked: typeof Lith.settings.useWebsockets !== "undefined" ? Lith.settings.useWebsockets : false

summary: qsTr("Use WebSockets to connect")
}
Fields.String {
id: websocketsEndpointInput
enabled: typeof Lith.settings.useWebsockets === "undefined" || useWebsocketsCheckbox.checked
text: Lith.settings.websocketsEndpoint
text: Lith.settings.websocketsEndpoint === "undefined" ? "" : Lith.settings.websocketsEndpoint

summary: qsTr("Websockets endpoint")
}
Expand Down

0 comments on commit 60de3cd

Please sign in to comment.