blob: 20b4e5084a06964ce247657f32ad6795236de5b3 [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
Ed Tanous04e438c2020-10-03 08:06:26 -07003#include "http_connection.hpp"
4#include "logging.hpp"
5#include "timer_queue.hpp"
Manojkiran Eda44250442020-06-16 12:51:38 +05306
Ed Tanouse278c182019-03-13 16:23:37 -07007#include <boost/asio/ip/address.hpp>
Ed Tanous3112a142018-11-29 15:45:10 -08008#include <boost/asio/ip/tcp.hpp>
9#include <boost/asio/signal_set.hpp>
Ed Tanous2f1ebcd2019-02-13 19:39:07 -080010#include <boost/asio/ssl/context.hpp>
Ed Tanous271584a2019-07-09 16:24:22 -070011#include <boost/asio/steady_timer.hpp>
Ed Tanouse278c182019-03-13 16:23:37 -070012#include <boost/beast/ssl/ssl_stream.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070013#include <boost/date_time/posix_time/posix_time.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050014#include <ssl_key_handler.hpp>
15
Manojkiran Eda44250442020-06-16 12:51:38 +053016#include <atomic>
Ed Tanous3dac7492017-08-02 13:46:20 -070017#include <chrono>
Ed Tanous911ac312017-08-15 09:37:42 -070018#include <cstdint>
Marri Devender Rao5968cae2019-01-21 10:27:12 -060019#include <filesystem>
Ed Tanous911ac312017-08-15 09:37:42 -070020#include <future>
21#include <memory>
22#include <utility>
23#include <vector>
Ed Tanous1abe55e2018-09-05 08:30:59 -070024
Ed Tanous1abe55e2018-09-05 08:30:59 -070025namespace crow
26{
Ed Tanous7045c8d2017-04-03 10:04:37 -070027
Ed Tanous52cc1122020-07-18 13:51:21 -070028template <typename Handler, typename Adaptor = boost::asio::ip::tcp::socket>
Ed Tanous1abe55e2018-09-05 08:30:59 -070029class Server
30{
31 public:
Ed Tanous23e64202020-09-15 19:21:30 -070032 Server(Handler* handlerIn,
33 std::unique_ptr<boost::asio::ip::tcp::acceptor>&& acceptorIn,
Ed Tanous81ce6092020-12-17 16:54:55 +000034 std::shared_ptr<boost::asio::ssl::context> adaptorCtx,
Ed Tanous8f626352018-12-19 14:51:54 -080035 std::shared_ptr<boost::asio::io_context> io =
36 std::make_shared<boost::asio::io_context>()) :
Ed Tanous1abe55e2018-09-05 08:30:59 -070037 ioService(std::move(io)),
Ed Tanouscb13a392020-07-25 19:02:03 +000038 acceptor(std::move(acceptorIn)),
Ed Tanousb74e4402020-09-09 20:26:26 -070039 signals(*ioService, SIGINT, SIGTERM, SIGHUP), timer(*ioService),
Ed Tanous81ce6092020-12-17 16:54:55 +000040 handler(handlerIn), adaptorCtx(std::move(adaptorCtx))
Gunnar Mills1214b7e2020-06-04 10:11:30 -050041 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070042
Ed Tanouscb13a392020-07-25 19:02:03 +000043 Server(Handler* handlerIn, const std::string& bindaddr, uint16_t port,
Ed Tanous81ce6092020-12-17 16:54:55 +000044 const std::shared_ptr<boost::asio::ssl::context>& adaptorCtx,
Ed Tanousb5a76932020-09-29 16:16:58 -070045 const std::shared_ptr<boost::asio::io_context>& io =
Ed Tanous8f626352018-12-19 14:51:54 -080046 std::make_shared<boost::asio::io_context>()) :
Ed Tanouscb13a392020-07-25 19:02:03 +000047 Server(handlerIn,
Ed Tanous23e64202020-09-15 19:21:30 -070048 std::make_unique<boost::asio::ip::tcp::acceptor>(
49 *io, boost::asio::ip::tcp::endpoint(
50 boost::asio::ip::make_address(bindaddr), port)),
Ed Tanous81ce6092020-12-17 16:54:55 +000051 adaptorCtx, io)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050052 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070053
Ed Tanous81ce6092020-12-17 16:54:55 +000054 Server(Handler* handlerIn, int existingSocket,
55 const std::shared_ptr<boost::asio::ssl::context>& adaptorCtx,
Ed Tanousb5a76932020-09-29 16:16:58 -070056 const std::shared_ptr<boost::asio::io_context>& io =
Ed Tanous8f626352018-12-19 14:51:54 -080057 std::make_shared<boost::asio::io_context>()) :
Ed Tanouscb13a392020-07-25 19:02:03 +000058 Server(handlerIn,
Ed Tanous23e64202020-09-15 19:21:30 -070059 std::make_unique<boost::asio::ip::tcp::acceptor>(
Ed Tanous81ce6092020-12-17 16:54:55 +000060 *io, boost::asio::ip::tcp::v6(), existingSocket),
61 adaptorCtx, io)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050062 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070063
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 void updateDateStr()
65 {
Ed Tanous99131cd2019-10-24 11:12:47 -070066 time_t lastTimeT = time(nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 tm myTm{};
Ed Tanous7045c8d2017-04-03 10:04:37 -070068
Ed Tanous1abe55e2018-09-05 08:30:59 -070069 gmtime_r(&lastTimeT, &myTm);
Ed Tanous271584a2019-07-09 16:24:22 -070070
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 dateStr.resize(100);
72 size_t dateStrSz =
73 strftime(&dateStr[0], 99, "%a, %d %b %Y %H:%M:%S GMT", &myTm);
74 dateStr.resize(dateStrSz);
Ed Tanous23a21a12020-07-25 04:45:05 +000075 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070076
Ed Tanous1abe55e2018-09-05 08:30:59 -070077 void run()
78 {
Marri Devender Rao5968cae2019-01-21 10:27:12 -060079 loadCertificate();
Ed Tanous1abe55e2018-09-05 08:30:59 -070080 updateDateStr();
Ed Tanous7045c8d2017-04-03 10:04:37 -070081
Ed Tanous1abe55e2018-09-05 08:30:59 -070082 getCachedDateStr = [this]() -> std::string {
83 static std::chrono::time_point<std::chrono::steady_clock>
84 lastDateUpdate = std::chrono::steady_clock::now();
85 if (std::chrono::steady_clock::now() - lastDateUpdate >=
86 std::chrono::seconds(10))
87 {
88 lastDateUpdate = std::chrono::steady_clock::now();
89 updateDateStr();
90 }
91 return this->dateStr;
92 };
Ed Tanous9e6e1b22018-03-16 13:08:50 -070093
Ed Tanous271584a2019-07-09 16:24:22 -070094 timer.expires_after(std::chrono::seconds(1));
Ed Tanous7045c8d2017-04-03 10:04:37 -070095
Jan Sowinskiee52ae12020-01-09 16:28:32 +000096 timerHandler = [this](const boost::system::error_code& ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -070097 if (ec)
98 {
99 return;
100 }
101 timerQueue.process();
Ed Tanous271584a2019-07-09 16:24:22 -0700102 timer.expires_after(std::chrono::seconds(1));
Marri Devender Rao92e07bf2019-04-04 01:33:34 -0500103 timer.async_wait(timerHandler);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104 };
Marri Devender Rao92e07bf2019-04-04 01:33:34 -0500105 timer.async_wait(timerHandler);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700106
Ed Tanouse7d1a1c2020-09-28 09:36:35 -0700107 BMCWEB_LOG_INFO << "bmcweb server is running, local endpoint "
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 << acceptor->local_endpoint();
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600109 startAsyncWaitForSignal();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110 doAccept();
111 }
112
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600113 void loadCertificate()
114 {
115#ifdef BMCWEB_ENABLE_SSL
116 namespace fs = std::filesystem;
117 // Cleanup older certificate file existing in the system
118 fs::path oldCert = "/home/root/server.pem";
119 if (fs::exists(oldCert))
120 {
121 fs::remove("/home/root/server.pem");
122 }
123 fs::path certPath = "/etc/ssl/certs/https/";
124 // if path does not exist create the path so that
125 // self signed certificate can be created in the
126 // path
127 if (!fs::exists(certPath))
128 {
129 fs::create_directories(certPath);
130 }
131 fs::path certFile = certPath / "server.pem";
132 BMCWEB_LOG_INFO << "Building SSL Context file=" << certFile;
133 std::string sslPemFile(certFile);
134 ensuressl::ensureOpensslKeyPresentAndValid(sslPemFile);
135 std::shared_ptr<boost::asio::ssl::context> sslContext =
136 ensuressl::getSslContext(sslPemFile);
137 adaptorCtx = sslContext;
138 handler->ssl(std::move(sslContext));
139#endif
140 }
141
142 void startAsyncWaitForSignal()
143 {
144 signals.async_wait([this](const boost::system::error_code& ec,
145 int signalNo) {
146 if (ec)
147 {
148 BMCWEB_LOG_INFO << "Error in signal handler" << ec.message();
149 }
150 else
151 {
152 if (signalNo == SIGHUP)
153 {
154 BMCWEB_LOG_INFO << "Receivied reload signal";
155 loadCertificate();
Ed Tanouscb13a392020-07-25 19:02:03 +0000156 boost::system::error_code ec2;
157 acceptor->cancel(ec2);
158 if (ec2)
Zbigniew Lukwinski7d0120b2019-10-15 09:12:45 +0200159 {
160 BMCWEB_LOG_ERROR
161 << "Error while canceling async operations:"
Ed Tanouscb13a392020-07-25 19:02:03 +0000162 << ec2.message();
Zbigniew Lukwinski7d0120b2019-10-15 09:12:45 +0200163 }
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600164 this->startAsyncWaitForSignal();
165 }
166 else
167 {
168 stop();
169 }
170 }
171 });
172 }
173
Ed Tanous1abe55e2018-09-05 08:30:59 -0700174 void stop()
175 {
176 ioService->stop();
177 }
178
179 void doAccept()
180 {
Ed Tanousceac6f72018-12-02 11:58:47 -0800181 std::optional<Adaptor> adaptorTemp;
182 if constexpr (std::is_same<Adaptor,
183 boost::beast::ssl_stream<
184 boost::asio::ip::tcp::socket>>::value)
185 {
186 adaptorTemp = Adaptor(*ioService, *adaptorCtx);
Ed Tanous52cc1122020-07-18 13:51:21 -0700187 auto p = std::make_shared<Connection<Adaptor, Handler>>(
Ed Tanouse7d1a1c2020-09-28 09:36:35 -0700188 handler, getCachedDateStr, timerQueue,
Ed Tanous52cc1122020-07-18 13:51:21 -0700189 std::move(adaptorTemp.value()));
Ed Tanouse278c182019-03-13 16:23:37 -0700190
191 acceptor->async_accept(p->socket().next_layer(),
192 [this, p](boost::system::error_code ec) {
193 if (!ec)
194 {
195 boost::asio::post(
196 *this->ioService,
197 [p] { p->start(); });
198 }
Ed Tanouse278c182019-03-13 16:23:37 -0700199 doAccept();
200 });
Ed Tanousceac6f72018-12-02 11:58:47 -0800201 }
202 else
203 {
204 adaptorTemp = Adaptor(*ioService);
Ed Tanous52cc1122020-07-18 13:51:21 -0700205 auto p = std::make_shared<Connection<Adaptor, Handler>>(
Ed Tanouse7d1a1c2020-09-28 09:36:35 -0700206 handler, getCachedDateStr, timerQueue,
Ed Tanous52cc1122020-07-18 13:51:21 -0700207 std::move(adaptorTemp.value()));
Ed Tanouse278c182019-03-13 16:23:37 -0700208
209 acceptor->async_accept(
210 p->socket(), [this, p](boost::system::error_code ec) {
211 if (!ec)
212 {
213 boost::asio::post(*this->ioService,
214 [p] { p->start(); });
215 }
Ed Tanouse278c182019-03-13 16:23:37 -0700216 doAccept();
217 });
Ed Tanousceac6f72018-12-02 11:58:47 -0800218 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219 }
220
221 private:
Ed Tanous23e64202020-09-15 19:21:30 -0700222 std::shared_ptr<boost::asio::io_context> ioService;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700223 detail::TimerQueue timerQueue;
224 std::function<std::string()> getCachedDateStr;
Ed Tanous23e64202020-09-15 19:21:30 -0700225 std::unique_ptr<boost::asio::ip::tcp::acceptor> acceptor;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700226 boost::asio::signal_set signals;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000227 boost::asio::steady_timer timer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700228
229 std::string dateStr;
230
231 Handler* handler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000233 std::function<void(const boost::system::error_code& ec)> timerHandler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700234
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700235#ifdef BMCWEB_ENABLE_SSL
Ed Tanous1abe55e2018-09-05 08:30:59 -0700236 bool useSsl{false};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700237#endif
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600238 std::shared_ptr<boost::asio::ssl::context> adaptorCtx;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700239}; // namespace crow
240} // namespace crow