//------------------------------------------------------------------------------ /* 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 #include #include #include #include #include #include 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 { boost::beast::websocket::stream> ws_; public: // Take ownership of the socket explicit SslWsSession( boost::asio::io_context& ioc, boost::beast::ssl_stream&& stream, std::optional ip, std::shared_ptr backend, std::shared_ptr rpcEngine, std::shared_ptr subscriptions, std::shared_ptr balancer, std::shared_ptr 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>& ws() { return ws_; } std::optional ip() { return ip_; } }; class SslWsUpgrader : public std::enable_shared_from_this { boost::asio::io_context& ioc_; boost::beast::ssl_stream https_; boost::optional> parser_; boost::beast::flat_buffer buffer_; std::optional ip_; std::shared_ptr backend_; std::shared_ptr rpcEngine_; std::shared_ptr subscriptions_; std::shared_ptr balancer_; std::shared_ptr etl_; util::TagDecoratorFactory const& tagFactory_; clio::DOSGuard& dosGuard_; http::request req_; public: SslWsUpgrader( boost::asio::io_context& ioc, std::optional ip, boost::asio::ip::tcp::socket&& socket, ssl::context& ctx, std::shared_ptr backend, std::shared_ptr rpcEngine, std::shared_ptr subscriptions, std::shared_ptr balancer, std::shared_ptr 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 stream, std::optional ip, std::shared_ptr backend, std::shared_ptr rpcEngine, std::shared_ptr subscriptions, std::shared_ptr balancer, std::shared_ptr etl, util::TagDecoratorFactory const& tagFactory, clio::DOSGuard& dosGuard, boost::beast::flat_buffer&& b, http::request 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( ioc_, std::move(https_), ip_, backend_, rpcEngine_, subscriptions_, balancer_, etl_, tagFactory_, dosGuard_, std::move(buffer_)) ->run(std::move(req_)); } };