diff --git a/connector/utils.cpp b/connector/utils.cpp index 8dc33ec..46872a6 100644 --- a/connector/utils.cpp +++ b/connector/utils.cpp @@ -104,12 +104,19 @@ QString rescanIntervalString(int rescanInterval, bool fileSystemWatcherEnabled) } /*! - * \brief Returns whether the specified \a hostname is the local machine. + * \brief Returns whether the specified \a hostName is the local machine. */ -bool isLocal(const QString &hostname) +bool isLocal(const QString &hostName) { - const QHostAddress hostAddress(hostname); - return hostname.compare(QLatin1String("localhost"), Qt::CaseInsensitive) == 0 || hostAddress.isLoopback() + return isLocal(hostName, QHostAddress(hostName)); +} + +/*! + * \brief Returns whether the specified \a hostName and \a hostAddress are the local machine. + */ +bool isLocal(const QString &hostName, const QHostAddress &hostAddress) +{ + return hostName.compare(QLatin1String("localhost"), Qt::CaseInsensitive) == 0 || hostAddress.isLoopback() || QNetworkInterface::allAddresses().contains(hostAddress); } diff --git a/connector/utils.h b/connector/utils.h index dc27ab1..54777b2 100644 --- a/connector/utils.h +++ b/connector/utils.h @@ -14,6 +14,7 @@ #include QT_FORWARD_DECLARE_CLASS(QJsonObject) +QT_FORWARD_DECLARE_CLASS(QHostAddress) namespace ChronoUtilities { class DateTime; @@ -31,7 +32,8 @@ QString LIB_SYNCTHING_CONNECTOR_EXPORT directoryStatusString(const Data::Syncthi QString LIB_SYNCTHING_CONNECTOR_EXPORT syncCompleteString( const std::vector &completedDirs, const SyncthingDev *remoteDevice = nullptr); QString LIB_SYNCTHING_CONNECTOR_EXPORT rescanIntervalString(int rescanInterval, bool fileSystemWatcherEnabled); -bool LIB_SYNCTHING_CONNECTOR_EXPORT isLocal(const QString &hostname); +bool LIB_SYNCTHING_CONNECTOR_EXPORT isLocal(const QString &hostName); +bool LIB_SYNCTHING_CONNECTOR_EXPORT isLocal(const QString &hostName, const QHostAddress &hostAddress); bool LIB_SYNCTHING_CONNECTOR_EXPORT setDirectoriesPaused(QJsonObject &syncthingConfig, const QStringList &dirIds, bool paused); bool LIB_SYNCTHING_CONNECTOR_EXPORT setDevicesPaused(QJsonObject &syncthingConfig, const QStringList &dirs, bool paused); diff --git a/widgets/settings/settingsdialog.cpp b/widgets/settings/settingsdialog.cpp index f7b26ba..cb9c469 100644 --- a/widgets/settings/settingsdialog.cpp +++ b/widgets/settings/settingsdialog.cpp @@ -5,6 +5,7 @@ #include "../../connector/syncthingconfig.h" #include "../../connector/syncthingconnection.h" #include "../../connector/syncthingprocess.h" +#include "../../connector/utils.h" #ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD #include "../../connector/syncthingservice.h" #include "../../model/colors.h" @@ -111,13 +112,25 @@ void ConnectionOptionPage::insertFromConfigFile() QCoreApplication::translate("QtGui::ConnectionOptionPage", "Unable to parse the Syncthing config file.")); return; } + if (!config.guiAddress.isEmpty()) { + const auto portStart(config.guiAddress.indexOf(QChar(':'))); + QString guiHost(config.guiAddress.mid(0, portStart)); + const QStringRef guiPort(portStart > 0 ? config.guiAddress.midRef(portStart) : QStringRef()); + const QHostAddress guiAddress(guiHost); + // assume local connection if address is eg. 0.0.0.0 + auto localConnection = true; + if (guiAddress == QHostAddress::AnyIPv4) { + guiHost = QStringLiteral("127.0.0.1"); + } else if (guiAddress == QHostAddress::AnyIPv6) { + guiHost = QStringLiteral("[::1]"); + } else if (!isLocal(guiHost, guiAddress)) { + localConnection = false; + } + const QString guiProtocol((config.guiEnforcesSecureConnection || !localConnection) ? QStringLiteral("https://") : QStringLiteral("http://")); + ui()->urlLineEdit->selectAll(); - ui()->urlLineEdit->insert( - ((config.guiEnforcesSecureConnection || !QHostAddress(config.guiAddress.mid(0, config.guiAddress.indexOf(QChar(':')))).isLoopback()) - ? QStringLiteral("https://") - : QStringLiteral("http://")) - + config.guiAddress); + ui()->urlLineEdit->insert(guiProtocol % guiHost % guiPort); } if (!config.guiUser.isEmpty() || !config.guiPasswordHash.isEmpty()) { ui()->authCheckBox->setChecked(true);