22#include "util/Taggable.hpp"
23#include "web/dosguard/DOSGuardInterface.hpp"
24#include "web/impl/WsBase.hpp"
25#include "web/interface/ConnectionBase.hpp"
27#include <boost/beast/core/flat_buffer.hpp>
28#include <boost/beast/core/stream_traits.hpp>
29#include <boost/beast/core/tcp_stream.hpp>
30#include <boost/beast/http/message.hpp>
31#include <boost/beast/http/parser.hpp>
32#include <boost/beast/http/string_body.hpp>
33#include <boost/beast/ssl.hpp>
34#include <boost/beast/ssl/ssl_stream.hpp>
35#include <boost/beast/websocket/stream.hpp>
36#include <boost/optional/optional.hpp>
52template <SomeServerHandler HandlerType>
55 boost::beast::websocket::stream<boost::asio::ssl::stream<boost::beast::tcp_stream>>;
72 boost::asio::ssl::stream<boost::beast::tcp_stream>&& stream,
74 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
75 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
76 std::shared_ptr<HandlerType>
const& handler,
77 boost::beast::flat_buffer&& buffer,
79 std::uint32_t maxWsSendingQueueSize
89 , ws_(std::move(stream))
91 ConnectionBase::isAdmin_ =
isAdmin;
107template <SomeServerHandler HandlerType>
108class SslWsUpgrader :
public std::enable_shared_from_this<SslWsUpgrader<HandlerType>> {
111 boost::asio::ssl::stream<boost::beast::tcp_stream> https_;
112 boost::optional<http::request_parser<http::string_body>> parser_;
113 boost::beast::flat_buffer buffer_;
115 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
116 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard_;
117 std::shared_ptr<HandlerType>
const handler_;
118 http::request<http::string_body> req_;
120 std::uint32_t maxWsSendingQueueSize_;
137 boost::asio::ssl::stream<boost::beast::tcp_stream> stream,
139 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
140 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
141 std::shared_ptr<HandlerType> handler,
142 boost::beast::flat_buffer&& buffer,
143 http::request<http::string_body> request,
145 std::uint32_t maxWsSendingQueueSize
147 : https_(std::move(stream))
148 , buffer_(std::move(buffer))
150 , tagFactory_(tagFactory)
151 , dosGuard_(dosGuard)
152 , handler_(std::move(handler))
153 , req_(std::move(request))
155 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
165 boost::beast::get_lowest_layer(https_).expires_after(std::chrono::seconds(30));
167 boost::asio::dispatch(
168 https_.get_executor(),
169 boost::beast::bind_front_handler(
170 &SslWsUpgrader<HandlerType>::doUpgrade, shared_from_this()
182 static constexpr auto kMAX_BODY_SIZE = 10000;
183 parser_->body_limit(kMAX_BODY_SIZE);
185 boost::beast::get_lowest_layer(https_).expires_after(std::chrono::seconds(30));
192 if (!boost::beast::websocket::is_upgrade(req_))
196 boost::beast::get_lowest_layer(https_).expires_never();
198 std::make_shared<SslWsSession<HandlerType>>(
206 maxWsSendingQueueSize_
208 ->
run(std::move(req_));
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:71
StreamType & ws()
Definition SslWsSession.hpp:96
The HTTPS upgrader class, upgrade from an HTTPS session to a secure websocket session.
Definition SslWsSession.hpp:108
void run()
Initiate the upgrade.
Definition SslWsSession.hpp:163
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:136
Web socket implementation. This class is the base class of the web socket session,...
Definition WsBase.hpp:76
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