Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 3 | #include "http_connection.hpp" |
| 4 | #include "logging.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 5 | #include "ssl_key_handler.hpp" |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 6 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 7 | #include <boost/asio/ip/address.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 8 | #include <boost/asio/ip/tcp.hpp> |
| 9 | #include <boost/asio/signal_set.hpp> |
Ed Tanous | 2f1ebcd | 2019-02-13 19:39:07 -0800 | [diff] [blame] | 10 | #include <boost/asio/ssl/context.hpp> |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 11 | #include <boost/asio/ssl/stream.hpp> |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 12 | #include <boost/asio/steady_timer.hpp> |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 13 | #include <boost/beast/core/stream_traits.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 14 | |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 15 | #include <atomic> |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 16 | #include <chrono> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 17 | #include <cstdint> |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 18 | #include <filesystem> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 19 | #include <future> |
| 20 | #include <memory> |
Ed Tanous | 099225c | 2024-03-27 22:03:05 -0700 | [diff] [blame] | 21 | #include <string> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 22 | #include <utility> |
| 23 | #include <vector> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 24 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 25 | namespace crow |
| 26 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 27 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 28 | template <typename Handler, typename Adaptor = boost::asio::ip::tcp::socket> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | class Server |
| 30 | { |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 31 | using self_t = Server<Handler, Adaptor>; |
| 32 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | public: |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 34 | Server(Handler* handlerIn, boost::asio::ip::tcp::acceptor&& acceptorIn, |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 35 | std::shared_ptr<boost::asio::ssl::context> adaptorCtxIn, |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 36 | std::shared_ptr<boost::asio::io_context> io) : |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 37 | ioService(std::move(io)), acceptor(std::move(acceptorIn)), |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 38 | signals(*ioService, SIGINT, SIGTERM, SIGHUP), handler(handlerIn), |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 39 | adaptorCtx(std::move(adaptorCtxIn)) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 40 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 41 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 42 | void updateDateStr() |
| 43 | { |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 44 | time_t lastTimeT = time(nullptr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 45 | tm myTm{}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 46 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 47 | gmtime_r(&lastTimeT, &myTm); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 48 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 49 | dateStr.resize(100); |
Ed Tanous | 0f83707 | 2023-06-30 09:57:26 -0700 | [diff] [blame] | 50 | size_t dateStrSz = strftime(dateStr.data(), dateStr.size() - 1, |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 51 | "%a, %d %b %Y %H:%M:%S GMT", &myTm); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 52 | dateStr.resize(dateStrSz); |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 53 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 54 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | void run() |
| 56 | { |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 57 | loadCertificate(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 58 | updateDateStr(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 59 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 60 | getCachedDateStr = [this]() -> std::string { |
| 61 | static std::chrono::time_point<std::chrono::steady_clock> |
| 62 | lastDateUpdate = std::chrono::steady_clock::now(); |
| 63 | if (std::chrono::steady_clock::now() - lastDateUpdate >= |
| 64 | std::chrono::seconds(10)) |
| 65 | { |
| 66 | lastDateUpdate = std::chrono::steady_clock::now(); |
| 67 | updateDateStr(); |
| 68 | } |
Ed Tanous | 21b4aba | 2023-06-05 11:42:43 -0700 | [diff] [blame] | 69 | return dateStr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | }; |
Ed Tanous | 9e6e1b2 | 2018-03-16 13:08:50 -0700 | [diff] [blame] | 71 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 72 | BMCWEB_LOG_INFO("bmcweb server is running, local endpoint {}", |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 73 | acceptor.local_endpoint().address().to_string()); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 74 | startAsyncWaitForSignal(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 75 | doAccept(); |
| 76 | } |
| 77 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 78 | void loadCertificate() |
| 79 | { |
Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 80 | if constexpr (BMCWEB_INSECURE_DISABLE_SSL) |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 81 | { |
| 82 | return; |
| 83 | } |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 84 | |
| 85 | auto sslContext = ensuressl::getSslServerContext(); |
| 86 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 87 | adaptorCtx = sslContext; |
| 88 | handler->ssl(std::move(sslContext)); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void startAsyncWaitForSignal() |
| 92 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 93 | signals.async_wait( |
| 94 | [this](const boost::system::error_code& ec, int signalNo) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 95 | if (ec) |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 96 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 97 | BMCWEB_LOG_INFO("Error in signal handler{}", ec.message()); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 98 | } |
| 99 | else |
| 100 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 101 | if (signalNo == SIGHUP) |
| 102 | { |
| 103 | BMCWEB_LOG_INFO("Receivied reload signal"); |
| 104 | loadCertificate(); |
| 105 | startAsyncWaitForSignal(); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | stop(); |
| 110 | } |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 111 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 112 | }); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 113 | } |
| 114 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 115 | void stop() |
| 116 | { |
| 117 | ioService->stop(); |
| 118 | } |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 119 | using Socket = boost::beast::lowest_layer_type<Adaptor>; |
| 120 | using SocketPtr = std::unique_ptr<Socket>; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 121 | |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 122 | void afterAccept(SocketPtr socket, const boost::system::error_code& ec) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 123 | { |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 124 | if (ec) |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 125 | { |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 126 | BMCWEB_LOG_ERROR("Failed to accept socket {}", ec); |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 127 | return; |
| 128 | } |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 129 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 130 | boost::asio::steady_timer timer(*ioService); |
Ed Tanous | 88e1612 | 2021-12-06 14:14:43 -0800 | [diff] [blame] | 131 | std::shared_ptr<Connection<Adaptor, Handler>> connection; |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 132 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 133 | if constexpr (std::is_same<Adaptor, |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 134 | boost::asio::ssl::stream< |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 135 | boost::asio::ip::tcp::socket>>::value) |
| 136 | { |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 137 | if (adaptorCtx == nullptr) |
| 138 | { |
| 139 | BMCWEB_LOG_CRITICAL( |
Abhilash Raju | d5fb584 | 2024-06-03 11:40:17 -0500 | [diff] [blame] | 140 | "Asked to launch TLS socket but no context available"); |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 141 | return; |
| 142 | } |
Ed Tanous | 88e1612 | 2021-12-06 14:14:43 -0800 | [diff] [blame] | 143 | connection = std::make_shared<Connection<Adaptor, Handler>>( |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 144 | handler, std::move(timer), getCachedDateStr, |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 145 | Adaptor(std::move(*socket), *adaptorCtx)); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 146 | } |
| 147 | else |
| 148 | { |
Ed Tanous | 88e1612 | 2021-12-06 14:14:43 -0800 | [diff] [blame] | 149 | connection = std::make_shared<Connection<Adaptor, Handler>>( |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 150 | handler, std::move(timer), getCachedDateStr, |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 151 | Adaptor(std::move(*socket))); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 152 | } |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 153 | |
| 154 | boost::asio::post(*ioService, [connection] { connection->start(); }); |
| 155 | |
| 156 | doAccept(); |
| 157 | } |
| 158 | |
| 159 | void doAccept() |
| 160 | { |
| 161 | if (ioService == nullptr) |
| 162 | { |
| 163 | BMCWEB_LOG_CRITICAL("IoService was null"); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | SocketPtr socket = std::make_unique<Socket>(*ioService); |
| 168 | // Keep a raw pointer so when the socket is moved, the pointer is still |
| 169 | // valid |
| 170 | Socket* socketPtr = socket.get(); |
| 171 | |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 172 | acceptor.async_accept( |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 173 | *socketPtr, |
| 174 | std::bind_front(&self_t::afterAccept, this, std::move(socket))); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | private: |
Ed Tanous | 23e6420 | 2020-09-15 19:21:30 -0700 | [diff] [blame] | 178 | std::shared_ptr<boost::asio::io_context> ioService; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 179 | std::function<std::string()> getCachedDateStr; |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 180 | boost::asio::ip::tcp::acceptor acceptor; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 181 | boost::asio::signal_set signals; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 182 | |
| 183 | std::string dateStr; |
| 184 | |
| 185 | Handler* handler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 186 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 187 | std::shared_ptr<boost::asio::ssl::context> adaptorCtx; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 188 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 189 | } // namespace crow |