* Connects to host:port and performs a TLS shandshake * * @returns AsioTlsStream pointer for future HTTP connections. */
| 524 | * @returns AsioTlsStream pointer for future HTTP connections. |
| 525 | */ |
| 526 | Shared<AsioTlsStream>::Ptr ConsoleCommand::Connect() |
| 527 | { |
| 528 | Shared<boost::asio::ssl::context>::Ptr sslContext; |
| 529 | |
| 530 | try { |
| 531 | sslContext = MakeAsioSslContext(Empty, Empty, Empty); //TODO: Add support for cert, key, ca parameters |
| 532 | } catch(const std::exception& ex) { |
| 533 | Log(LogCritical, "DebugConsole") |
| 534 | << "Cannot make SSL context: " << ex.what(); |
| 535 | throw; |
| 536 | } |
| 537 | |
| 538 | String host = l_Url->GetHost(); |
| 539 | String port = l_Url->GetPort(); |
| 540 | |
| 541 | Shared<AsioTlsStream>::Ptr stream = Shared<AsioTlsStream>::Make(IoEngine::Get().GetIoContext(), *sslContext, host); |
| 542 | |
| 543 | try { |
| 544 | icinga::Connect(stream->lowest_layer(), host, port); |
| 545 | } catch (const std::exception& ex) { |
| 546 | Log(LogWarning, "DebugConsole") |
| 547 | << "Cannot connect to REST API on host '" << host << "' port '" << port << "': " << ex.what(); |
| 548 | throw; |
| 549 | } |
| 550 | |
| 551 | auto& tlsStream (stream->next_layer()); |
| 552 | |
| 553 | try { |
| 554 | tlsStream.handshake(tlsStream.client); |
| 555 | } catch (const std::exception& ex) { |
| 556 | Log(LogWarning, "DebugConsole") |
| 557 | << "TLS handshake with host '" << host << "' failed: " << ex.what(); |
| 558 | throw; |
| 559 | } |
| 560 | |
| 561 | return stream; |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Sends the request via REST API and returns the parsed response. |