mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-24 05:35:53 +00:00
216 lines
6.6 KiB
C++
216 lines
6.6 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
This file is part of clio: https://github.com/XRPLF/clio
|
|
Copyright (c) 2022, the clio developers.
|
|
|
|
Permission to use, copy, modify, and distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#pragma once
|
|
|
|
#include <boost/asio/dispatch.hpp>
|
|
#include <boost/beast/core.hpp>
|
|
#include <boost/beast/ssl.hpp>
|
|
#include <boost/beast/websocket.hpp>
|
|
#include <boost/beast/websocket/ssl.hpp>
|
|
|
|
#include <etl/ETLService.h>
|
|
|
|
#include <webserver/WsBase.h>
|
|
|
|
namespace http = boost::beast::http;
|
|
namespace net = boost::asio;
|
|
namespace ssl = boost::asio::ssl;
|
|
namespace websocket = boost::beast::websocket;
|
|
using tcp = boost::asio::ip::tcp;
|
|
|
|
class ETLService;
|
|
|
|
class SslWsSession : public WsSession<SslWsSession>
|
|
{
|
|
boost::beast::websocket::stream<boost::beast::ssl_stream<boost::beast::tcp_stream>> ws_;
|
|
|
|
public:
|
|
// Take ownership of the socket
|
|
explicit SslWsSession(
|
|
boost::asio::io_context& ioc,
|
|
boost::beast::ssl_stream<boost::beast::tcp_stream>&& stream,
|
|
std::optional<std::string> ip,
|
|
std::shared_ptr<BackendInterface const> backend,
|
|
std::shared_ptr<RPC::RPCEngine> rpcEngine,
|
|
std::shared_ptr<SubscriptionManager> subscriptions,
|
|
std::shared_ptr<LoadBalancer> balancer,
|
|
std::shared_ptr<ETLService const> etl,
|
|
util::TagDecoratorFactory const& tagFactory,
|
|
clio::DOSGuard& dosGuard,
|
|
boost::beast::flat_buffer&& b)
|
|
: WsSession(ioc, ip, backend, rpcEngine, subscriptions, balancer, etl, tagFactory, dosGuard, std::move(b))
|
|
, ws_(std::move(stream))
|
|
{
|
|
}
|
|
|
|
boost::beast::websocket::stream<boost::beast::ssl_stream<boost::beast::tcp_stream>>&
|
|
ws()
|
|
{
|
|
return ws_;
|
|
}
|
|
|
|
std::optional<std::string>
|
|
ip()
|
|
{
|
|
return ip_;
|
|
}
|
|
};
|
|
|
|
class SslWsUpgrader : public std::enable_shared_from_this<SslWsUpgrader>
|
|
{
|
|
boost::asio::io_context& ioc_;
|
|
boost::beast::ssl_stream<boost::beast::tcp_stream> https_;
|
|
boost::optional<http::request_parser<http::string_body>> parser_;
|
|
boost::beast::flat_buffer buffer_;
|
|
std::optional<std::string> ip_;
|
|
std::shared_ptr<BackendInterface const> backend_;
|
|
std::shared_ptr<RPC::RPCEngine> rpcEngine_;
|
|
std::shared_ptr<SubscriptionManager> subscriptions_;
|
|
std::shared_ptr<LoadBalancer> balancer_;
|
|
std::shared_ptr<ETLService const> etl_;
|
|
util::TagDecoratorFactory const& tagFactory_;
|
|
clio::DOSGuard& dosGuard_;
|
|
http::request<http::string_body> req_;
|
|
|
|
public:
|
|
SslWsUpgrader(
|
|
boost::asio::io_context& ioc,
|
|
std::optional<std::string> ip,
|
|
boost::asio::ip::tcp::socket&& socket,
|
|
ssl::context& ctx,
|
|
std::shared_ptr<BackendInterface const> backend,
|
|
std::shared_ptr<RPC::RPCEngine> rpcEngine,
|
|
std::shared_ptr<SubscriptionManager> subscriptions,
|
|
std::shared_ptr<LoadBalancer> balancer,
|
|
std::shared_ptr<ETLService const> etl,
|
|
util::TagDecoratorFactory const& tagFactory,
|
|
clio::DOSGuard& dosGuard,
|
|
boost::beast::flat_buffer&& b)
|
|
: ioc_(ioc)
|
|
, https_(std::move(socket), ctx)
|
|
, buffer_(std::move(b))
|
|
, ip_(ip)
|
|
, backend_(backend)
|
|
, rpcEngine_(rpcEngine)
|
|
, subscriptions_(subscriptions)
|
|
, balancer_(balancer)
|
|
, etl_(etl)
|
|
, tagFactory_(tagFactory)
|
|
, dosGuard_(dosGuard)
|
|
{
|
|
}
|
|
|
|
SslWsUpgrader(
|
|
boost::asio::io_context& ioc,
|
|
boost::beast::ssl_stream<boost::beast::tcp_stream> stream,
|
|
std::optional<std::string> ip,
|
|
std::shared_ptr<BackendInterface const> backend,
|
|
std::shared_ptr<RPC::RPCEngine> rpcEngine,
|
|
std::shared_ptr<SubscriptionManager> subscriptions,
|
|
std::shared_ptr<LoadBalancer> balancer,
|
|
std::shared_ptr<ETLService const> etl,
|
|
util::TagDecoratorFactory const& tagFactory,
|
|
clio::DOSGuard& dosGuard,
|
|
boost::beast::flat_buffer&& b,
|
|
http::request<http::string_body> req)
|
|
: ioc_(ioc)
|
|
, https_(std::move(stream))
|
|
, buffer_(std::move(b))
|
|
, ip_(ip)
|
|
, backend_(backend)
|
|
, rpcEngine_(rpcEngine)
|
|
, subscriptions_(subscriptions)
|
|
, balancer_(balancer)
|
|
, etl_(etl)
|
|
, tagFactory_(tagFactory)
|
|
, dosGuard_(dosGuard)
|
|
, req_(std::move(req))
|
|
{
|
|
}
|
|
|
|
~SslWsUpgrader() = default;
|
|
|
|
void
|
|
run()
|
|
{
|
|
// Set the timeout.
|
|
boost::beast::get_lowest_layer(https_).expires_after(std::chrono::seconds(30));
|
|
|
|
net::dispatch(
|
|
https_.get_executor(), boost::beast::bind_front_handler(&SslWsUpgrader::doUpgrade, shared_from_this()));
|
|
}
|
|
|
|
private:
|
|
void
|
|
onHandshake(boost::beast::error_code ec, std::size_t bytes_used)
|
|
{
|
|
if (ec)
|
|
return logError(ec, "handshake");
|
|
|
|
// Consume the portion of the buffer used by the handshake
|
|
buffer_.consume(bytes_used);
|
|
|
|
doUpgrade();
|
|
}
|
|
|
|
void
|
|
doUpgrade()
|
|
{
|
|
parser_.emplace();
|
|
|
|
// Apply a reasonable limit to the allowed size
|
|
// of the body in bytes to prevent abuse.
|
|
parser_->body_limit(10000);
|
|
|
|
// Set the timeout.
|
|
boost::beast::get_lowest_layer(https_).expires_after(std::chrono::seconds(30));
|
|
|
|
onUpgrade();
|
|
}
|
|
|
|
void
|
|
onUpgrade()
|
|
{
|
|
// See if it is a WebSocket Upgrade
|
|
if (!websocket::is_upgrade(req_))
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Disable the timeout.
|
|
// The websocket::stream uses its own timeout settings.
|
|
boost::beast::get_lowest_layer(https_).expires_never();
|
|
|
|
std::make_shared<SslWsSession>(
|
|
ioc_,
|
|
std::move(https_),
|
|
ip_,
|
|
backend_,
|
|
rpcEngine_,
|
|
subscriptions_,
|
|
balancer_,
|
|
etl_,
|
|
tagFactory_,
|
|
dosGuard_,
|
|
std::move(buffer_))
|
|
->run(std::move(req_));
|
|
}
|
|
};
|