|
1 | | -// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | | -// SPDX-License-Identifier: Apache-2.0 |
3 | | - |
4 | 1 | #include "WebSocketStream.h" |
5 | 2 |
|
6 | 3 | #include <boost/log/sources/severity_feature.hpp> |
@@ -175,35 +172,77 @@ namespace aws { |
175 | 172 |
|
176 | 173 | void WebSocketStream::async_ssl_handshake(const ssl::stream_base::handshake_type &type, const std::string &host, |
177 | 174 | const BoostCallbackFunc &handler) { |
178 | | - if (localproxyConfig.is_web_proxy_using_tls) { |
179 | | - BOOST_LOG_SEV(*log, trace) << "Calling next_layer().async_handshake with type: " |
180 | | - << WEB_PROXY_WITH_TLS_TYPE_NAME; |
181 | | - // Set SNI Hostname (many hosts need this to handshake successfully) |
182 | | - if(!SSL_set_tlsext_host_name(boost::get<unique_ptr<WEB_PROXY_WITH_TLS_TYPE>>(wss)->next_layer().native_handle(), host.c_str())) |
183 | | - { |
184 | | - BOOST_LOG_SEV(*log, trace) << "SSL next_layer() failed to set SNI"; |
185 | | - } |
186 | | - else |
187 | | - { |
188 | | - BOOST_LOG_SEV(*log, trace) << "SSL next_layer() SNI is set : " |
189 | | - << host; |
190 | | - } |
191 | | - return boost::get<unique_ptr<WEB_PROXY_WITH_TLS_TYPE>>(wss)->next_layer().async_handshake(type, handler); |
192 | | - } else { |
193 | | - BOOST_LOG_SEV(*log, trace) << "Calling next_layer().async_handshake with type: " |
194 | | - << WEB_PROXY_NO_TLS_TYPE_NAME; |
195 | | - // Set SNI Hostname (many hosts need this to handshake successfully) |
196 | | - if(!SSL_set_tlsext_host_name(boost::get<unique_ptr<WEB_PROXY_NO_TLS_TYPE>>(wss)->next_layer().native_handle(), host.c_str())) |
197 | | - { |
198 | | - BOOST_LOG_SEV(*log, trace) << "SSL next_layer() failed to set SNI"; |
199 | | - } |
200 | | - else |
201 | | - { |
202 | | - BOOST_LOG_SEV(*log, trace) << "SSL next_layer() SNI is set : " |
203 | | - << host; |
| 175 | + auto retry_count = std::make_shared<int>(0); |
| 176 | + auto retry_limit = 3; |
| 177 | + auto retry_delay = std::chrono::seconds(1); |
| 178 | + |
| 179 | + auto perform_handshake = [this, type, host, handler, retry_count, retry_limit, retry_delay]() { |
| 180 | + if (localproxyConfig.is_web_proxy_using_tls) { |
| 181 | + BOOST_LOG_SEV(*log, trace) << "Calling next_layer().async_handshake with type: " |
| 182 | + << WEB_PROXY_WITH_TLS_TYPE_NAME; |
| 183 | + // Set SNI Hostname (many hosts need this to handshake successfully) |
| 184 | + if(!SSL_set_tlsext_host_name(boost::get<unique_ptr<WEB_PROXY_WITH_TLS_TYPE>>(wss)->next_layer().native_handle(), host.c_str())) |
| 185 | + { |
| 186 | + BOOST_LOG_SEV(*log, trace) << "SSL next_layer() failed to set SNI"; |
| 187 | + } |
| 188 | + else |
| 189 | + { |
| 190 | + BOOST_LOG_SEV(*log, trace) << "SSL next_layer() SNI is set : " |
| 191 | + << host; |
| 192 | + } |
| 193 | + boost::get<unique_ptr<WEB_PROXY_WITH_TLS_TYPE>>(wss)->next_layer().async_handshake(type, [this, handler, retry_count, retry_limit, retry_delay](const boost::system::error_code& ec) { |
| 194 | + if (ec) { |
| 195 | + BOOST_LOG_SEV(*log, error) << "SSL handshake failed: " << ec.message(); |
| 196 | + if (*retry_count < retry_limit) { |
| 197 | + (*retry_count)++; |
| 198 | + BOOST_LOG_SEV(*log, warning) << "Retrying SSL handshake (" << *retry_count << "/" << retry_limit << ")..."; |
| 199 | + boost::asio::steady_timer timer(io_context, retry_delay); |
| 200 | + timer.async_wait([this, handler](const boost::system::error_code&) { |
| 201 | + perform_handshake(); |
| 202 | + }); |
| 203 | + } else { |
| 204 | + BOOST_LOG_SEV(*log, error) << "SSL handshake failed after " << retry_limit << " attempts."; |
| 205 | + handler(ec); |
| 206 | + } |
| 207 | + } else { |
| 208 | + handler(ec); |
| 209 | + } |
| 210 | + }); |
| 211 | + } else { |
| 212 | + BOOST_LOG_SEV(*log, trace) << "Calling next_layer().async_handshake with type: " |
| 213 | + << WEB_PROXY_NO_TLS_TYPE_NAME; |
| 214 | + // Set SNI Hostname (many hosts need this to handshake successfully) |
| 215 | + if(!SSL_set_tlsext_host_name(boost::get<unique_ptr<WEB_PROXY_NO_TLS_TYPE>>(wss)->next_layer().native_handle(), host.c_str())) |
| 216 | + { |
| 217 | + BOOST_LOG_SEV(*log, trace) << "SSL next_layer() failed to set SNI"; |
| 218 | + } |
| 219 | + else |
| 220 | + { |
| 221 | + BOOST_LOG_SEV(*log, trace) << "SSL next_layer() SNI is set : " |
| 222 | + << host; |
| 223 | + } |
| 224 | + boost::get<unique_ptr<WEB_PROXY_NO_TLS_TYPE>>(wss)->next_layer().async_handshake(type, [this, handler, retry_count, retry_limit, retry_delay](const boost::system::error_code& ec) { |
| 225 | + if (ec) { |
| 226 | + BOOST_LOG_SEV(*log, error) << "SSL handshake failed: " << ec.message(); |
| 227 | + if (*retry_count < retry_limit) { |
| 228 | + (*retry_count)++; |
| 229 | + BOOST_LOG_SEV(*log, warning) << "Retrying SSL handshake (" << *retry_count << "/" << retry_limit << ")..."; |
| 230 | + boost::asio::steady_timer timer(io_context, retry_delay); |
| 231 | + timer.async_wait([this, handler](const boost::system::error_code&) { |
| 232 | + perform_handshake(); |
| 233 | + }); |
| 234 | + } else { |
| 235 | + BOOST_LOG_SEV(*log, error) << "SSL handshake failed after " << retry_limit << " attempts."; |
| 236 | + handler(ec); |
| 237 | + } |
| 238 | + } else { |
| 239 | + handler(ec); |
| 240 | + } |
| 241 | + }); |
204 | 242 | } |
205 | | - return boost::get<unique_ptr<WEB_PROXY_NO_TLS_TYPE>>(wss)->next_layer().async_handshake(type, handler); |
206 | | - } |
| 243 | + }; |
| 244 | + |
| 245 | + perform_handshake(); |
207 | 246 | } |
208 | 247 | #endif |
209 | 248 |
|
|
0 commit comments