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