Skip to content

Commit 57f17e5

Browse files
committed
net: Pass onion service target to Tor controller
1 parent e3f0785 commit 57f17e5

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
19121912
LogPrintf("nBestHeight = %d\n", chain_active_height);
19131913

19141914
if (args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION))
1915-
StartTorControl();
1915+
StartTorControl(DefaultOnionServiceTarget());
19161916

19171917
Discover();
19181918

src/torcontrol.cpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6-
#include <chainparams.h>
76
#include <torcontrol.h>
8-
#include <util/strencodings.h>
9-
#include <netbase.h>
7+
8+
#include <chainparams.h>
9+
#include <chainparamsbase.h>
10+
#include <crypto/hmac_sha256.h>
1011
#include <net.h>
12+
#include <netaddress.h>
13+
#include <netbase.h>
14+
#include <util/strencodings.h>
1115
#include <util/system.h>
12-
#include <crypto/hmac_sha256.h>
1316

1417
#include <vector>
1518
#include <deque>
@@ -410,7 +413,7 @@ static bool WriteBinaryFile(const fs::path &filename, const std::string &data)
410413
class TorController
411414
{
412415
public:
413-
TorController(struct event_base* base, const std::string& tor_control_center);
416+
TorController(struct event_base* base, const std::string& tor_control_center, const CService& target);
414417
~TorController();
415418

416419
/** Get name of file to store private key in */
@@ -428,6 +431,7 @@ class TorController
428431
struct event *reconnect_ev;
429432
float reconnect_timeout;
430433
CService service;
434+
const CService m_target;
431435
/** Cookie for SAFECOOKIE auth */
432436
std::vector<uint8_t> cookie;
433437
/** ClientNonce for SAFECOOKIE auth */
@@ -450,10 +454,11 @@ class TorController
450454
static void reconnect_cb(evutil_socket_t fd, short what, void *arg);
451455
};
452456

453-
TorController::TorController(struct event_base* _base, const std::string& tor_control_center):
457+
TorController::TorController(struct event_base* _base, const std::string& tor_control_center, const CService& target):
454458
base(_base),
455459
m_tor_control_center(tor_control_center), conn(base), reconnect(true), reconnect_ev(0),
456-
reconnect_timeout(RECONNECT_TIMEOUT_START)
460+
reconnect_timeout(RECONNECT_TIMEOUT_START),
461+
m_target(target)
457462
{
458463
reconnect_ev = event_new(base, -1, 0, reconnect_cb, this);
459464
if (!reconnect_ev)
@@ -536,7 +541,7 @@ void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply&
536541
private_key = "NEW:RSA1024"; // Explicitly request RSA1024 - see issue #9214
537542
// Request onion service, redirect port.
538543
// Note that the 'virtual' port is always the default port to avoid decloaking nodes using other ports.
539-
_conn.Command(strprintf("ADD_ONION %s Port=%i,127.0.0.1:%i", private_key, Params().GetDefaultPort(), GetListenPort()),
544+
_conn.Command(strprintf("ADD_ONION %s Port=%i,%s", private_key, Params().GetDefaultPort(), m_target.ToStringIPPort()),
540545
std::bind(&TorController::add_onion_cb, this, std::placeholders::_1, std::placeholders::_2));
541546
} else {
542547
LogPrintf("tor: Authentication failed\n");
@@ -731,14 +736,14 @@ void TorController::reconnect_cb(evutil_socket_t fd, short what, void *arg)
731736
static struct event_base *gBase;
732737
static std::thread torControlThread;
733738

734-
static void TorControlThread()
739+
static void TorControlThread(CService onion_service_target)
735740
{
736-
TorController ctrl(gBase, gArgs.GetArg("-torcontrol", DEFAULT_TOR_CONTROL));
741+
TorController ctrl(gBase, gArgs.GetArg("-torcontrol", DEFAULT_TOR_CONTROL), onion_service_target);
737742

738743
event_base_dispatch(gBase);
739744
}
740745

741-
void StartTorControl()
746+
void StartTorControl(CService onion_service_target)
742747
{
743748
assert(!gBase);
744749
#ifdef WIN32
@@ -752,7 +757,9 @@ void StartTorControl()
752757
return;
753758
}
754759

755-
torControlThread = std::thread(std::bind(&TraceThread<void (*)()>, "torcontrol", &TorControlThread));
760+
torControlThread = std::thread(&TraceThread<std::function<void()>>, "torcontrol", [onion_service_target] {
761+
TorControlThread(onion_service_target);
762+
});
756763
}
757764

758765
void InterruptTorControl()
@@ -773,3 +780,10 @@ void StopTorControl()
773780
gBase = nullptr;
774781
}
775782
}
783+
784+
CService DefaultOnionServiceTarget()
785+
{
786+
struct in_addr onion_service_target;
787+
onion_service_target.s_addr = htonl(INADDR_LOOPBACK);
788+
return {onion_service_target, BaseParams().OnionServiceTargetPort()};
789+
}

src/torcontrol.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
#ifndef BITCOIN_TORCONTROL_H
99
#define BITCOIN_TORCONTROL_H
1010

11+
#include <string>
12+
13+
class CService;
1114

1215
extern const std::string DEFAULT_TOR_CONTROL;
1316
static const bool DEFAULT_LISTEN_ONION = true;
1417

15-
void StartTorControl();
18+
void StartTorControl(CService onion_service_target);
1619
void InterruptTorControl();
1720
void StopTorControl();
1821

22+
CService DefaultOnionServiceTarget();
23+
1924
#endif /* BITCOIN_TORCONTROL_H */

0 commit comments

Comments
 (0)