3
3
// Distributed under the MIT software license, see the accompanying
4
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
6
- #include < chainparams.h>
7
6
#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>
10
11
#include < net.h>
12
+ #include < netaddress.h>
13
+ #include < netbase.h>
14
+ #include < util/strencodings.h>
11
15
#include < util/system.h>
12
- #include < crypto/hmac_sha256.h>
13
16
14
17
#include < vector>
15
18
#include < deque>
@@ -410,7 +413,7 @@ static bool WriteBinaryFile(const fs::path &filename, const std::string &data)
410
413
class TorController
411
414
{
412
415
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 );
414
417
~TorController ();
415
418
416
419
/* * Get name of file to store private key in */
@@ -428,6 +431,7 @@ class TorController
428
431
struct event *reconnect_ev;
429
432
float reconnect_timeout;
430
433
CService service;
434
+ const CService m_target;
431
435
/* * Cookie for SAFECOOKIE auth */
432
436
std::vector<uint8_t > cookie;
433
437
/* * ClientNonce for SAFECOOKIE auth */
@@ -450,10 +454,11 @@ class TorController
450
454
static void reconnect_cb (evutil_socket_t fd, short what, void *arg);
451
455
};
452
456
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 ):
454
458
base(_base),
455
459
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)
457
462
{
458
463
reconnect_ev = event_new (base, -1 , 0 , reconnect_cb, this );
459
464
if (!reconnect_ev)
@@ -536,7 +541,7 @@ void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply&
536
541
private_key = " NEW:RSA1024" ; // Explicitly request RSA1024 - see issue #9214
537
542
// Request onion service, redirect port.
538
543
// 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 ()),
540
545
std::bind (&TorController::add_onion_cb, this , std::placeholders::_1, std::placeholders::_2));
541
546
} else {
542
547
LogPrintf (" tor: Authentication failed\n " );
@@ -731,14 +736,14 @@ void TorController::reconnect_cb(evutil_socket_t fd, short what, void *arg)
731
736
static struct event_base *gBase ;
732
737
static std::thread torControlThread;
733
738
734
- static void TorControlThread ()
739
+ static void TorControlThread (CService onion_service_target )
735
740
{
736
- TorController ctrl (gBase , gArgs .GetArg (" -torcontrol" , DEFAULT_TOR_CONTROL));
741
+ TorController ctrl (gBase , gArgs .GetArg (" -torcontrol" , DEFAULT_TOR_CONTROL), onion_service_target );
737
742
738
743
event_base_dispatch (gBase );
739
744
}
740
745
741
- void StartTorControl ()
746
+ void StartTorControl (CService onion_service_target )
742
747
{
743
748
assert (!gBase );
744
749
#ifdef WIN32
@@ -752,7 +757,9 @@ void StartTorControl()
752
757
return ;
753
758
}
754
759
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
+ });
756
763
}
757
764
758
765
void InterruptTorControl ()
@@ -773,3 +780,10 @@ void StopTorControl()
773
780
gBase = nullptr ;
774
781
}
775
782
}
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