Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
SslWsSession.hpp
1#pragma once
2
3#include "util/Taggable.hpp"
4#include "web/dosguard/DOSGuardInterface.hpp"
5#include "web/impl/WsBase.hpp"
6#include "web/interface/ConnectionBase.hpp"
7
8#include <boost/beast/core/flat_buffer.hpp>
9#include <boost/beast/core/stream_traits.hpp>
10#include <boost/beast/core/tcp_stream.hpp>
11#include <boost/beast/http/message.hpp>
12#include <boost/beast/http/parser.hpp>
13#include <boost/beast/http/string_body.hpp>
14#include <boost/beast/ssl.hpp>
15#include <boost/beast/ssl/ssl_stream.hpp>
16#include <boost/beast/websocket/stream.hpp>
17#include <boost/optional/optional.hpp>
18
19#include <chrono>
20#include <cstdint>
21#include <functional>
22#include <memory>
23#include <string>
24#include <utility>
25
26namespace web {
27
33template <SomeServerHandler HandlerType>
34class SslWsSession : public impl::WsBase<SslWsSession, HandlerType> {
35 using StreamType =
36 boost::beast::websocket::stream<boost::asio::ssl::stream<boost::beast::tcp_stream>>;
37 StreamType ws_;
38
39public:
52 explicit SslWsSession(
53 boost::asio::ssl::stream<boost::beast::tcp_stream>&& stream,
54 std::string ip,
55 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
56 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
57 std::shared_ptr<HandlerType> const& handler,
58 boost::beast::flat_buffer&& buffer,
59 bool isAdmin,
60 std::uint32_t maxWsSendingQueueSize
61 )
62 : impl::WsBase<SslWsSession, HandlerType>(
63 ip,
64 tagFactory,
65 dosGuard,
66 handler,
67 std::move(buffer),
68 maxWsSendingQueueSize
69 )
70 , ws_(std::move(stream))
71 {
72 ConnectionBase::isAdmin_ = isAdmin; // NOLINT(cppcoreguidelines-prefer-member-initializer)
73 }
74
76 StreamType&
78 {
79 return ws_;
80 }
81};
82
88template <SomeServerHandler HandlerType>
89class SslWsUpgrader : public std::enable_shared_from_this<SslWsUpgrader<HandlerType>> {
90 using std::enable_shared_from_this<SslWsUpgrader<HandlerType>>::shared_from_this;
91
92 boost::asio::ssl::stream<boost::beast::tcp_stream> https_;
93 boost::optional<http::request_parser<http::string_body>> parser_;
94 boost::beast::flat_buffer buffer_;
95 std::string ip_;
96 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
97 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard_;
98 std::shared_ptr<HandlerType> const handler_;
99 http::request<http::string_body> req_;
100 bool isAdmin_;
101 std::uint32_t maxWsSendingQueueSize_;
102
103public:
118 boost::asio::ssl::stream<boost::beast::tcp_stream> stream,
119 std::string ip,
120 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
121 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
122 std::shared_ptr<HandlerType> handler,
123 boost::beast::flat_buffer&& buffer,
124 http::request<http::string_body> request,
125 bool isAdmin,
126 std::uint32_t maxWsSendingQueueSize
127 )
128 : https_(std::move(stream))
129 , buffer_(std::move(buffer))
130 , ip_(std::move(ip))
131 , tagFactory_(tagFactory)
132 , dosGuard_(dosGuard)
133 , handler_(std::move(handler))
134 , req_(std::move(request))
135 , isAdmin_(isAdmin)
136 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
137 {
138 }
139
140 ~SslWsUpgrader() = default;
141
143 void
145 {
146 boost::beast::get_lowest_layer(https_).expires_after(std::chrono::seconds(30));
147
148 boost::asio::dispatch(
149 https_.get_executor(),
150 boost::beast::bind_front_handler(
151 &SslWsUpgrader<HandlerType>::doUpgrade, shared_from_this()
152 )
153 );
154 }
155
156private:
157 void
158 doUpgrade()
159 {
160 parser_.emplace();
161
162 // Apply a reasonable limit to the allowed size of the body in bytes to prevent abuse.
163 static constexpr auto kMAX_BODY_SIZE = 10000;
164 parser_->body_limit(kMAX_BODY_SIZE);
165
166 boost::beast::get_lowest_layer(https_).expires_after(std::chrono::seconds(30));
167 onUpgrade();
168 }
169
170 void
171 onUpgrade()
172 {
173 if (!boost::beast::websocket::is_upgrade(req_))
174 return;
175
176 // Disable the timeout. The websocket::stream uses its own timeout settings.
177 boost::beast::get_lowest_layer(https_).expires_never();
178
179 std::make_shared<SslWsSession<HandlerType>>(
180 std::move(https_),
181 ip_,
182 tagFactory_,
183 dosGuard_,
184 handler_,
185 std::move(buffer_),
186 isAdmin_,
187 maxWsSendingQueueSize_
188 )
189 ->run(std::move(req_));
190 }
191};
192} // namespace web
SslWsSession(boost::asio::ssl::stream< boost::beast::tcp_stream > &&stream, std::string ip, std::reference_wrapper< util::TagDecoratorFactory const > tagFactory, std::reference_wrapper< dosguard::DOSGuardInterface > dosGuard, std::shared_ptr< HandlerType > const &handler, boost::beast::flat_buffer &&buffer, bool isAdmin, std::uint32_t maxWsSendingQueueSize)
Create a new non-secure websocket session.
Definition SslWsSession.hpp:52
StreamType & ws()
Definition SslWsSession.hpp:77
The HTTPS upgrader class, upgrade from an HTTPS session to a secure websocket session.
Definition SslWsSession.hpp:89
void run()
Initiate the upgrade.
Definition SslWsSession.hpp:144
SslWsUpgrader(boost::asio::ssl::stream< boost::beast::tcp_stream > stream, std::string ip, std::reference_wrapper< util::TagDecoratorFactory const > tagFactory, std::reference_wrapper< dosguard::DOSGuardInterface > dosGuard, std::shared_ptr< HandlerType > handler, boost::beast::flat_buffer &&buffer, http::request< http::string_body > request, bool isAdmin, std::uint32_t maxWsSendingQueueSize)
Create a new upgrader to secure websocket.
Definition SslWsSession.hpp:117
Web socket implementation. This class is the base class of the web socket session,...
Definition WsBase.hpp:57
This namespace implements the web server and related components.
Definition Types.hpp:24
bool isAdmin() const
Indicates whether the connection has admin privileges.
Definition ConnectionBase.hpp:99