Bitcoin Core 31.99.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#include <util/sock.h>
14#include <util/threadinterrupt.h>
15
16#include <cstdint>
17#include <deque>
18#include <functional>
19#include <memory>
20#include <string>
21#include <thread>
22#include <vector>
23
24constexpr uint16_t DEFAULT_TOR_SOCKS_PORT{9050};
25constexpr int DEFAULT_TOR_CONTROL_PORT = 9051;
26extern const std::string DEFAULT_TOR_CONTROL;
27static const bool DEFAULT_LISTEN_ONION = true;
28
30constexpr int TOR_REPLY_OK{250};
31constexpr int TOR_REPLY_UNRECOGNIZED{510};
32constexpr int TOR_REPLY_SYNTAX_ERROR{512};
33
35
38{
39public:
41
42 int code;
43 std::vector<std::string> lines;
44
45 void Clear()
46 {
47 code = 0;
48 lines.clear();
49 }
50};
51
56{
57public:
58 typedef std::function<void(TorControlConnection &,const TorControlReply &)> ReplyHandlerCB;
59
62 explicit TorControlConnection(CThreadInterrupt& interrupt);
64
70 bool Connect(const std::string& tor_control_center);
71
75 void Disconnect();
76
81 bool Command(const std::string &cmd, const ReplyHandlerCB& reply_handler);
82
86 bool IsConnected() const;
87
93 bool WaitForData(std::chrono::milliseconds timeout);
94
100 bool ReceiveAndProcess();
101
102private:
106 std::unique_ptr<Sock> m_sock;
110 std::deque<ReplyHandlerCB> m_reply_handlers;
112 std::vector<std::byte> m_recv_buffer;
114 bool ProcessBuffer();
115};
116
117/****** Bitcoin specific TorController implementation ********/
118
123{
124public:
125 TorController(const std::string& tor_control_center, const CService& target);
127 // Used for testing only.
128 }
130
132 fs::path GetPrivateKeyFile();
133
135 void Interrupt();
136
138 void Join();
139private:
141 std::thread m_thread;
142 const std::string m_tor_control_center;
144 std::string m_private_key;
145 std::string m_service_id;
146 std::atomic<bool> m_reconnect;
147 std::chrono::duration<double> m_reconnect_timeout;
151 std::vector<uint8_t> m_cookie;
153 std::vector<uint8_t> m_client_nonce;
155 void ThreadControl();
156
157public:
159 void get_socks_cb(TorControlConnection& conn, const TorControlReply& reply);
161 void add_onion_cb(TorControlConnection& conn, const TorControlReply& reply, bool pow_was_enabled);
163 void auth_cb(TorControlConnection& conn, const TorControlReply& reply);
165 void authchallenge_cb(TorControlConnection& conn, const TorControlReply& reply);
167 void protocolinfo_cb(TorControlConnection& conn, const TorControlReply& reply);
172};
173
174#endif // BITCOIN_TORCONTROL_H
const auto cmd
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
A helper class for interruptible sleeps.
Low-level handling for Tor control connection.
Definition: torcontrol.h:56
TorControlReply m_message
Message being received.
Definition: torcontrol.h:108
std::deque< ReplyHandlerCB > m_reply_handlers
Response handlers.
Definition: torcontrol.h:110
bool Command(const std::string &cmd, const ReplyHandlerCB &reply_handler)
Send a command, register a handler for the reply.
Definition: torcontrol.cpp:209
std::vector< std::byte > m_recv_buffer
Buffer for incoming data.
Definition: torcontrol.h:112
CThreadInterrupt & m_interrupt
Reference to interrupt object for clean shutdown.
Definition: torcontrol.h:104
bool WaitForData(std::chrono::milliseconds timeout)
Wait for data to be available on the socket.
Definition: torcontrol.cpp:126
std::function< void(TorControlConnection &, const TorControlReply &)> ReplyHandlerCB
Definition: torcontrol.h:58
bool ProcessBuffer()
Process complete lines from the receive buffer.
Definition: torcontrol.cpp:174
bool ReceiveAndProcess()
Read available data from socket and process complete replies.
Definition: torcontrol.cpp:143
void Disconnect()
Disconnect from Tor control port.
Definition: torcontrol.cpp:107
TorControlConnection(CThreadInterrupt &interrupt)
Create a new TorControlConnection.
Definition: torcontrol.cpp:71
bool Connect(const std::string &tor_control_center)
Connect to a Tor control port.
Definition: torcontrol.cpp:81
std::unique_ptr< Sock > m_sock
Socket for the connection.
Definition: torcontrol.h:106
bool IsConnected() const
Check if the connection is established.
Definition: torcontrol.cpp:115
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:123
CThreadInterrupt m_interrupt
Definition: torcontrol.h:140
std::thread m_thread
Definition: torcontrol.h:141
CService m_service
Definition: torcontrol.h:148
void ThreadControl()
Main control thread.
Definition: torcontrol.cpp:382
fs::path GetPrivateKeyFile()
Get name of file to store private key in.
Definition: torcontrol.cpp:737
void connected_cb(TorControlConnection &conn)
Callback after successful connection.
Definition: torcontrol.cpp:716
void get_socks_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for GETINFO net/listeners/socks result.
Definition: torcontrol.cpp:427
void add_onion_cb(TorControlConnection &conn, const TorControlReply &reply, bool pow_was_enabled)
Callback for ADD_ONION result.
Definition: torcontrol.cpp:508
const std::string m_tor_control_center
Definition: torcontrol.h:142
std::vector< uint8_t > m_client_nonce
ClientNonce for SAFECOOKIE auth.
Definition: torcontrol.h:153
void disconnected_cb(TorControlConnection &conn)
Callback after connection lost or failed connection attempt.
Definition: torcontrol.cpp:724
const CService m_target
Definition: torcontrol.h:149
void authchallenge_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for AUTHCHALLENGE result.
Definition: torcontrol.cpp:601
TorControlConnection m_conn
Definition: torcontrol.h:143
std::string m_service_id
Definition: torcontrol.h:145
std::atomic< bool > m_reconnect
Definition: torcontrol.h:146
void Interrupt()
Interrupt the controller thread.
Definition: torcontrol.cpp:369
std::string m_private_key
Definition: torcontrol.h:144
void Join()
Wait for the controller thread to exit.
Definition: torcontrol.cpp:375
void auth_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for AUTHENTICATE result.
Definition: torcontrol.cpp:549
void protocolinfo_cb(TorControlConnection &conn, const TorControlReply &reply)
Callback for PROTOCOLINFO result.
Definition: torcontrol.cpp:640
std::chrono::duration< double > m_reconnect_timeout
Definition: torcontrol.h:147
std::vector< uint8_t > m_cookie
Cookie for SAFECOOKIE auth.
Definition: torcontrol.h:151
constexpr int TOR_REPLY_SYNTAX_ERROR
Syntax error in command argument.
Definition: torcontrol.h:32
constexpr uint16_t DEFAULT_TOR_SOCKS_PORT
Functionality for communicating with Tor.
Definition: torcontrol.h:24
const std::string DEFAULT_TOR_CONTROL
Default control ip and port.
Definition: torcontrol.cpp:46
constexpr int TOR_REPLY_OK
Tor control reply code.
Definition: torcontrol.h:30
static const bool DEFAULT_LISTEN_ONION
Definition: torcontrol.h:27
constexpr int TOR_REPLY_UNRECOGNIZED
Definition: torcontrol.h:31
constexpr int DEFAULT_TOR_CONTROL_PORT
Definition: torcontrol.h:25
CService DefaultOnionServiceTarget(uint16_t port)
Definition: torcontrol.cpp:742