Skip to content

Commit f24775a

Browse files
committed
Bugfix: GUI/OptionsDialog: fixed qvalidatedlineedit instances proxyIp, proxyPort, proxyIpTor, proxyPortTor to properly validate including visual feedback
1 parent 16d829d commit f24775a

File tree

1 file changed

+67
-7
lines changed

1 file changed

+67
-7
lines changed

src/qt/optionsdialog.cpp

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -720,10 +720,14 @@ OptionsDialog::OptionsDialog(QWidget* parent, bool enableWallet)
720720
/* setup/change UI elements when proxy IPs are invalid/valid */
721721
ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
722722
ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
723+
ui->proxyIp->setAllowEmptyInput(false);
724+
ui->proxyIpTor->setAllowEmptyInput(false);
725+
ui->proxyPort->setAllowEmptyInput(false);
726+
ui->proxyPortTor->setAllowEmptyInput(false);
723727
connect(ui->proxyIp, &QValidatedLineEdit::validationDidChange, this, &OptionsDialog::updateProxyValidationState);
724728
connect(ui->proxyIpTor, &QValidatedLineEdit::validationDidChange, this, &OptionsDialog::updateProxyValidationState);
725-
connect(ui->proxyPort, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
726-
connect(ui->proxyPortTor, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
729+
connect(ui->proxyPort, &QValidatedLineEdit::validationDidChange, this, &OptionsDialog::updateProxyValidationState);
730+
connect(ui->proxyPortTor, &QValidatedLineEdit::validationDidChange, this, &OptionsDialog::updateProxyValidationState);
727731

728732
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
729733
ui->showTrayIcon->setChecked(false);
@@ -1152,6 +1156,16 @@ void OptionsDialog::on_okButton_clicked()
11521156
model->setData(model->index(OptionsModel::dustdynamic, 0), "off");
11531157
}
11541158

1159+
// Before mapper->submit()
1160+
if (!ui->connectSocks->isChecked()) {
1161+
ui->proxyIp->clear();
1162+
ui->proxyPort->clear();
1163+
}
1164+
if (!ui->connectSocksTor->isChecked()) {
1165+
ui->proxyIpTor->clear();
1166+
ui->proxyPortTor->clear();
1167+
}
1168+
11551169
mapper->submit();
11561170
accept();
11571171
updateDefaultProxyNets();
@@ -1211,17 +1225,47 @@ void OptionsDialog::clearStatusLabel()
12111225

12121226
void OptionsDialog::updateProxyValidationState()
12131227
{
1214-
QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
1215-
QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
1216-
if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
1228+
bool socksProxyEnabled = ui->connectSocks->isChecked();
1229+
bool torProxyEnabled = ui->connectSocksTor->isChecked();
1230+
1231+
bool proxyIpValid = ui->proxyIp->isValid();
1232+
bool proxyPortValid = ui->proxyPort->isValid();
1233+
bool proxyIpTorValid = ui->proxyIpTor->isValid();
1234+
bool proxyPortTorValid = ui->proxyPortTor->isValid();
1235+
1236+
// proxy is OK if: disabled OR (enabled AND valid ip and valid port)
1237+
bool socksProxyOk = !socksProxyEnabled || (proxyIpValid && proxyPortValid);
1238+
bool torProxyOk = !torProxyEnabled || (proxyIpTorValid && proxyPortTorValid);
1239+
1240+
// Clear visual feedback when proxies are disabled
1241+
if (!socksProxyEnabled) { ui->proxyIp->setStyleSheet(""); ui->proxyPort->setStyleSheet(""); }
1242+
if (!torProxyEnabled) { ui->proxyIpTor->setStyleSheet(""); ui->proxyPortTor->setStyleSheet(""); }
1243+
1244+
// Both must be OK for the form to be valid
1245+
if (socksProxyOk && torProxyOk)
12171246
{
1218-
setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxies are valid
1247+
setOkButtonState(true);
12191248
clearStatusLabel();
12201249
}
12211250
else
12221251
{
12231252
setOkButtonState(false);
1224-
ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
1253+
QStringList socksErrors;
1254+
1255+
if (socksProxyEnabled) {
1256+
if (!proxyIpValid || !proxyPortValid) {
1257+
socksErrors.append(tr("The supplied proxy address and port is invalid."));
1258+
}
1259+
}
1260+
if (torProxyEnabled) {
1261+
if (!proxyIpTorValid || !proxyPortTorValid) {
1262+
socksErrors.append(tr("The supplied Tor proxy address and port is invalid."));
1263+
}
1264+
}
1265+
1266+
if (socksErrors.size() > 0) {
1267+
ui->statusLabel->setText(socksErrors.join(" "));
1268+
}
12251269
}
12261270
}
12271271

@@ -1278,6 +1322,22 @@ QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) cons
12781322
std::string hostname;
12791323
if (!SplitHostPort(input.toStdString(), port, hostname) || port != 0) return QValidator::Invalid;
12801324

1325+
// Check for incomplete IPv4 addresses using the extracted hostname
1326+
size_t dotCount = std::count(hostname.begin(), hostname.end(), '.');
1327+
1328+
// If it contains dots, it's trying to be an IPv4 address
1329+
if (dotCount > 0) {
1330+
// IPv4 must have exactly 3 dots (4 octets)
1331+
if (dotCount != 3) {
1332+
return QValidator::Invalid; // Reject "127.0", "192.168.1", etc.
1333+
}
1334+
// Also verify it's numeric (digits and dots only)
1335+
if (!std::all_of(hostname.begin(), hostname.end(),
1336+
[](char c) { return (c >= '0' && c <= '9') || c == '.'; })) {
1337+
return QValidator::Invalid;
1338+
}
1339+
}
1340+
12811341
CService serv(LookupNumeric(input.toStdString(), DEFAULT_GUI_PROXY_PORT));
12821342
Proxy addrProxy = Proxy(serv, true);
12831343
if (addrProxy.IsValid())

0 commit comments

Comments
 (0)