Use isLocal() in SyncthingConfig::syncthingUrl()

This commit is contained in:
Martchus 2018-10-10 22:46:55 +02:00
parent 99cb584249
commit c031440c05
3 changed files with 16 additions and 11 deletions

View File

@ -1,7 +1,7 @@
#include "./syncthingconfig.h" #include "./syncthingconfig.h"
#include "./utils.h"
#include <QFile> #include <QFile>
#include <QHostAddress>
#include <QStandardPaths> #include <QStandardPaths>
#include <QXmlStreamReader> #include <QXmlStreamReader>
@ -83,9 +83,7 @@ bool SyncthingConfig::restore(const QString &configFilePath)
QString SyncthingConfig::syncthingUrl() const QString SyncthingConfig::syncthingUrl() const
{ {
return (guiEnforcesSecureConnection || !QHostAddress(guiAddress.mid(0, guiAddress.indexOf(QChar(':')))).isLoopback() ? QStringLiteral("https://") return (guiEnforcesSecureConnection || isLocal(guiAddress) ? QStringLiteral("https://") : QStringLiteral("http://")) + guiAddress;
: QStringLiteral("http://"))
+ guiAddress;
} }
} // namespace Data } // namespace Data

View File

@ -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(hostname);
const QHostAddress hostAddress(host); return hostname.compare(QLatin1String("localhost"), Qt::CaseInsensitive) == 0 || hostAddress.isLoopback()
return host.compare(QLatin1String("localhost"), Qt::CaseInsensitive) == 0 || hostAddress.isLoopback()
|| QNetworkInterface::allAddresses().contains(hostAddress); || QNetworkInterface::allAddresses().contains(hostAddress);
} }

View File

@ -8,11 +8,11 @@
#include <QJsonValue> #include <QJsonValue>
#include <QStringList> #include <QStringList>
#include <QUrl>
#include <limits> #include <limits>
#include <vector> #include <vector>
QT_FORWARD_DECLARE_CLASS(QUrl)
QT_FORWARD_DECLARE_CLASS(QJsonObject) QT_FORWARD_DECLARE_CLASS(QJsonObject)
namespace ChronoUtilities { namespace ChronoUtilities {
@ -31,10 +31,18 @@ QString LIB_SYNCTHING_CONNECTOR_EXPORT directoryStatusString(const Data::Syncthi
QString LIB_SYNCTHING_CONNECTOR_EXPORT syncCompleteString( QString LIB_SYNCTHING_CONNECTOR_EXPORT syncCompleteString(
const std::vector<const SyncthingDir *> &completedDirs, const SyncthingDev *remoteDevice = nullptr); const std::vector<const SyncthingDir *> &completedDirs, const SyncthingDev *remoteDevice = nullptr);
QString LIB_SYNCTHING_CONNECTOR_EXPORT rescanIntervalString(int rescanInterval, bool fileSystemWatcherEnabled); 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 setDirectoriesPaused(QJsonObject &syncthingConfig, const QStringList &dirIds, bool paused);
bool LIB_SYNCTHING_CONNECTOR_EXPORT setDevicesPaused(QJsonObject &syncthingConfig, const QStringList &dirs, 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 <typename IntType = quint64, Traits::EnableIf<std::is_integral<IntType>> * = nullptr> template <typename IntType = quint64, Traits::EnableIf<std::is_integral<IntType>> * = nullptr>
inline IntType LIB_SYNCTHING_CONNECTOR_EXPORT jsonValueToInt(const QJsonValue &value, double defaultValue = 0.0) inline IntType LIB_SYNCTHING_CONNECTOR_EXPORT jsonValueToInt(const QJsonValue &value, double defaultValue = 0.0)
{ {