diff --git a/connector/syncthingconfig.cpp b/connector/syncthingconfig.cpp index 9dda8e8..9727c88 100644 --- a/connector/syncthingconfig.cpp +++ b/connector/syncthingconfig.cpp @@ -1,7 +1,7 @@ #include "./syncthingconfig.h" +#include "./utils.h" #include -#include #include #include @@ -83,9 +83,7 @@ bool SyncthingConfig::restore(const QString &configFilePath) QString SyncthingConfig::syncthingUrl() const { - return (guiEnforcesSecureConnection || !QHostAddress(guiAddress.mid(0, guiAddress.indexOf(QChar(':')))).isLoopback() ? QStringLiteral("https://") - : QStringLiteral("http://")) - + guiAddress; + return (guiEnforcesSecureConnection || isLocal(guiAddress) ? QStringLiteral("https://") : QStringLiteral("http://")) + guiAddress; } } // namespace Data diff --git a/connector/utils.cpp b/connector/utils.cpp index 01621a6..8dc33ec 100644 --- a/connector/utils.cpp +++ b/connector/utils.cpp @@ -104,13 +104,12 @@ QString rescanIntervalString(int rescanInterval, bool fileSystemWatcherEnabled) } /*! - * \brief Returns whether the host specified by the given \a url is the local machine. + * \brief Returns whether the specified \a hostname is the local machine. */ -bool isLocal(const QUrl &url) +bool isLocal(const QString &hostname) { - const QString host(url.host()); - const QHostAddress hostAddress(host); - return host.compare(QLatin1String("localhost"), Qt::CaseInsensitive) == 0 || hostAddress.isLoopback() + const QHostAddress hostAddress(hostname); + 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 859efb5..dc27ab1 100644 --- a/connector/utils.h +++ b/connector/utils.h @@ -8,11 +8,11 @@ #include #include +#include #include #include -QT_FORWARD_DECLARE_CLASS(QUrl) QT_FORWARD_DECLARE_CLASS(QJsonObject) namespace ChronoUtilities { @@ -31,10 +31,18 @@ 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 QUrl &url); +bool LIB_SYNCTHING_CONNECTOR_EXPORT isLocal(const QString &hostname); 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); +/*! + * \brief Returns whether the host specified by the given \a url is the local machine. + */ +inline bool isLocal(const QUrl &url) +{ + return isLocal(url.host()); +} + template > * = nullptr> inline IntType LIB_SYNCTHING_CONNECTOR_EXPORT jsonValueToInt(const QJsonValue &value, double defaultValue = 0.0) {