Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
SslHttpSession.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "data/LedgerCacheInterface.hpp"
23#include "util/Taggable.hpp"
24#include "web/AdminVerificationStrategy.hpp"
25#include "web/ProxyIpResolver.hpp"
26#include "web/SslWsSession.hpp"
27#include "web/dosguard/DOSGuardInterface.hpp"
28#include "web/impl/HttpBase.hpp"
29#include "web/interface/Concepts.hpp"
30#include "web/interface/ConnectionBase.hpp"
31
32#include <boost/asio/ip/tcp.hpp>
33#include <boost/asio/ssl/context.hpp>
34#include <boost/asio/ssl/stream_base.hpp>
35#include <boost/beast/core/error.hpp>
36#include <boost/beast/core/flat_buffer.hpp>
37#include <boost/beast/core/stream_traits.hpp>
38#include <boost/beast/core/tcp_stream.hpp>
39#include <boost/beast/ssl/ssl_stream.hpp>
40
41#include <chrono>
42#include <cstddef>
43#include <cstdint>
44#include <functional>
45#include <memory>
46#include <string>
47#include <utility>
48
49namespace web {
50
51using tcp = boost::asio::ip::tcp;
52
61template <SomeServerHandler HandlerType>
62class SslHttpSession : public impl::HttpBase<SslHttpSession, HandlerType>,
63 public std::enable_shared_from_this<SslHttpSession<HandlerType>> {
64 boost::beast::ssl_stream<boost::beast::tcp_stream> stream_;
65 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
66 std::uint32_t maxWsSendingQueueSize_;
67
68public:
85 tcp::socket&& socket,
86 std::string const& ip,
87 std::shared_ptr<AdminVerificationStrategy> const& adminVerification,
88 std::shared_ptr<ProxyIpResolver> proxyIpResolver,
89 boost::asio::ssl::context& ctx,
90 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
91 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
92 std::shared_ptr<HandlerType> const& handler,
93 std::reference_wrapper<data::LedgerCacheInterface const> cache,
94 boost::beast::flat_buffer buffer,
95 std::uint32_t maxWsSendingQueueSize
96 )
97 : impl::HttpBase<SslHttpSession, HandlerType>(
98 ip,
99 tagFactory,
100 adminVerification,
101 std::move(proxyIpResolver),
102 dosGuard,
103 handler,
104 cache,
105 std::move(buffer)
106 )
107 , stream_(std::move(socket), ctx)
108 , tagFactory_(tagFactory)
109 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
110 {
111 }
112
113 ~SslHttpSession() override = default;
114
116 boost::beast::ssl_stream<boost::beast::tcp_stream>&
118 {
119 return stream_;
120 }
121
123 void
125 {
126 auto self = this->shared_from_this();
127 boost::asio::dispatch(stream_.get_executor(), [self]() {
128 // Set the timeout.
129 boost::beast::get_lowest_layer(self->stream()).expires_after(std::chrono::seconds(30));
130
131 // Perform the SSL handshake
132 // Note, this is the buffered version of the handshake.
133 self->stream_.async_handshake(
134 boost::asio::ssl::stream_base::server,
135 self->buffer_.data(),
136 boost::beast::bind_front_handler(&SslHttpSession<HandlerType>::onHandshake, self)
137 );
138 });
139 }
140
147 void
148 onHandshake(boost::beast::error_code ec, std::size_t bytesUsed)
149 {
150 if (ec)
151 return this->httpFail(ec, "handshake");
152
153 this->buffer_.consume(bytesUsed);
154 this->doRead();
155 }
156
158 void
160 {
161 boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
162 stream_.async_shutdown(boost::beast::bind_front_handler(&SslHttpSession::onShutdown, this->shared_from_this()));
163 }
164
170 void
171 onShutdown(boost::beast::error_code ec)
172 {
173 if (ec)
174 return this->httpFail(ec, "shutdown");
175 // At this point the connection is closed gracefully
176 }
177
179 void
181 {
182 std::make_shared<SslWsUpgrader<HandlerType>>(
183 std::move(stream_),
184 this->clientIp_,
185 tagFactory_,
186 this->dosGuard_,
187 this->handler_,
188 std::move(this->buffer_),
189 std::move(this->req_),
191 maxWsSendingQueueSize_
192 )
193 ->run();
194 }
195};
196} // namespace web
Represents a HTTPS connection established by a client.
Definition SslHttpSession.hpp:63
void upgrade()
Upgrades connection to secure websocket.
Definition SslHttpSession.hpp:180
void run()
Initiates the handshake.
Definition SslHttpSession.hpp:124
SslHttpSession(tcp::socket &&socket, std::string const &ip, std::shared_ptr< AdminVerificationStrategy > const &adminVerification, std::shared_ptr< ProxyIpResolver > proxyIpResolver, boost::asio::ssl::context &ctx, std::reference_wrapper< util::TagDecoratorFactory const > tagFactory, std::reference_wrapper< dosguard::DOSGuardInterface > dosGuard, std::shared_ptr< HandlerType > const &handler, std::reference_wrapper< data::LedgerCacheInterface const > cache, boost::beast::flat_buffer buffer, std::uint32_t maxWsSendingQueueSize)
Create a new SSL session.
Definition SslHttpSession.hpp:84
void onShutdown(boost::beast::error_code ec)
Handles a connection shutdown.
Definition SslHttpSession.hpp:171
void doClose()
Closes the underlying connection.
Definition SslHttpSession.hpp:159
boost::beast::ssl_stream< boost::beast::tcp_stream > & stream()
Definition SslHttpSession.hpp:117
void onHandshake(boost::beast::error_code ec, std::size_t bytesUsed)
Handles the handshake.
Definition SslHttpSession.hpp:148
This is the implementation class for http sessions.
Definition HttpBase.hpp:100
This namespace implements the web server and related components.
Definition Types.hpp:43
bool isAdmin() const
Indicates whether the connection has admin privileges.
Definition ConnectionBase.hpp:118