| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#pragma once |
| 31 |
|
| 32 |
|
| 33 |
#include <boost/thread.hpp> |
| 34 |
#include <boost/bind/bind.hpp> |
| 35 |
|
| 36 |
#include "cryptonote_config.h" |
| 37 |
#include "net/abstract_tcp_server2.h" |
| 38 |
#include "http_protocol_handler.h" |
| 39 |
#include "net/http_server_handlers_map2.h" |
| 40 |
|
| 41 |
#undef MONERO_DEFAULT_LOG_CATEGORY |
| 42 |
#define MONERO_DEFAULT_LOG_CATEGORY "net.http" |
| 43 |
|
| 44 |
namespace epee |
| 45 |
{ |
| 46 |
|
| 47 |
template<class t_child_class, class t_connection_context = epee::net_utils::connection_context_base> |
| 48 |
class http_server_impl_base: public net_utils::http::i_http_server_handler<t_connection_context>, |
| 49 |
net_utils::i_connection_limit |
| 50 |
{ |
| 51 |
|
| 52 |
public: |
| 53 |
http_server_impl_base() |
| 54 |
: m_net_server(epee::net_utils::e_connection_type_RPC) |
| 55 |
{} |
| 56 |
|
| 57 |
explicit http_server_impl_base(boost::asio::io_context& external_io_service) |
| 58 |
: m_net_server(external_io_service) |
| 59 |
{} |
| 60 |
|
| 61 |
bool init(std::function<void(size_t, uint8_t*)> rng, const std::string& bind_port = "0", const std::string& bind_ip = "0.0.0.0", |
| 62 |
const std::string& bind_ipv6_address = "::", bool use_ipv6 = false, bool require_ipv4 = true, |
| 63 |
std::vector<std::string> access_control_origins = std::vector<std::string>(), |
| 64 |
boost::optional<net_utils::http::login> user = boost::none, |
| 65 |
net_utils::ssl_options_t ssl_options = net_utils::ssl_support_t::e_ssl_support_autodetect, |
| 66 |
const std::size_t max_public_ip_connections = DEFAULT_RPC_MAX_CONNECTIONS_PER_PUBLIC_IP, |
| 67 |
const std::size_t max_private_ip_connections = DEFAULT_RPC_MAX_CONNECTIONS_PER_PRIVATE_IP, |
| 68 |
const std::size_t max_connections = DEFAULT_RPC_MAX_CONNECTIONS, |
| 69 |
const std::size_t response_soft_limit = DEFAULT_RPC_SOFT_LIMIT_SIZE) |
| 70 |
{ |
| 71 |
if (max_connections < max_public_ip_connections) |
| 72 |
throw std::invalid_argument{"Max public IP connections cannot be more than max connections"}; |
| 73 |
if (max_connections < max_private_ip_connections) |
| 74 |
throw std::invalid_argument{"Max private IP connections cannot be more than max connections"}; |
| 75 |
|
| 76 |
|
| 77 |
m_net_server.get_config_object().m_phandler = static_cast<t_child_class*>(this); |
| 78 |
m_net_server.get_config_object().rng = std::move(rng); |
| 79 |
|
| 80 |
|
| 81 |
m_net_server.get_config_object().m_folder = ""; |
| 82 |
|
| 83 |
|
| 84 |
std::sort(access_control_origins.begin(), access_control_origins.end()); |
| 85 |
m_net_server.get_config_object().m_access_control_origins = std::move(access_control_origins); |
| 86 |
|
| 87 |
m_net_server.get_config_object().m_user = std::move(user); |
| 88 |
m_net_server.get_config_object().m_max_public_ip_connections = max_public_ip_connections; |
| 89 |
m_net_server.get_config_object().m_max_private_ip_connections = max_private_ip_connections; |
| 90 |
m_net_server.get_config_object().m_max_connections = max_connections; |
| 91 |
m_net_server.set_response_soft_limit(response_soft_limit); |
| 92 |
m_net_server.set_connection_limit(this); |
| 93 |
|
| 94 |
MGINFO("Binding on " << bind_ip << " (IPv4):" << bind_port); |
| 95 |
if (use_ipv6) |
| 96 |
{ |
| 97 |
MGINFO("Binding on " << bind_ipv6_address << " (IPv6):" << bind_port); |
| 98 |
} |
| 99 |
bool res = m_net_server.init_server(bind_port, bind_ip, bind_port, bind_ipv6_address, use_ipv6, require_ipv4, std::move(ssl_options)); |
| 100 |
if(!res) |
| 101 |
{ |
| 102 |
LOG_ERROR("Failed to bind server"); |
| 103 |
return false; |
| 104 |
} |
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
bool run(size_t threads_count, bool wait = true) |
| 109 |
{ |
| 110 |
|
| 111 |
MINFO("Run net_service loop( " << threads_count << " threads)..."); |
| 112 |
if(!m_net_server.run_server(threads_count, wait)) |
| 113 |
{ |
| 114 |
LOG_ERROR("Failed to run net tcp server!"); |
| 115 |
} |
| 116 |
|
| 117 |
if(wait) |
| 118 |
MINFO("net_service loop stopped."); |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
bool deinit() |
| 123 |
{ |
| 124 |
return m_net_server.deinit_server(); |
| 125 |
} |
| 126 |
|
| 127 |
bool timed_wait_server_stop(uint64_t ms) |
| 128 |
{ |
| 129 |
return m_net_server.timed_wait_server_stop(ms); |
| 130 |
} |
| 131 |
|
| 132 |
bool send_stop_signal() |
| 133 |
{ |
| 134 |
m_net_server.send_stop_signal(); |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
int get_binded_port() |
| 139 |
{ |
| 140 |
return m_net_server.get_binded_port(); |
| 141 |
} |
| 142 |
|
| 143 |
long get_connections_count() const |
| 144 |
{ |
| 145 |
return m_net_server.get_connections_count(); |
| 146 |
} |
| 147 |
|
| 148 |
protected: |
| 149 |
|
| 150 |
virtual bool is_host_limit(const net_utils::network_address& na) override final |
| 151 |
{ |
| 152 |
auto& config = m_net_server.get_config_object(); |
| 153 |
CRITICAL_REGION_LOCAL(config.m_lock); |
| 154 |
if (config.m_max_connections <= config.m_connection_count) |
| 155 |
return true; |
| 156 |
|
| 157 |
const bool is_private = na.is_loopback() || na.is_local(); |
| 158 |
const auto elem = config.m_connections.find(na.host_str()); |
| 159 |
if (elem != config.m_connections.end()) |
| 160 |
{ |
| 161 |
if (is_private) |
| 162 |
return config.m_max_private_ip_connections <= elem->second; |
| 163 |
else |
| 164 |
return config.m_max_public_ip_connections <= elem->second; |
| 165 |
} |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
net_utils::boosted_tcp_server<net_utils::http::http_custom_handler<t_connection_context> > m_net_server; |
| 170 |
}; |
| 171 |
} |