Skip to content

Commit

Permalink
add client cert support for websocket (drogonframework#1967)
Browse files Browse the repository at this point in the history
  • Loading branch information
I-LOVE-C2H5OH authored Mar 4, 2024
1 parent 88d0668 commit 4cbac30
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
25 changes: 25 additions & 0 deletions lib/inc/drogon/WebSocketClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ class DROGON_EXPORT WebSocketClient
virtual void connectToServer(const HttpRequestPtr &request,
const WebSocketRequestCallback &callback) = 0;

/**
* @brief Set the client certificate used by the HTTP connection
*
* @param cert Path to the certificate
* @param key Path to the certificate's private key
* @note this method has no effect if the HTTP client is communicating via
* unencrypted HTTP
*/
virtual void setCertPath(const std::string &cert,
const std::string &key) = 0;

/**
* @brief Supplies command style options for `SSL_CONF_cmd`
*
* @param sslConfCmds options for SSL_CONF_cmd
* @note this method has no effect if the HTTP client is communicating via
* unencrypted HTTP
* @code
addSSLConfigs({{"-dhparam", "/path/to/dhparam"}, {"-strict", ""}});
* @endcode
*/
virtual void addSSLConfigs(
const std::vector<std::pair<std::string, std::string>>
&sslConfCmds) = 0;

#ifdef __cpp_impl_coroutine
/**
* @brief Set messages handler. When a message is received from the server,
Expand Down
21 changes: 20 additions & 1 deletion lib/src/WebSocketClientImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ void WebSocketClientImpl::createTcpClient()
auto policy = trantor::TLSPolicy::defaultClientPolicy();
policy->setUseOldTLS(useOldTLS_)
.setValidate(validateCert_)
.setHostname(domain_);
.setHostname(domain_)
.setConfCmds(sslConfCmds_)
.setCertPath(clientCertPath_)
.setKeyPath(clientKeyPath_);
tcpClientPtr_->enableSSL(std::move(policy));
}
auto thisPtr = shared_from_this();
Expand Down Expand Up @@ -452,6 +455,22 @@ void WebSocketClientImpl::connectToServer(
}
}

void WebSocketClientImpl::setCertPath(const std::string &cert,
const std::string &key)
{
clientCertPath_ = cert;
clientKeyPath_ = key;
}

void WebSocketClientImpl::addSSLConfigs(
const std::vector<std::pair<std::string, std::string>> &sslConfCmds)
{
for (const auto &cmd : sslConfCmds)
{
sslConfCmds_.push_back(cmd);
}
}

WebSocketClientPtr WebSocketClient::newWebSocketClient(const std::string &ip,
uint16_t port,
bool useSSL,
Expand Down
8 changes: 8 additions & 0 deletions lib/src/WebSocketClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class WebSocketClientImpl
void connectToServer(const HttpRequestPtr &request,
const WebSocketRequestCallback &callback) override;

void setCertPath(const std::string &cert, const std::string &key) override;

void addSSLConfigs(const std::vector<std::pair<std::string, std::string>>
&sslConfCmds) override;

trantor::EventLoop *getLoop() override
{
return loop_;
Expand Down Expand Up @@ -83,6 +88,9 @@ class WebSocketClientImpl
bool stop_{false};
std::string wsKey_;
std::string wsAccept_;
std::string clientCertPath_;
std::string clientKeyPath_;
std::vector<std::pair<std::string, std::string>> sslConfCmds_;

HttpRequestPtr upgradeRequest_;
std::function<void(std::string &&,
Expand Down

0 comments on commit 4cbac30

Please sign in to comment.