18#ifndef TRINITYCORE_SSL_STREAM_H
19#define TRINITYCORE_SSL_STREAM_H
24#include <boost/asio/ip/tcp.hpp>
25#include <boost/asio/ssl/stream.hpp>
26#include <boost/system/error_code.hpp>
30namespace SslHandshakeHelpers
35template <
typename SocketImpl>
42 _socket->underlying_stream().async_handshake(boost::asio::ssl::stream_base::server,
43 [socketRef =
_socket->weak_from_this(), self = this->shared_from_this()](boost::system::error_code
const& error)
45 std::shared_ptr<SocketImpl> socket = static_pointer_cast<SocketImpl>(socketRef.lock());
51 SslHandshakeHelpers::LogFailure(socket->GetRemoteIpAddress(), socket->GetRemotePort(), error);
52 socket->CloseSocket();
64template<
class WrappedStream = IoContextTcpSocket>
72 _sslSocket.set_verify_mode(boost::asio::ssl::verify_none);
75 explicit SslStream(boost::asio::io_context& context, boost::asio::ssl::context& sslContext) : _sslSocket(context, sslContext)
77 _sslSocket.set_verify_mode(boost::asio::ssl::verify_none);
83 return _sslSocket.get_executor();
88 return _sslSocket.next_layer().is_open();
91 void close(boost::system::error_code& error)
93 _sslSocket.next_layer().close(error);
96 void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code& shutdownError)
98 _sslSocket.shutdown(shutdownError);
99 _sslSocket.next_layer().shutdown(what, shutdownError);
102 template<
typename ConnectHandlerType>
103 decltype(
auto)
async_connect(boost::asio::ip::tcp::endpoint
const& endpoint, ConnectHandlerType&& handler)
105 return _sslSocket.next_layer().async_connect(endpoint, std::forward<ConnectHandlerType>(handler));
108 template<
typename MutableBufferSequence,
typename ReadHandlerType>
109 decltype(
auto)
async_read_some(MutableBufferSequence
const& buffers, ReadHandlerType&& handler)
111 return _sslSocket.async_read_some(buffers, std::forward<ReadHandlerType>(handler));
114 template<
typename ConstBufferSequence,
typename WriteHandlerType>
115 decltype(
auto)
async_write_some(ConstBufferSequence
const& buffers, WriteHandlerType&& handler)
117 return _sslSocket.async_write_some(buffers, std::forward<WriteHandlerType>(handler));
120 template<
typename ConstBufferSequence>
121 std::size_t
write_some(ConstBufferSequence
const& buffers, boost::system::error_code& error)
123 return _sslSocket.write_some(buffers, error);
126 template<
typename WaitHandlerType>
127 decltype(
auto)
async_wait(boost::asio::socket_base::wait_type type, WaitHandlerType&& handler)
129 return _sslSocket.next_layer().async_wait(type, std::forward<WaitHandlerType>(handler));
132 template<
typename SettableSocketOption>
133 void set_option(SettableSocketOption
const& option, boost::system::error_code& error)
135 _sslSocket.next_layer().set_option(option, error);
140 return _sslSocket.next_layer().remote_endpoint();
144 template<
typename HandshakeHandlerType>
145 decltype(
auto)
async_handshake(boost::asio::ssl::stream_base::handshake_type type, HandshakeHandlerType&& handler)
147 return _sslSocket.async_handshake(type, std::forward<HandshakeHandlerType>(handler));
152 if (!SSL_set_tlsext_host_name(_sslSocket.native_handle(), serverName.c_str()))
153 error.assign(
static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category());
SslStream(IoContextTcpSocket &&socket, boost::asio::ssl::context &sslContext)
decltype(auto) async_connect(boost::asio::ip::tcp::endpoint const &endpoint, ConnectHandlerType &&handler)
typename WrappedStream::executor_type executor_type
decltype(auto) async_handshake(boost::asio::ssl::stream_base::handshake_type type, HandshakeHandlerType &&handler)
void set_server_name(std::string const &serverName, boost::system::error_code &error)
decltype(auto) async_read_some(MutableBufferSequence const &buffers, ReadHandlerType &&handler)
IoContextTcpSocket::endpoint_type remote_endpoint() const
SslStream(boost::asio::io_context &context, boost::asio::ssl::context &sslContext)
void set_option(SettableSocketOption const &option, boost::system::error_code &error)
decltype(auto) async_write_some(ConstBufferSequence const &buffers, WriteHandlerType &&handler)
void close(boost::system::error_code &error)
std::size_t write_some(ConstBufferSequence const &buffers, boost::system::error_code &error)
void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code &shutdownError)
boost::asio::io_context::executor_type get_executor()
decltype(auto) async_wait(boost::asio::socket_base::wait_type type, WaitHandlerType &&handler)
boost::asio::ssl::stream< WrappedStream > _sslSocket
TC_NETWORK_API void LogFailure(boost::asio::ip::address const &ipAddress, uint16 port, boost::system::error_code const &error)
boost::asio::basic_stream_socket< boost::asio::ip::tcp, boost::asio::io_context::executor_type > IoContextTcpSocket
SslHandshakeConnectionInitializer(SocketImpl *socket)