Bitcoin Core  24.99.0
P2P Digital Currency
Go to the documentation of this file.
1 // Copyright (c) 2015-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or
11 #include <fs.h>
12 #include <netaddress.h>
14 #include <boost/signals2/signal.hpp>
16 #include <event2/bufferevent.h>
17 #include <event2/event.h>
19 #include <cstdlib>
20 #include <deque>
21 #include <functional>
22 #include <string>
23 #include <vector>
25 class CService;
27 extern const std::string DEFAULT_TOR_CONTROL;
28 static const bool DEFAULT_LISTEN_ONION = true;
30 void StartTorControl(CService onion_service_target);
31 void InterruptTorControl();
32 void StopTorControl();
38 {
39 public:
42  int code;
43  std::vector<std::string> lines;
45  void Clear()
46  {
47  code = 0;
48  lines.clear();
49  }
50 };
56 {
57 public:
58  typedef std::function<void(TorControlConnection&)> ConnectionCB;
59  typedef std::function<void(TorControlConnection &,const TorControlReply &)> ReplyHandlerCB;
63  explicit TorControlConnection(struct event_base *base);
73  bool Connect(const std::string& tor_control_center, const ConnectionCB& connected, const ConnectionCB& disconnected);
78  void Disconnect();
84  bool Command(const std::string &cmd, const ReplyHandlerCB& reply_handler);
87  boost::signals2::signal<void(TorControlConnection &,const TorControlReply &)> async_handler;
88 private:
90  std::function<void(TorControlConnection&)> connected;
92  std::function<void(TorControlConnection&)> disconnected;
94  struct event_base *base;
96  struct bufferevent* b_conn{nullptr};
100  std::deque<ReplyHandlerCB> reply_handlers;
103  static void readcb(struct bufferevent *bev, void *ctx);
104  static void eventcb(struct bufferevent *bev, short what, void *ctx);
105 };
107 /****** Bitcoin specific TorController implementation ********/
113 {
114 public:
115  TorController(struct event_base* base, const std::string& tor_control_center, const CService& target);
116  TorController() : conn{nullptr} {
117  // Used for testing only.
118  }
119  ~TorController();
125  void Reconnect();
126 private:
127  struct event_base* base;
128  const std::string m_tor_control_center;
130  std::string private_key;
131  std::string service_id;
132  bool reconnect;
133  struct event *reconnect_ev = nullptr;
138  std::vector<uint8_t> cookie;
140  std::vector<uint8_t> clientNonce;
142 public:
148  void auth_cb(TorControlConnection& conn, const TorControlReply& reply);
159  static void reconnect_cb(evutil_socket_t fd, short what, void *arg);
160 };
const auto cmd
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:523
Low-level handling for Tor control connection.
Definition: torcontrol.h:56
TorControlReply message
Message being received.
Definition: torcontrol.h:98
std::deque< ReplyHandlerCB > reply_handlers
Response handlers.
Definition: torcontrol.h:100
boost::signals2::signal< void(TorControlConnection &, const TorControlReply &)> async_handler
Response handlers for async replies.
Definition: torcontrol.h:87
bool Connect(const std::string &tor_control_center, const ConnectionCB &connected, const ConnectionCB &disconnected)
Connect to a Tor control port.
Definition: torcontrol.cpp:129
TorControlConnection(struct event_base *base)
Create a new TorControlConnection.
Definition: torcontrol.cpp:56
bool Command(const std::string &cmd, const ReplyHandlerCB &reply_handler)
Send a command, register a handler for the reply.
Definition: torcontrol.cpp:173
std::function< void(TorControlConnection &)> connected
Callback when ready for use.
Definition: torcontrol.h:90
static void readcb(struct bufferevent *bev, void *ctx)
Libevent handlers: internal.
Definition: torcontrol.cpp:67
std::function< void(TorControlConnection &, const TorControlReply &)> ReplyHandlerCB
Definition: torcontrol.h:59
static void eventcb(struct bufferevent *bev, short what, void *ctx)
Definition: torcontrol.cpp:112
std::function< void(TorControlConnection &)> disconnected
Callback when connection lost.
Definition: torcontrol.h:92
std::function< void(TorControlConnection &)> ConnectionCB
Definition: torcontrol.h:58
void Disconnect()
Disconnect from Tor control port.
Definition: torcontrol.cpp:166
struct bufferevent * b_conn
Connection to control socket.
Definition: torcontrol.h:96
struct event_base * base
Libevent event base.
Definition: torcontrol.h:94
Reply from Tor, can be single or multi-line.
Definition: torcontrol.h:38
std::vector< std::string > lines
Definition: torcontrol.h:43
Controller that connects to Tor control socket, authenticate, then create and maintain an ephemeral o...
Definition: torcontrol.h:113
TorControlConnection conn
Definition: torcontrol.h:129
static void reconnect_cb(evutil_socket_t fd, short what, void *arg)
Callback for reconnect timer.
Definition: torcontrol.cpp:643
std::string service_id
Definition: torcontrol.h:131
struct event_base * base
Definition: torcontrol.h:127
fs::path GetPrivateKeyFile()
Get name of file to store private key in.
Definition: torcontrol.cpp:638
std::vector< uint8_t > clientNonce
ClientNonce for SAFECOOKIE auth.
Definition: torcontrol.h:140
void connected_cb(TorControlConnection &conn)
Callback after successful connection.
Definition: torcontrol.cpp:601
void get_socks_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for GETINFO net/listeners/socks result.
Definition: torcontrol.cpp:337
void add_onion_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for ADD_ONION result.
Definition: torcontrol.cpp:404
const std::string m_tor_control_center
Definition: torcontrol.h:128
void disconnected_cb(TorControlConnection &conn)
Callback after connection lost or failed connection attempt.
Definition: torcontrol.cpp:609
const CService m_target
Definition: torcontrol.h:136
void authchallenge_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for AUTHCHALLENGE result.
Definition: torcontrol.cpp:490
CService service
Definition: torcontrol.h:135
std::vector< uint8_t > cookie
Cookie for SAFECOOKIE auth.
Definition: torcontrol.h:138
struct event * reconnect_ev
Definition: torcontrol.h:133
float reconnect_timeout
Definition: torcontrol.h:134
void auth_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for AUTHENTICATE result.
Definition: torcontrol.cpp:439
void Reconnect()
Reconnect, after getting disconnected.
Definition: torcontrol.cpp:627
std::string private_key
Definition: torcontrol.h:130
void protocolinfo_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for PROTOCOLINFO result.
Definition: torcontrol.cpp:525
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
static secp256k1_context * ctx
Definition: tests.c:35
CService DefaultOnionServiceTarget()
Definition: torcontrol.cpp:699
const std::string DEFAULT_TOR_CONTROL
Default control port.
Definition: torcontrol.cpp:34
static const bool DEFAULT_LISTEN_ONION
Definition: torcontrol.h:28
void InterruptTorControl()
Definition: torcontrol.cpp:680
void StartTorControl(CService onion_service_target)
Definition: torcontrol.cpp:661
void StopTorControl()
Definition: torcontrol.cpp:690