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)
410413class TorController
411414{
412415public:
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)
731736static struct event_base *gBase ;
732737static 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
758765void 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+ }
0 commit comments