From c5e8b01c2a6df7992e885ec49d464bb836b2c027 Mon Sep 17 00:00:00 2001 From: adityapk Date: Wed, 12 Dec 2018 14:45:10 -0800 Subject: [PATCH] #74 Add Tor Support --- src/connection.cpp | 5 ++- src/connection.h | 1 + src/mainwindow.cpp | 30 ++++++++++++++++++ src/precompiled.h | 1 + src/settings.cpp | 44 ++++++++++++++++++++++++++ src/settings.h | 3 ++ src/settings.ui | 78 ++++++++++++++++++++++++++++++---------------- 7 files changed, 134 insertions(+), 28 deletions(-) diff --git a/src/connection.cpp b/src/connection.cpp index 7215527..5db52a3 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -550,6 +550,9 @@ std::shared_ptr ConnectionLoader::autoDetectZcashConf() { if (name == "daemon" && value == "1") { zcashconf->zcashDaemon = true; } + if (name == "proxy") { + zcashconf->proxy = value; + } if (name == "testnet" && value == "1" && zcashconf->port.isEmpty()) { @@ -581,7 +584,7 @@ std::shared_ptr ConnectionLoader::loadFromSettings() { if (username.isEmpty() || password.isEmpty()) return nullptr; - auto uiConfig = new ConnectionConfig{ host, port, username, password, false, false, "", ConnectionType::UISettingsZCashD}; + auto uiConfig = new ConnectionConfig{ host, port, username, password, false, false, "", "", ConnectionType::UISettingsZCashD}; return std::shared_ptr(uiConfig); } diff --git a/src/connection.h b/src/connection.h index 46f6802..590ba2e 100644 --- a/src/connection.h +++ b/src/connection.h @@ -23,6 +23,7 @@ struct ConnectionConfig { bool usingZcashConf; bool zcashDaemon; QString zcashDir; + QString proxy; ConnectionType connType; }; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index c0305ca..d430155 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -393,6 +393,17 @@ void MainWindow::setupSettingsModal() { // Auto shielding settings.chkAutoShield->setChecked(Settings::getInstance()->getAutoShield()); + // Use Tor + bool isUsingTor = !rpc->getConnection()->config->proxy.isEmpty(); + settings.chkTor->setChecked(isUsingTor); + if (rpc->getEZcashD() == nullptr) { + settings.chkTor->setEnabled(false); + settings.lblTor->setEnabled(false); + QString tooltip = tr("Tor configuration is available only when running an embedded zcashd."); + settings.chkTor->setToolTip(tooltip); + settings.lblTor->setToolTip(tooltip); + } + // Connection Settings QIntValidator validator(0, 65535); settings.port->setValidator(&validator); @@ -435,6 +446,25 @@ void MainWindow::setupSettingsModal() { // Auto shield Settings::getInstance()->setAutoShield(settings.chkAutoShield->isChecked()); + if (!isUsingTor && settings.chkTor->isChecked()) { + // If "use tor" was previously unchecked and now checked + Settings::addToZcashConf(zcashConfLocation, "proxy=127.0.0.1:9050"); + rpc->getConnection()->config->proxy = "proxy=127.0.0.1:9050"; + + QMessageBox::information(this, tr("Enable Tor"), + tr("Connection over Tor has been enabled. To use this feature, you need to restart zec-qt-wallet."), + QMessageBox::Ok); + } + if (isUsingTor && !settings.chkTor->isChecked()) { + // If "use tor" was previously checked and now is unchecked + Settings::removeFromZcashConf(zcashConfLocation, "proxy"); + rpc->getConnection()->config->proxy.clear(); + + QMessageBox::information(this, tr("Disable Tor"), + tr("Connection over Tor has been disabled. To fully disconnect from Tor, you need to restart zec-qt-wallet."), + QMessageBox::Ok); + } + if (zcashConfLocation.isEmpty()) { // Save settings Settings::getInstance()->saveSettings( diff --git a/src/precompiled.h b/src/precompiled.h index 59cdc9d..2938652 100644 --- a/src/precompiled.h +++ b/src/precompiled.h @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include diff --git a/src/settings.cpp b/src/settings.cpp index a4f416f..d729c89 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -201,6 +201,50 @@ QString Settings::getDonationAddr(bool sapling) { return "zcEgrceTwvoiFdEvPWcsJHAMrpLsprMF6aRJiQa3fan5ZphyXLPuHghnEPrEPRoEVzUy65GnMVyCTRdkT6BYBepnXh6NBYs"; } +bool Settings::addToZcashConf(QString confLocation, QString line) { + QFile file(confLocation); + if (!file.open(QIODevice::ReadWrite | QIODevice::Append)) + return false; + + + QTextStream out(&file); + out << line << "\n"; + file.close(); + + return true; +} + +bool Settings::removeFromZcashConf(QString confLocation, QString option) { + // To remove an option, we'll create a new file, and copy over everything but the option. + QFile file(confLocation); + if (!file.open(QIODevice::ReadOnly)) + return false; + + QList lines; + QTextStream in(&file); + while (!in.atEnd()) { + QString line = in.readLine(); + auto s = line.indexOf("="); + QString name = line.left(s).trimmed().toLower(); + if (name != option) { + qDebug() << "Copied " << line; + lines.append(line); + } + } + file.close(); + + QFile newfile(confLocation); + if (!newfile.open(QIODevice::ReadWrite | QIODevice::Truncate)) + return false; + + QTextStream out(&newfile); + for (QString line : lines) { + out << line << endl; + } + newfile.close(); + + return true; +} double Settings::getMinerFee() { return 0.0001; diff --git a/src/settings.h b/src/settings.h index 5d9de63..e6eee4e 100644 --- a/src/settings.h +++ b/src/settings.h @@ -76,6 +76,9 @@ public: static bool isValidAddress(QString addr); + static bool addToZcashConf(QString confLocation, QString line); + static bool removeFromZcashConf(QString confLocation, QString option); + static const QString labelRegExp; static const int updateSpeed = 20 * 1000; // 20 sec diff --git a/src/settings.ui b/src/settings.ui index a01fc4e..47e3961 100644 --- a/src/settings.ui +++ b/src/settings.ui @@ -6,14 +6,14 @@ 0 0 - 460 - 464 + 540 + 504 - 460 - 464 + 540 + 500 @@ -26,7 +26,7 @@ - 0 + 1 @@ -145,10 +145,10 @@ Options - - + + - Shielded transactions are saved locally and shown in the transactions tab. If you uncheck this, shielded transactions will not appear in the transactions tab. + Connect to the Tor network via SOCKS proxy running on 127.0.0.1:9050. Please note that you'll have to install and run the Tor service externally. true @@ -162,6 +162,16 @@ + + + + Shielded transactions are saved locally and shown in the transactions tab. If you uncheck this, shielded transactions will not appear in the transactions tab. + + + true + + + @@ -182,14 +192,31 @@ - + + + + Allow custom fees + + + + + + + Normally, change from t-Addresses goes to another t-Address. Checking this option will send the change to your shielded sapling address instead. Check this option to increase your privacy. + + + true + + + + Qt::Horizontal - + Qt::Vertical @@ -202,13 +229,6 @@ - - - - Allow custom fees - - - @@ -219,16 +239,6 @@ - - - - Normally, change from t-Addresses goes to another t-Address. Checking this option will send the change to your shielded sapling address instead. Check this option to increase your privacy. - - - true - - - @@ -243,6 +253,20 @@ + + + + Connect via Tor + + + + + + + + + +