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" |
| 5 | #include "timer_queue.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 | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 11 | #include <boost/asio/steady_timer.hpp> |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 12 | #include <boost/beast/ssl/ssl_stream.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 13 | #include <boost/date_time/posix_time/posix_time.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 14 | #include <ssl_key_handler.hpp> |
| 15 | |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 16 | #include <atomic> |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 17 | #include <chrono> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 18 | #include <cstdint> |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 19 | #include <filesystem> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 20 | #include <future> |
| 21 | #include <memory> |
| 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 | { |
| 31 | public: |
Ed Tanous | 23e6420 | 2020-09-15 19:21:30 -0700 | [diff] [blame] | 32 | Server(Handler* handlerIn, |
| 33 | std::unique_ptr<boost::asio::ip::tcp::acceptor>&& acceptorIn, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 34 | std::shared_ptr<boost::asio::ssl::context> adaptorCtx, |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 35 | std::shared_ptr<boost::asio::io_context> io = |
| 36 | std::make_shared<boost::asio::io_context>()) : |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 37 | ioService(std::move(io)), |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 38 | acceptor(std::move(acceptorIn)), |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 39 | signals(*ioService, SIGINT, SIGTERM, SIGHUP), timer(*ioService), |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 40 | handler(handlerIn), adaptorCtx(std::move(adaptorCtx)) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 41 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 42 | |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 43 | Server(Handler* handlerIn, const std::string& bindaddr, uint16_t port, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 44 | const std::shared_ptr<boost::asio::ssl::context>& adaptorCtx, |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 45 | const std::shared_ptr<boost::asio::io_context>& io = |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 46 | std::make_shared<boost::asio::io_context>()) : |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 47 | Server(handlerIn, |
Ed Tanous | 23e6420 | 2020-09-15 19:21:30 -0700 | [diff] [blame] | 48 | std::make_unique<boost::asio::ip::tcp::acceptor>( |
| 49 | *io, boost::asio::ip::tcp::endpoint( |
| 50 | boost::asio::ip::make_address(bindaddr), port)), |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 51 | adaptorCtx, io) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 52 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 53 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 54 | Server(Handler* handlerIn, int existingSocket, |
| 55 | const std::shared_ptr<boost::asio::ssl::context>& adaptorCtx, |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 56 | const std::shared_ptr<boost::asio::io_context>& io = |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 57 | std::make_shared<boost::asio::io_context>()) : |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 58 | Server(handlerIn, |
Ed Tanous | 23e6420 | 2020-09-15 19:21:30 -0700 | [diff] [blame] | 59 | std::make_unique<boost::asio::ip::tcp::acceptor>( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 60 | *io, boost::asio::ip::tcp::v6(), existingSocket), |
| 61 | adaptorCtx, io) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 62 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 63 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | void updateDateStr() |
| 65 | { |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 66 | time_t lastTimeT = time(nullptr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 67 | tm myTm{}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 68 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 69 | gmtime_r(&lastTimeT, &myTm); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 70 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 71 | dateStr.resize(100); |
| 72 | size_t dateStrSz = |
| 73 | strftime(&dateStr[0], 99, "%a, %d %b %Y %H:%M:%S GMT", &myTm); |
| 74 | dateStr.resize(dateStrSz); |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 75 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 76 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 77 | void run() |
| 78 | { |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 79 | loadCertificate(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 80 | updateDateStr(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 81 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 82 | getCachedDateStr = [this]() -> std::string { |
| 83 | static std::chrono::time_point<std::chrono::steady_clock> |
| 84 | lastDateUpdate = std::chrono::steady_clock::now(); |
| 85 | if (std::chrono::steady_clock::now() - lastDateUpdate >= |
| 86 | std::chrono::seconds(10)) |
| 87 | { |
| 88 | lastDateUpdate = std::chrono::steady_clock::now(); |
| 89 | updateDateStr(); |
| 90 | } |
| 91 | return this->dateStr; |
| 92 | }; |
Ed Tanous | 9e6e1b2 | 2018-03-16 13:08:50 -0700 | [diff] [blame] | 93 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 94 | timer.expires_after(std::chrono::seconds(1)); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 95 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 96 | timerHandler = [this](const boost::system::error_code& ec) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 97 | if (ec) |
| 98 | { |
| 99 | return; |
| 100 | } |
| 101 | timerQueue.process(); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 102 | timer.expires_after(std::chrono::seconds(1)); |
Marri Devender Rao | 92e07bf | 2019-04-04 01:33:34 -0500 | [diff] [blame] | 103 | timer.async_wait(timerHandler); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 104 | }; |
Marri Devender Rao | 92e07bf | 2019-04-04 01:33:34 -0500 | [diff] [blame] | 105 | timer.async_wait(timerHandler); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 106 | |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 107 | BMCWEB_LOG_INFO << "bmcweb server is running, local endpoint " |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 108 | << acceptor->local_endpoint(); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 109 | startAsyncWaitForSignal(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 110 | doAccept(); |
| 111 | } |
| 112 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 113 | void loadCertificate() |
| 114 | { |
| 115 | #ifdef BMCWEB_ENABLE_SSL |
| 116 | namespace fs = std::filesystem; |
| 117 | // Cleanup older certificate file existing in the system |
| 118 | fs::path oldCert = "/home/root/server.pem"; |
| 119 | if (fs::exists(oldCert)) |
| 120 | { |
| 121 | fs::remove("/home/root/server.pem"); |
| 122 | } |
| 123 | fs::path certPath = "/etc/ssl/certs/https/"; |
| 124 | // if path does not exist create the path so that |
| 125 | // self signed certificate can be created in the |
| 126 | // path |
| 127 | if (!fs::exists(certPath)) |
| 128 | { |
| 129 | fs::create_directories(certPath); |
| 130 | } |
| 131 | fs::path certFile = certPath / "server.pem"; |
| 132 | BMCWEB_LOG_INFO << "Building SSL Context file=" << certFile; |
| 133 | std::string sslPemFile(certFile); |
| 134 | ensuressl::ensureOpensslKeyPresentAndValid(sslPemFile); |
| 135 | std::shared_ptr<boost::asio::ssl::context> sslContext = |
| 136 | ensuressl::getSslContext(sslPemFile); |
| 137 | adaptorCtx = sslContext; |
| 138 | handler->ssl(std::move(sslContext)); |
| 139 | #endif |
| 140 | } |
| 141 | |
| 142 | void startAsyncWaitForSignal() |
| 143 | { |
| 144 | signals.async_wait([this](const boost::system::error_code& ec, |
| 145 | int signalNo) { |
| 146 | if (ec) |
| 147 | { |
| 148 | BMCWEB_LOG_INFO << "Error in signal handler" << ec.message(); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | if (signalNo == SIGHUP) |
| 153 | { |
| 154 | BMCWEB_LOG_INFO << "Receivied reload signal"; |
| 155 | loadCertificate(); |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 156 | boost::system::error_code ec2; |
| 157 | acceptor->cancel(ec2); |
| 158 | if (ec2) |
Zbigniew Lukwinski | 7d0120b | 2019-10-15 09:12:45 +0200 | [diff] [blame] | 159 | { |
| 160 | BMCWEB_LOG_ERROR |
| 161 | << "Error while canceling async operations:" |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 162 | << ec2.message(); |
Zbigniew Lukwinski | 7d0120b | 2019-10-15 09:12:45 +0200 | [diff] [blame] | 163 | } |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 164 | this->startAsyncWaitForSignal(); |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | stop(); |
| 169 | } |
| 170 | } |
| 171 | }); |
| 172 | } |
| 173 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 174 | void stop() |
| 175 | { |
| 176 | ioService->stop(); |
| 177 | } |
| 178 | |
| 179 | void doAccept() |
| 180 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 181 | std::optional<Adaptor> adaptorTemp; |
| 182 | if constexpr (std::is_same<Adaptor, |
| 183 | boost::beast::ssl_stream< |
| 184 | boost::asio::ip::tcp::socket>>::value) |
| 185 | { |
| 186 | adaptorTemp = Adaptor(*ioService, *adaptorCtx); |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 187 | auto p = std::make_shared<Connection<Adaptor, Handler>>( |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 188 | handler, getCachedDateStr, timerQueue, |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 189 | std::move(adaptorTemp.value())); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 190 | |
| 191 | acceptor->async_accept(p->socket().next_layer(), |
| 192 | [this, p](boost::system::error_code ec) { |
| 193 | if (!ec) |
| 194 | { |
| 195 | boost::asio::post( |
| 196 | *this->ioService, |
| 197 | [p] { p->start(); }); |
| 198 | } |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 199 | doAccept(); |
| 200 | }); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 201 | } |
| 202 | else |
| 203 | { |
| 204 | adaptorTemp = Adaptor(*ioService); |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 205 | auto p = std::make_shared<Connection<Adaptor, Handler>>( |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 206 | handler, getCachedDateStr, timerQueue, |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 207 | std::move(adaptorTemp.value())); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 208 | |
| 209 | acceptor->async_accept( |
| 210 | p->socket(), [this, p](boost::system::error_code ec) { |
| 211 | if (!ec) |
| 212 | { |
| 213 | boost::asio::post(*this->ioService, |
| 214 | [p] { p->start(); }); |
| 215 | } |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 216 | doAccept(); |
| 217 | }); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 218 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | private: |
Ed Tanous | 23e6420 | 2020-09-15 19:21:30 -0700 | [diff] [blame] | 222 | std::shared_ptr<boost::asio::io_context> ioService; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 223 | detail::TimerQueue timerQueue; |
| 224 | std::function<std::string()> getCachedDateStr; |
Ed Tanous | 23e6420 | 2020-09-15 19:21:30 -0700 | [diff] [blame] | 225 | std::unique_ptr<boost::asio::ip::tcp::acceptor> acceptor; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 226 | boost::asio::signal_set signals; |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 227 | boost::asio::steady_timer timer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 228 | |
| 229 | std::string dateStr; |
| 230 | |
| 231 | Handler* handler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 232 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 233 | std::function<void(const boost::system::error_code& ec)> timerHandler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 234 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 235 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 236 | bool useSsl{false}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 237 | #endif |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 238 | std::shared_ptr<boost::asio::ssl::context> adaptorCtx; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 239 | }; // namespace crow |
| 240 | } // namespace crow |