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