* Connects to host:port and performs a TLS shandshake * * @param host To connect to. * @param port To connect to. * * @returns AsioTlsStream pointer for future HTTP connections. */
| 176 | * @returns AsioTlsStream pointer for future HTTP connections. |
| 177 | */ |
| 178 | static Shared<AsioTlsStream>::Ptr Connect(const String& host, const String& port) |
| 179 | { |
| 180 | Shared<boost::asio::ssl::context>::Ptr sslContext; |
| 181 | |
| 182 | try { |
| 183 | sslContext = MakeAsioSslContext(Empty, Empty, Empty); //TODO: Add support for cert, key, ca parameters |
| 184 | } catch(const std::exception& ex) { |
| 185 | Log(LogCritical, "DebugConsole") |
| 186 | << "Cannot make SSL context: " << ex.what(); |
| 187 | throw; |
| 188 | } |
| 189 | |
| 190 | Shared<AsioTlsStream>::Ptr stream = Shared<AsioTlsStream>::Make(IoEngine::Get().GetIoContext(), *sslContext, host); |
| 191 | |
| 192 | try { |
| 193 | icinga::Connect(stream->lowest_layer(), host, port); |
| 194 | } catch (const std::exception& ex) { |
| 195 | Log(LogWarning, "DebugConsole") |
| 196 | << "Cannot connect to REST API on host '" << host << "' port '" << port << "': " << ex.what(); |
| 197 | throw; |
| 198 | } |
| 199 | |
| 200 | auto& tlsStream (stream->next_layer()); |
| 201 | |
| 202 | try { |
| 203 | tlsStream.handshake(tlsStream.client); |
| 204 | } catch (const std::exception& ex) { |
| 205 | Log(LogWarning, "DebugConsole") |
| 206 | << "TLS handshake with host '" << host << "' failed: " << ex.what(); |
| 207 | throw; |
| 208 | } |
| 209 | |
| 210 | return stream; |
| 211 | } |
| 212 | |
| 213 | static const char l_ReasonToInject[2] = {' ', 'X'}; |
| 214 |
no test coverage detected