2020-12-31 18:50:11 +01:00
|
|
|
// Copyright (c) 2015-2020 The Bitcoin Core developers
|
2015-08-25 20:12:08 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functionality for communicating with Tor.
|
|
|
|
*/
|
|
|
|
#ifndef BITCOIN_TORCONTROL_H
|
|
|
|
#define BITCOIN_TORCONTROL_H
|
|
|
|
|
2020-10-06 21:01:37 +02:00
|
|
|
#include <fs.h>
|
|
|
|
#include <netaddress.h>
|
|
|
|
|
|
|
|
#include <boost/signals2/signal.hpp>
|
|
|
|
|
|
|
|
#include <event2/bufferevent.h>
|
|
|
|
#include <event2/event.h>
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <deque>
|
|
|
|
#include <functional>
|
2023-04-17 10:27:07 +02:00
|
|
|
#include <string>
|
2020-10-06 21:01:37 +02:00
|
|
|
#include <vector>
|
2023-04-17 10:27:07 +02:00
|
|
|
|
|
|
|
class CService;
|
2015-08-25 20:12:08 +02:00
|
|
|
|
|
|
|
extern const std::string DEFAULT_TOR_CONTROL;
|
|
|
|
static const bool DEFAULT_LISTEN_ONION = true;
|
|
|
|
|
2023-04-17 10:27:07 +02:00
|
|
|
void StartTorControl(CService onion_service_target);
|
2015-09-08 17:48:45 +02:00
|
|
|
void InterruptTorControl();
|
2015-08-25 20:12:08 +02:00
|
|
|
void StopTorControl();
|
|
|
|
|
2023-04-17 10:27:07 +02:00
|
|
|
CService DefaultOnionServiceTarget();
|
|
|
|
|
2020-10-06 21:01:37 +02:00
|
|
|
/** Reply from Tor, can be single or multi-line */
|
|
|
|
class TorControlReply
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TorControlReply() { Clear(); }
|
|
|
|
|
|
|
|
int code;
|
|
|
|
std::vector<std::string> lines;
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
code = 0;
|
|
|
|
lines.clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Low-level handling for Tor control connection.
|
|
|
|
* Speaks the SMTP-like protocol as defined in torspec/control-spec.txt
|
|
|
|
*/
|
|
|
|
class TorControlConnection
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef std::function<void(TorControlConnection&)> ConnectionCB;
|
|
|
|
typedef std::function<void(TorControlConnection &,const TorControlReply &)> ReplyHandlerCB;
|
|
|
|
|
|
|
|
/** Create a new TorControlConnection.
|
|
|
|
*/
|
|
|
|
explicit TorControlConnection(struct event_base *base);
|
|
|
|
~TorControlConnection();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to a Tor control port.
|
|
|
|
* tor_control_center is address of the form host:port.
|
|
|
|
* connected is the handler that is called when connection is successfully established.
|
|
|
|
* disconnected is a handler that is called when the connection is broken.
|
|
|
|
* Return true on success.
|
|
|
|
*/
|
|
|
|
bool Connect(const std::string& tor_control_center, const ConnectionCB& connected, const ConnectionCB& disconnected);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect from Tor control port.
|
|
|
|
*/
|
|
|
|
void Disconnect();
|
|
|
|
|
|
|
|
/** Send a command, register a handler for the reply.
|
|
|
|
* A trailing CRLF is automatically added.
|
|
|
|
* Return true on success.
|
|
|
|
*/
|
|
|
|
bool Command(const std::string &cmd, const ReplyHandlerCB& reply_handler);
|
|
|
|
|
|
|
|
/** Response handlers for async replies */
|
|
|
|
boost::signals2::signal<void(TorControlConnection &,const TorControlReply &)> async_handler;
|
|
|
|
private:
|
|
|
|
/** Callback when ready for use */
|
|
|
|
std::function<void(TorControlConnection&)> connected;
|
|
|
|
/** Callback when connection lost */
|
|
|
|
std::function<void(TorControlConnection&)> disconnected;
|
|
|
|
/** Libevent event base */
|
|
|
|
struct event_base *base;
|
|
|
|
/** Connection to control socket */
|
|
|
|
struct bufferevent *b_conn;
|
|
|
|
/** Message being received */
|
|
|
|
TorControlReply message;
|
|
|
|
/** Response handlers */
|
|
|
|
std::deque<ReplyHandlerCB> reply_handlers;
|
|
|
|
|
|
|
|
/** Libevent handlers: internal */
|
|
|
|
static void readcb(struct bufferevent *bev, void *ctx);
|
|
|
|
static void eventcb(struct bufferevent *bev, short what, void *ctx);
|
|
|
|
};
|
|
|
|
|
|
|
|
/****** Bitcoin specific TorController implementation ********/
|
|
|
|
|
|
|
|
/** Controller that connects to Tor control socket, authenticate, then create
|
|
|
|
* and maintain an ephemeral onion service.
|
|
|
|
*/
|
|
|
|
class TorController
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TorController(struct event_base* base, const std::string& tor_control_center, const CService& target);
|
|
|
|
TorController() : conn{nullptr} {
|
|
|
|
// Used for testing only.
|
|
|
|
}
|
|
|
|
~TorController();
|
|
|
|
|
|
|
|
/** Get name of file to store private key in */
|
|
|
|
fs::path GetPrivateKeyFile();
|
|
|
|
|
|
|
|
/** Reconnect, after getting disconnected */
|
|
|
|
void Reconnect();
|
|
|
|
private:
|
|
|
|
struct event_base* base;
|
|
|
|
const std::string m_tor_control_center;
|
|
|
|
TorControlConnection conn;
|
|
|
|
std::string private_key;
|
|
|
|
std::string service_id;
|
|
|
|
bool reconnect;
|
|
|
|
struct event *reconnect_ev = nullptr;
|
|
|
|
float reconnect_timeout;
|
|
|
|
CService service;
|
|
|
|
const CService m_target;
|
|
|
|
/** Cookie for SAFECOOKIE auth */
|
|
|
|
std::vector<uint8_t> cookie;
|
|
|
|
/** ClientNonce for SAFECOOKIE auth */
|
|
|
|
std::vector<uint8_t> clientNonce;
|
|
|
|
|
|
|
|
public:
|
2019-02-16 17:40:00 +01:00
|
|
|
/** Callback for GETINFO net/listeners/socks result */
|
|
|
|
void get_socks_cb(TorControlConnection& conn, const TorControlReply& reply);
|
2020-10-06 21:01:37 +02:00
|
|
|
/** Callback for ADD_ONION result */
|
|
|
|
void add_onion_cb(TorControlConnection& conn, const TorControlReply& reply);
|
|
|
|
/** Callback for AUTHENTICATE result */
|
|
|
|
void auth_cb(TorControlConnection& conn, const TorControlReply& reply);
|
|
|
|
/** Callback for AUTHCHALLENGE result */
|
|
|
|
void authchallenge_cb(TorControlConnection& conn, const TorControlReply& reply);
|
|
|
|
/** Callback for PROTOCOLINFO result */
|
|
|
|
void protocolinfo_cb(TorControlConnection& conn, const TorControlReply& reply);
|
|
|
|
/** Callback after successful connection */
|
|
|
|
void connected_cb(TorControlConnection& conn);
|
|
|
|
/** Callback after connection lost or failed connection attempt */
|
|
|
|
void disconnected_cb(TorControlConnection& conn);
|
|
|
|
|
|
|
|
/** Callback for reconnect timer */
|
|
|
|
static void reconnect_cb(evutil_socket_t fd, short what, void *arg);
|
|
|
|
};
|
|
|
|
|
2021-11-12 11:19:44 +01:00
|
|
|
#endif // BITCOIN_TORCONTROL_H
|