| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | |
| 31 | #include <memory> |
| 32 | #include <stdexcept> |
| 33 | #include <boost/algorithm/string/split.hpp> |
| 34 | #include <boost/asio/ip/address.hpp> |
| 35 | #include "misc_log_ex.h" |
| 36 | #include "daemon/daemon.h" |
| 37 | #include "rpc/daemon_handler.h" |
| 38 | #include "rpc/zmq_pub.h" |
| 39 | #include "rpc/zmq_server.h" |
| 40 | |
| 41 | #include "common/password.h" |
| 42 | #include "common/util.h" |
| 43 | #include "cryptonote_basic/events.h" |
| 44 | #include "daemon/core.h" |
| 45 | #include "daemon/p2p.h" |
| 46 | #include "daemon/protocol.h" |
| 47 | #include "daemon/rpc.h" |
| 48 | #include "daemon/command_server.h" |
| 49 | #include "daemon/command_line_args.h" |
| 50 | #include "net/net_ssl.h" |
| 51 | #include "version.h" |
| 52 | |
| 53 | using namespace epee; |
| 54 | |
| 55 | #include <functional> |
| 56 | |
| 57 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 58 | #define MONERO_DEFAULT_LOG_CATEGORY "daemon" |
| 59 | |
| 60 | namespace daemonize { |
| 61 | |
| 62 | namespace |
| 63 | { |
| 64 | void verify_zmq_rpc_bind(const boost::program_options::variables_map& vm) |
| 65 | { |
| 66 | std::string bind_ip = command_line::get_arg(vm, daemon_args::arg_zmq_rpc_bind_ip); |
| 67 | if (bind_ip.empty()) |
| 68 | return; |
| 69 | |
| 70 | // ZMQ bind input already accepts bracketed IPv6 literals, but |
| 71 | // boost::asio::ip::make_address does not. |
| 72 | if (bind_ip.size() >= 2 && bind_ip.front() == '[' && bind_ip.back() == ']') |
| 73 | bind_ip = bind_ip.substr(1, bind_ip.size() - 2); |
| 74 | |
| 75 | boost::system::error_code ec{}; |
| 76 | const auto parsed_ip = boost::asio::ip::make_address(bind_ip, ec); |
| 77 | if (ec) |
| 78 | throw std::runtime_error{"Invalid IP address given for --" + std::string(daemon_args::arg_zmq_rpc_bind_ip.name)}; |
| 79 | |
| 80 | if (!parsed_ip.is_loopback() && !command_line::get_arg(vm, daemon_args::arg_confirm_zmq_rpc_external_bind)) |
| 81 | { |
| 82 | throw std::runtime_error{ |
| 83 | std::string{"--"} + daemon_args::arg_zmq_rpc_bind_ip.name + |
| 84 | " permits inbound unencrypted external connections. Consider SSH tunnel or SSL proxy instead. Override with --" + |
| 85 | daemon_args::arg_confirm_zmq_rpc_external_bind.name |
| 86 | }; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | struct zmq_internals |
| 92 | { |
| 93 | explicit zmq_internals(t_core& core, t_p2p& p2p, const bool restricted) |
| 94 | : rpc_handler{core.get(), p2p.get(), restricted} |
| 95 | , server{rpc_handler} |
| 96 | {} |
| 97 | |
| 98 | cryptonote::rpc::DaemonHandler rpc_handler; |
| 99 | cryptonote::rpc::ZmqServer server; |
| 100 | }; |
| 101 | |
| 102 | struct t_internals { |
| 103 | private: |
| 104 | t_protocol protocol; |
| 105 | public: |
| 106 | t_core core; |
| 107 | t_p2p p2p; |
| 108 | std::vector<std::unique_ptr<t_rpc>> rpcs; |
| 109 | std::unique_ptr<zmq_internals> zmq; |
| 110 | |
| 111 | t_internals( |
| 112 | boost::program_options::variables_map const & vm |
| 113 | ) |
| 114 | : core{vm} |
| 115 | , protocol{vm, core, command_line::get_arg(vm, cryptonote::arg_offline)} |
| 116 | , p2p{vm, protocol} |
| 117 | , zmq{nullptr} |
| 118 | { |
| 119 | // Handle circular dependencies |
| 120 | protocol.set_p2p_endpoint(p2p.get()); |
| 121 | core.set_protocol(protocol.get()); |
| 122 | |
| 123 | const auto restricted = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_restricted_rpc); |
| 124 | const auto main_rpc_port = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_rpc_bind_port); |
| 125 | const auto restricted_rpc_port_arg = cryptonote::core_rpc_server::arg_rpc_restricted_bind_port; |
| 126 | const bool has_restricted_rpc_port_arg = !command_line::is_arg_defaulted(vm, restricted_rpc_port_arg); |
| 127 | rpcs.emplace_back(new t_rpc{vm, core, p2p, restricted, main_rpc_port, "core", !has_restricted_rpc_port_arg}); |
| 128 | |
| 129 | if(has_restricted_rpc_port_arg) |
| 130 | { |
| 131 | auto restricted_rpc_port = command_line::get_arg(vm, restricted_rpc_port_arg); |
| 132 | rpcs.emplace_back(new t_rpc{vm, core, p2p, true, restricted_rpc_port, "restricted", true}); |
| 133 | } |
| 134 | |
| 135 | if (!command_line::get_arg(vm, daemon_args::arg_zmq_rpc_disabled)) |
| 136 | { |
| 137 | verify_zmq_rpc_bind(vm); |
| 138 | |
| 139 | const bool restricted = command_line::get_arg(vm, daemon_args::arg_restricted_zmq_rpc); |
| 140 | zmq.reset(new zmq_internals{core, p2p, restricted}); |
| 141 | |
| 142 | const std::string zmq_port = command_line::get_arg(vm, daemon_args::arg_zmq_rpc_bind_port); |
| 143 | const std::string zmq_address = command_line::get_arg(vm, daemon_args::arg_zmq_rpc_bind_ip); |
| 144 | |
| 145 | if (!zmq->server.init_rpc(zmq_address, zmq_port)) |
| 146 | throw std::runtime_error{"Failed to add TCP socket(" + zmq_address + ":" + zmq_port + ") to ZMQ RPC Server"}; |
| 147 | |
| 148 | std::shared_ptr<cryptonote::listener::zmq_pub> shared; |
| 149 | const std::vector<std::string> zmq_pub = command_line::get_arg(vm, daemon_args::arg_zmq_pub); |
| 150 | if (!zmq_pub.empty() && !(shared = zmq->server.init_pub(epee::to_span(zmq_pub)))) |
| 151 | throw std::runtime_error{"Failed to initialize zmq_pub"}; |
| 152 | |
| 153 | if (shared) |
| 154 | { |
| 155 | core.get().get_blockchain_storage().set_txpool_notify(cryptonote::listener::zmq_pub::txpool_add{shared}); |
| 156 | core.get().get_blockchain_storage().add_block_notify(cryptonote::listener::zmq_pub::chain_main{shared}); |
| 157 | core.get().get_blockchain_storage().add_miner_notify(cryptonote::listener::zmq_pub::miner_data{shared}); |
| 158 | } |
| 159 | } |
| 160 | else // if --no-zmq specified |
| 161 | { |
| 162 | // Assert that none of --zmq-rpc-bind-port, --zmq-rpc-bind-ip, and --zmq-pub are specified b/c |
| 163 | // that does not make semantic sense with --no-zmq. |
| 164 | if (command_line::get_arg(vm, daemon_args::arg_zmq_rpc_bind_port) != |
| 165 | daemon_args::arg_zmq_rpc_bind_port.default_value) |
| 166 | { |
| 167 | MWARNING("WARN: --zmq-rpc-bind-port has no effect because --no-zmq was specified"); |
| 168 | } |
| 169 | if (command_line::get_arg(vm, daemon_args::arg_zmq_rpc_bind_ip) != |
| 170 | daemon_args::arg_zmq_rpc_bind_ip.default_value) |
| 171 | { |
| 172 | MWARNING("WARN: --zmq-rpc-bind-ip has no effect because --no-zmq was specified"); |
| 173 | } |
| 174 | if (command_line::get_arg(vm, daemon_args::arg_confirm_zmq_rpc_external_bind)) |
| 175 | { |
| 176 | MWARNING("WARN: --confirm-zmq-rpc-external-bind has no effect because --no-zmq was specified"); |
| 177 | } |
| 178 | if (command_line::get_arg(vm, daemon_args::arg_restricted_zmq_rpc)) |
| 179 | { |
| 180 | MWARNING("WARN: --restricted-zmq-rpc has no effect because --no-zmq was specified"); |
| 181 | } |
| 182 | if (!command_line::get_arg(vm, daemon_args::arg_zmq_pub).empty()) |
| 183 | { |
| 184 | MWARNING("WARN: --zmq-pub has no effect because --no-zmq was specified"); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | }; |
| 189 | |
| 190 | void t_daemon::init_options(boost::program_options::options_description & option_spec) |
| 191 | { |
| 192 | t_core::init_options(option_spec); |
| 193 | t_p2p::init_options(option_spec); |
| 194 | t_rpc::init_options(option_spec); |
| 195 | } |
| 196 | |
| 197 | t_daemon::t_daemon( |
| 198 | boost::program_options::variables_map const & vm, |
| 199 | uint16_t public_rpc_port |
| 200 | ) |
| 201 | : mp_internals{new t_internals{vm}}, |
| 202 | public_rpc_port(public_rpc_port) |
| 203 | { |
| 204 | } |
| 205 | |
| 206 | t_daemon::~t_daemon() = default; |
| 207 | |
| 208 | // MSVC is brain-dead and can't default this... |
| 209 | t_daemon::t_daemon(t_daemon && other) |
| 210 | { |
| 211 | if (this != &other) |
| 212 | { |
| 213 | mp_internals = std::move(other.mp_internals); |
| 214 | other.mp_internals.reset(nullptr); |
| 215 | public_rpc_port = other.public_rpc_port; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // or this |
| 220 | t_daemon & t_daemon::operator=(t_daemon && other) |
| 221 | { |
| 222 | if (this != &other) |
| 223 | { |
| 224 | mp_internals = std::move(other.mp_internals); |
| 225 | other.mp_internals.reset(nullptr); |
| 226 | public_rpc_port = other.public_rpc_port; |
| 227 | } |
| 228 | return *this; |
| 229 | } |
| 230 | |
| 231 | bool t_daemon::run(bool interactive) |
| 232 | { |
| 233 | if (nullptr == mp_internals) |
| 234 | { |
| 235 | throw std::runtime_error{"Can't run stopped daemon"}; |
| 236 | } |
| 237 | |
| 238 | std::atomic<bool> stop(false), shutdown(false); |
| 239 | boost::thread stop_thread = boost::thread([&stop, &shutdown, this] { |
| 240 | while (!stop) |
| 241 | epee::misc_utils::sleep_no_w(100); |
| 242 | if (shutdown) |
| 243 | this->stop_p2p(); |
| 244 | }); |
| 245 | epee::misc_utils::auto_scope_leave_caller scope_exit_handler = epee::misc_utils::create_scope_leave_handler([&](){ |
| 246 | stop = true; |
| 247 | stop_thread.join(); |
| 248 | }); |
| 249 | tools::signal_handler::install([&stop, &shutdown](int){ stop = shutdown = true; }); |
| 250 | |
| 251 | try |
| 252 | { |
| 253 | if (!mp_internals->core.run()) |
| 254 | return false; |
| 255 | |
| 256 | for(auto& rpc: mp_internals->rpcs) |
| 257 | rpc->run(); |
| 258 | |
| 259 | std::unique_ptr<daemonize::t_command_server> rpc_commands; |
| 260 | if (interactive && mp_internals->rpcs.size()) |
| 261 | { |
| 262 | // The first three variables are not used when the fourth is false |
| 263 | rpc_commands.reset(new daemonize::t_command_server(0, 0, boost::none, epee::net_utils::ssl_support_t::e_ssl_support_disabled, false, mp_internals->rpcs.front()->get_server())); |
| 264 | rpc_commands->start_handling(std::bind(&daemonize::t_daemon::stop_p2p, this)); |
| 265 | } |
| 266 | |
| 267 | if (mp_internals->zmq) |
| 268 | mp_internals->zmq->server.run(); |
| 269 | else |
| 270 | MINFO("ZMQ server disabled"); |
| 271 | |
| 272 | if (public_rpc_port > 0) |
| 273 | { |
| 274 | MGINFO("Public RPC port " << public_rpc_port << " will be advertised to other peers over P2P"); |
| 275 | mp_internals->p2p.get().set_rpc_port(public_rpc_port); |
| 276 | } |
| 277 | |
| 278 | mp_internals->p2p.run(); // blocks until p2p goes down |
| 279 | |
| 280 | if (rpc_commands) |
| 281 | rpc_commands->stop_handling(); |
| 282 | |
| 283 | if (mp_internals->zmq) |
| 284 | mp_internals->zmq->server.stop(); |
| 285 | |
| 286 | for(auto& rpc : mp_internals->rpcs) |
| 287 | rpc->stop(); |
| 288 | MGINFO("Node stopped."); |
| 289 | return true; |
| 290 | } |
| 291 | catch (std::exception const & ex) |
| 292 | { |
| 293 | MFATAL("Uncaught exception! " << ex.what()); |
| 294 | return false; |
| 295 | } |
| 296 | catch (...) |
| 297 | { |
| 298 | MFATAL("Uncaught exception!"); |
| 299 | return false; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void t_daemon::stop() |
| 304 | { |
| 305 | if (nullptr == mp_internals) |
| 306 | { |
| 307 | throw std::runtime_error{"Can't stop stopped daemon"}; |
| 308 | } |
| 309 | mp_internals->p2p.stop(); |
| 310 | for(auto& rpc : mp_internals->rpcs) |
| 311 | rpc->stop(); |
| 312 | |
| 313 | mp_internals.reset(nullptr); // Ensure resources are cleaned up before we return |
| 314 | } |
| 315 | |
| 316 | void t_daemon::stop_p2p() |
| 317 | { |
| 318 | if (nullptr == mp_internals) |
| 319 | { |
| 320 | throw std::runtime_error{"Can't send stop signal to a stopped daemon"}; |
| 321 | } |
| 322 | mp_internals->p2p.get().send_stop_signal(); |
| 323 | } |
| 324 | |
| 325 | } // namespace daemonize |