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), |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 47 | handler(handler), middlewares(middlewares), adaptorCtx(adaptor_ctx) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 48 | { |
| 49 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 50 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 51 | Server(Handler* handler, const std::string& bindaddr, uint16_t port, |
Jason M. Bills | e3e2961 | 2019-09-13 08:05:13 -0700 | [diff] [blame] | 52 | std::shared_ptr<boost::asio::ssl::context> adaptor_ctx, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 53 | std::tuple<Middlewares...>* middlewares = nullptr, |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 54 | std::shared_ptr<boost::asio::io_context> io = |
| 55 | std::make_shared<boost::asio::io_context>()) : |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 56 | Server(handler, |
Ed Tanous | 9e6e1b2 | 2018-03-16 13:08:50 -0700 | [diff] [blame] | 57 | std::make_unique<tcp::acceptor>( |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 58 | *io, tcp::endpoint(boost::asio::ip::make_address(bindaddr), |
| 59 | port)), |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 60 | adaptor_ctx, middlewares, io) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 61 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | Server(Handler* handler, int existing_socket, |
Jason M. Bills | e3e2961 | 2019-09-13 08:05:13 -0700 | [diff] [blame] | 65 | std::shared_ptr<boost::asio::ssl::context> adaptor_ctx, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | std::tuple<Middlewares...>* middlewares = nullptr, |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 67 | std::shared_ptr<boost::asio::io_context> io = |
| 68 | std::make_shared<boost::asio::io_context>()) : |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 69 | Server(handler, |
| 70 | std::make_unique<tcp::acceptor>(*io, boost::asio::ip::tcp::v6(), |
| 71 | existing_socket), |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 72 | adaptor_ctx, middlewares, io) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 73 | { |
| 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 | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 98 | time_t lastTimeT = time(0); |
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 | boost::asio::steady_timer timer(*ioService); |
| 127 | timer.expires_after(std::chrono::seconds(1)); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 128 | |
Marri Devender Rao | 92e07bf | 2019-04-04 01:33:34 -0500 | [diff] [blame] | 129 | std::function<void(const boost::system::error_code& ec)> timerHandler; |
| 130 | timerHandler = [&](const boost::system::error_code& ec) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 131 | if (ec) |
| 132 | { |
| 133 | return; |
| 134 | } |
| 135 | timerQueue.process(); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 136 | timer.expires_after(std::chrono::seconds(1)); |
Marri Devender Rao | 92e07bf | 2019-04-04 01:33:34 -0500 | [diff] [blame] | 137 | timer.async_wait(timerHandler); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 138 | }; |
Marri Devender Rao | 92e07bf | 2019-04-04 01:33:34 -0500 | [diff] [blame] | 139 | timer.async_wait(timerHandler); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 140 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 141 | if (tickFunction && tickInterval.count() > 0) |
| 142 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 143 | tickTimer.expires_after( |
| 144 | std::chrono::milliseconds(tickInterval.count())); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 145 | tickTimer.async_wait([this](const boost::system::error_code& ec) { |
| 146 | if (ec) |
| 147 | { |
| 148 | return; |
| 149 | } |
| 150 | onTick(); |
| 151 | }); |
| 152 | } |
| 153 | |
| 154 | BMCWEB_LOG_INFO << serverName << " server is running, local endpoint " |
| 155 | << acceptor->local_endpoint(); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 156 | startAsyncWaitForSignal(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 157 | doAccept(); |
| 158 | } |
| 159 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 160 | void loadCertificate() |
| 161 | { |
| 162 | #ifdef BMCWEB_ENABLE_SSL |
| 163 | namespace fs = std::filesystem; |
| 164 | // Cleanup older certificate file existing in the system |
| 165 | fs::path oldCert = "/home/root/server.pem"; |
| 166 | if (fs::exists(oldCert)) |
| 167 | { |
| 168 | fs::remove("/home/root/server.pem"); |
| 169 | } |
| 170 | fs::path certPath = "/etc/ssl/certs/https/"; |
| 171 | // if path does not exist create the path so that |
| 172 | // self signed certificate can be created in the |
| 173 | // path |
| 174 | if (!fs::exists(certPath)) |
| 175 | { |
| 176 | fs::create_directories(certPath); |
| 177 | } |
| 178 | fs::path certFile = certPath / "server.pem"; |
| 179 | BMCWEB_LOG_INFO << "Building SSL Context file=" << certFile; |
| 180 | std::string sslPemFile(certFile); |
| 181 | ensuressl::ensureOpensslKeyPresentAndValid(sslPemFile); |
| 182 | std::shared_ptr<boost::asio::ssl::context> sslContext = |
| 183 | ensuressl::getSslContext(sslPemFile); |
| 184 | adaptorCtx = sslContext; |
| 185 | handler->ssl(std::move(sslContext)); |
| 186 | #endif |
| 187 | } |
| 188 | |
| 189 | void startAsyncWaitForSignal() |
| 190 | { |
| 191 | signals.async_wait([this](const boost::system::error_code& ec, |
| 192 | int signalNo) { |
| 193 | if (ec) |
| 194 | { |
| 195 | BMCWEB_LOG_INFO << "Error in signal handler" << ec.message(); |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | if (signalNo == SIGHUP) |
| 200 | { |
| 201 | BMCWEB_LOG_INFO << "Receivied reload signal"; |
| 202 | loadCertificate(); |
| 203 | this->startAsyncWaitForSignal(); |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | stop(); |
| 208 | } |
| 209 | } |
| 210 | }); |
| 211 | } |
| 212 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 213 | void stop() |
| 214 | { |
| 215 | ioService->stop(); |
| 216 | } |
| 217 | |
| 218 | void doAccept() |
| 219 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 220 | std::optional<Adaptor> adaptorTemp; |
| 221 | if constexpr (std::is_same<Adaptor, |
| 222 | boost::beast::ssl_stream< |
| 223 | boost::asio::ip::tcp::socket>>::value) |
| 224 | { |
| 225 | adaptorTemp = Adaptor(*ioService, *adaptorCtx); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 226 | Connection<Adaptor, Handler, Middlewares...>* p = |
| 227 | new Connection<Adaptor, Handler, Middlewares...>( |
| 228 | *ioService, handler, serverName, middlewares, |
| 229 | getCachedDateStr, timerQueue, |
| 230 | std::move(adaptorTemp.value())); |
| 231 | |
| 232 | acceptor->async_accept(p->socket().next_layer(), |
| 233 | [this, p](boost::system::error_code ec) { |
| 234 | if (!ec) |
| 235 | { |
| 236 | boost::asio::post( |
| 237 | *this->ioService, |
| 238 | [p] { p->start(); }); |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | delete p; |
| 243 | } |
| 244 | doAccept(); |
| 245 | }); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 246 | } |
| 247 | else |
| 248 | { |
| 249 | adaptorTemp = Adaptor(*ioService); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 250 | Connection<Adaptor, Handler, Middlewares...>* p = |
| 251 | new Connection<Adaptor, Handler, Middlewares...>( |
| 252 | *ioService, handler, serverName, middlewares, |
| 253 | getCachedDateStr, timerQueue, |
| 254 | std::move(adaptorTemp.value())); |
| 255 | |
| 256 | acceptor->async_accept( |
| 257 | p->socket(), [this, p](boost::system::error_code ec) { |
| 258 | if (!ec) |
| 259 | { |
| 260 | boost::asio::post(*this->ioService, |
| 261 | [p] { p->start(); }); |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | delete p; |
| 266 | } |
| 267 | doAccept(); |
| 268 | }); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 269 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | private: |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 273 | std::shared_ptr<asio::io_context> ioService; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 274 | detail::TimerQueue timerQueue; |
| 275 | std::function<std::string()> getCachedDateStr; |
| 276 | std::unique_ptr<tcp::acceptor> acceptor; |
| 277 | boost::asio::signal_set signals; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 278 | boost::asio::steady_timer tickTimer; |
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; |
| 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 |