Bitcoin Core  31.0.0
P2P Digital Currency
torcontrol.h
Go to the documentation of this file.
1 // Copyright (c) 2015-present The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
8 #ifndef BITCOIN_TORCONTROL_H
9 #define BITCOIN_TORCONTROL_H
10 
11 #include <netaddress.h>
12 #include <util/fs.h>
13 
14 #include <event2/util.h>
15 
16 #include <cstdint>
17 #include <deque>
18 #include <functional>
19 #include <string>
20 #include <vector>
21 
22 constexpr uint16_t DEFAULT_TOR_SOCKS_PORT{9050};
23 constexpr int DEFAULT_TOR_CONTROL_PORT = 9051;
24 extern const std::string DEFAULT_TOR_CONTROL;
25 static const bool DEFAULT_LISTEN_ONION = true;
26 
27 void StartTorControl(CService onion_service_target);
28 void InterruptTorControl();
29 void StopTorControl();
30 
31 CService DefaultOnionServiceTarget(uint16_t port);
32 
35 {
36 public:
38 
39  int code;
40  std::vector<std::string> lines;
41 
42  void Clear()
43  {
44  code = 0;
45  lines.clear();
46  }
47 };
48 
53 {
54 public:
55  typedef std::function<void(TorControlConnection&)> ConnectionCB;
56  typedef std::function<void(TorControlConnection &,const TorControlReply &)> ReplyHandlerCB;
57 
60  explicit TorControlConnection(struct event_base *base);
62 
70  bool Connect(const std::string& tor_control_center, const ConnectionCB& connected, const ConnectionCB& disconnected);
71 
75  void Disconnect();
76 
81  bool Command(const std::string &cmd, const ReplyHandlerCB& reply_handler);
82 
83 private:
85  std::function<void(TorControlConnection&)> connected;
87  std::function<void(TorControlConnection&)> disconnected;
89  struct event_base *base;
91  struct bufferevent* b_conn{nullptr};
95  std::deque<ReplyHandlerCB> reply_handlers;
96 
98  static void readcb(struct bufferevent *bev, void *ctx);
99  static void eventcb(struct bufferevent *bev, short what, void *ctx);
100 };
101 
102 /****** Bitcoin specific TorController implementation ********/
103 
108 {
109 public:
110  TorController(struct event_base* base, const std::string& tor_control_center, const CService& target);
111  TorController() : conn{nullptr} {
112  // Used for testing only.
113  }
114  ~TorController();
115 
118 
120  void Reconnect();
121 private:
122  struct event_base* base;
123  const std::string m_tor_control_center;
125  std::string private_key;
126  std::string service_id;
127  bool reconnect;
128  struct event *reconnect_ev = nullptr;
133  std::vector<uint8_t> cookie;
135  std::vector<uint8_t> clientNonce;
136 
137 public:
143  void auth_cb(TorControlConnection& conn, const TorControlReply& reply);
152 
154  static void reconnect_cb(evutil_socket_t fd, short what, void *arg);
155 };
156 
157 #endif // BITCOIN_TORCONTROL_H
void authchallenge_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for AUTHCHALLENGE result.
Definition: torcontrol.cpp:515
const CService m_target
Definition: torcontrol.h:131
std::function< void(TorControlConnection &)> disconnected
Callback when connection lost.
Definition: torcontrol.h:87
std::function< void(TorControlConnection &)> ConnectionCB
Definition: torcontrol.h:55
bool Connect(const std::string &tor_control_center, const ConnectionCB &connected, const ConnectionCB &disconnected)
Connect to a Tor control port.
Definition: torcontrol.cpp:150
struct bufferevent * b_conn
Connection to control socket.
Definition: torcontrol.h:91
std::vector< uint8_t > clientNonce
ClientNonce for SAFECOOKIE auth.
Definition: torcontrol.h:135
Reply from Tor, can be single or multi-line.
Definition: torcontrol.h:34
constexpr int DEFAULT_TOR_CONTROL_PORT
Definition: torcontrol.h:23
void protocolinfo_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for PROTOCOLINFO result.
Definition: torcontrol.cpp:550
void Reconnect()
Reconnect, after getting disconnected.
Definition: torcontrol.cpp:654
std::vector< std::string > lines
Definition: torcontrol.h:40
const auto cmd
float reconnect_timeout
Definition: torcontrol.h:129
std::deque< ReplyHandlerCB > reply_handlers
Response handlers.
Definition: torcontrol.h:95
std::function< void(TorControlConnection &, const TorControlReply &)> ReplyHandlerCB
Definition: torcontrol.h:56
void disconnected_cb(TorControlConnection &conn)
Callback after connection lost or failed connection attempt.
Definition: torcontrol.cpp:634
std::string private_key
Definition: torcontrol.h:125
const std::string m_tor_control_center
Definition: torcontrol.h:123
static void readcb(struct bufferevent *bev, void *ctx)
Libevent handlers: internal.
Definition: torcontrol.cpp:88
fs::path GetPrivateKeyFile()
Get name of file to store private key in.
Definition: torcontrol.cpp:665
struct event * reconnect_ev
Definition: torcontrol.h:128
TorControlConnection(struct event_base *base)
Create a new TorControlConnection.
Definition: torcontrol.cpp:77
constexpr uint16_t DEFAULT_TOR_SOCKS_PORT
Functionality for communicating with Tor.
Definition: torcontrol.h:22
std::vector< uint8_t > cookie
Cookie for SAFECOOKIE auth.
Definition: torcontrol.h:133
TorControlConnection conn
Definition: torcontrol.h:124
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:529
static void reconnect_cb(evutil_socket_t fd, short what, void *arg)
Callback for reconnect timer.
Definition: torcontrol.cpp:670
void InterruptTorControl()
Definition: torcontrol.cpp:706
void add_onion_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for ADD_ONION result.
Definition: torcontrol.cpp:429
void get_socks_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for GETINFO net/listeners/socks result.
Definition: torcontrol.cpp:358
const std::string DEFAULT_TOR_CONTROL
Default control ip and port.
Definition: torcontrol.cpp:51
std::string service_id
Definition: torcontrol.h:126
void auth_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for AUTHENTICATE result.
Definition: torcontrol.cpp:464
void StartTorControl(CService onion_service_target)
Definition: torcontrol.cpp:687
void Disconnect()
Disconnect from Tor control port.
Definition: torcontrol.cpp:187
void StopTorControl()
Definition: torcontrol.cpp:716
CService service
Definition: torcontrol.h:130
struct event_base * base
Definition: torcontrol.h:122
static void eventcb(struct bufferevent *bev, short what, void *ctx)
Definition: torcontrol.cpp:133
bool Command(const std::string &cmd, const ReplyHandlerCB &reply_handler)
Send a command, register a handler for the reply.
Definition: torcontrol.cpp:194
void connected_cb(TorControlConnection &conn)
Callback after successful connection.
Definition: torcontrol.cpp:626
Controller that connects to Tor control socket, authenticate, then create and maintain an ephemeral o...
Definition: torcontrol.h:107
Low-level handling for Tor control connection.
Definition: torcontrol.h:52
CService DefaultOnionServiceTarget(uint16_t port)
Definition: torcontrol.cpp:725
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
static const bool DEFAULT_LISTEN_ONION
Definition: torcontrol.h:25
TorControlReply message
Message being received.
Definition: torcontrol.h:93
struct event_base * base
Libevent event base.
Definition: torcontrol.h:89
std::function< void(TorControlConnection &)> connected
Callback when ready for use.
Definition: torcontrol.h:85