blob: 50e96c22c9ff9ff07c62f105b45a20b5d0b03dac [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
Ed Tanouse278c182019-03-13 16:23:37 -07003#include <boost/asio/ip/address.hpp>
Ed Tanous3112a142018-11-29 15:45:10 -08004#include <boost/asio/ip/tcp.hpp>
5#include <boost/asio/signal_set.hpp>
Ed Tanous2f1ebcd2019-02-13 19:39:07 -08006#include <boost/asio/ssl/context.hpp>
Ed Tanous271584a2019-07-09 16:24:22 -07007#include <boost/asio/steady_timer.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05008
9#include <atomic>
Ed Tanouse278c182019-03-13 16:23:37 -070010#if BOOST_VERSION >= 107000
11#include <boost/beast/ssl/ssl_stream.hpp>
12#else
Ed Tanous2f1ebcd2019-02-13 19:39:07 -080013#include <boost/beast/experimental/core/ssl_stream.hpp>
Ed Tanouse278c182019-03-13 16:23:37 -070014#endif
15
Gunnar Mills1214b7e2020-06-04 10:11:30 -050016#include "http_connection.h"
17#include "logging.h"
18#include "timer_queue.h"
19
Ed Tanous1abe55e2018-09-05 08:30:59 -070020#include <boost/date_time/posix_time/posix_time.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050021#include <ssl_key_handler.hpp>
22
Ed Tanous3dac7492017-08-02 13:46:20 -070023#include <chrono>
Ed Tanous911ac312017-08-15 09:37:42 -070024#include <cstdint>
Marri Devender Rao5968cae2019-01-21 10:27:12 -060025#include <filesystem>
Ed Tanous911ac312017-08-15 09:37:42 -070026#include <future>
27#include <memory>
28#include <utility>
29#include <vector>
Ed Tanous1abe55e2018-09-05 08:30:59 -070030
Ed Tanous1abe55e2018-09-05 08:30:59 -070031namespace crow
32{
Ed Tanous7045c8d2017-04-03 10:04:37 -070033using namespace boost;
34using tcp = asio::ip::tcp;
35
Ed Tanousceac6f72018-12-02 11:58:47 -080036template <typename Handler, typename Adaptor = boost::asio::ip::tcp::socket,
Ed Tanous7045c8d2017-04-03 10:04:37 -070037 typename... Middlewares>
Ed Tanous1abe55e2018-09-05 08:30:59 -070038class Server
39{
40 public:
41 Server(Handler* handler, std::unique_ptr<tcp::acceptor>&& acceptor,
Jason M. Billse3e29612019-09-13 08:05:13 -070042 std::shared_ptr<boost::asio::ssl::context> adaptor_ctx,
Ed Tanous1abe55e2018-09-05 08:30:59 -070043 std::tuple<Middlewares...>* middlewares = nullptr,
Ed Tanous8f626352018-12-19 14:51:54 -080044 std::shared_ptr<boost::asio::io_context> io =
45 std::make_shared<boost::asio::io_context>()) :
Ed Tanous1abe55e2018-09-05 08:30:59 -070046 ioService(std::move(io)),
Marri Devender Rao5968cae2019-01-21 10:27:12 -060047 acceptor(std::move(acceptor)),
48 signals(*ioService, SIGINT, SIGTERM, SIGHUP), tickTimer(*ioService),
Jan Sowinskiee52ae12020-01-09 16:28:32 +000049 timer(*ioService), handler(handler), middlewares(middlewares),
50 adaptorCtx(adaptor_ctx)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050051 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070052
Ed Tanous1abe55e2018-09-05 08:30:59 -070053 Server(Handler* handler, const std::string& bindaddr, uint16_t port,
Jason M. Billse3e29612019-09-13 08:05:13 -070054 std::shared_ptr<boost::asio::ssl::context> adaptor_ctx,
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 std::tuple<Middlewares...>* middlewares = nullptr,
Ed Tanous8f626352018-12-19 14:51:54 -080056 std::shared_ptr<boost::asio::io_context> io =
57 std::make_shared<boost::asio::io_context>()) :
Ed Tanous1abe55e2018-09-05 08:30:59 -070058 Server(handler,
Ed Tanous9e6e1b22018-03-16 13:08:50 -070059 std::make_unique<tcp::acceptor>(
Ed Tanouse278c182019-03-13 16:23:37 -070060 *io, tcp::endpoint(boost::asio::ip::make_address(bindaddr),
61 port)),
Marri Devender Rao5968cae2019-01-21 10:27:12 -060062 adaptor_ctx, middlewares, io)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050063 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070064
Ed Tanous1abe55e2018-09-05 08:30:59 -070065 Server(Handler* handler, int existing_socket,
Jason M. Billse3e29612019-09-13 08:05:13 -070066 std::shared_ptr<boost::asio::ssl::context> adaptor_ctx,
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 std::tuple<Middlewares...>* middlewares = nullptr,
Ed Tanous8f626352018-12-19 14:51:54 -080068 std::shared_ptr<boost::asio::io_context> io =
69 std::make_shared<boost::asio::io_context>()) :
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 Server(handler,
71 std::make_unique<tcp::acceptor>(*io, boost::asio::ip::tcp::v6(),
72 existing_socket),
Marri Devender Rao5968cae2019-01-21 10:27:12 -060073 adaptor_ctx, middlewares, io)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050074 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070075
Ed Tanous1abe55e2018-09-05 08:30:59 -070076 void setTickFunction(std::chrono::milliseconds d, std::function<void()> f)
77 {
78 tickInterval = d;
79 tickFunction = f;
80 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070081
Ed Tanous1abe55e2018-09-05 08:30:59 -070082 void onTick()
83 {
84 tickFunction();
Ed Tanous271584a2019-07-09 16:24:22 -070085 tickTimer.expires_after(
86 std::chrono::milliseconds(tickInterval.count()));
Ed Tanous1abe55e2018-09-05 08:30:59 -070087 tickTimer.async_wait([this](const boost::system::error_code& ec) {
88 if (ec)
89 {
90 return;
91 }
92 onTick();
93 });
94 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070095
Ed Tanous1abe55e2018-09-05 08:30:59 -070096 void updateDateStr()
97 {
Ed Tanous99131cd2019-10-24 11:12:47 -070098 time_t lastTimeT = time(nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -070099 tm myTm{};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700100
Ed Tanous1abe55e2018-09-05 08:30:59 -0700101 gmtime_r(&lastTimeT, &myTm);
Ed Tanous271584a2019-07-09 16:24:22 -0700102
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 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 Tanous7045c8d2017-04-03 10:04:37 -0700108
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109 void run()
110 {
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600111 loadCertificate();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112 updateDateStr();
Ed Tanous7045c8d2017-04-03 10:04:37 -0700113
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114 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 Tanous9e6e1b22018-03-16 13:08:50 -0700125
Ed Tanous271584a2019-07-09 16:24:22 -0700126 timer.expires_after(std::chrono::seconds(1));
Ed Tanous7045c8d2017-04-03 10:04:37 -0700127
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000128 timerHandler = [this](const boost::system::error_code& ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700129 if (ec)
130 {
131 return;
132 }
133 timerQueue.process();
Ed Tanous271584a2019-07-09 16:24:22 -0700134 timer.expires_after(std::chrono::seconds(1));
Marri Devender Rao92e07bf2019-04-04 01:33:34 -0500135 timer.async_wait(timerHandler);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700136 };
Marri Devender Rao92e07bf2019-04-04 01:33:34 -0500137 timer.async_wait(timerHandler);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700138
Ed Tanous1abe55e2018-09-05 08:30:59 -0700139 if (tickFunction && tickInterval.count() > 0)
140 {
Ed Tanous271584a2019-07-09 16:24:22 -0700141 tickTimer.expires_after(
142 std::chrono::milliseconds(tickInterval.count()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700143 tickTimer.async_wait([this](const boost::system::error_code& ec) {
144 if (ec)
145 {
146 return;
147 }
148 onTick();
149 });
150 }
151
152 BMCWEB_LOG_INFO << serverName << " server is running, local endpoint "
153 << acceptor->local_endpoint();
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600154 startAsyncWaitForSignal();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700155 doAccept();
156 }
157
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600158 void loadCertificate()
159 {
160#ifdef BMCWEB_ENABLE_SSL
161 namespace fs = std::filesystem;
162 // Cleanup older certificate file existing in the system
163 fs::path oldCert = "/home/root/server.pem";
164 if (fs::exists(oldCert))
165 {
166 fs::remove("/home/root/server.pem");
167 }
168 fs::path certPath = "/etc/ssl/certs/https/";
169 // if path does not exist create the path so that
170 // self signed certificate can be created in the
171 // path
172 if (!fs::exists(certPath))
173 {
174 fs::create_directories(certPath);
175 }
176 fs::path certFile = certPath / "server.pem";
177 BMCWEB_LOG_INFO << "Building SSL Context file=" << certFile;
178 std::string sslPemFile(certFile);
179 ensuressl::ensureOpensslKeyPresentAndValid(sslPemFile);
180 std::shared_ptr<boost::asio::ssl::context> sslContext =
181 ensuressl::getSslContext(sslPemFile);
182 adaptorCtx = sslContext;
183 handler->ssl(std::move(sslContext));
184#endif
185 }
186
187 void startAsyncWaitForSignal()
188 {
189 signals.async_wait([this](const boost::system::error_code& ec,
190 int signalNo) {
191 if (ec)
192 {
193 BMCWEB_LOG_INFO << "Error in signal handler" << ec.message();
194 }
195 else
196 {
197 if (signalNo == SIGHUP)
198 {
199 BMCWEB_LOG_INFO << "Receivied reload signal";
200 loadCertificate();
Zbigniew Lukwinski7d0120b2019-10-15 09:12:45 +0200201 boost::system::error_code ec;
202 acceptor->cancel(ec);
203 if (ec)
204 {
205 BMCWEB_LOG_ERROR
206 << "Error while canceling async operations:"
207 << ec.message();
208 }
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600209 this->startAsyncWaitForSignal();
210 }
211 else
212 {
213 stop();
214 }
215 }
216 });
217 }
218
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219 void stop()
220 {
221 ioService->stop();
222 }
223
224 void doAccept()
225 {
Ed Tanousceac6f72018-12-02 11:58:47 -0800226 std::optional<Adaptor> adaptorTemp;
227 if constexpr (std::is_same<Adaptor,
228 boost::beast::ssl_stream<
229 boost::asio::ip::tcp::socket>>::value)
230 {
231 adaptorTemp = Adaptor(*ioService, *adaptorCtx);
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000232 auto p =
233 std::make_shared<Connection<Adaptor, Handler, Middlewares...>>(
Ed Tanouse278c182019-03-13 16:23:37 -0700234 *ioService, handler, serverName, middlewares,
235 getCachedDateStr, timerQueue,
236 std::move(adaptorTemp.value()));
237
238 acceptor->async_accept(p->socket().next_layer(),
239 [this, p](boost::system::error_code ec) {
240 if (!ec)
241 {
242 boost::asio::post(
243 *this->ioService,
244 [p] { p->start(); });
245 }
Ed Tanouse278c182019-03-13 16:23:37 -0700246 doAccept();
247 });
Ed Tanousceac6f72018-12-02 11:58:47 -0800248 }
249 else
250 {
251 adaptorTemp = Adaptor(*ioService);
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000252 auto p =
253 std::make_shared<Connection<Adaptor, Handler, Middlewares...>>(
Ed Tanouse278c182019-03-13 16:23:37 -0700254 *ioService, handler, serverName, middlewares,
255 getCachedDateStr, timerQueue,
256 std::move(adaptorTemp.value()));
257
258 acceptor->async_accept(
259 p->socket(), [this, p](boost::system::error_code ec) {
260 if (!ec)
261 {
262 boost::asio::post(*this->ioService,
263 [p] { p->start(); });
264 }
Ed Tanouse278c182019-03-13 16:23:37 -0700265 doAccept();
266 });
Ed Tanousceac6f72018-12-02 11:58:47 -0800267 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700268 }
269
270 private:
Ed Tanous8f626352018-12-19 14:51:54 -0800271 std::shared_ptr<asio::io_context> ioService;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700272 detail::TimerQueue timerQueue;
273 std::function<std::string()> getCachedDateStr;
274 std::unique_ptr<tcp::acceptor> acceptor;
275 boost::asio::signal_set signals;
Ed Tanous271584a2019-07-09 16:24:22 -0700276 boost::asio::steady_timer tickTimer;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000277 boost::asio::steady_timer timer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700278
279 std::string dateStr;
280
281 Handler* handler;
282 std::string serverName = "iBMC";
283
284 std::chrono::milliseconds tickInterval{};
285 std::function<void()> tickFunction;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000286 std::function<void(const boost::system::error_code& ec)> timerHandler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700287
288 std::tuple<Middlewares...>* middlewares;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700289
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700290#ifdef BMCWEB_ENABLE_SSL
Ed Tanous1abe55e2018-09-05 08:30:59 -0700291 bool useSsl{false};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700292#endif
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600293 std::shared_ptr<boost::asio::ssl::context> adaptorCtx;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700294}; // namespace crow
295} // namespace crow