Branch data Line data Source code
# 1 : : // Copyright (c) 2015-2020 The Bitcoin Core developers
# 2 : : // Distributed under the MIT software license, see the accompanying
# 3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 4 : :
# 5 : : /**
# 6 : : * Functionality for communicating with Tor.
# 7 : : */
# 8 : : #ifndef BITCOIN_TORCONTROL_H
# 9 : : #define BITCOIN_TORCONTROL_H
# 10 : :
# 11 : : #include <fs.h>
# 12 : : #include <netaddress.h>
# 13 : :
# 14 : : #include <boost/signals2/signal.hpp>
# 15 : :
# 16 : : #include <event2/bufferevent.h>
# 17 : : #include <event2/event.h>
# 18 : :
# 19 : : #include <cstdlib>
# 20 : : #include <deque>
# 21 : : #include <functional>
# 22 : : #include <string>
# 23 : : #include <vector>
# 24 : :
# 25 : : class CService;
# 26 : :
# 27 : : extern const std::string DEFAULT_TOR_CONTROL;
# 28 : : static const bool DEFAULT_LISTEN_ONION = true;
# 29 : :
# 30 : : void StartTorControl(CService onion_service_target);
# 31 : : void InterruptTorControl();
# 32 : : void StopTorControl();
# 33 : :
# 34 : : CService DefaultOnionServiceTarget();
# 35 : :
# 36 : : /** Reply from Tor, can be single or multi-line */
# 37 : : class TorControlReply
# 38 : : {
# 39 : : public:
# 40 : 0 : TorControlReply() { Clear(); }
# 41 : :
# 42 : : int code;
# 43 : : std::vector<std::string> lines;
# 44 : :
# 45 : : void Clear()
# 46 : 0 : {
# 47 : 0 : code = 0;
# 48 : 0 : lines.clear();
# 49 : 0 : }
# 50 : : };
# 51 : :
# 52 : : /** Low-level handling for Tor control connection.
# 53 : : * Speaks the SMTP-like protocol as defined in torspec/control-spec.txt
# 54 : : */
# 55 : : class TorControlConnection
# 56 : : {
# 57 : : public:
# 58 : : typedef std::function<void(TorControlConnection&)> ConnectionCB;
# 59 : : typedef std::function<void(TorControlConnection &,const TorControlReply &)> ReplyHandlerCB;
# 60 : :
# 61 : : /** Create a new TorControlConnection.
# 62 : : */
# 63 : : explicit TorControlConnection(struct event_base *base);
# 64 : : ~TorControlConnection();
# 65 : :
# 66 : : /**
# 67 : : * Connect to a Tor control port.
# 68 : : * tor_control_center is address of the form host:port.
# 69 : : * connected is the handler that is called when connection is successfully established.
# 70 : : * disconnected is a handler that is called when the connection is broken.
# 71 : : * Return true on success.
# 72 : : */
# 73 : : bool Connect(const std::string& tor_control_center, const ConnectionCB& connected, const ConnectionCB& disconnected);
# 74 : :
# 75 : : /**
# 76 : : * Disconnect from Tor control port.
# 77 : : */
# 78 : : void Disconnect();
# 79 : :
# 80 : : /** Send a command, register a handler for the reply.
# 81 : : * A trailing CRLF is automatically added.
# 82 : : * Return true on success.
# 83 : : */
# 84 : : bool Command(const std::string &cmd, const ReplyHandlerCB& reply_handler);
# 85 : :
# 86 : : /** Response handlers for async replies */
# 87 : : boost::signals2::signal<void(TorControlConnection &,const TorControlReply &)> async_handler;
# 88 : : private:
# 89 : : /** Callback when ready for use */
# 90 : : std::function<void(TorControlConnection&)> connected;
# 91 : : /** Callback when connection lost */
# 92 : : std::function<void(TorControlConnection&)> disconnected;
# 93 : : /** Libevent event base */
# 94 : : struct event_base *base;
# 95 : : /** Connection to control socket */
# 96 : : struct bufferevent *b_conn;
# 97 : : /** Message being received */
# 98 : : TorControlReply message;
# 99 : : /** Response handlers */
# 100 : : std::deque<ReplyHandlerCB> reply_handlers;
# 101 : :
# 102 : : /** Libevent handlers: internal */
# 103 : : static void readcb(struct bufferevent *bev, void *ctx);
# 104 : : static void eventcb(struct bufferevent *bev, short what, void *ctx);
# 105 : : };
# 106 : :
# 107 : : /****** Bitcoin specific TorController implementation ********/
# 108 : :
# 109 : : /** Controller that connects to Tor control socket, authenticate, then create
# 110 : : * and maintain an ephemeral onion service.
# 111 : : */
# 112 : : class TorController
# 113 : : {
# 114 : : public:
# 115 : : TorController(struct event_base* base, const std::string& tor_control_center, const CService& target);
# 116 : 0 : TorController() : conn{nullptr} {
# 117 : 0 : // Used for testing only.
# 118 : 0 : }
# 119 : : ~TorController();
# 120 : :
# 121 : : /** Get name of file to store private key in */
# 122 : : fs::path GetPrivateKeyFile();
# 123 : :
# 124 : : /** Reconnect, after getting disconnected */
# 125 : : void Reconnect();
# 126 : : private:
# 127 : : struct event_base* base;
# 128 : : const std::string m_tor_control_center;
# 129 : : TorControlConnection conn;
# 130 : : std::string private_key;
# 131 : : std::string service_id;
# 132 : : bool reconnect;
# 133 : : struct event *reconnect_ev = nullptr;
# 134 : : float reconnect_timeout;
# 135 : : CService service;
# 136 : : const CService m_target;
# 137 : : /** Cookie for SAFECOOKIE auth */
# 138 : : std::vector<uint8_t> cookie;
# 139 : : /** ClientNonce for SAFECOOKIE auth */
# 140 : : std::vector<uint8_t> clientNonce;
# 141 : :
# 142 : : public:
# 143 : : /** Callback for ADD_ONION result */
# 144 : : void add_onion_cb(TorControlConnection& conn, const TorControlReply& reply);
# 145 : : /** Callback for AUTHENTICATE result */
# 146 : : void auth_cb(TorControlConnection& conn, const TorControlReply& reply);
# 147 : : /** Callback for AUTHCHALLENGE result */
# 148 : : void authchallenge_cb(TorControlConnection& conn, const TorControlReply& reply);
# 149 : : /** Callback for PROTOCOLINFO result */
# 150 : : void protocolinfo_cb(TorControlConnection& conn, const TorControlReply& reply);
# 151 : : /** Callback after successful connection */
# 152 : : void connected_cb(TorControlConnection& conn);
# 153 : : /** Callback after connection lost or failed connection attempt */
# 154 : : void disconnected_cb(TorControlConnection& conn);
# 155 : :
# 156 : : /** Callback for reconnect timer */
# 157 : : static void reconnect_cb(evutil_socket_t fd, short what, void *arg);
# 158 : : };
# 159 : :
# 160 : : #endif /* BITCOIN_TORCONTROL_H */
|