22#include "data/LedgerCacheInterface.hpp"
23#include "util/Taggable.hpp"
24#include "util/log/Logger.hpp"
25#include "web/AdminVerificationStrategy.hpp"
26#include "web/HttpSession.hpp"
27#include "web/ProxyIpResolver.hpp"
28#include "web/SslHttpSession.hpp"
29#include "web/dosguard/DOSGuardInterface.hpp"
30#include "web/interface/Concepts.hpp"
31#include "web/ng/impl/ServerSslContext.hpp"
33#include <boost/asio/io_context.hpp>
34#include <boost/asio/ip/address.hpp>
35#include <boost/asio/ip/tcp.hpp>
36#include <boost/asio/socket_base.hpp>
37#include <boost/asio/spawn.hpp>
38#include <boost/asio/ssl/context.hpp>
39#include <boost/asio/ssl/error.hpp>
40#include <boost/asio/strand.hpp>
41#include <boost/beast/core/error.hpp>
42#include <boost/beast/core/flat_buffer.hpp>
43#include <boost/beast/core/stream_traits.hpp>
44#include <boost/beast/core/tcp_stream.hpp>
45#include <fmt/format.h>
79 template <
typename>
class PlainSessionType,
80 template <
typename>
class SslSessionType,
83 :
public std::enable_shared_from_this<Detector<PlainSessionType, SslSessionType, HandlerType>> {
84 using std::enable_shared_from_this<
88 boost::beast::tcp_stream stream_;
89 std::optional<std::reference_wrapper<boost::asio::ssl::context>> ctx_;
90 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
91 std::reference_wrapper<dosguard::DOSGuardInterface>
const dosGuard_;
92 std::shared_ptr<HandlerType>
const handler_;
93 std::reference_wrapper<data::LedgerCacheInterface const> cache_;
94 boost::beast::flat_buffer buffer_;
95 std::shared_ptr<AdminVerificationStrategy>
const adminVerification_;
96 std::uint32_t maxWsSendingQueueSize_;
97 std::shared_ptr<ProxyIpResolver> proxyIpResolver_;
114 tcp::socket&& socket,
115 std::optional<std::reference_wrapper<boost::asio::ssl::context>> ctx,
116 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
117 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
118 std::shared_ptr<HandlerType> handler,
119 std::reference_wrapper<data::LedgerCacheInterface const> cache,
120 std::shared_ptr<AdminVerificationStrategy> adminVerification,
121 std::uint32_t maxWsSendingQueueSize,
122 std::shared_ptr<ProxyIpResolver> proxyIpResolver
124 : stream_(std::move(socket))
126 , tagFactory_(std::cref(tagFactory))
127 , dosGuard_(dosGuard)
128 , handler_(std::move(handler))
130 , adminVerification_(std::move(adminVerification))
131 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
132 , proxyIpResolver_(std::move(proxyIpResolver))
143 fail(boost::system::error_code ec,
char const* message)
145 if (ec == boost::asio::ssl::error::stream_truncated)
148 LOG(log_.info()) <<
"Detector failed (" << message <<
"): " << ec.message();
155 boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
173 return fail(ec,
"detect");
177 ip = stream_.socket().remote_endpoint().address().to_string();
178 }
catch (std::exception
const&) {
179 return fail(ec,
"cannot get remote endpoint");
184 return fail(ec,
"SSL is not supported by this server");
186 std::make_shared<SslSessionType<HandlerType>>(
187 stream_.release_socket(),
197 maxWsSendingQueueSize_
203 std::make_shared<PlainSessionType<HandlerType>>(
204 stream_.release_socket(),
213 maxWsSendingQueueSize_
230 template <
typename>
class PlainSessionType,
231 template <
typename>
class SslSessionType,
232 SomeServerHandler HandlerType>
235 public std::enable_shared_from_this<Server<PlainSessionType, SslSessionType, HandlerType>> {
236 using std::enable_shared_from_this<
240 std::reference_wrapper<boost::asio::io_context> ioc_;
241 std::optional<boost::asio::ssl::context> ctx_;
243 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard_;
244 std::shared_ptr<HandlerType> handler_;
245 std::reference_wrapper<data::LedgerCacheInterface const> cache_;
246 tcp::acceptor acceptor_;
247 std::shared_ptr<AdminVerificationStrategy> adminVerification_;
248 std::uint32_t maxWsSendingQueueSize_;
249 std::shared_ptr<ProxyIpResolver> proxyIpResolver_;
250 std::atomic_bool isStopped_{
false};
268 boost::asio::io_context& ioc,
269 std::optional<boost::asio::ssl::context> ctx,
270 tcp::endpoint endpoint,
273 std::shared_ptr<HandlerType> handler,
274 std::reference_wrapper<data::LedgerCacheInterface const> cache,
275 std::shared_ptr<AdminVerificationStrategy> adminVerification,
276 std::uint32_t maxWsSendingQueueSize,
279 : ioc_(std::ref(ioc))
280 , ctx_(std::move(ctx))
281 , tagFactory_(tagFactory)
282 , dosGuard_(std::ref(dosGuard))
283 , handler_(std::move(handler))
285 , acceptor_(boost::asio::make_strand(ioc))
286 , adminVerification_(std::move(adminVerification))
287 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
288 , proxyIpResolver_(std::make_shared<
ProxyIpResolver>(std::move(proxyIpResolver)))
290 boost::beast::error_code ec;
292 acceptor_.open(endpoint.protocol(), ec);
296 acceptor_.set_option(boost::asio::socket_base::reuse_address(
true), ec);
300 acceptor_.bind(endpoint, ec);
302 LOG(log_.error()) <<
"Failed to bind to endpoint: " << endpoint
303 <<
". message: " << ec.message();
304 throw std::runtime_error(
306 "Failed to bind to endpoint: {}:{}",
307 endpoint.address().to_string(),
313 acceptor_.listen(boost::asio::socket_base::max_listen_connections, ec);
315 LOG(log_.error()) <<
"Failed to listen at endpoint: " << endpoint
316 <<
". message: " << ec.message();
317 throw std::runtime_error(
319 "Failed to listen at endpoint: {}:{}",
320 endpoint.address().to_string(),
336 stop(boost::asio::yield_context)
345 acceptor_.async_accept(
346 boost::asio::make_strand(ioc_.get()),
347 boost::beast::bind_front_handler(&Server::onAccept, shared_from_this())
352 onAccept(boost::beast::error_code ec, tcp::socket socket)
360 ? std::optional<std::reference_wrapper<boost::asio::ssl::context>>{ctx_.value()}
363 std::make_shared<Detector<PlainSessionType, SslSessionType, HandlerType>>(
366 std::cref(tagFactory_),
371 maxWsSendingQueueSize_,
382template <
typename HandlerType>
396template <
typename HandlerType>
397static std::shared_ptr<HttpServer<HandlerType>>
400 boost::asio::io_context& ioc,
402 std::shared_ptr<HandlerType>
const& handler,
403 std::reference_wrapper<data::LedgerCacheInterface const> cache
408 auto expectedSslContext = ng::impl::makeServerSslContext(config);
409 if (not expectedSslContext) {
410 LOG(log.
error()) <<
"Failed to create SSL context: " << expectedSslContext.error();
414 auto const serverConfig = config.
getObject(
"server");
415 auto const address = boost::asio::ip::make_address(serverConfig.get<std::string>(
"ip"));
416 auto const port = serverConfig.get<
unsigned short>(
"port");
419 if (not expectedAdminVerification.has_value()) {
420 LOG(log.
error()) << expectedAdminVerification.error();
421 throw std::logic_error{expectedAdminVerification.error()};
426 auto const maxWsSendingQueueSize = serverConfig.get<uint32_t>(
"ws_max_sending_queue_size");
430 auto server = std::make_shared<HttpServer<HandlerType>>(
432 std::move(expectedSslContext).value(),
433 boost::asio::ip::tcp::endpoint{address, port},
438 std::move(expectedAdminVerification).value(),
439 maxWsSendingQueueSize,
440 std::move(proxyIpResolver)
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:96
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:517
A factory for TagDecorator instantiation.
Definition Taggable.hpp:184
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:50
ObjectView getObject(std::string_view prefix, std::optional< std::size_t > idx=std::nullopt) const
Returns the ObjectView specified with the prefix.
Definition ConfigDefinition.cpp:63
void onDetect(boost::beast::error_code ec, bool result)
Handles detection result.
Definition Server.hpp:170
void run()
Initiate the detection.
Definition Server.hpp:153
Detector(tcp::socket &&socket, std::optional< std::reference_wrapper< boost::asio::ssl::context > > ctx, std::reference_wrapper< util::TagDecoratorFactory const > tagFactory, std::reference_wrapper< dosguard::DOSGuardInterface > dosGuard, std::shared_ptr< HandlerType > handler, std::reference_wrapper< data::LedgerCacheInterface const > cache, std::shared_ptr< AdminVerificationStrategy > adminVerification, std::uint32_t maxWsSendingQueueSize, std::shared_ptr< ProxyIpResolver > proxyIpResolver)
Create a new detector.
Definition Server.hpp:113
void fail(boost::system::error_code ec, char const *message)
A helper function that is called when any error occurs.
Definition Server.hpp:143
Resolves the client's IP address, considering proxy servers.
Definition ProxyIpResolver.hpp:44
static ProxyIpResolver fromConfig(util::config::ClioConfigDefinition const &config)
Creates a ProxyIpResolver from a configuration.
Definition ProxyIpResolver.cpp:50
The WebServer class. It creates server socket and start listening on it.
Definition Server.hpp:235
void run()
Start accepting incoming connections.
Definition Server.hpp:329
void stop(boost::asio::yield_context)
Stop accepting new connections.
Definition Server.hpp:336
Server(boost::asio::io_context &ioc, std::optional< boost::asio::ssl::context > ctx, tcp::endpoint endpoint, util::TagDecoratorFactory tagFactory, dosguard::DOSGuardInterface &dosGuard, std::shared_ptr< HandlerType > handler, std::reference_wrapper< data::LedgerCacheInterface const > cache, std::shared_ptr< AdminVerificationStrategy > adminVerification, std::uint32_t maxWsSendingQueueSize, ProxyIpResolver proxyIpResolver)
Create a new instance of the web server.
Definition Server.hpp:267
The interface of a denial of service guard.
Definition DOSGuardInterface.hpp:46
Specifies the requirements a Webserver handler must fulfill.
Definition Concepts.hpp:37
This namespace implements the web server and related components.
Definition Types.hpp:43
static std::shared_ptr< HttpServer< HandlerType > > makeHttpServer(util::config::ClioConfigDefinition const &config, boost::asio::io_context &ioc, dosguard::DOSGuardInterface &dosGuard, std::shared_ptr< HandlerType > const &handler, std::reference_wrapper< data::LedgerCacheInterface const > cache)
A factory function that spawns a ready to use HTTP server.
Definition Server.hpp:398
Server< HttpSession, SslHttpSession, HandlerType > HttpServer
The final type of the HttpServer used by Clio.
Definition Server.hpp:383
std::shared_ptr< AdminVerificationStrategy > makeAdminVerificationStrategy(std::optional< std::string > password)
Factory function for creating an admin verification strategy.
Definition AdminVerificationStrategy.cpp:67
A tag class for server to help identify Server in templated code.
Definition Concepts.hpp:50