1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-20 06:53:52 +00:00
openmw-tes3mp/apps/master/SimpleWeb/https_server.hpp

83 lines
3.3 KiB
C++
Raw Normal View History

2017-09-12 12:21:32 +00:00
#pragma once
2017-05-19 16:02:12 +00:00
#include "base_server.hpp"
2017-09-12 12:21:32 +00:00
#ifdef USE_STANDALONE_ASIO
#include <asio/ssl.hpp>
#else
2017-05-19 16:02:12 +00:00
#include <boost/asio/ssl.hpp>
2017-09-12 12:21:32 +00:00
#endif
2017-05-19 16:02:12 +00:00
#include <algorithm>
2017-09-12 12:21:32 +00:00
#include <openssl/ssl.h>
2017-05-19 16:02:12 +00:00
2017-09-12 12:21:32 +00:00
namespace SimpleWeb {
using HTTPS = asio::ssl::stream<asio::ip::tcp::socket>;
2017-05-19 16:02:12 +00:00
2017-09-12 12:21:32 +00:00
template <>
class Server<HTTPS> : public ServerBase<HTTPS> {
2017-05-19 16:02:12 +00:00
std::string session_id_context;
bool set_session_id_context = false;
2017-09-12 12:21:32 +00:00
2017-05-19 16:02:12 +00:00
public:
2017-09-12 12:21:32 +00:00
Server(const std::string &cert_file, const std::string &private_key_file, const std::string &verify_file = std::string())
: ServerBase<HTTPS>::ServerBase(443), context(asio::ssl::context::tlsv12) {
2017-05-19 16:02:12 +00:00
context.use_certificate_chain_file(cert_file);
2017-09-12 12:21:32 +00:00
context.use_private_key_file(private_key_file, asio::ssl::context::pem);
2017-05-19 16:02:12 +00:00
2017-09-12 12:21:32 +00:00
if(verify_file.size() > 0) {
2017-05-19 16:02:12 +00:00
context.load_verify_file(verify_file);
2017-09-12 12:21:32 +00:00
context.set_verify_mode(asio::ssl::verify_peer | asio::ssl::verify_fail_if_no_peer_cert | asio::ssl::verify_client_once);
2017-05-19 16:02:12 +00:00
set_session_id_context = true;
}
}
2017-09-12 12:21:32 +00:00
void start() override {
if(set_session_id_context) {
2017-05-19 16:02:12 +00:00
// Creating session_id_context from address:port but reversed due to small SSL_MAX_SSL_SESSION_ID_LENGTH
session_id_context = std::to_string(config.port) + ':';
session_id_context.append(config.address.rbegin(), config.address.rend());
2017-09-12 12:21:32 +00:00
SSL_CTX_set_session_id_context(context.native_handle(), reinterpret_cast<const unsigned char *>(session_id_context.data()),
std::min<size_t>(session_id_context.size(), SSL_MAX_SSL_SESSION_ID_LENGTH));
2017-05-19 16:02:12 +00:00
}
ServerBase::start();
}
protected:
2017-09-12 12:21:32 +00:00
asio::ssl::context context;
2017-05-19 16:02:12 +00:00
2017-09-12 12:21:32 +00:00
void accept() override {
auto session = std::make_shared<Session>(create_connection(*io_service, context));
2017-05-19 16:02:12 +00:00
2017-09-12 12:21:32 +00:00
acceptor->async_accept(session->connection->socket->lowest_layer(), [this, session](const error_code &ec) {
auto lock = session->connection->handler_runner->continue_lock();
if(!lock)
return;
2017-05-19 16:02:12 +00:00
2017-09-12 12:21:32 +00:00
if(ec != asio::error::operation_aborted)
this->accept();
2017-05-19 16:02:12 +00:00
2017-09-12 12:21:32 +00:00
if(!ec) {
asio::ip::tcp::no_delay option(true);
error_code ec;
session->connection->socket->lowest_layer().set_option(option, ec);
2017-05-19 16:02:12 +00:00
2017-09-12 12:21:32 +00:00
session->connection->set_timeout(config.timeout_request);
session->connection->socket->async_handshake(asio::ssl::stream_base::server, [this, session](const error_code &ec) {
session->connection->cancel_timeout();
auto lock = session->connection->handler_runner->continue_lock();
if(!lock)
return;
if(!ec)
this->read_request_and_content(session);
else if(this->on_error)
this->on_error(session->request, ec);
2017-05-19 16:02:12 +00:00
});
}
2017-09-12 12:21:32 +00:00
else if(this->on_error)
this->on_error(session->request, ec);
2017-05-19 16:02:12 +00:00
});
}
};
2017-09-12 12:21:32 +00:00
} // namespace SimpleWeb