Improve inserting address from config file

* Assume local connection if address is eg. 0.0.0.0
* Use isLocal() from utils which considers the hostname
  and interfaces as well
This commit is contained in:
Martchus 2018-11-01 19:55:08 +01:00
parent ec02a9e483
commit dd5cf12b1c
3 changed files with 32 additions and 10 deletions

View File

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

View File

@ -14,6 +14,7 @@
#include <vector>
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<const SyncthingDir *> &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);

View File

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