Skip to content

Commit 8f4e67f

Browse files
committed
net: Automatically create hidden service, listen on Tor
Starting with Tor version 0.2.7.1 it is possible, through Tor's control socket API, to create and destroy 'ephemeral' hidden services programmatically. https://stem.torproject.org/api/control.html#stem.control.Controller.create_ephemeral_hidden_service This means that if Tor is running (and proper authorization is available), bitcoin automatically creates a hidden service to listen on, without user manual configuration. This will positively affect the number of available .onion nodes. - When the node is started, connect to Tor through control socket - Send `ADD_ONION` command - First time: - Make it create a hidden service key - Save the key in the data directory for later usage - Make it redirect port 8333 to the local port 8333 (or whatever port we're listening on). - Keep control socket connection open for as long node is running. The hidden service will (by default) automatically go away when the connection is closed.
1 parent b56953e commit 8f4e67f

File tree

8 files changed

+611
-4
lines changed

8 files changed

+611
-4
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ BITCOIN_CORE_H = \
149149
threadsafety.h \
150150
timedata.h \
151151
tinyformat.h \
152+
torcontrol.h \
152153
txdb.h \
153154
txmempool.h \
154155
ui_interface.h \
@@ -206,6 +207,7 @@ libbitcoin_server_a_SOURCES = \
206207
rpcserver.cpp \
207208
script/sigcache.cpp \
208209
timedata.cpp \
210+
torcontrol.cpp \
209211
txdb.cpp \
210212
txmempool.cpp \
211213
validationinterface.cpp \

src/init.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "scheduler.h"
2929
#include "txdb.h"
3030
#include "txmempool.h"
31+
#include "torcontrol.h"
3132
#include "ui_interface.h"
3233
#include "util.h"
3334
#include "utilmoneystr.h"
@@ -187,6 +188,7 @@ void Shutdown()
187188
#endif
188189
GenerateBitcoins(false, 0, Params());
189190
StopNode();
191+
StopTorControl();
190192
UnregisterNodeSignals(GetNodeSignals());
191193

192194
if (fFeeEstimatesInitialized)
@@ -347,6 +349,7 @@ std::string HelpMessage(HelpMessageMode mode)
347349
strUsage += HelpMessageOpt("-externalip=<ip>", _("Specify your own public address"));
348350
strUsage += HelpMessageOpt("-forcednsseed", strprintf(_("Always query for peer addresses via DNS lookup (default: %u)"), 0));
349351
strUsage += HelpMessageOpt("-listen", _("Accept connections from outside (default: 1 if no -proxy or -connect)"));
352+
strUsage += HelpMessageOpt("-listenonion", strprintf(_("Automatically create Tor hidden service (default: %d)"), DEFAULT_LISTEN_ONION));
350353
strUsage += HelpMessageOpt("-maxconnections=<n>", strprintf(_("Maintain at most <n> connections to peers (default: %u)"), DEFAULT_MAX_PEER_CONNECTIONS));
351354
strUsage += HelpMessageOpt("-maxreceivebuffer=<n>", strprintf(_("Maximum per-connection receive buffer, <n>*1000 bytes (default: %u)"), 5000));
352355
strUsage += HelpMessageOpt("-maxsendbuffer=<n>", strprintf(_("Maximum per-connection send buffer, <n>*1000 bytes (default: %u)"), 1000));
@@ -358,6 +361,7 @@ std::string HelpMessage(HelpMessageMode mode)
358361
strUsage += HelpMessageOpt("-proxyrandomize", strprintf(_("Randomize credentials for every proxy connection. This enables Tor stream isolation (default: %u)"), 1));
359362
strUsage += HelpMessageOpt("-seednode=<ip>", _("Connect to a node to retrieve peer addresses, and disconnect"));
360363
strUsage += HelpMessageOpt("-timeout=<n>", strprintf(_("Specify connection timeout in milliseconds (minimum: 1, default: %d)"), DEFAULT_CONNECT_TIMEOUT));
364+
strUsage += HelpMessageOpt("-torcontrol=<ip>:<port>", strprintf(_("Tor control port to use if onion listening enabled (default: %s)"), DEFAULT_TOR_CONTROL));
361365
#ifdef USE_UPNP
362366
#if USE_UPNP
363367
strUsage += HelpMessageOpt("-upnp", _("Use UPnP to map the listening port (default: 1 when listening and no -proxy)"));
@@ -777,6 +781,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
777781
LogPrintf("%s: parameter interaction: -listen=0 -> setting -upnp=0\n", __func__);
778782
if (SoftSetBoolArg("-discover", false))
779783
LogPrintf("%s: parameter interaction: -listen=0 -> setting -discover=0\n", __func__);
784+
if (SoftSetBoolArg("-listenonion", false))
785+
LogPrintf("%s: parameter interaction: -listen=0 -> setting -listenonion=0\n", __func__);
780786
}
781787

782788
if (mapArgs.count("-externalip")) {
@@ -1567,6 +1573,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
15671573
LogPrintf("mapAddressBook.size() = %u\n", pwalletMain ? pwalletMain->mapAddressBook.size() : 0);
15681574
#endif
15691575

1576+
if (GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION))
1577+
StartTorControl(threadGroup, scheduler);
1578+
15701579
StartNode(threadGroup, scheduler);
15711580

15721581
// Monitor the chain, and alert if we get blocks much quicker or slower than expected

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4038,9 +4038,11 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
40384038
CAddress addr = GetLocalAddress(&pfrom->addr);
40394039
if (addr.IsRoutable())
40404040
{
4041+
LogPrintf("ProcessMessages: advertizing address %s\n", addr.ToString());
40414042
pfrom->PushAddress(addr);
40424043
} else if (IsPeerAddrLocalGood(pfrom)) {
40434044
addr.SetIP(pfrom->addrLocal);
4045+
LogPrintf("ProcessMessages: advertizing address %s\n", addr.ToString());
40444046
pfrom->PushAddress(addr);
40454047
}
40464048
}

src/net.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ void AdvertizeLocal(CNode *pnode)
216216
}
217217
if (addrLocal.IsRoutable())
218218
{
219+
LogPrintf("AdvertizeLocal: advertizing address %s\n", addrLocal.ToString());
219220
pnode->PushAddress(addrLocal);
220221
}
221222
}

src/netbase.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,7 @@ bool LookupNumeric(const char *pszName, CService& addr, int portDefault)
227227
return Lookup(pszName, addr, portDefault, false);
228228
}
229229

230-
/**
231-
* Convert milliseconds to a struct timeval for select.
232-
*/
233-
struct timeval static MillisToTimeval(int64_t nTimeout)
230+
struct timeval MillisToTimeval(int64_t nTimeout)
234231
{
235232
struct timeval timeout;
236233
timeout.tv_sec = nTimeout / 1000;

src/netbase.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,5 +215,9 @@ std::string NetworkErrorString(int err);
215215
bool CloseSocket(SOCKET& hSocket);
216216
/** Disable or enable blocking-mode for a socket */
217217
bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking);
218+
/**
219+
* Convert milliseconds to a struct timeval for e.g. select.
220+
*/
221+
struct timeval MillisToTimeval(int64_t nTimeout);
218222

219223
#endif // BITCOIN_NETBASE_H

0 commit comments

Comments
 (0)