parse proxy address with QUrl to support IPv6 hosts

This commit is contained in:
kvazar-network 2025-07-20 03:08:44 +03:00
parent 5543b7c21a
commit e2e0e54e47

View File

@ -26,6 +26,7 @@
#include <QNetworkProxy> #include <QNetworkProxy>
#include <QSettings> #include <QSettings>
#include <QStringList> #include <QStringList>
#include <QUrl>
const char *DEFAULT_GUI_PROXY_HOST = "127.0.0.1"; const char *DEFAULT_GUI_PROXY_HOST = "127.0.0.1";
@ -212,17 +213,20 @@ struct ProxySetting {
static ProxySetting GetProxySetting(QSettings &settings, const QString &name) static ProxySetting GetProxySetting(QSettings &settings, const QString &name)
{ {
static const ProxySetting default_val = {false, DEFAULT_GUI_PROXY_HOST, QString("%1").arg(DEFAULT_GUI_PROXY_PORT)}; static const ProxySetting default_val = {false, DEFAULT_GUI_PROXY_HOST, QString("%1").arg(DEFAULT_GUI_PROXY_PORT)};
// Handle the case that the setting is not set at all // Handle the case that the setting is not set at all
if (!settings.contains(name)) { if (!settings.contains(name)) {
return default_val; return default_val;
} }
// contains IP at index 0 and port at index 1
QStringList ip_port = settings.value(name).toString().split(":", QString::SkipEmptyParts); auto url = QUrl::fromUserInput(settings.value(name).toString());
if (ip_port.size() == 2) { auto host = url.host();
return {true, ip_port.at(0), ip_port.at(1)}; auto port = url.port();
} else { // Invalid: return default
if (!host.isEmpty() && port > 0)
return {true, host, QString::number(port)};
else
return default_val; return default_val;
}
} }
static void SetProxySetting(QSettings &settings, const QString &name, const ProxySetting &ip_port) static void SetProxySetting(QSettings &settings, const QString &name, const ProxySetting &ip_port)
@ -338,8 +342,9 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
break; break;
case ProxyIP: { case ProxyIP: {
auto ip_port = GetProxySetting(settings, "addrProxy"); auto ip_port = GetProxySetting(settings, "addrProxy");
if (!ip_port.is_set || ip_port.ip != value.toString()) { auto ip = QUrl::fromUserInput(value.toString()).host();
ip_port.ip = value.toString(); if (!ip_port.is_set || ip_port.ip != ip) {
ip_port.ip = ip;
SetProxySetting(settings, "addrProxy", ip_port); SetProxySetting(settings, "addrProxy", ip_port);
setRestartRequired(true); setRestartRequired(true);
} }