2020-12-31 18:50:11 +01:00
|
|
|
// Copyright (c) 2015-2020 The Bitcoin Core developers
|
2017-05-18 19:58:34 +02:00
|
|
|
// Copyright (c) 2017 The Zcash developers
|
2016-01-27 12:05:25 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <torcontrol.h>
|
2023-04-17 10:27:07 +02:00
|
|
|
|
|
|
|
#include <chainparams.h>
|
|
|
|
#include <chainparamsbase.h>
|
2020-12-03 14:52:23 +01:00
|
|
|
#include <compat.h>
|
2023-04-17 10:27:07 +02:00
|
|
|
#include <crypto/hmac_sha256.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <net.h>
|
2023-04-17 10:27:07 +02:00
|
|
|
#include <netaddress.h>
|
|
|
|
#include <netbase.h>
|
2020-11-18 17:13:27 +01:00
|
|
|
#include <util/readwritefile.h>
|
2023-04-17 10:27:07 +02:00
|
|
|
#include <util/strencodings.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/system.h>
|
2023-08-26 11:50:37 +02:00
|
|
|
#include <util/thread.h>
|
2022-10-26 13:25:11 +02:00
|
|
|
#include <util/time.h>
|
2015-08-25 20:12:08 +02:00
|
|
|
|
|
|
|
#include <deque>
|
2021-01-26 11:14:06 +01:00
|
|
|
#include <functional>
|
2015-08-25 20:12:08 +02:00
|
|
|
#include <set>
|
2021-01-26 11:14:06 +01:00
|
|
|
#include <vector>
|
2015-08-25 20:12:08 +02:00
|
|
|
|
|
|
|
#include <event2/buffer.h>
|
2024-10-01 06:05:07 +02:00
|
|
|
#include <event2/bufferevent.h>
|
2015-08-25 20:12:08 +02:00
|
|
|
#include <event2/event.h>
|
2015-09-08 17:48:45 +02:00
|
|
|
#include <event2/thread.h>
|
2024-10-01 06:05:07 +02:00
|
|
|
#include <event2/util.h>
|
2015-08-25 20:12:08 +02:00
|
|
|
|
2015-09-08 17:48:45 +02:00
|
|
|
/** Default control port */
|
2015-08-25 20:12:08 +02:00
|
|
|
const std::string DEFAULT_TOR_CONTROL = "127.0.0.1:9051";
|
2015-09-08 17:48:45 +02:00
|
|
|
/** Tor cookie size (from control-spec.txt) */
|
|
|
|
static const int TOR_COOKIE_SIZE = 32;
|
|
|
|
/** Size of client/server nonce for SAFECOOKIE */
|
|
|
|
static const int TOR_NONCE_SIZE = 32;
|
|
|
|
/** For computing serverHash in SAFECOOKIE */
|
|
|
|
static const std::string TOR_SAFE_SERVERKEY = "Tor safe cookie authentication server-to-controller hash";
|
|
|
|
/** For computing clientHash in SAFECOOKIE */
|
|
|
|
static const std::string TOR_SAFE_CLIENTKEY = "Tor safe cookie authentication controller-to-server hash";
|
|
|
|
/** Exponential backoff configuration - initial timeout in seconds */
|
|
|
|
static const float RECONNECT_TIMEOUT_START = 1.0;
|
|
|
|
/** Exponential backoff configuration - growth factor */
|
|
|
|
static const float RECONNECT_TIMEOUT_EXP = 1.5;
|
|
|
|
/** Maximum length for lines received on TorControlConnection.
|
|
|
|
* tor-control-spec.txt mentions that there is explicitly no limit defined to line length,
|
|
|
|
* this is belt-and-suspenders sanity limit to prevent memory exhaustion.
|
|
|
|
*/
|
|
|
|
static const int MAX_LINE_LENGTH = 100000;
|
2015-08-25 20:12:08 +02:00
|
|
|
|
|
|
|
/****** Low-level TorControlConnection ********/
|
|
|
|
|
2016-09-27 13:25:42 +02:00
|
|
|
TorControlConnection::TorControlConnection(struct event_base *_base):
|
2017-08-16 15:54:51 +02:00
|
|
|
base(_base), b_conn(nullptr)
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TorControlConnection::~TorControlConnection()
|
|
|
|
{
|
|
|
|
if (b_conn)
|
|
|
|
bufferevent_free(b_conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorControlConnection::readcb(struct bufferevent *bev, void *ctx)
|
|
|
|
{
|
2018-02-07 22:15:16 +01:00
|
|
|
TorControlConnection *self = static_cast<TorControlConnection*>(ctx);
|
2015-08-25 20:12:08 +02:00
|
|
|
struct evbuffer *input = bufferevent_get_input(bev);
|
|
|
|
size_t n_read_out = 0;
|
|
|
|
char *line;
|
|
|
|
assert(input);
|
2019-08-06 05:08:33 +02:00
|
|
|
// If there is not a whole line to read, evbuffer_readln returns nullptr
|
|
|
|
while((line = evbuffer_readln(input, &n_read_out, EVBUFFER_EOL_CRLF)) != nullptr)
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
|
|
|
std::string s(line, n_read_out);
|
|
|
|
free(line);
|
|
|
|
if (s.size() < 4) // Short line
|
|
|
|
continue;
|
|
|
|
// <status>(-|+| )<data><CRLF>
|
2022-12-20 17:18:10 +01:00
|
|
|
self->message.code = LocaleIndependentAtoi<int>(s.substr(0,3));
|
2015-08-25 20:12:08 +02:00
|
|
|
self->message.lines.push_back(s.substr(4));
|
|
|
|
char ch = s[3]; // '-','+' or ' '
|
|
|
|
if (ch == ' ') {
|
|
|
|
// Final line, dispatch reply and clean up
|
|
|
|
if (self->message.code >= 600) {
|
|
|
|
// Dispatch async notifications to async handler
|
|
|
|
// Synchronous and asynchronous messages are never interleaved
|
|
|
|
self->async_handler(*self, self->message);
|
|
|
|
} else {
|
|
|
|
if (!self->reply_handlers.empty()) {
|
|
|
|
// Invoke reply handler with message
|
|
|
|
self->reply_handlers.front()(*self, self->message);
|
|
|
|
self->reply_handlers.pop_front();
|
|
|
|
} else {
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::TOR, "tor: Received unexpected sync reply %i\n", self->message.code);
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self->message.Clear();
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 17:48:45 +02:00
|
|
|
// Check for size of buffer - protect against memory exhaustion with very long lines
|
|
|
|
// Do this after evbuffer_readln to make sure all full lines have been
|
|
|
|
// removed from the buffer. Everything left is an incomplete line.
|
|
|
|
if (evbuffer_get_length(input) > MAX_LINE_LENGTH) {
|
|
|
|
LogPrintf("tor: Disconnecting because MAX_LINE_LENGTH exceeded\n");
|
|
|
|
self->Disconnect();
|
|
|
|
}
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TorControlConnection::eventcb(struct bufferevent *bev, short what, void *ctx)
|
|
|
|
{
|
2018-02-07 22:15:16 +01:00
|
|
|
TorControlConnection *self = static_cast<TorControlConnection*>(ctx);
|
2015-08-25 20:12:08 +02:00
|
|
|
if (what & BEV_EVENT_CONNECTED) {
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::TOR, "tor: Successfully connected!\n");
|
2015-08-25 20:12:08 +02:00
|
|
|
self->connected(*self);
|
|
|
|
} else if (what & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
|
2019-05-22 23:51:39 +02:00
|
|
|
if (what & BEV_EVENT_ERROR) {
|
|
|
|
LogPrint(BCLog::TOR, "tor: Error connecting to Tor control socket\n");
|
|
|
|
} else {
|
|
|
|
LogPrint(BCLog::TOR, "tor: End of stream\n");
|
|
|
|
}
|
2015-08-25 20:12:08 +02:00
|
|
|
self->Disconnect();
|
|
|
|
self->disconnected(*self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 10:27:07 +02:00
|
|
|
bool TorControlConnection::Connect(const std::string& tor_control_center, const ConnectionCB& _connected, const ConnectionCB& _disconnected)
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
2021-07-21 12:14:55 +02:00
|
|
|
if (b_conn) {
|
2015-08-25 20:12:08 +02:00
|
|
|
Disconnect();
|
2021-07-21 12:14:55 +02:00
|
|
|
}
|
|
|
|
|
2024-09-11 20:34:06 +02:00
|
|
|
const std::optional<CService> control_service{Lookup(tor_control_center, 9051, fNameLookup)};
|
|
|
|
if (!control_service.has_value()) {
|
2021-07-21 12:14:55 +02:00
|
|
|
LogPrintf("tor: Failed to look up control center %s\n", tor_control_center);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_storage control_address;
|
|
|
|
socklen_t control_address_len = sizeof(control_address);
|
2024-09-11 20:34:06 +02:00
|
|
|
if (!control_service.value().GetSockAddr(reinterpret_cast<struct sockaddr*>(&control_address), &control_address_len)) {
|
2023-04-17 10:27:07 +02:00
|
|
|
LogPrintf("tor: Error parsing socket address %s\n", tor_control_center);
|
2015-08-25 20:12:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new socket, set up callbacks and enable notification bits
|
|
|
|
b_conn = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
|
2021-07-21 12:14:55 +02:00
|
|
|
if (!b_conn) {
|
2015-08-25 20:12:08 +02:00
|
|
|
return false;
|
2021-07-21 12:14:55 +02:00
|
|
|
}
|
2019-08-06 05:08:33 +02:00
|
|
|
bufferevent_setcb(b_conn, TorControlConnection::readcb, nullptr, TorControlConnection::eventcb, this);
|
2015-08-25 20:12:08 +02:00
|
|
|
bufferevent_enable(b_conn, EV_READ|EV_WRITE);
|
2016-09-27 13:25:42 +02:00
|
|
|
this->connected = _connected;
|
|
|
|
this->disconnected = _disconnected;
|
2015-08-25 20:12:08 +02:00
|
|
|
|
2023-04-17 10:27:07 +02:00
|
|
|
// Finally, connect to tor_control_center
|
2021-07-21 12:14:55 +02:00
|
|
|
if (bufferevent_socket_connect(b_conn, reinterpret_cast<struct sockaddr*>(&control_address), control_address_len) < 0) {
|
2023-04-17 10:27:07 +02:00
|
|
|
LogPrintf("tor: Error connecting to address %s\n", tor_control_center);
|
2015-08-25 20:12:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:46:52 +02:00
|
|
|
void TorControlConnection::Disconnect()
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
|
|
|
if (b_conn)
|
|
|
|
bufferevent_free(b_conn);
|
2017-08-16 15:54:51 +02:00
|
|
|
b_conn = nullptr;
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TorControlConnection::Command(const std::string &cmd, const ReplyHandlerCB& reply_handler)
|
|
|
|
{
|
|
|
|
if (!b_conn)
|
|
|
|
return false;
|
|
|
|
struct evbuffer *buf = bufferevent_get_output(b_conn);
|
|
|
|
if (!buf)
|
|
|
|
return false;
|
|
|
|
evbuffer_add(buf, cmd.data(), cmd.size());
|
|
|
|
evbuffer_add(buf, "\r\n", 2);
|
|
|
|
reply_handlers.push_back(reply_handler);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****** General parsing utilities ********/
|
|
|
|
|
|
|
|
/* Split reply line in the form 'AUTH METHODS=...' into a type
|
|
|
|
* 'AUTH' and arguments 'METHODS=...'.
|
2017-05-18 19:58:34 +02:00
|
|
|
* Grammar is implicitly defined in https://spec.torproject.org/control-spec by
|
|
|
|
* the server reply formats for PROTOCOLINFO (S3.21) and AUTHCHALLENGE (S3.24).
|
2015-08-25 20:12:08 +02:00
|
|
|
*/
|
2018-05-24 15:09:33 +02:00
|
|
|
std::pair<std::string,std::string> SplitTorReplyLine(const std::string &s)
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
|
|
|
size_t ptr=0;
|
|
|
|
std::string type;
|
|
|
|
while (ptr < s.size() && s[ptr] != ' ') {
|
|
|
|
type.push_back(s[ptr]);
|
|
|
|
++ptr;
|
|
|
|
}
|
|
|
|
if (ptr < s.size())
|
|
|
|
++ptr; // skip ' '
|
|
|
|
return make_pair(type, s.substr(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Parse reply arguments in the form 'METHODS=COOKIE,SAFECOOKIE COOKIEFILE=".../control_auth_cookie"'.
|
2017-05-18 19:58:34 +02:00
|
|
|
* Returns a map of keys to values, or an empty map if there was an error.
|
|
|
|
* Grammar is implicitly defined in https://spec.torproject.org/control-spec by
|
|
|
|
* the server reply formats for PROTOCOLINFO (S3.21), AUTHCHALLENGE (S3.24),
|
|
|
|
* and ADD_ONION (S3.27). See also sections 2.1 and 2.3.
|
2015-08-25 20:12:08 +02:00
|
|
|
*/
|
2018-05-24 15:09:33 +02:00
|
|
|
std::map<std::string,std::string> ParseTorReplyMapping(const std::string &s)
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
|
|
|
std::map<std::string,std::string> mapping;
|
|
|
|
size_t ptr=0;
|
|
|
|
while (ptr < s.size()) {
|
|
|
|
std::string key, value;
|
2017-05-18 19:58:34 +02:00
|
|
|
while (ptr < s.size() && s[ptr] != '=' && s[ptr] != ' ') {
|
2015-08-25 20:12:08 +02:00
|
|
|
key.push_back(s[ptr]);
|
|
|
|
++ptr;
|
|
|
|
}
|
|
|
|
if (ptr == s.size()) // unexpected end of line
|
|
|
|
return std::map<std::string,std::string>();
|
2017-05-18 19:58:34 +02:00
|
|
|
if (s[ptr] == ' ') // The remaining string is an OptArguments
|
|
|
|
break;
|
2015-08-25 20:12:08 +02:00
|
|
|
++ptr; // skip '='
|
|
|
|
if (ptr < s.size() && s[ptr] == '"') { // Quoted string
|
2017-05-18 19:58:34 +02:00
|
|
|
++ptr; // skip opening '"'
|
2015-08-25 20:12:08 +02:00
|
|
|
bool escape_next = false;
|
2017-05-18 19:58:34 +02:00
|
|
|
while (ptr < s.size() && (escape_next || s[ptr] != '"')) {
|
|
|
|
// Repeated backslashes must be interpreted as pairs
|
|
|
|
escape_next = (s[ptr] == '\\' && !escape_next);
|
2015-08-25 20:12:08 +02:00
|
|
|
value.push_back(s[ptr]);
|
|
|
|
++ptr;
|
|
|
|
}
|
|
|
|
if (ptr == s.size()) // unexpected end of line
|
|
|
|
return std::map<std::string,std::string>();
|
|
|
|
++ptr; // skip closing '"'
|
2017-05-18 19:58:34 +02:00
|
|
|
/**
|
|
|
|
* Unescape value. Per https://spec.torproject.org/control-spec section 2.1.1:
|
|
|
|
*
|
|
|
|
* For future-proofing, controller implementors MAY use the following
|
|
|
|
* rules to be compatible with buggy Tor implementations and with
|
|
|
|
* future ones that implement the spec as intended:
|
|
|
|
*
|
|
|
|
* Read \n \t \r and \0 ... \377 as C escapes.
|
|
|
|
* Treat a backslash followed by any other character as that character.
|
2015-08-25 20:12:08 +02:00
|
|
|
*/
|
2017-05-18 19:58:34 +02:00
|
|
|
std::string escaped_value;
|
|
|
|
for (size_t i = 0; i < value.size(); ++i) {
|
|
|
|
if (value[i] == '\\') {
|
|
|
|
// This will always be valid, because if the QuotedString
|
|
|
|
// ended in an odd number of backslashes, then the parser
|
|
|
|
// would already have returned above, due to a missing
|
|
|
|
// terminating double-quote.
|
|
|
|
++i;
|
|
|
|
if (value[i] == 'n') {
|
|
|
|
escaped_value.push_back('\n');
|
|
|
|
} else if (value[i] == 't') {
|
|
|
|
escaped_value.push_back('\t');
|
|
|
|
} else if (value[i] == 'r') {
|
|
|
|
escaped_value.push_back('\r');
|
|
|
|
} else if ('0' <= value[i] && value[i] <= '7') {
|
|
|
|
size_t j;
|
|
|
|
// Octal escape sequences have a limit of three octal digits,
|
|
|
|
// but terminate at the first character that is not a valid
|
|
|
|
// octal digit if encountered sooner.
|
|
|
|
for (j = 1; j < 3 && (i+j) < value.size() && '0' <= value[i+j] && value[i+j] <= '7'; ++j) {}
|
|
|
|
// Tor restricts first digit to 0-3 for three-digit octals.
|
|
|
|
// A leading digit of 4-7 would therefore be interpreted as
|
|
|
|
// a two-digit octal.
|
|
|
|
if (j == 3 && value[i] > '3') {
|
|
|
|
j--;
|
|
|
|
}
|
2024-10-01 06:05:07 +02:00
|
|
|
const auto end{i + j};
|
|
|
|
uint8_t val{0};
|
|
|
|
while (i < end) {
|
|
|
|
val *= 8;
|
|
|
|
val += value[i++] - '0';
|
|
|
|
}
|
|
|
|
escaped_value.push_back(char(val));
|
2017-05-18 19:58:34 +02:00
|
|
|
// Account for automatic incrementing at loop end
|
2024-10-01 06:05:07 +02:00
|
|
|
--i;
|
2017-05-18 19:58:34 +02:00
|
|
|
} else {
|
|
|
|
escaped_value.push_back(value[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
escaped_value.push_back(value[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
value = escaped_value;
|
2015-08-25 20:12:08 +02:00
|
|
|
} else { // Unquoted value. Note that values can contain '=' at will, just no spaces
|
|
|
|
while (ptr < s.size() && s[ptr] != ' ') {
|
|
|
|
value.push_back(s[ptr]);
|
|
|
|
++ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ptr < s.size() && s[ptr] == ' ')
|
|
|
|
++ptr; // skip ' ' after key=value
|
|
|
|
mapping[key] = value;
|
|
|
|
}
|
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
|
2023-04-17 10:27:07 +02:00
|
|
|
TorController::TorController(struct event_base* _base, const std::string& tor_control_center, const CService& target):
|
2016-09-27 13:25:42 +02:00
|
|
|
base(_base),
|
2023-04-17 10:27:07 +02:00
|
|
|
m_tor_control_center(tor_control_center), conn(base), reconnect(true), reconnect_ev(0),
|
|
|
|
reconnect_timeout(RECONNECT_TIMEOUT_START),
|
|
|
|
m_target(target)
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
2016-03-03 13:28:07 +01:00
|
|
|
reconnect_ev = event_new(base, -1, 0, reconnect_cb, this);
|
|
|
|
if (!reconnect_ev)
|
|
|
|
LogPrintf("tor: Failed to create event for reconnection: out of memory?\n");
|
2015-08-25 20:12:08 +02:00
|
|
|
// Start connection attempts immediately
|
2023-04-17 10:27:07 +02:00
|
|
|
if (!conn.Connect(m_tor_control_center, std::bind(&TorController::connected_cb, this, std::placeholders::_1),
|
2021-08-17 22:31:09 +02:00
|
|
|
std::bind(&TorController::disconnected_cb, this, std::placeholders::_1) )) {
|
2023-04-17 10:27:07 +02:00
|
|
|
LogPrintf("tor: Initiating connection to Tor control port %s failed\n", m_tor_control_center);
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
// Read service private key if cached
|
|
|
|
std::pair<bool,std::string> pkf = ReadBinaryFile(GetPrivateKeyFile());
|
|
|
|
if (pkf.first) {
|
2024-08-06 19:39:26 +02:00
|
|
|
LogPrint(BCLog::TOR, "tor: Reading cached private key from %s\n", fs::PathToString(GetPrivateKeyFile()));
|
2015-08-25 20:12:08 +02:00
|
|
|
private_key = pkf.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TorController::~TorController()
|
|
|
|
{
|
2016-03-03 13:28:07 +01:00
|
|
|
if (reconnect_ev) {
|
|
|
|
event_free(reconnect_ev);
|
2017-08-16 15:54:51 +02:00
|
|
|
reconnect_ev = nullptr;
|
2016-03-03 13:28:07 +01:00
|
|
|
}
|
2015-09-08 17:48:45 +02:00
|
|
|
if (service.IsValid()) {
|
|
|
|
RemoveLocal(service);
|
|
|
|
}
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
|
2016-09-27 13:25:42 +02:00
|
|
|
void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlReply& reply)
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
|
|
|
if (reply.code == 250) {
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::TOR, "tor: ADD_ONION successful\n");
|
2019-07-05 09:06:28 +02:00
|
|
|
for (const std::string &s : reply.lines) {
|
2015-08-25 20:12:08 +02:00
|
|
|
std::map<std::string,std::string> m = ParseTorReplyMapping(s);
|
|
|
|
std::map<std::string,std::string>::iterator i;
|
|
|
|
if ((i = m.find("ServiceID")) != m.end())
|
|
|
|
service_id = i->second;
|
|
|
|
if ((i = m.find("PrivateKey")) != m.end())
|
|
|
|
private_key = i->second;
|
|
|
|
}
|
2017-05-18 19:58:34 +02:00
|
|
|
if (service_id.empty()) {
|
|
|
|
LogPrintf("tor: Error parsing ADD_ONION parameters:\n");
|
|
|
|
for (const std::string &s : reply.lines) {
|
|
|
|
LogPrintf(" %s\n", SanitizeString(s));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
+bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
service = LookupNumeric(std::string(service_id+".onion"), Params().GetDefaultPort());
|
2024-09-07 00:10:55 +02:00
|
|
|
LogPrintf("tor: Got service ID %s, advertising service %s\n", service_id, service.ToStringAddrPort());
|
2015-08-25 20:12:08 +02:00
|
|
|
if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) {
|
2024-08-06 19:39:26 +02:00
|
|
|
LogPrint(BCLog::TOR, "tor: Cached service private key to %s\n", fs::PathToString(GetPrivateKeyFile()));
|
2015-08-25 20:12:08 +02:00
|
|
|
} else {
|
2024-08-06 19:39:26 +02:00
|
|
|
LogPrintf("tor: Error writing service private key to %s\n", fs::PathToString(GetPrivateKeyFile()));
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
AddLocal(service, LOCAL_MANUAL);
|
|
|
|
// ... onion requested - keep connection open
|
2015-08-27 06:43:18 +02:00
|
|
|
} else if (reply.code == 510) { // 510 Unrecognized command
|
2015-09-08 17:48:45 +02:00
|
|
|
LogPrintf("tor: Add onion failed with unrecognized command (You probably need to upgrade Tor)\n");
|
2015-08-25 20:12:08 +02:00
|
|
|
} else {
|
2015-09-08 17:48:45 +02:00
|
|
|
LogPrintf("tor: Add onion failed; error code %d\n", reply.code);
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:25:42 +02:00
|
|
|
void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply& reply)
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
|
|
|
if (reply.code == 250) {
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::TOR, "tor: Authentication successful\n");
|
2015-11-24 16:27:38 +01:00
|
|
|
|
|
|
|
// Now that we know Tor is running setup the proxy for onion addresses
|
|
|
|
// if -onion isn't set to something else.
|
2019-06-24 18:44:27 +02:00
|
|
|
if (gArgs.GetArg("-onion", "") == "") {
|
2017-09-03 15:29:10 +02:00
|
|
|
CService resolved(LookupNumeric("127.0.0.1", 9050));
|
2021-08-30 14:33:29 +02:00
|
|
|
Proxy addrOnion = Proxy(resolved, true);
|
2020-04-16 07:21:49 +02:00
|
|
|
SetProxy(NET_ONION, addrOnion);
|
2021-08-30 14:33:29 +02:00
|
|
|
|
|
|
|
const auto onlynets = gArgs.GetArgs("-onlynet");
|
|
|
|
|
|
|
|
const bool onion_allowed_by_onlynet{
|
|
|
|
!gArgs.IsArgSet("-onlynet") ||
|
|
|
|
std::any_of(onlynets.begin(), onlynets.end(), [](const auto& n) {
|
|
|
|
return ParseNetwork(n) == NET_ONION;
|
|
|
|
})};
|
|
|
|
|
|
|
|
if (onion_allowed_by_onlynet) {
|
|
|
|
// If NET_ONION is reachable, then the below is a noop.
|
|
|
|
//
|
|
|
|
// If NET_ONION is not reachable, then none of -proxy or -onion was given.
|
|
|
|
// Since we are here, then -torcontrol and -torpassword were given.
|
|
|
|
SetReachable(NET_ONION, true);
|
|
|
|
}
|
2015-11-24 16:27:38 +01:00
|
|
|
}
|
|
|
|
|
2015-08-25 20:12:08 +02:00
|
|
|
// Finally - now create the service
|
2021-05-29 22:24:52 +02:00
|
|
|
if (private_key.empty()) { // No private key, generate one
|
|
|
|
private_key = "NEW:ED25519-V3"; // Explicitly request key type - see issue #9214
|
|
|
|
}
|
2020-08-01 15:10:26 +02:00
|
|
|
// Request onion service, redirect port.
|
2019-06-18 17:28:32 +02:00
|
|
|
// Note that the 'virtual' port is always the default port to avoid decloaking nodes using other ports.
|
2024-09-07 00:10:55 +02:00
|
|
|
_conn.Command(strprintf("ADD_ONION %s Port=%i,%s", private_key, Params().GetDefaultPort(), m_target.ToStringAddrPort()),
|
2021-08-17 22:31:09 +02:00
|
|
|
std::bind(&TorController::add_onion_cb, this, std::placeholders::_1, std::placeholders::_2));
|
2015-08-25 20:12:08 +02:00
|
|
|
} else {
|
2015-09-08 17:48:45 +02:00
|
|
|
LogPrintf("tor: Authentication failed\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Compute Tor SAFECOOKIE response.
|
|
|
|
*
|
|
|
|
* ServerHash is computed as:
|
|
|
|
* HMAC-SHA256("Tor safe cookie authentication server-to-controller hash",
|
|
|
|
* CookieString | ClientNonce | ServerNonce)
|
|
|
|
* (with the HMAC key as its first argument)
|
|
|
|
*
|
|
|
|
* After a controller sends a successful AUTHCHALLENGE command, the
|
|
|
|
* next command sent on the connection must be an AUTHENTICATE command,
|
|
|
|
* and the only authentication string which that AUTHENTICATE command
|
|
|
|
* will accept is:
|
|
|
|
*
|
|
|
|
* HMAC-SHA256("Tor safe cookie authentication controller-to-server hash",
|
|
|
|
* CookieString | ClientNonce | ServerNonce)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static std::vector<uint8_t> ComputeResponse(const std::string &key, const std::vector<uint8_t> &cookie, const std::vector<uint8_t> &clientNonce, const std::vector<uint8_t> &serverNonce)
|
|
|
|
{
|
|
|
|
CHMAC_SHA256 computeHash((const uint8_t*)key.data(), key.size());
|
|
|
|
std::vector<uint8_t> computedHash(CHMAC_SHA256::OUTPUT_SIZE, 0);
|
2016-12-13 12:20:26 +01:00
|
|
|
computeHash.Write(cookie.data(), cookie.size());
|
|
|
|
computeHash.Write(clientNonce.data(), clientNonce.size());
|
|
|
|
computeHash.Write(serverNonce.data(), serverNonce.size());
|
|
|
|
computeHash.Finalize(computedHash.data());
|
2015-09-08 17:48:45 +02:00
|
|
|
return computedHash;
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:25:42 +02:00
|
|
|
void TorController::authchallenge_cb(TorControlConnection& _conn, const TorControlReply& reply)
|
2015-09-08 17:48:45 +02:00
|
|
|
{
|
|
|
|
if (reply.code == 250) {
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::TOR, "tor: SAFECOOKIE authentication challenge successful\n");
|
2015-09-08 17:48:45 +02:00
|
|
|
std::pair<std::string,std::string> l = SplitTorReplyLine(reply.lines[0]);
|
|
|
|
if (l.first == "AUTHCHALLENGE") {
|
|
|
|
std::map<std::string,std::string> m = ParseTorReplyMapping(l.second);
|
2017-05-18 19:58:34 +02:00
|
|
|
if (m.empty()) {
|
|
|
|
LogPrintf("tor: Error parsing AUTHCHALLENGE parameters: %s\n", SanitizeString(l.second));
|
|
|
|
return;
|
|
|
|
}
|
2015-09-08 17:48:45 +02:00
|
|
|
std::vector<uint8_t> serverHash = ParseHex(m["SERVERHASH"]);
|
|
|
|
std::vector<uint8_t> serverNonce = ParseHex(m["SERVERNONCE"]);
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::TOR, "tor: AUTHCHALLENGE ServerHash %s ServerNonce %s\n", HexStr(serverHash), HexStr(serverNonce));
|
2015-09-08 17:48:45 +02:00
|
|
|
if (serverNonce.size() != 32) {
|
|
|
|
LogPrintf("tor: ServerNonce is not 32 bytes, as required by spec\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint8_t> computedServerHash = ComputeResponse(TOR_SAFE_SERVERKEY, cookie, clientNonce, serverNonce);
|
|
|
|
if (computedServerHash != serverHash) {
|
|
|
|
LogPrintf("tor: ServerHash %s does not match expected ServerHash %s\n", HexStr(serverHash), HexStr(computedServerHash));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint8_t> computedClientHash = ComputeResponse(TOR_SAFE_CLIENTKEY, cookie, clientNonce, serverNonce);
|
2021-08-17 22:31:09 +02:00
|
|
|
_conn.Command("AUTHENTICATE " + HexStr(computedClientHash), std::bind(&TorController::auth_cb, this, std::placeholders::_1, std::placeholders::_2));
|
2015-09-08 17:48:45 +02:00
|
|
|
} else {
|
|
|
|
LogPrintf("tor: Invalid reply to AUTHCHALLENGE\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LogPrintf("tor: SAFECOOKIE authentication challenge failed\n");
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:25:42 +02:00
|
|
|
void TorController::protocolinfo_cb(TorControlConnection& _conn, const TorControlReply& reply)
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
|
|
|
if (reply.code == 250) {
|
|
|
|
std::set<std::string> methods;
|
|
|
|
std::string cookiefile;
|
|
|
|
/*
|
|
|
|
* 250-AUTH METHODS=COOKIE,SAFECOOKIE COOKIEFILE="/home/x/.tor/control_auth_cookie"
|
|
|
|
* 250-AUTH METHODS=NULL
|
|
|
|
* 250-AUTH METHODS=HASHEDPASSWORD
|
|
|
|
*/
|
2019-07-05 09:06:28 +02:00
|
|
|
for (const std::string &s : reply.lines) {
|
2015-08-25 20:12:08 +02:00
|
|
|
std::pair<std::string,std::string> l = SplitTorReplyLine(s);
|
|
|
|
if (l.first == "AUTH") {
|
|
|
|
std::map<std::string,std::string> m = ParseTorReplyMapping(l.second);
|
|
|
|
std::map<std::string,std::string>::iterator i;
|
2022-12-24 14:35:24 +01:00
|
|
|
if ((i = m.find("METHODS")) != m.end()) {
|
|
|
|
std::vector<std::string> m_vec = SplitString(i->second, ',');
|
|
|
|
methods = std::set<std::string>(m_vec.begin(), m_vec.end());
|
|
|
|
}
|
2015-08-25 20:12:08 +02:00
|
|
|
if ((i = m.find("COOKIEFILE")) != m.end())
|
|
|
|
cookiefile = i->second;
|
|
|
|
} else if (l.first == "VERSION") {
|
|
|
|
std::map<std::string,std::string> m = ParseTorReplyMapping(l.second);
|
|
|
|
std::map<std::string,std::string>::iterator i;
|
|
|
|
if ((i = m.find("Tor")) != m.end()) {
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::TOR, "tor: Connected to Tor version %s\n", i->second);
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-05 09:06:28 +02:00
|
|
|
for (const std::string &s : methods) {
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::TOR, "tor: Supported authentication method: %s\n", s);
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
2015-09-08 17:48:45 +02:00
|
|
|
// Prefer NULL, otherwise SAFECOOKIE. If a password is provided, use HASHEDPASSWORD
|
2015-08-25 20:12:08 +02:00
|
|
|
/* Authentication:
|
|
|
|
* cookie: hex-encoded ~/.tor/control_auth_cookie
|
|
|
|
* password: "password"
|
|
|
|
*/
|
2019-06-24 18:44:27 +02:00
|
|
|
std::string torpassword = gArgs.GetArg("-torpassword", "");
|
2016-06-08 13:09:01 +02:00
|
|
|
if (!torpassword.empty()) {
|
|
|
|
if (methods.count("HASHEDPASSWORD")) {
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::TOR, "tor: Using HASHEDPASSWORD authentication\n");
|
2022-05-05 08:28:29 +02:00
|
|
|
ReplaceAll(torpassword, "\"", "\\\"");
|
2021-08-17 22:31:09 +02:00
|
|
|
_conn.Command("AUTHENTICATE \"" + torpassword + "\"", std::bind(&TorController::auth_cb, this, std::placeholders::_1, std::placeholders::_2));
|
2016-06-08 13:09:01 +02:00
|
|
|
} else {
|
|
|
|
LogPrintf("tor: Password provided with -torpassword, but HASHEDPASSWORD authentication is not available\n");
|
|
|
|
}
|
|
|
|
} else if (methods.count("NULL")) {
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::TOR, "tor: Using NULL authentication\n");
|
2021-08-17 22:31:09 +02:00
|
|
|
_conn.Command("AUTHENTICATE", std::bind(&TorController::auth_cb, this, std::placeholders::_1, std::placeholders::_2));
|
2015-09-08 17:48:45 +02:00
|
|
|
} else if (methods.count("SAFECOOKIE")) {
|
2015-08-25 20:12:08 +02:00
|
|
|
// Cookie: hexdump -e '32/1 "%02x""\n"' ~/.tor/control_auth_cookie
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::TOR, "tor: Using SAFECOOKIE authentication, reading cookie authentication from %s\n", cookiefile);
|
2024-08-06 19:39:26 +02:00
|
|
|
std::pair<bool,std::string> status_cookie = ReadBinaryFile(fs::PathFromString(cookiefile), TOR_COOKIE_SIZE);
|
2015-09-08 17:48:45 +02:00
|
|
|
if (status_cookie.first && status_cookie.second.size() == TOR_COOKIE_SIZE) {
|
2021-08-17 22:31:09 +02:00
|
|
|
// _conn.Command("AUTHENTICATE " + HexStr(status_cookie.second), std::bind(&TorController::auth_cb, this, std::placeholders::_1, std::placeholders::_2));
|
2015-09-08 17:48:45 +02:00
|
|
|
cookie = std::vector<uint8_t>(status_cookie.second.begin(), status_cookie.second.end());
|
|
|
|
clientNonce = std::vector<uint8_t>(TOR_NONCE_SIZE, 0);
|
2022-04-21 15:42:41 +02:00
|
|
|
GetRandBytes(clientNonce);
|
2021-08-17 22:31:09 +02:00
|
|
|
_conn.Command("AUTHCHALLENGE SAFECOOKIE " + HexStr(clientNonce), std::bind(&TorController::authchallenge_cb, this, std::placeholders::_1, std::placeholders::_2));
|
2015-08-25 20:12:08 +02:00
|
|
|
} else {
|
2015-09-08 17:48:45 +02:00
|
|
|
if (status_cookie.first) {
|
|
|
|
LogPrintf("tor: Authentication cookie %s is not exactly %i bytes, as is required by the spec\n", cookiefile, TOR_COOKIE_SIZE);
|
|
|
|
} else {
|
|
|
|
LogPrintf("tor: Authentication cookie %s could not be opened (check permissions)\n", cookiefile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (methods.count("HASHEDPASSWORD")) {
|
2016-06-08 13:09:01 +02:00
|
|
|
LogPrintf("tor: The only supported authentication mechanism left is password, but no password provided with -torpassword\n");
|
2015-08-25 20:12:08 +02:00
|
|
|
} else {
|
2015-09-08 17:48:45 +02:00
|
|
|
LogPrintf("tor: No supported authentication method\n");
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-09-08 17:48:45 +02:00
|
|
|
LogPrintf("tor: Requesting protocol info failed\n");
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 13:25:42 +02:00
|
|
|
void TorController::connected_cb(TorControlConnection& _conn)
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
|
|
|
reconnect_timeout = RECONNECT_TIMEOUT_START;
|
|
|
|
// First send a PROTOCOLINFO command to figure out what authentication is expected
|
2021-08-17 22:31:09 +02:00
|
|
|
if (!_conn.Command("PROTOCOLINFO 1", std::bind(&TorController::protocolinfo_cb, this, std::placeholders::_1, std::placeholders::_2)))
|
2015-09-08 17:48:45 +02:00
|
|
|
LogPrintf("tor: Error sending initial protocolinfo command\n");
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
|
2016-09-27 13:25:42 +02:00
|
|
|
void TorController::disconnected_cb(TorControlConnection& _conn)
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
2016-02-12 19:35:32 +01:00
|
|
|
// Stop advertising service when disconnected
|
2015-09-08 17:48:45 +02:00
|
|
|
if (service.IsValid())
|
|
|
|
RemoveLocal(service);
|
|
|
|
service = CService();
|
2015-08-25 20:12:08 +02:00
|
|
|
if (!reconnect)
|
|
|
|
return;
|
2015-11-17 02:10:28 +01:00
|
|
|
|
2023-04-17 10:27:07 +02:00
|
|
|
LogPrint(BCLog::TOR, "tor: Not connected to Tor control port %s, trying to reconnect\n", m_tor_control_center);
|
2015-11-17 02:10:28 +01:00
|
|
|
|
2015-08-25 20:12:08 +02:00
|
|
|
// Single-shot timer for reconnect. Use exponential backoff.
|
|
|
|
struct timeval time = MillisToTimeval(int64_t(reconnect_timeout * 1000.0));
|
2016-03-03 13:28:07 +01:00
|
|
|
if (reconnect_ev)
|
|
|
|
event_add(reconnect_ev, &time);
|
2015-08-25 20:12:08 +02:00
|
|
|
reconnect_timeout *= RECONNECT_TIMEOUT_EXP;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorController::Reconnect()
|
|
|
|
{
|
|
|
|
/* Try to reconnect and reestablish if we get booted - for example, Tor
|
|
|
|
* may be restarting.
|
|
|
|
*/
|
2023-04-17 10:27:07 +02:00
|
|
|
if (!conn.Connect(m_tor_control_center, std::bind(&TorController::connected_cb, this, std::placeholders::_1),
|
2021-08-17 22:31:09 +02:00
|
|
|
std::bind(&TorController::disconnected_cb, this, std::placeholders::_1) )) {
|
2023-04-17 10:27:07 +02:00
|
|
|
LogPrintf("tor: Re-initiating connection to Tor control port %s failed\n", m_tor_control_center);
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 20:19:21 +02:00
|
|
|
fs::path TorController::GetPrivateKeyFile()
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
2024-07-20 22:22:58 +02:00
|
|
|
return gArgs.GetDataDirNet() / "onion_v3_private_key";
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TorController::reconnect_cb(evutil_socket_t fd, short what, void *arg)
|
|
|
|
{
|
2018-02-07 22:15:16 +01:00
|
|
|
TorController *self = static_cast<TorController*>(arg);
|
2015-08-25 20:12:08 +02:00
|
|
|
self->Reconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
/****** Thread ********/
|
2017-03-18 11:00:00 +01:00
|
|
|
static struct event_base *gBase;
|
2020-06-13 20:21:30 +02:00
|
|
|
static std::thread torControlThread;
|
2015-08-25 20:12:08 +02:00
|
|
|
|
2023-04-17 10:27:07 +02:00
|
|
|
static void TorControlThread(CService onion_service_target)
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
2023-04-17 10:27:07 +02:00
|
|
|
TorController ctrl(gBase, gArgs.GetArg("-torcontrol", DEFAULT_TOR_CONTROL), onion_service_target);
|
2015-08-25 20:12:08 +02:00
|
|
|
|
2017-03-18 11:00:00 +01:00
|
|
|
event_base_dispatch(gBase);
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-17 10:27:07 +02:00
|
|
|
void StartTorControl(CService onion_service_target)
|
2015-08-25 20:12:08 +02:00
|
|
|
{
|
2017-03-18 11:00:00 +01:00
|
|
|
assert(!gBase);
|
2015-09-08 17:48:45 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
evthread_use_windows_threads();
|
|
|
|
#else
|
|
|
|
evthread_use_pthreads();
|
|
|
|
#endif
|
2017-03-18 11:00:00 +01:00
|
|
|
gBase = event_base_new();
|
|
|
|
if (!gBase) {
|
2015-09-08 17:48:45 +02:00
|
|
|
LogPrintf("tor: Unable to create event_base\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-26 11:50:37 +02:00
|
|
|
torControlThread = std::thread(&util::TraceThread, "torcontrol", [onion_service_target] {
|
2023-04-17 10:27:07 +02:00
|
|
|
TorControlThread(onion_service_target);
|
|
|
|
});
|
2015-09-08 17:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterruptTorControl()
|
|
|
|
{
|
2017-03-18 11:00:00 +01:00
|
|
|
if (gBase) {
|
2015-09-08 17:48:45 +02:00
|
|
|
LogPrintf("tor: Thread interrupt\n");
|
2019-07-18 13:24:51 +02:00
|
|
|
event_base_once(gBase, -1, EV_TIMEOUT, [](evutil_socket_t, short, void*) {
|
|
|
|
event_base_loopbreak(gBase);
|
|
|
|
}, nullptr, nullptr);
|
2015-09-08 17:48:45 +02:00
|
|
|
}
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StopTorControl()
|
|
|
|
{
|
2017-03-18 11:00:00 +01:00
|
|
|
if (gBase) {
|
2020-06-13 20:21:30 +02:00
|
|
|
torControlThread.join();
|
2017-03-18 11:00:00 +01:00
|
|
|
event_base_free(gBase);
|
2017-08-16 15:54:51 +02:00
|
|
|
gBase = nullptr;
|
2015-09-08 17:48:45 +02:00
|
|
|
}
|
2015-08-25 20:12:08 +02:00
|
|
|
}
|
2023-04-17 10:27:07 +02:00
|
|
|
|
|
|
|
CService DefaultOnionServiceTarget()
|
|
|
|
{
|
|
|
|
struct in_addr onion_service_target;
|
|
|
|
onion_service_target.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
return {onion_service_target, BaseParams().OnionServiceTargetPort()};
|
|
|
|
}
|