/Users/brunogarcia/projects/bitcoin-core-dev/src/torcontrol.cpp
Line | Count | Source |
1 | | // Copyright (c) 2015-present The Bitcoin Core developers |
2 | | // Copyright (c) 2017 The Zcash developers |
3 | | // Distributed under the MIT software license, see the accompanying |
4 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | | |
6 | | #include <torcontrol.h> |
7 | | |
8 | | #include <chainparams.h> |
9 | | #include <chainparamsbase.h> |
10 | | #include <common/args.h> |
11 | | #include <compat/compat.h> |
12 | | #include <crypto/hmac_sha256.h> |
13 | | #include <logging.h> |
14 | | #include <net.h> |
15 | | #include <netaddress.h> |
16 | | #include <netbase.h> |
17 | | #include <random.h> |
18 | | #include <tinyformat.h> |
19 | | #include <util/check.h> |
20 | | #include <util/fs.h> |
21 | | #include <util/readwritefile.h> |
22 | | #include <util/strencodings.h> |
23 | | #include <util/string.h> |
24 | | #include <util/thread.h> |
25 | | #include <util/time.h> |
26 | | |
27 | | #include <algorithm> |
28 | | #include <cassert> |
29 | | #include <chrono> |
30 | | #include <cstdint> |
31 | | #include <cstdlib> |
32 | | #include <deque> |
33 | | #include <functional> |
34 | | #include <map> |
35 | | #include <optional> |
36 | | #include <set> |
37 | | #include <thread> |
38 | | #include <utility> |
39 | | #include <vector> |
40 | | |
41 | | using util::ReplaceAll; |
42 | | using util::SplitString; |
43 | | using util::ToString; |
44 | | |
45 | | /** Default control ip and port */ |
46 | | const std::string DEFAULT_TOR_CONTROL = "127.0.0.1:" + ToString(DEFAULT_TOR_CONTROL_PORT); |
47 | | /** Tor cookie size (from control-spec.txt) */ |
48 | | constexpr int TOR_COOKIE_SIZE = 32; |
49 | | /** Size of client/server nonce for SAFECOOKIE */ |
50 | | constexpr int TOR_NONCE_SIZE = 32; |
51 | | /** For computing server_hash in SAFECOOKIE */ |
52 | | static const std::string TOR_SAFE_SERVERKEY = "Tor safe cookie authentication server-to-controller hash"; |
53 | | /** For computing clientHash in SAFECOOKIE */ |
54 | | static const std::string TOR_SAFE_CLIENTKEY = "Tor safe cookie authentication controller-to-server hash"; |
55 | | /** Exponential backoff configuration - initial timeout in seconds */ |
56 | | constexpr std::chrono::duration<double> RECONNECT_TIMEOUT_START{1.0}; |
57 | | /** Exponential backoff configuration - growth factor */ |
58 | | constexpr double RECONNECT_TIMEOUT_EXP = 1.5; |
59 | | /** Maximum reconnect timeout in seconds to prevent excessive delays */ |
60 | | constexpr std::chrono::duration<double> RECONNECT_TIMEOUT_MAX{600.0}; |
61 | | /** Maximum length for lines received on TorControlConnection. |
62 | | * tor-control-spec.txt mentions that there is explicitly no limit defined to line length, |
63 | | * this is belt-and-suspenders sanity limit to prevent memory exhaustion. |
64 | | */ |
65 | | constexpr int MAX_LINE_LENGTH = 100000; |
66 | | /** Timeout for socket operations */ |
67 | | constexpr auto SOCKET_SEND_TIMEOUT = 10s; |
68 | | |
69 | | /****** Low-level TorControlConnection ********/ |
70 | | |
71 | | TorControlConnection::TorControlConnection(CThreadInterrupt& interrupt) |
72 | 0 | : m_interrupt(interrupt) |
73 | 0 | { |
74 | 0 | } |
75 | | |
76 | | TorControlConnection::~TorControlConnection() |
77 | 0 | { |
78 | 0 | Disconnect(); |
79 | 0 | } |
80 | | |
81 | | bool TorControlConnection::Connect(const std::string& tor_control_center) |
82 | 0 | { |
83 | 0 | if (m_sock) { |
84 | 0 | Disconnect(); |
85 | 0 | } |
86 | |
|
87 | 0 | std::optional<CService> control_service = Lookup(tor_control_center, DEFAULT_TOR_CONTROL_PORT, fNameLookup); |
88 | 0 | if (!control_service.has_value()) { |
89 | 0 | LogWarning("tor: Failed to look up control center %s", tor_control_center);Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
90 | 0 | return false; |
91 | 0 | } |
92 | | |
93 | 0 | m_sock = ConnectDirectly(control_service.value(), /*manual_connection=*/true); |
94 | 0 | if (!m_sock) { |
95 | 0 | LogWarning("tor: Error connecting to address %s", tor_control_center);Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
96 | 0 | return false; |
97 | 0 | } |
98 | | |
99 | 0 | m_recv_buffer.clear(); |
100 | 0 | m_message.Clear(); |
101 | 0 | m_reply_handlers.clear(); |
102 | |
|
103 | 0 | LogDebug(BCLog::TOR, "Successfully connected to Tor control port"); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
104 | 0 | return true; |
105 | 0 | } |
106 | | |
107 | | void TorControlConnection::Disconnect() |
108 | 0 | { |
109 | 0 | m_sock.reset(); |
110 | 0 | m_recv_buffer.clear(); |
111 | 0 | m_message.Clear(); |
112 | 0 | m_reply_handlers.clear(); |
113 | 0 | } |
114 | | |
115 | | bool TorControlConnection::IsConnected() const |
116 | 0 | { |
117 | 0 | if (!m_sock) return false; |
118 | 0 | std::string errmsg; |
119 | 0 | const bool connected{m_sock->IsConnected(errmsg)}; |
120 | 0 | if (!connected && !errmsg.empty()) { |
121 | 0 | LogDebug(BCLog::TOR, "Connection check failed: %s", errmsg); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
122 | 0 | } |
123 | 0 | return connected; |
124 | 0 | } |
125 | | |
126 | | bool TorControlConnection::WaitForData(std::chrono::milliseconds timeout) |
127 | 0 | { |
128 | 0 | if (!m_sock) return false; |
129 | | |
130 | 0 | Sock::Event event{0}; |
131 | 0 | if (!m_sock->Wait(timeout, Sock::RECV, &event)) { |
132 | 0 | return false; |
133 | 0 | } |
134 | 0 | if (event & Sock::ERR) { |
135 | 0 | LogDebug(BCLog::TOR, "Socket error detected"); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
136 | 0 | Disconnect(); |
137 | 0 | return false; |
138 | 0 | } |
139 | | |
140 | 0 | return (event & Sock::RECV); |
141 | 0 | } |
142 | | |
143 | | bool TorControlConnection::ReceiveAndProcess() |
144 | 0 | { |
145 | 0 | if (!m_sock) return false; |
146 | | |
147 | 0 | std::byte buf[4096]; |
148 | 0 | ssize_t nread = m_sock->Recv(buf, sizeof(buf), MSG_DONTWAIT); |
149 | |
|
150 | 0 | if (nread < 0) { |
151 | 0 | int err = WSAGetLastError(); Line | Count | Source | 59 | 0 | #define WSAGetLastError() errno |
|
152 | 0 | if (err == WSAEWOULDBLOCK || err == WSAEINTR || err == WSAEINPROGRESS) {Line | Count | Source | 61 | 0 | #define WSAEWOULDBLOCK EWOULDBLOCK |
| if (err == WSAEWOULDBLOCK || err == WSAEINTR || err == WSAEINPROGRESS) {Line | Count | Source | 64 | 0 | #define WSAEINTR EINTR |
| if (err == WSAEWOULDBLOCK || err == WSAEINTR || err == WSAEINPROGRESS) {Line | Count | Source | 65 | 0 | #define WSAEINPROGRESS EINPROGRESS |
|
153 | | // No data available currently |
154 | 0 | return true; |
155 | 0 | } |
156 | 0 | LogWarning("tor: Error reading from socket: %s", NetworkErrorString(err));Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
157 | 0 | return false; |
158 | 0 | } |
159 | | |
160 | 0 | if (nread == 0) { |
161 | 0 | LogDebug(BCLog::TOR, "End of stream"); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
162 | 0 | return false; |
163 | 0 | } |
164 | | |
165 | 0 | m_recv_buffer.insert(m_recv_buffer.end(), buf, buf + nread); |
166 | 0 | try { |
167 | 0 | return ProcessBuffer(); |
168 | 0 | } catch (const std::runtime_error& e) { |
169 | 0 | LogWarning("tor: Error processing receive buffer: %s", e.what());Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
170 | 0 | return false; |
171 | 0 | } |
172 | 0 | } |
173 | | |
174 | | bool TorControlConnection::ProcessBuffer() |
175 | 0 | { |
176 | 0 | util::LineReader reader(m_recv_buffer, MAX_LINE_LENGTH); |
177 | 0 | auto start = reader.it; |
178 | |
|
179 | 0 | while (auto line = reader.ReadLine()) { |
180 | | // Skip short lines |
181 | 0 | if (line->size() < 4) continue; |
182 | | |
183 | | // Parse: <code><separator><data> |
184 | | // <status>(-|+| )<data> |
185 | 0 | m_message.code = ToIntegral<int>(line->substr(0, 3)).value_or(0); |
186 | 0 | m_message.lines.push_back(line->substr(4)); |
187 | 0 | char separator = (*line)[3]; // '-', '+', or ' ' |
188 | |
|
189 | 0 | if (separator == ' ') { |
190 | 0 | if (m_message.code >= 600) { |
191 | | // Async notifications are currently unused |
192 | | // Synchronous and asynchronous messages are never interleaved |
193 | 0 | LogDebug(BCLog::TOR, "Received async notification %i", m_message.code); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
194 | 0 | } else if (!m_reply_handlers.empty()) { |
195 | | // Invoke reply handler with message |
196 | 0 | m_reply_handlers.front()(*this, m_message); |
197 | 0 | m_reply_handlers.pop_front(); |
198 | 0 | } else { |
199 | 0 | LogDebug(BCLog::TOR, "Received unexpected sync reply %i", m_message.code); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
200 | 0 | } |
201 | 0 | m_message.Clear(); |
202 | 0 | } |
203 | 0 | } |
204 | |
|
205 | 0 | m_recv_buffer.erase(m_recv_buffer.begin(), m_recv_buffer.begin() + std::distance(start, reader.it)); |
206 | 0 | return true; |
207 | 0 | } |
208 | | |
209 | | bool TorControlConnection::Command(const std::string &cmd, const ReplyHandlerCB& reply_handler) |
210 | 0 | { |
211 | 0 | if (!m_sock) return false; |
212 | | |
213 | 0 | std::string command = cmd + "\r\n"; |
214 | 0 | try { |
215 | 0 | m_sock->SendComplete(std::span<const char>{command}, SOCKET_SEND_TIMEOUT, m_interrupt); |
216 | 0 | } catch (const std::runtime_error& e) { |
217 | 0 | LogWarning("tor: Error sending command: %s", e.what());Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
218 | 0 | return false; |
219 | 0 | } |
220 | | |
221 | 0 | m_reply_handlers.push_back(reply_handler); |
222 | 0 | return true; |
223 | 0 | } |
224 | | |
225 | | /****** General parsing utilities ********/ |
226 | | |
227 | | /* Split reply line in the form 'AUTH METHODS=...' into a type |
228 | | * 'AUTH' and arguments 'METHODS=...'. |
229 | | * Grammar is implicitly defined in https://spec.torproject.org/control-spec by |
230 | | * the server reply formats for PROTOCOLINFO (S3.21) and AUTHCHALLENGE (S3.24). |
231 | | */ |
232 | | std::pair<std::string,std::string> SplitTorReplyLine(const std::string &s) |
233 | 0 | { |
234 | 0 | size_t ptr=0; |
235 | 0 | std::string type; |
236 | 0 | while (ptr < s.size() && s[ptr] != ' ') { |
237 | 0 | type.push_back(s[ptr]); |
238 | 0 | ++ptr; |
239 | 0 | } |
240 | 0 | if (ptr < s.size()) |
241 | 0 | ++ptr; // skip ' ' |
242 | 0 | return make_pair(type, s.substr(ptr)); |
243 | 0 | } |
244 | | |
245 | | /** Parse reply arguments in the form 'METHODS=COOKIE,SAFECOOKIE COOKIEFILE=".../control_auth_cookie"'. |
246 | | * Returns a map of keys to values, or an empty map if there was an error. |
247 | | * Grammar is implicitly defined in https://spec.torproject.org/control-spec by |
248 | | * the server reply formats for PROTOCOLINFO (S3.21), AUTHCHALLENGE (S3.24), |
249 | | * and ADD_ONION (S3.27). See also sections 2.1 and 2.3. |
250 | | */ |
251 | | std::map<std::string,std::string> ParseTorReplyMapping(const std::string &s) |
252 | 0 | { |
253 | 0 | std::map<std::string,std::string> mapping; |
254 | 0 | size_t ptr=0; |
255 | 0 | while (ptr < s.size()) { |
256 | 0 | std::string key, value; |
257 | 0 | while (ptr < s.size() && s[ptr] != '=' && s[ptr] != ' ') { |
258 | 0 | key.push_back(s[ptr]); |
259 | 0 | ++ptr; |
260 | 0 | } |
261 | 0 | if (ptr == s.size()) // unexpected end of line |
262 | 0 | return std::map<std::string,std::string>(); |
263 | 0 | if (s[ptr] == ' ') // The remaining string is an OptArguments |
264 | 0 | break; |
265 | 0 | ++ptr; // skip '=' |
266 | 0 | if (ptr < s.size() && s[ptr] == '"') { // Quoted string |
267 | 0 | ++ptr; // skip opening '"' |
268 | 0 | bool escape_next = false; |
269 | 0 | while (ptr < s.size() && (escape_next || s[ptr] != '"')) { |
270 | | // Repeated backslashes must be interpreted as pairs |
271 | 0 | escape_next = (s[ptr] == '\\' && !escape_next); |
272 | 0 | value.push_back(s[ptr]); |
273 | 0 | ++ptr; |
274 | 0 | } |
275 | 0 | if (ptr == s.size()) // unexpected end of line |
276 | 0 | return std::map<std::string,std::string>(); |
277 | 0 | ++ptr; // skip closing '"' |
278 | | /** |
279 | | * Unescape value. Per https://spec.torproject.org/control-spec section 2.1.1: |
280 | | * |
281 | | * For future-proofing, controller implementers MAY use the following |
282 | | * rules to be compatible with buggy Tor implementations and with |
283 | | * future ones that implement the spec as intended: |
284 | | * |
285 | | * Read \n \t \r and \0 ... \377 as C escapes. |
286 | | * Treat a backslash followed by any other character as that character. |
287 | | */ |
288 | 0 | std::string escaped_value; |
289 | 0 | for (size_t i = 0; i < value.size(); ++i) { |
290 | 0 | if (value[i] == '\\') { |
291 | | // This will always be valid, because if the QuotedString |
292 | | // ended in an odd number of backslashes, then the parser |
293 | | // would already have returned above, due to a missing |
294 | | // terminating double-quote. |
295 | 0 | ++i; |
296 | 0 | if (value[i] == 'n') { |
297 | 0 | escaped_value.push_back('\n'); |
298 | 0 | } else if (value[i] == 't') { |
299 | 0 | escaped_value.push_back('\t'); |
300 | 0 | } else if (value[i] == 'r') { |
301 | 0 | escaped_value.push_back('\r'); |
302 | 0 | } else if ('0' <= value[i] && value[i] <= '7') { |
303 | 0 | size_t j; |
304 | | // Octal escape sequences have a limit of three octal digits, |
305 | | // but terminate at the first character that is not a valid |
306 | | // octal digit if encountered sooner. |
307 | 0 | for (j = 1; j < 3 && (i+j) < value.size() && '0' <= value[i+j] && value[i+j] <= '7'; ++j) {} |
308 | | // Tor restricts first digit to 0-3 for three-digit octals. |
309 | | // A leading digit of 4-7 would therefore be interpreted as |
310 | | // a two-digit octal. |
311 | 0 | if (j == 3 && value[i] > '3') { |
312 | 0 | j--; |
313 | 0 | } |
314 | 0 | const auto end{i + j}; |
315 | 0 | uint8_t val{0}; |
316 | 0 | while (i < end) { |
317 | 0 | val *= 8; |
318 | 0 | val += value[i++] - '0'; |
319 | 0 | } |
320 | 0 | escaped_value.push_back(char(val)); |
321 | | // Account for automatic incrementing at loop end |
322 | 0 | --i; |
323 | 0 | } else { |
324 | 0 | escaped_value.push_back(value[i]); |
325 | 0 | } |
326 | 0 | } else { |
327 | 0 | escaped_value.push_back(value[i]); |
328 | 0 | } |
329 | 0 | } |
330 | 0 | value = escaped_value; |
331 | 0 | } else { // Unquoted value. Note that values can contain '=' at will, just no spaces |
332 | 0 | while (ptr < s.size() && s[ptr] != ' ') { |
333 | 0 | value.push_back(s[ptr]); |
334 | 0 | ++ptr; |
335 | 0 | } |
336 | 0 | } |
337 | 0 | if (ptr < s.size() && s[ptr] == ' ') |
338 | 0 | ++ptr; // skip ' ' after key=value |
339 | 0 | mapping[key] = value; |
340 | 0 | } |
341 | 0 | return mapping; |
342 | 0 | } |
343 | | |
344 | | TorController::TorController(const std::string& tor_control_center, const CService& target) |
345 | 0 | : m_tor_control_center(tor_control_center), |
346 | 0 | m_conn(m_interrupt), |
347 | 0 | m_reconnect(true), |
348 | 0 | m_reconnect_timeout(RECONNECT_TIMEOUT_START), |
349 | 0 | m_target(target) |
350 | 0 | { |
351 | | // Read service private key if cached |
352 | 0 | std::pair<bool,std::string> pkf = ReadBinaryFile(GetPrivateKeyFile()); |
353 | 0 | if (pkf.first) { |
354 | 0 | LogDebug(BCLog::TOR, "Reading cached private key from %s", fs::PathToString(GetPrivateKeyFile())); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
355 | 0 | m_private_key = pkf.second; |
356 | 0 | } |
357 | 0 | m_thread = std::thread(&util::TraceThread, "torcontrol", [this] { ThreadControl(); }); |
358 | 0 | } |
359 | | |
360 | | TorController::~TorController() |
361 | 0 | { |
362 | 0 | Interrupt(); |
363 | 0 | Join(); |
364 | 0 | if (m_service.IsValid()) { |
365 | 0 | RemoveLocal(m_service); |
366 | 0 | } |
367 | 0 | } |
368 | | |
369 | | void TorController::Interrupt() |
370 | 0 | { |
371 | 0 | m_reconnect = false; |
372 | 0 | m_interrupt(); |
373 | 0 | } |
374 | | |
375 | | void TorController::Join() |
376 | 0 | { |
377 | 0 | if (m_thread.joinable()) { |
378 | 0 | m_thread.join(); |
379 | 0 | } |
380 | 0 | } |
381 | | |
382 | | void TorController::ThreadControl() |
383 | 0 | { |
384 | 0 | LogDebug(BCLog::TOR, "Entering Tor control thread"); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
385 | |
|
386 | 0 | while (!m_interrupt) { |
387 | | // Try to connect if not connected already |
388 | 0 | if (!m_conn.IsConnected()) { |
389 | 0 | LogDebug(BCLog::TOR, "Attempting to connect to Tor control port %s", m_tor_control_center); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
390 | |
|
391 | 0 | if (!m_conn.Connect(m_tor_control_center)) { |
392 | 0 | LogWarning("tor: Initiating connection to Tor control port %s failed", m_tor_control_center);Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
393 | 0 | if (!m_reconnect) { |
394 | 0 | break; |
395 | 0 | } |
396 | | // Wait before retrying with exponential backoff |
397 | 0 | LogDebug(BCLog::TOR, "Retrying in %.1f seconds", m_reconnect_timeout.count()); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
398 | 0 | if (!m_interrupt.sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>(m_reconnect_timeout))) { |
399 | 0 | break; |
400 | 0 | } |
401 | 0 | m_reconnect_timeout = std::min(m_reconnect_timeout * RECONNECT_TIMEOUT_EXP, RECONNECT_TIMEOUT_MAX); |
402 | 0 | continue; |
403 | 0 | } |
404 | | // Successfully connected, reset timeout and trigger connected callback |
405 | 0 | m_reconnect_timeout = RECONNECT_TIMEOUT_START; |
406 | 0 | connected_cb(m_conn); |
407 | 0 | } |
408 | | // Wait for data with a timeout |
409 | 0 | if (!m_conn.WaitForData(std::chrono::seconds(1))) { |
410 | | // Check if still connected |
411 | 0 | if (!m_conn.IsConnected()) { |
412 | 0 | LogDebug(BCLog::TOR, "Lost connection to Tor control port"); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
413 | 0 | disconnected_cb(m_conn); |
414 | 0 | continue; |
415 | 0 | } |
416 | | // Just a timeout, continue waiting |
417 | 0 | continue; |
418 | 0 | } |
419 | | // Process incoming data |
420 | 0 | if (!m_conn.ReceiveAndProcess()) { |
421 | 0 | disconnected_cb(m_conn); |
422 | 0 | } |
423 | 0 | } |
424 | 0 | LogDebug(BCLog::TOR, "Exited Tor control thread"); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
425 | 0 | } |
426 | | |
427 | | void TorController::get_socks_cb(TorControlConnection& _conn, const TorControlReply& reply) |
428 | 0 | { |
429 | | // NOTE: We can only get here if -onion is unset |
430 | 0 | std::string socks_location; |
431 | 0 | if (reply.code == TOR_REPLY_OK) { |
432 | 0 | for (const auto& line : reply.lines) { |
433 | 0 | if (line.starts_with("net/listeners/socks=")) { |
434 | 0 | const std::string port_list_str = line.substr(20); |
435 | 0 | std::vector<std::string> port_list = SplitString(port_list_str, ' '); |
436 | |
|
437 | 0 | for (auto& portstr : port_list) { |
438 | 0 | if (portstr.empty()) continue; |
439 | 0 | if ((portstr[0] == '"' || portstr[0] == '\'') && portstr.size() >= 2 && (*portstr.rbegin() == portstr[0])) { |
440 | 0 | portstr = portstr.substr(1, portstr.size() - 2); |
441 | 0 | if (portstr.empty()) continue; |
442 | 0 | } |
443 | 0 | socks_location = portstr; |
444 | 0 | if (portstr.starts_with("127.0.0.1:")) { |
445 | | // Prefer localhost - ignore other ports |
446 | 0 | break; |
447 | 0 | } |
448 | 0 | } |
449 | 0 | } |
450 | 0 | } |
451 | 0 | if (!socks_location.empty()) { |
452 | 0 | LogDebug(BCLog::TOR, "Get SOCKS port command yielded %s", socks_location); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
453 | 0 | } else { |
454 | 0 | LogWarning("tor: Get SOCKS port command returned nothing");Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
455 | 0 | } |
456 | 0 | } else if (reply.code == TOR_REPLY_UNRECOGNIZED) { |
457 | 0 | LogWarning("tor: Get SOCKS port command failed with unrecognized command (You probably should upgrade Tor)");Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
458 | 0 | } else { |
459 | 0 | LogWarning("tor: Get SOCKS port command failed; error code %d", reply.code);Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
460 | 0 | } |
461 | |
|
462 | 0 | CService resolved; |
463 | 0 | Assume(!resolved.IsValid()); Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
464 | 0 | if (!socks_location.empty()) { |
465 | 0 | resolved = LookupNumeric(socks_location, DEFAULT_TOR_SOCKS_PORT); |
466 | 0 | } |
467 | 0 | if (!resolved.IsValid()) { |
468 | | // Fallback to old behaviour |
469 | 0 | resolved = LookupNumeric("127.0.0.1", DEFAULT_TOR_SOCKS_PORT); |
470 | 0 | } |
471 | |
|
472 | 0 | Assume(resolved.IsValid()); Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
473 | 0 | LogDebug(BCLog::TOR, "Configuring onion proxy for %s", resolved.ToStringAddrPort()); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
474 | | |
475 | | // Add Tor as proxy for .onion addresses. |
476 | | // Enable stream isolation to prevent connection correlation and enhance privacy, by forcing a different Tor circuit for every connection. |
477 | | // For this to work, the IsolateSOCKSAuth flag must be enabled on SOCKSPort (which is the default, see the IsolateSOCKSAuth section of Tor's manual page). |
478 | 0 | Proxy addrOnion = Proxy(resolved, /*tor_stream_isolation=*/ true); |
479 | 0 | SetProxy(NET_ONION, addrOnion); |
480 | |
|
481 | 0 | const auto onlynets = gArgs.GetArgs("-onlynet"); |
482 | |
|
483 | 0 | const bool onion_allowed_by_onlynet{ |
484 | 0 | onlynets.empty() || |
485 | 0 | std::any_of(onlynets.begin(), onlynets.end(), [](const auto& n) { |
486 | 0 | return ParseNetwork(n) == NET_ONION; |
487 | 0 | })}; |
488 | |
|
489 | 0 | if (onion_allowed_by_onlynet) { |
490 | | // If NET_ONION is reachable, then the below is a noop. |
491 | | // |
492 | | // If NET_ONION is not reachable, then none of -proxy or -onion was given. |
493 | | // Since we are here, then -torcontrol and -torpassword were given. |
494 | 0 | g_reachable_nets.Add(NET_ONION); |
495 | 0 | } |
496 | 0 | } |
497 | | |
498 | | static std::string MakeAddOnionCmd(const std::string& private_key, const std::string& target, bool enable_pow) |
499 | 0 | { |
500 | | // Note that the 'virtual' port is always the default port to avoid decloaking nodes using other ports. |
501 | 0 | return strprintf("ADD_ONION %s%s Port=%i,%s",Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
502 | 0 | private_key, |
503 | 0 | enable_pow ? " PoWDefensesEnabled=1" : "", |
504 | 0 | Params().GetDefaultPort(), |
505 | 0 | target); |
506 | 0 | } |
507 | | |
508 | | void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlReply& reply, bool pow_was_enabled) |
509 | 0 | { |
510 | 0 | if (reply.code == TOR_REPLY_OK) { |
511 | 0 | LogDebug(BCLog::TOR, "ADD_ONION successful (PoW defenses %s)", pow_was_enabled ? "enabled" : "disabled"); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
512 | 0 | for (const std::string &s : reply.lines) { |
513 | 0 | std::map<std::string,std::string> m = ParseTorReplyMapping(s); |
514 | 0 | std::map<std::string,std::string>::iterator i; |
515 | 0 | if ((i = m.find("ServiceID")) != m.end()) |
516 | 0 | m_service_id = i->second; |
517 | 0 | if ((i = m.find("PrivateKey")) != m.end()) |
518 | 0 | m_private_key = i->second; |
519 | 0 | } |
520 | 0 | if (m_service_id.empty()) { |
521 | 0 | LogWarning("tor: Error parsing ADD_ONION parameters:");Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
522 | 0 | for (const std::string &s : reply.lines) { |
523 | 0 | LogWarning(" %s", SanitizeString(s));Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
524 | 0 | } |
525 | 0 | return; |
526 | 0 | } |
527 | 0 | m_service = LookupNumeric(std::string(m_service_id+".onion"), Params().GetDefaultPort()); |
528 | 0 | LogInfo("Got tor service ID %s, advertising service %s", m_service_id, m_service.ToStringAddrPort());Line | Count | Source | 97 | 0 | #define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
529 | 0 | if (WriteBinaryFile(GetPrivateKeyFile(), m_private_key)) { |
530 | 0 | LogDebug(BCLog::TOR, "Cached service private key to %s", fs::PathToString(GetPrivateKeyFile())); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
531 | 0 | } else { |
532 | 0 | LogWarning("tor: Error writing service private key to %s", fs::PathToString(GetPrivateKeyFile()));Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
533 | 0 | } |
534 | 0 | AddLocal(m_service, LOCAL_MANUAL); |
535 | | // ... onion requested - keep connection open |
536 | 0 | } else if (reply.code == TOR_REPLY_UNRECOGNIZED) { |
537 | 0 | LogWarning("tor: Add onion failed with unrecognized command (You probably need to upgrade Tor)");Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
538 | 0 | } else if (pow_was_enabled && reply.code == TOR_REPLY_SYNTAX_ERROR) { |
539 | 0 | LogDebug(BCLog::TOR, "ADD_ONION failed with PoW defenses, retrying without"); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
540 | 0 | _conn.Command(MakeAddOnionCmd(m_private_key, m_target.ToStringAddrPort(), /*enable_pow=*/false), |
541 | 0 | [this](TorControlConnection& conn, const TorControlReply& reply) { |
542 | 0 | add_onion_cb(conn, reply, /*pow_was_enabled=*/false); |
543 | 0 | }); |
544 | 0 | } else { |
545 | 0 | LogWarning("tor: Add onion failed; error code %d", reply.code);Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
546 | 0 | } |
547 | 0 | } |
548 | | |
549 | | void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply& reply) |
550 | 0 | { |
551 | 0 | if (reply.code == TOR_REPLY_OK) { |
552 | 0 | LogDebug(BCLog::TOR, "Authentication successful"); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
553 | | |
554 | | // Now that we know Tor is running setup the proxy for onion addresses |
555 | | // if -onion isn't set to something else. |
556 | 0 | if (gArgs.GetArg("-onion", "") == "") { |
557 | 0 | _conn.Command("GETINFO net/listeners/socks", std::bind_front(&TorController::get_socks_cb, this)); |
558 | 0 | } |
559 | | |
560 | | // Finally - now create the service |
561 | 0 | if (m_private_key.empty()) { // No private key, generate one |
562 | 0 | m_private_key = "NEW:ED25519-V3"; // Explicitly request key type - see issue #9214 |
563 | 0 | } |
564 | | // Request onion service, redirect port. |
565 | 0 | _conn.Command(MakeAddOnionCmd(m_private_key, m_target.ToStringAddrPort(), /*enable_pow=*/true), |
566 | 0 | [this](TorControlConnection& conn, const TorControlReply& reply) { |
567 | 0 | add_onion_cb(conn, reply, /*pow_was_enabled=*/true); |
568 | 0 | }); |
569 | 0 | } else { |
570 | 0 | LogWarning("tor: Authentication failed");Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
571 | 0 | } |
572 | 0 | } |
573 | | |
574 | | /** Compute Tor SAFECOOKIE response. |
575 | | * |
576 | | * ServerHash is computed as: |
577 | | * HMAC-SHA256("Tor safe cookie authentication server-to-controller hash", |
578 | | * CookieString | ClientNonce | ServerNonce) |
579 | | * (with the HMAC key as its first argument) |
580 | | * |
581 | | * After a controller sends a successful AUTHCHALLENGE command, the |
582 | | * next command sent on the connection must be an AUTHENTICATE command, |
583 | | * and the only authentication string which that AUTHENTICATE command |
584 | | * will accept is: |
585 | | * |
586 | | * HMAC-SHA256("Tor safe cookie authentication controller-to-server hash", |
587 | | * CookieString | ClientNonce | ServerNonce) |
588 | | * |
589 | | */ |
590 | | static std::vector<uint8_t> ComputeResponse(std::string_view key, std::span<const uint8_t> cookie, std::span<const uint8_t> client_nonce, std::span<const uint8_t> server_nonce) |
591 | 0 | { |
592 | 0 | CHMAC_SHA256 computeHash((const uint8_t*)key.data(), key.size()); |
593 | 0 | std::vector<uint8_t> computedHash(CHMAC_SHA256::OUTPUT_SIZE, 0); |
594 | 0 | computeHash.Write(cookie.data(), cookie.size()); |
595 | 0 | computeHash.Write(client_nonce.data(), client_nonce.size()); |
596 | 0 | computeHash.Write(server_nonce.data(), server_nonce.size()); |
597 | 0 | computeHash.Finalize(computedHash.data()); |
598 | 0 | return computedHash; |
599 | 0 | } |
600 | | |
601 | | void TorController::authchallenge_cb(TorControlConnection& _conn, const TorControlReply& reply) |
602 | 0 | { |
603 | 0 | if (reply.code == TOR_REPLY_OK) { |
604 | 0 | LogDebug(BCLog::TOR, "SAFECOOKIE authentication challenge successful"); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
605 | 0 | if (reply.lines.empty()) { |
606 | 0 | LogWarning("tor: AUTHCHALLENGE reply was empty");Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
607 | 0 | return; |
608 | 0 | } |
609 | 0 | std::pair<std::string,std::string> l = SplitTorReplyLine(reply.lines[0]); |
610 | 0 | if (l.first == "AUTHCHALLENGE") { |
611 | 0 | std::map<std::string,std::string> m = ParseTorReplyMapping(l.second); |
612 | 0 | if (m.empty()) { |
613 | 0 | LogWarning("tor: Error parsing AUTHCHALLENGE parameters: %s", SanitizeString(l.second));Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
614 | 0 | return; |
615 | 0 | } |
616 | 0 | std::vector<uint8_t> server_hash = ParseHex(m["SERVERHASH"]); |
617 | 0 | std::vector<uint8_t> server_nonce = ParseHex(m["SERVERNONCE"]); |
618 | 0 | LogDebug(BCLog::TOR, "AUTHCHALLENGE ServerHash %s ServerNonce %s", HexStr(server_hash), HexStr(server_nonce)); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
619 | 0 | if (server_nonce.size() != 32) { |
620 | 0 | LogWarning("tor: ServerNonce is not 32 bytes, as required by spec");Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
621 | 0 | return; |
622 | 0 | } |
623 | | |
624 | 0 | std::vector<uint8_t> computed_server_hash = ComputeResponse(TOR_SAFE_SERVERKEY, m_cookie, m_client_nonce, server_nonce); |
625 | 0 | if (computed_server_hash != server_hash) { |
626 | 0 | LogWarning("tor: ServerHash %s does not match expected ServerHash %s", HexStr(server_hash), HexStr(computed_server_hash));Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
627 | 0 | return; |
628 | 0 | } |
629 | | |
630 | 0 | std::vector<uint8_t> computedClientHash = ComputeResponse(TOR_SAFE_CLIENTKEY, m_cookie, m_client_nonce, server_nonce); |
631 | 0 | _conn.Command("AUTHENTICATE " + HexStr(computedClientHash), std::bind_front(&TorController::auth_cb, this)); |
632 | 0 | } else { |
633 | 0 | LogWarning("tor: Invalid reply to AUTHCHALLENGE");Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
634 | 0 | } |
635 | 0 | } else { |
636 | 0 | LogWarning("tor: SAFECOOKIE authentication challenge failed");Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
637 | 0 | } |
638 | 0 | } |
639 | | |
640 | | void TorController::protocolinfo_cb(TorControlConnection& _conn, const TorControlReply& reply) |
641 | 0 | { |
642 | 0 | if (reply.code == TOR_REPLY_OK) { |
643 | 0 | std::set<std::string> methods; |
644 | 0 | std::string cookiefile; |
645 | | /* |
646 | | * 250-AUTH METHODS=COOKIE,SAFECOOKIE COOKIEFILE="/home/x/.tor/control_auth_cookie" |
647 | | * 250-AUTH METHODS=NULL |
648 | | * 250-AUTH METHODS=HASHEDPASSWORD |
649 | | */ |
650 | 0 | for (const std::string &s : reply.lines) { |
651 | 0 | std::pair<std::string,std::string> l = SplitTorReplyLine(s); |
652 | 0 | if (l.first == "AUTH") { |
653 | 0 | std::map<std::string,std::string> m = ParseTorReplyMapping(l.second); |
654 | 0 | std::map<std::string,std::string>::iterator i; |
655 | 0 | if ((i = m.find("METHODS")) != m.end()) { |
656 | 0 | std::vector<std::string> m_vec = SplitString(i->second, ','); |
657 | 0 | methods = std::set<std::string>(m_vec.begin(), m_vec.end()); |
658 | 0 | } |
659 | 0 | if ((i = m.find("COOKIEFILE")) != m.end()) |
660 | 0 | cookiefile = i->second; |
661 | 0 | } else if (l.first == "VERSION") { |
662 | 0 | std::map<std::string,std::string> m = ParseTorReplyMapping(l.second); |
663 | 0 | std::map<std::string,std::string>::iterator i; |
664 | 0 | if ((i = m.find("Tor")) != m.end()) { |
665 | 0 | LogDebug(BCLog::TOR, "Connected to Tor version %s", i->second); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
666 | 0 | } |
667 | 0 | } |
668 | 0 | } |
669 | 0 | for (const std::string &s : methods) { |
670 | 0 | LogDebug(BCLog::TOR, "Supported authentication method: %s", s); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
671 | 0 | } |
672 | | // Prefer NULL, otherwise SAFECOOKIE. If a password is provided, use HASHEDPASSWORD |
673 | | /* Authentication: |
674 | | * cookie: hex-encoded ~/.tor/control_auth_cookie |
675 | | * password: "password" |
676 | | */ |
677 | 0 | std::string torpassword = gArgs.GetArg("-torpassword", ""); |
678 | 0 | if (!torpassword.empty()) { |
679 | 0 | if (methods.contains("HASHEDPASSWORD")) { |
680 | 0 | LogDebug(BCLog::TOR, "Using HASHEDPASSWORD authentication"); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
681 | 0 | ReplaceAll(torpassword, "\"", "\\\""); |
682 | 0 | _conn.Command("AUTHENTICATE \"" + torpassword + "\"", std::bind_front(&TorController::auth_cb, this)); |
683 | 0 | } else { |
684 | 0 | LogWarning("tor: Password provided with -torpassword, but HASHEDPASSWORD authentication is not available");Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
685 | 0 | } |
686 | 0 | } else if (methods.contains("NULL")) { |
687 | 0 | LogDebug(BCLog::TOR, "Using NULL authentication"); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
688 | 0 | _conn.Command("AUTHENTICATE", std::bind_front(&TorController::auth_cb, this)); |
689 | 0 | } else if (methods.contains("SAFECOOKIE")) { |
690 | | // Cookie: hexdump -e '32/1 "%02x""\n"' ~/.tor/control_auth_cookie |
691 | 0 | LogDebug(BCLog::TOR, "Using SAFECOOKIE authentication, reading cookie authentication from %s", cookiefile); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
692 | 0 | std::pair<bool,std::string> status_cookie = ReadBinaryFile(fs::PathFromString(cookiefile), TOR_COOKIE_SIZE); |
693 | 0 | if (status_cookie.first && status_cookie.second.size() == TOR_COOKIE_SIZE) { |
694 | | // _conn.Command("AUTHENTICATE " + HexStr(status_cookie.second), std::bind_front(&TorController::auth_cb, this)); |
695 | 0 | m_cookie = std::vector<uint8_t>(status_cookie.second.begin(), status_cookie.second.end()); |
696 | 0 | m_client_nonce = std::vector<uint8_t>(TOR_NONCE_SIZE, 0); |
697 | 0 | GetRandBytes(m_client_nonce); |
698 | 0 | _conn.Command("AUTHCHALLENGE SAFECOOKIE " + HexStr(m_client_nonce), std::bind_front(&TorController::authchallenge_cb, this)); |
699 | 0 | } else { |
700 | 0 | if (status_cookie.first) { |
701 | 0 | LogWarning("tor: Authentication cookie %s is not exactly %i bytes, as is required by the spec", cookiefile, TOR_COOKIE_SIZE);Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
702 | 0 | } else { |
703 | 0 | LogWarning("tor: Authentication cookie %s could not be opened (check permissions)", cookiefile);Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
704 | 0 | } |
705 | 0 | } |
706 | 0 | } else if (methods.contains("HASHEDPASSWORD")) { |
707 | 0 | LogWarning("tor: The only supported authentication mechanism left is password, but no password provided with -torpassword");Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
708 | 0 | } else { |
709 | 0 | LogWarning("tor: No supported authentication method");Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
710 | 0 | } |
711 | 0 | } else { |
712 | 0 | LogWarning("tor: Requesting protocol info failed");Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
713 | 0 | } |
714 | 0 | } |
715 | | |
716 | | void TorController::connected_cb(TorControlConnection& _conn) |
717 | 0 | { |
718 | 0 | m_reconnect_timeout = RECONNECT_TIMEOUT_START; |
719 | | // First send a PROTOCOLINFO command to figure out what authentication is expected |
720 | 0 | if (!_conn.Command("PROTOCOLINFO 1", std::bind_front(&TorController::protocolinfo_cb, this))) |
721 | 0 | LogWarning("tor: Error sending initial protocolinfo command");Line | Count | Source | 98 | 0 | #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
722 | 0 | } |
723 | | |
724 | | void TorController::disconnected_cb(TorControlConnection& _conn) |
725 | 0 | { |
726 | | // Stop advertising service when disconnected |
727 | 0 | if (m_service.IsValid()) |
728 | 0 | RemoveLocal(m_service); |
729 | 0 | m_service = CService(); |
730 | 0 | if (!m_reconnect) |
731 | 0 | return; |
732 | | |
733 | 0 | LogDebug(BCLog::TOR, "Not connected to Tor control port %s, will retry", m_tor_control_center); Line | Count | Source | 117 | 0 | #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__) Line | Count | Source | 108 | 0 | do { \ | 109 | 0 | if (util::log::ShouldLog((category), (level))) { \ | 110 | 0 | bool rate_limit{level >= BCLog::Level::Info}; \ | 111 | 0 | Assume(!rate_limit); /*Only called with the levels below*/ \ Line | Count | Source | 128 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
| 112 | 0 | LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \ Line | Count | Source | 91 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
| 113 | 0 | } \ | 114 | 0 | } while (0) |
|
|
734 | 0 | _conn.Disconnect(); |
735 | 0 | } |
736 | | |
737 | | fs::path TorController::GetPrivateKeyFile() |
738 | 0 | { |
739 | 0 | return gArgs.GetDataDirNet() / "onion_v3_private_key"; |
740 | 0 | } |
741 | | |
742 | | CService DefaultOnionServiceTarget(uint16_t port) |
743 | 0 | { |
744 | 0 | struct in_addr onion_service_target; |
745 | | onion_service_target.s_addr = htonl(INADDR_LOOPBACK); |
746 | 0 | return {onion_service_target, port}; |
747 | 0 | } |