From 1a6c06376d8dce16e973525a5a66939b0446a397 Mon Sep 17 00:00:00 2001 From: Nate Koenig Date: Thu, 11 Jun 2020 13:03:14 -0700 Subject: [PATCH 1/7] Working on ssl Signed-off-by: Nate Koenig --- examples/websocket.ign | 2 ++ plugins/websocket_server/WebsocketServer.cc | 40 +++++++++++++++++++-- plugins/websocket_server/index.html | 2 +- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/examples/websocket.ign b/examples/websocket.ign index a2be0c1d..bc76717a 100644 --- a/examples/websocket.ign +++ b/examples/websocket.ign @@ -9,6 +9,8 @@ 9002 auth_key admin_key + /home/nkoenig/localhost.cert + /home/nkoenig/localhost.key diff --git a/plugins/websocket_server/WebsocketServer.cc b/plugins/websocket_server/WebsocketServer.cc index b2e2b64b..ea3f6942 100644 --- a/plugins/websocket_server/WebsocketServer.cc +++ b/plugins/websocket_server/WebsocketServer.cc @@ -62,6 +62,13 @@ int rootCallback(struct lws *_wsi, return 0; int fd = lws_get_socket_fd(_wsi); + std::cout << "Here[" << _reason << "]\n"; + + /*struct per_vhost_data__minimal *vhd = + (struct per_vhost_data__minimal *) + lws_protocol_vh_priv_get(lws_get_vhost(_wsi), + lws_get_protocol(_wsi)); + */ // std::lock_guard mainLock(self->mutex); switch (_reason) @@ -119,6 +126,11 @@ int rootCallback(struct lws *_wsi, self->OnMessage(fd, std::string((const char *)_in)); break; + case LWS_CALLBACK_PROTOCOL_INIT: + igndbg << "LWS_CALLBACK_PROTOCOL_INIT\n"; + break; + + default: // Do nothing on default. break; @@ -206,6 +218,22 @@ bool WebsocketServer::Load(const tinyxml2::XMLElement *_elem) } igndbg << "Using port[" << port << "]\n"; + std::string sslCertFile = ""; + // Get the ssl cert file, if present. + elem = _elem->FirstChildElement("ssl_cert_file"); + if (elem) + { + sslCertFile = elem->GetText(); + } + + std::string sslPrivateKeyFile = ""; + // Get the ssl private key file, if present. + elem = _elem->FirstChildElement("ssl_private_key_file"); + if (elem) + { + sslPrivateKeyFile = elem->GetText(); + } + // All of the protocols handled by this websocket server. this->protocols.push_back( { @@ -239,9 +267,15 @@ bool WebsocketServer::Load(const tinyxml2::XMLElement *_elem) info.port = port; info.iface = NULL; info.protocols = &this->protocols[0]; - // We are not using SSL right now - info.ssl_cert_filepath = NULL; - info.ssl_private_key_filepath = NULL; + + if (!sslCertFile.empty() && !sslPrivateKeyFile.empty()) + { + std::cout << "SSL!!\n"; + info.options = LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT; + info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; + info.ssl_cert_filepath = sslCertFile.c_str(); + info.ssl_private_key_filepath = sslPrivateKeyFile.c_str(); + } // keep alive time of 60 seconds info.ka_time = 60; diff --git a/plugins/websocket_server/index.html b/plugins/websocket_server/index.html index f5cedadf..a22be96b 100644 --- a/plugins/websocket_server/index.html +++ b/plugins/websocket_server/index.html @@ -11,7 +11,7 @@