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