server_factory.cpp
Go to the documentation of this file.
1 #include <websocketpp/common/connection_hdl.hpp>
2 
4 
5 #define ASIO_STANDALONE
9 
10 namespace foxglove {
11 
12 template <>
13 std::unique_ptr<ServerInterface<websocketpp::connection_hdl>> ServerFactory::createServer(
14  const std::string& name, const std::function<void(WebSocketLogLevel, char const*)>& logHandler,
15  const ServerOptions& options) {
16  if (options.useTls) {
17  return std::make_unique<foxglove::Server<foxglove::WebSocketTls>>(name, logHandler, options);
18  } else {
19  return std::make_unique<foxglove::Server<foxglove::WebSocketNoTls>>(name, logHandler, options);
20  }
21 }
22 
23 template <>
25  _server.get_alog().write(APP, "Server running without TLS");
26 }
27 
28 template <>
30  _server.set_tls_init_handler([this](ConnHandle hdl) {
31  (void)hdl;
32 
33  namespace asio = websocketpp::lib::asio;
34  auto ctx = websocketpp::lib::make_shared<asio::ssl::context>(asio::ssl::context::sslv23);
35 
36  try {
37  ctx->set_options(asio::ssl::context::default_workarounds | asio::ssl::context::no_tlsv1 |
38  asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3);
39  ctx->use_certificate_chain_file(_options.certfile);
40  ctx->use_private_key_file(_options.keyfile, asio::ssl::context::pem);
41 
42  // Ciphers are taken from the websocketpp example echo tls server:
43  // https://github.com/zaphoyd/websocketpp/blob/1b11fd301/examples/echo_server_tls/echo_server_tls.cpp#L119
44  constexpr char ciphers[] =
45  "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:"
46  "ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+"
47  "AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-"
48  "AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-"
49  "ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-"
50  "AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:"
51  "!MD5:!PSK";
52 
53  if (SSL_CTX_set_cipher_list(ctx->native_handle(), ciphers) != 1) {
54  _server.get_elog().write(RECOVERABLE, "Error setting cipher list");
55  }
56  } catch (const std::exception& ex) {
57  _server.get_elog().write(RECOVERABLE,
58  std::string("Exception in TLS handshake: ") + ex.what());
59  }
60  return ctx;
61  });
62 }
63 
64 } // namespace foxglove
static const websocketpp::log::level APP
static std::unique_ptr< ServerInterface< ConnectionHandle > > createServer(const std::string &name, const std::function< void(WebSocketLogLevel, char const *)> &logHandler, const ServerOptions &options)
void setupTlsHandler()
static const websocketpp::log::level RECOVERABLE
websocketpp::connection_hdl ConnHandle
WebSocketLogLevel
Definition: common.hpp:41


foxglove_bridge
Author(s): Foxglove
autogenerated on Mon Jul 3 2023 02:12:22