Skip to content

Commit

Permalink
Improve Tor support
Browse files Browse the repository at this point in the history
* Add list of seed nodes;
* Implement Tor settings in the GUI client;
* Disable DNS and IRC seeding, public IP discovery in -onlynet=tor mode;
* Fix mapMultiArgs issue in SoftSetArg function.
  • Loading branch information
CryptoManiac committed Jan 25, 2015
1 parent 5364916 commit 24b2f2d
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 25 deletions.
17 changes: 13 additions & 4 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,12 +661,21 @@ bool AppInit2()
}

// see Step 2: parameter interactions for more information about these
fNoListen = !GetBoolArg("-listen", true);
fDiscover = GetBoolArg("-discover", true);
fNameLookup = GetBoolArg("-dns", true);
if (!IsLimited(NET_IPV4) || !IsLimited(NET_IPV6))
{
fNoListen = !GetBoolArg("-listen", true);
fDiscover = GetBoolArg("-discover", true);
fNameLookup = GetBoolArg("-dns", true);
#ifdef USE_UPNP
fUseUPnP = GetBoolArg("-upnp", USE_UPNP);
fUseUPnP = GetBoolArg("-upnp", USE_UPNP);
#endif
} else {
// Don't listen, discover addresses or search for nodes if IPv4 and IPv6 networking is disabled.
fNoListen = true;
fDiscover = fNameLookup = fUseUPnP = false;
SoftSetBoolArg("-irc", false);
SoftSetBoolArg("-dnsseed", false);
}

bool fBound = false;
if (!fNoListen)
Expand Down
43 changes: 39 additions & 4 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,22 @@ uint32_t pnSeed[] =
0x4d226805,
};

const char* pchTorSeed[] =
{
"seedp4knqnoei57u.onion",
"seedr3hhlepyi7fd.onion",
"seed3uuomkclbiz4.onion",
"seedeh7qck3ouff5.onion",
"5rg3vq4jagckeckf.onion",
"seedt3sraf53ajiy.onion",
"seedg4qyccsg42oq.onion",
"novaqrtoywpg7jly.onion",
"seed3d5wolqbgrcb.onion",
"seed24u5dwph3qw4.onion",
"mj26ulzbs2oskgym.onion",
"eqon4usunavt76m7.onion",
};

void DumpAddresses()
{
int64_t nStart = GetTimeMillis();
Expand Down Expand Up @@ -1316,7 +1332,7 @@ void ThreadOpenConnections2(void* parg)
return;

// Add seed nodes if IRC isn't working
if (addrman.size()==0 && (GetTime() - nStart > 60) && !fTestNet)
if (!IsLimited(NET_IPV4) && addrman.size()==0 && (GetTime() - nStart > 60) && !fTestNet)
{
std::vector<CAddress> vAdd;
for (unsigned int i = 0; i < ARRAYLEN(pnSeed); i++)
Expand All @@ -1335,6 +1351,20 @@ void ThreadOpenConnections2(void* parg)
addrman.Add(vAdd, CNetAddr("127.0.0.1"));
}

// Add Tor nodes if we have connection with onion router
if (mapArgs.count("-tor"))
{
std::vector<CAddress> vAdd;
for (unsigned int i = 0; i < ARRAYLEN(pchTorSeed); i++)
{
const int64_t nOneWeek = 7*24*60*60;
CAddress addr(CService(pchTorSeed[i], GetDefaultPort()));
addr.nTime = GetTime()-GetRand(nOneWeek)-nOneWeek;
vAdd.push_back(addr);
}
addrman.Add(vAdd, CNetAddr("dummyaddress.onion"));
}

//
// Choose an address to connect to based on most recently seen
//
Expand Down Expand Up @@ -1855,12 +1885,17 @@ void StartNode(void* parg)
printf("Error: NewThread(ThreadDNSAddressSeed) failed\n");

// Map ports with UPnP
if (fUseUPnP)
if (!fUseUPnP)
printf("UPNP port mapping is disabled\n");
else
MapPort();

// Get addresses from IRC and advertise ours
if (!NewThread(ThreadIRCSeed, NULL))
printf("Error: NewThread(ThreadIRCSeed) failed\n");
if (!GetBoolArg("-irc", true))
printf("IRC seeding disabled\n");
else
if (!NewThread(ThreadIRCSeed, NULL))
printf("Error: NewThread(ThreadIRCSeed) failed\n");

// Send and receive from sockets, accept connections
if (!NewThread(ThreadSocketHandler, NULL))
Expand Down
73 changes: 72 additions & 1 deletion src/qt/forms/optionsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<enum>QTabWidget::North</enum>
</property>
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<widget class="QWidget" name="tabMain">
<attribute name="title">
Expand Down Expand Up @@ -232,6 +232,77 @@
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="connectTor">
<property name="text">
<string>Connect through &amp;Tor:</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_Tor">
<item>
<widget class="QLabel" name="torIpLabel">
<property name="minimumSize">
<size>
<width>60</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Tor IP:</string>
</property>
</widget>
</item>
<item>
<widget class="QValidatedLineEdit" name="torIp">
<property name="maximumSize">
<size>
<width>130</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="torPortLabel">
<property name="text">
<string>Port:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="torPort">
<property name="maximumSize">
<size>
<width>55</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="TorOnly">
<property name="text">
<string>Use Tor only</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_Network">
<property name="orientation">
Expand Down
60 changes: 58 additions & 2 deletions src/qt/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ OptionsDialog::OptionsDialog(QWidget *parent) :
model(0),
mapper(0),
fRestartWarningDisplayed_Proxy(false),
fRestartWarningDisplayed_Tor(false),
fRestartWarningDisplayed_Lang(false),
fRestartWarningDisplayed_URL(false),
fProxyIpValid(true)
fProxyIpValid(true),
fTorIpValid(true)
{
ui->setupUi(this);

Expand All @@ -35,6 +37,11 @@ OptionsDialog::OptionsDialog(QWidget *parent) :
ui->proxyPort->setEnabled(false);
ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));

ui->torIp->setEnabled(false);
ui->torPort->setEnabled(false);
ui->torPort->setValidator(new QIntValidator(1, 65535, this));
ui->TorOnly->setEnabled(false);

ui->socksVersion->setEnabled(false);
ui->socksVersion->addItem("5", 5);
ui->socksVersion->addItem("4", 4);
Expand All @@ -45,7 +52,14 @@ OptionsDialog::OptionsDialog(QWidget *parent) :
connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->socksVersion, SLOT(setEnabled(bool)));
connect(ui->connectSocks, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning_Proxy()));

connect(ui->connectTor, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning_Tor()));
connect(ui->connectTor, SIGNAL(toggled(bool)), ui->torIp, SLOT(setEnabled(bool)));
connect(ui->connectTor, SIGNAL(toggled(bool)), ui->torPort, SLOT(setEnabled(bool)));
connect(ui->connectTor, SIGNAL(toggled(bool)), ui->TorOnly, SLOT(setEnabled(bool)));
connect(ui->TorOnly, SIGNAL(toggled(bool)), ui->connectSocks, SLOT(setDisabled(bool)));

ui->proxyIp->installEventFilter(this);
ui->torIp->installEventFilter(this);

/* Window elements init */
#ifdef Q_OS_MAC
Expand Down Expand Up @@ -100,6 +114,8 @@ OptionsDialog::OptionsDialog(QWidget *parent) :
connect(mapper, SIGNAL(currentIndexChanged(int)), this, SLOT(disableApplyButton()));
/* setup/change UI elements when proxy IP is invalid/valid */
connect(this, SIGNAL(proxyIpValid(QValidatedLineEdit *, bool)), this, SLOT(handleProxyIpValid(QValidatedLineEdit *, bool)));
/* setup/change UI elements when Tor IP is invalid/valid */
connect(this, SIGNAL(torIpValid(QValidatedLineEdit *, bool)), this, SLOT(handleTorIpValid(QValidatedLineEdit *, bool)));
}

OptionsDialog::~OptionsDialog()
Expand Down Expand Up @@ -146,6 +162,11 @@ void OptionsDialog::setMapper()
mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
mapper->addMapping(ui->socksVersion, OptionsModel::ProxySocksVersion);

mapper->addMapping(ui->connectTor, OptionsModel::TorUse);
mapper->addMapping(ui->torIp, OptionsModel::TorIP);
mapper->addMapping(ui->torPort, OptionsModel::TorPort);
mapper->addMapping(ui->TorOnly, OptionsModel::TorOnly);

/* Window */
#ifndef Q_OS_MAC
mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
Expand Down Expand Up @@ -173,7 +194,7 @@ void OptionsDialog::disableApplyButton()
void OptionsDialog::enableSaveButtons()
{
/* prevent enabling of the save buttons when data modified, if there is an invalid proxy address present */
if(fProxyIpValid)
if(fProxyIpValid && fTorIpValid)
setSaveButtonState(true);
}

Expand Down Expand Up @@ -214,6 +235,15 @@ void OptionsDialog::showRestartWarning_Proxy()
}
}

void OptionsDialog::showRestartWarning_Tor()
{
if(!fRestartWarningDisplayed_Proxy)
{
QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting NovaCoin."), QMessageBox::Ok);
fRestartWarningDisplayed_Tor = true;
}
}

void OptionsDialog::showRestartWarning_Lang()
{
if(!fRestartWarningDisplayed_Lang)
Expand Down Expand Up @@ -261,6 +291,25 @@ void OptionsDialog::handleProxyIpValid(QValidatedLineEdit *object, bool fState)
}
}

void OptionsDialog::handleTorIpValid(QValidatedLineEdit *object, bool fState)
{
// this is used in a check before re-enabling the save buttons
fTorIpValid = fState;

if(fTorIpValid)
{
enableSaveButtons();
ui->statusLabel->clear();
}
else
{
disableSaveButtons();
object->setValid(fTorIpValid);
ui->statusLabel->setStyleSheet("QLabel { color: red; }");
ui->statusLabel->setText(tr("The supplied tor address is invalid."));
}
}

bool OptionsDialog::eventFilter(QObject *object, QEvent *event)
{
if(event->type() == QEvent::FocusOut)
Expand All @@ -271,6 +320,13 @@ bool OptionsDialog::eventFilter(QObject *object, QEvent *event)
/* Check proxyIp for a valid IPv4/IPv6 address and emit the proxyIpValid signal */
emit proxyIpValid(ui->proxyIp, LookupNumeric(ui->proxyIp->text().toStdString().c_str(), addr));
}

if(object == ui->torIp)
{
CService addr;
/* Check proxyIp for a valid IPv4/IPv6 address and emit the torIpValid signal */
emit torIpValid(ui->torIp, LookupNumeric(ui->torIp->text().toStdString().c_str(), addr));
}
}
return QDialog::eventFilter(object, event);
}
5 changes: 5 additions & 0 deletions src/qt/optionsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,27 @@ private slots:
void on_applyButton_clicked();

void showRestartWarning_Proxy();
void showRestartWarning_Tor();
void showRestartWarning_Lang();
void showRestartWarning_URL();
void updateDisplayUnit();
void handleProxyIpValid(QValidatedLineEdit *object, bool fState);
void handleTorIpValid(QValidatedLineEdit *object, bool fState);

signals:
void proxyIpValid(QValidatedLineEdit *object, bool fValid);
void torIpValid(QValidatedLineEdit *object, bool fValid);

private:
Ui::OptionsDialog *ui;
OptionsModel *model;
MonitoredDataMapper *mapper;
bool fRestartWarningDisplayed_Proxy;
bool fRestartWarningDisplayed_Tor;
bool fRestartWarningDisplayed_Lang;
bool fRestartWarningDisplayed_URL;
bool fProxyIpValid;
bool fTorIpValid;
};

#endif // OPTIONSDIALOG_H
Loading

0 comments on commit 24b2f2d

Please sign in to comment.