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