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
35template <SomeServerHandler HandlerType>
36class SslWsSession : public impl::WsBase<SslWsSession, HandlerType> {
37 using StreamType =
38 boost::beast::websocket::stream<boost::asio::ssl::stream<boost::beast::tcp_stream>>;
39 StreamType ws_;
40
41public:
54 explicit SslWsSession(
55 boost::asio::ssl::stream<boost::beast::tcp_stream>&& stream,
56 std::string ip,
57 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
58 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
59 std::shared_ptr<HandlerType> const& handler,
60 boost::beast::flat_buffer&& buffer,
61 bool isAdmin,
62 std::uint32_t maxWsSendingQueueSize
63 )
64 : impl::WsBase<SslWsSession, HandlerType>(
65 ip,
66 tagFactory,
67 dosGuard,
68 handler,
69 std::move(buffer),
70 maxWsSendingQueueSize
71 )
72 , ws_(std::move(stream))
73 {
74 ConnectionBase::isAdmin_ = isAdmin; // NOLINT(cppcoreguidelines-prefer-member-initializer)
75 }
76
80 StreamType&
82 {
83 return ws_;
84 }
85};
86
94template <SomeServerHandler HandlerType>
95class SslWsUpgrader : public std::enable_shared_from_this<SslWsUpgrader<HandlerType>> {
96 using std::enable_shared_from_this<SslWsUpgrader<HandlerType>>::shared_from_this;
97
98 boost::asio::ssl::stream<boost::beast::tcp_stream> https_;
99 boost::optional<http::request_parser<http::string_body>> parser_;
100 boost::beast::flat_buffer buffer_;
101 std::string ip_;
102 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
103 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard_;
104 std::shared_ptr<HandlerType> const handler_;
105 http::request<http::string_body> req_;
106 bool isAdmin_;
107 std::uint32_t maxWsSendingQueueSize_;
108
109public:
124 boost::asio::ssl::stream<boost::beast::tcp_stream> stream,
125 std::string ip,
126 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
127 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
128 std::shared_ptr<HandlerType> handler,
129 boost::beast::flat_buffer&& buffer,
130 http::request<http::string_body> request,
131 bool isAdmin,
132 std::uint32_t maxWsSendingQueueSize
133 )
134 : https_(std::move(stream))
135 , buffer_(std::move(buffer))
136 , ip_(std::move(ip))
137 , tagFactory_(tagFactory)
138 , dosGuard_(dosGuard)
139 , handler_(std::move(handler))
140 , req_(std::move(request))
141 , isAdmin_(isAdmin)
142 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
143 {
144 }
145
146 ~SslWsUpgrader() = default;
147
151 void
153 {
154 boost::beast::get_lowest_layer(https_).expires_after(std::chrono::seconds(30));
155
156 boost::asio::dispatch(
157 https_.get_executor(),
158 boost::beast::bind_front_handler(
159 &SslWsUpgrader<HandlerType>::doUpgrade, shared_from_this()
160 )
161 );
162 }
163
164private:
165 void
166 doUpgrade()
167 {
168 parser_.emplace();
169
170 // Apply a reasonable limit to the allowed size of the body in bytes to prevent abuse.
171 static constexpr auto kMaxBodySize = 10000;
172 parser_->body_limit(kMaxBodySize);
173
174 boost::beast::get_lowest_layer(https_).expires_after(std::chrono::seconds(30));
175 onUpgrade();
176 }
177
178 void
179 onUpgrade()
180 {
181 if (!boost::beast::websocket::is_upgrade(req_))
182 return;
183
184 // Disable the timeout. The websocket::stream uses its own timeout settings.
185 boost::beast::get_lowest_layer(https_).expires_never();
186
187 std::make_shared<SslWsSession<HandlerType>>(
188 std::move(https_),
189 ip_,
190 tagFactory_,
191 dosGuard_,
192 handler_,
193 std::move(buffer_),
194 isAdmin_,
195 maxWsSendingQueueSize_
196 )
197 ->run(std::move(req_));
198 }
199};
200} // 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:54
StreamType & ws()
Definition SslWsSession.hpp:81
The HTTPS upgrader class, upgrade from an HTTPS session to a secure websocket session.
Definition SslWsSession.hpp:95
void run()
Initiate the upgrade.
Definition SslWsSession.hpp:152
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:123
Web socket implementation. This class is the base class of the web socket session,...
Definition WsBase.hpp:58
This namespace implements the web server and related components.
Definition Types.hpp:23
bool isAdmin() const
Indicates whether the connection has admin privileges.
Definition ConnectionBase.hpp:99