| 1 | #include "bootstrap_daemon.h" |
| 2 | |
| 3 | #include <stdexcept> |
| 4 | |
| 5 | #include <boost/thread/locks.hpp> |
| 6 | |
| 7 | #include "crypto/crypto.h" |
| 8 | #include "cryptonote_core/cryptonote_core.h" |
| 9 | #include "misc_log_ex.h" |
| 10 | #include "net/parse.h" |
| 11 | |
| 12 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 13 | #define MONERO_DEFAULT_LOG_CATEGORY "daemon.rpc.bootstrap_daemon" |
| 14 | |
| 15 | namespace cryptonote |
| 16 | { |
| 17 | |
| 18 | bootstrap_daemon::bootstrap_daemon( |
| 19 | std::function<std::map<std::string, bool>()> get_public_nodes, |
| 20 | bool rpc_payment_enabled, |
| 21 | const std::string &proxy) |
| 22 | : m_selector(new bootstrap_node::selector_auto(std::move(get_public_nodes))) |
| 23 | , m_rpc_payment_enabled(rpc_payment_enabled) |
| 24 | { |
| 25 | set_proxy(proxy); |
| 26 | } |
| 27 | |
| 28 | bootstrap_daemon::bootstrap_daemon( |
| 29 | const std::string &address, |
| 30 | boost::optional<epee::net_utils::http::login> credentials, |
| 31 | bool rpc_payment_enabled, |
| 32 | const std::string &proxy) |
| 33 | : m_selector(nullptr) |
| 34 | , m_rpc_payment_enabled(rpc_payment_enabled) |
| 35 | { |
| 36 | set_proxy(proxy); |
| 37 | if (!set_server(address, std::move(credentials))) |
| 38 | { |
| 39 | throw std::runtime_error("invalid bootstrap daemon address or credentials"); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | std::string bootstrap_daemon::address() const noexcept |
| 44 | { |
| 45 | const auto& host = m_http_client.get_host(); |
| 46 | if (host.empty()) |
| 47 | { |
| 48 | return std::string(); |
| 49 | } |
| 50 | return host + ":" + m_http_client.get_port(); |
| 51 | } |
| 52 | |
| 53 | boost::optional<std::pair<uint64_t, uint64_t>> bootstrap_daemon::get_height() |
| 54 | { |
| 55 | cryptonote::COMMAND_RPC_GET_INFO::request req; |
| 56 | cryptonote::COMMAND_RPC_GET_INFO::response res; |
| 57 | |
| 58 | if (!invoke_http_json("/getinfo", req, res)) |
| 59 | { |
| 60 | return boost::none; |
| 61 | } |
| 62 | |
| 63 | if (res.status != CORE_RPC_STATUS_OK) |
| 64 | { |
| 65 | return boost::none; |
| 66 | } |
| 67 | |
| 68 | return {{res.height, res.target_height}}; |
| 69 | } |
| 70 | |
| 71 | bool bootstrap_daemon::handle_result(bool success, const std::string &status) |
| 72 | { |
| 73 | const bool failed = !success || (!m_rpc_payment_enabled && status == CORE_RPC_STATUS_PAYMENT_REQUIRED); |
| 74 | if (failed && m_selector) |
| 75 | { |
| 76 | const std::string current_address = address(); |
| 77 | m_http_client.disconnect(); |
| 78 | |
| 79 | const boost::unique_lock<boost::mutex> lock(m_selector_mutex); |
| 80 | m_selector->handle_result(current_address, !failed); |
| 81 | } |
| 82 | |
| 83 | return success; |
| 84 | } |
| 85 | |
| 86 | void bootstrap_daemon::set_proxy(const std::string &address) |
| 87 | { |
| 88 | if (!address.empty() && !net::get_tcp_endpoint(address)) |
| 89 | { |
| 90 | throw std::runtime_error("invalid proxy address format"); |
| 91 | } |
| 92 | if (!m_http_client.set_proxy(address)) |
| 93 | { |
| 94 | throw std::runtime_error("failed to set proxy address"); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | bool bootstrap_daemon::set_server(const std::string &address, const boost::optional<epee::net_utils::http::login> &credentials /* = boost::none */) |
| 99 | { |
| 100 | if (!m_http_client.set_server(address, credentials)) |
| 101 | { |
| 102 | MERROR("Failed to set bootstrap daemon address " << address); |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | MINFO("Changed bootstrap daemon address to " << address); |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | bool bootstrap_daemon::switch_server_if_needed() |
| 112 | { |
| 113 | if (m_http_client.is_connected() || !m_selector) |
| 114 | { |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | boost::optional<bootstrap_node::node_info> node; |
| 119 | { |
| 120 | const boost::unique_lock<boost::mutex> lock(m_selector_mutex); |
| 121 | node = m_selector->next_node(); |
| 122 | } |
| 123 | if (node) { |
| 124 | return set_server(node->address, node->credentials); |
| 125 | } |
| 126 | |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | } |