blob: 0e8a702ec2e2e67a15edebb5719b74b5b517a70d [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
Manojkiran Eda44250442020-06-16 12:51:38 +05303#include "http_connection.h"
4#include "logging.h"
5#include "timer_queue.h"
6
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 -070027using namespace boost;
28using tcp = asio::ip::tcp;
29
Ed Tanousceac6f72018-12-02 11:58:47 -080030template <typename Handler, typename Adaptor = boost::asio::ip::tcp::socket,
Ed Tanous7045c8d2017-04-03 10:04:37 -070031 typename... Middlewares>
Ed Tanous1abe55e2018-09-05 08:30:59 -070032class Server
33{
34 public:
35 Server(Handler* handler, std::unique_ptr<tcp::acceptor>&& acceptor,
Jason M. Billse3e29612019-09-13 08:05:13 -070036 std::shared_ptr<boost::asio::ssl::context> adaptor_ctx,
Ed Tanous1abe55e2018-09-05 08:30:59 -070037 std::tuple<Middlewares...>* middlewares = nullptr,
Ed Tanous8f626352018-12-19 14:51:54 -080038 std::shared_ptr<boost::asio::io_context> io =
39 std::make_shared<boost::asio::io_context>()) :
Ed Tanous1abe55e2018-09-05 08:30:59 -070040 ioService(std::move(io)),
Marri Devender Rao5968cae2019-01-21 10:27:12 -060041 acceptor(std::move(acceptor)),
42 signals(*ioService, SIGINT, SIGTERM, SIGHUP), tickTimer(*ioService),
Jan Sowinskiee52ae12020-01-09 16:28:32 +000043 timer(*ioService), handler(handler), middlewares(middlewares),
44 adaptorCtx(adaptor_ctx)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050045 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070046
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 Server(Handler* handler, const std::string& bindaddr, uint16_t port,
Jason M. Billse3e29612019-09-13 08:05:13 -070048 std::shared_ptr<boost::asio::ssl::context> adaptor_ctx,
Ed Tanous1abe55e2018-09-05 08:30:59 -070049 std::tuple<Middlewares...>* middlewares = nullptr,
Ed Tanous8f626352018-12-19 14:51:54 -080050 std::shared_ptr<boost::asio::io_context> io =
51 std::make_shared<boost::asio::io_context>()) :
Ed Tanous1abe55e2018-09-05 08:30:59 -070052 Server(handler,
Ed Tanous9e6e1b22018-03-16 13:08:50 -070053 std::make_unique<tcp::acceptor>(
Ed Tanouse278c182019-03-13 16:23:37 -070054 *io, tcp::endpoint(boost::asio::ip::make_address(bindaddr),
55 port)),
Marri Devender Rao5968cae2019-01-21 10:27:12 -060056 adaptor_ctx, middlewares, io)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050057 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070058
Ed Tanous1abe55e2018-09-05 08:30:59 -070059 Server(Handler* handler, int existing_socket,
Jason M. Billse3e29612019-09-13 08:05:13 -070060 std::shared_ptr<boost::asio::ssl::context> adaptor_ctx,
Ed Tanous1abe55e2018-09-05 08:30:59 -070061 std::tuple<Middlewares...>* middlewares = nullptr,
Ed Tanous8f626352018-12-19 14:51:54 -080062 std::shared_ptr<boost::asio::io_context> io =
63 std::make_shared<boost::asio::io_context>()) :
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 Server(handler,
65 std::make_unique<tcp::acceptor>(*io, boost::asio::ip::tcp::v6(),
66 existing_socket),
Marri Devender Rao5968cae2019-01-21 10:27:12 -060067 adaptor_ctx, middlewares, io)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050068 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070069
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 void setTickFunction(std::chrono::milliseconds d, std::function<void()> f)
71 {
72 tickInterval = d;
73 tickFunction = f;
74 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070075
Ed Tanous1abe55e2018-09-05 08:30:59 -070076 void onTick()
77 {
78 tickFunction();
Ed Tanous271584a2019-07-09 16:24:22 -070079 tickTimer.expires_after(
80 std::chrono::milliseconds(tickInterval.count()));
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 tickTimer.async_wait([this](const boost::system::error_code& ec) {
82 if (ec)
83 {
84 return;
85 }
86 onTick();
87 });
88 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070089
Ed Tanous1abe55e2018-09-05 08:30:59 -070090 void updateDateStr()
91 {
Ed Tanous99131cd2019-10-24 11:12:47 -070092 time_t lastTimeT = time(nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -070093 tm myTm{};
Ed Tanous7045c8d2017-04-03 10:04:37 -070094
Ed Tanous1abe55e2018-09-05 08:30:59 -070095 gmtime_r(&lastTimeT, &myTm);
Ed Tanous271584a2019-07-09 16:24:22 -070096
Ed Tanous1abe55e2018-09-05 08:30:59 -070097 dateStr.resize(100);
98 size_t dateStrSz =
99 strftime(&dateStr[0], 99, "%a, %d %b %Y %H:%M:%S GMT", &myTm);
100 dateStr.resize(dateStrSz);
101 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700102
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 void run()
104 {
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600105 loadCertificate();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700106 updateDateStr();
Ed Tanous7045c8d2017-04-03 10:04:37 -0700107
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 getCachedDateStr = [this]() -> std::string {
109 static std::chrono::time_point<std::chrono::steady_clock>
110 lastDateUpdate = std::chrono::steady_clock::now();
111 if (std::chrono::steady_clock::now() - lastDateUpdate >=
112 std::chrono::seconds(10))
113 {
114 lastDateUpdate = std::chrono::steady_clock::now();
115 updateDateStr();
116 }
117 return this->dateStr;
118 };
Ed Tanous9e6e1b22018-03-16 13:08:50 -0700119
Ed Tanous271584a2019-07-09 16:24:22 -0700120 timer.expires_after(std::chrono::seconds(1));
Ed Tanous7045c8d2017-04-03 10:04:37 -0700121
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000122 timerHandler = [this](const boost::system::error_code& ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700123 if (ec)
124 {
125 return;
126 }
127 timerQueue.process();
Ed Tanous271584a2019-07-09 16:24:22 -0700128 timer.expires_after(std::chrono::seconds(1));
Marri Devender Rao92e07bf2019-04-04 01:33:34 -0500129 timer.async_wait(timerHandler);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700130 };
Marri Devender Rao92e07bf2019-04-04 01:33:34 -0500131 timer.async_wait(timerHandler);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700132
Ed Tanous1abe55e2018-09-05 08:30:59 -0700133 if (tickFunction && tickInterval.count() > 0)
134 {
Ed Tanous271584a2019-07-09 16:24:22 -0700135 tickTimer.expires_after(
136 std::chrono::milliseconds(tickInterval.count()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700137 tickTimer.async_wait([this](const boost::system::error_code& ec) {
138 if (ec)
139 {
140 return;
141 }
142 onTick();
143 });
144 }
145
146 BMCWEB_LOG_INFO << serverName << " server is running, local endpoint "
147 << acceptor->local_endpoint();
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600148 startAsyncWaitForSignal();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700149 doAccept();
150 }
151
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600152 void loadCertificate()
153 {
154#ifdef BMCWEB_ENABLE_SSL
155 namespace fs = std::filesystem;
156 // Cleanup older certificate file existing in the system
157 fs::path oldCert = "/home/root/server.pem";
158 if (fs::exists(oldCert))
159 {
160 fs::remove("/home/root/server.pem");
161 }
162 fs::path certPath = "/etc/ssl/certs/https/";
163 // if path does not exist create the path so that
164 // self signed certificate can be created in the
165 // path
166 if (!fs::exists(certPath))
167 {
168 fs::create_directories(certPath);
169 }
170 fs::path certFile = certPath / "server.pem";
171 BMCWEB_LOG_INFO << "Building SSL Context file=" << certFile;
172 std::string sslPemFile(certFile);
173 ensuressl::ensureOpensslKeyPresentAndValid(sslPemFile);
174 std::shared_ptr<boost::asio::ssl::context> sslContext =
175 ensuressl::getSslContext(sslPemFile);
176 adaptorCtx = sslContext;
177 handler->ssl(std::move(sslContext));
178#endif
179 }
180
181 void startAsyncWaitForSignal()
182 {
183 signals.async_wait([this](const boost::system::error_code& ec,
184 int signalNo) {
185 if (ec)
186 {
187 BMCWEB_LOG_INFO << "Error in signal handler" << ec.message();
188 }
189 else
190 {
191 if (signalNo == SIGHUP)
192 {
193 BMCWEB_LOG_INFO << "Receivied reload signal";
194 loadCertificate();
Zbigniew Lukwinski7d0120b2019-10-15 09:12:45 +0200195 boost::system::error_code ec;
196 acceptor->cancel(ec);
197 if (ec)
198 {
199 BMCWEB_LOG_ERROR
200 << "Error while canceling async operations:"
201 << ec.message();
202 }
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600203 this->startAsyncWaitForSignal();
204 }
205 else
206 {
207 stop();
208 }
209 }
210 });
211 }
212
Ed Tanous1abe55e2018-09-05 08:30:59 -0700213 void stop()
214 {
215 ioService->stop();
216 }
217
218 void doAccept()
219 {
Ed Tanousceac6f72018-12-02 11:58:47 -0800220 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);
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000226 auto p =
227 std::make_shared<Connection<Adaptor, Handler, Middlewares...>>(
Ed Tanouse278c182019-03-13 16:23:37 -0700228 *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 }
Ed Tanouse278c182019-03-13 16:23:37 -0700240 doAccept();
241 });
Ed Tanousceac6f72018-12-02 11:58:47 -0800242 }
243 else
244 {
245 adaptorTemp = Adaptor(*ioService);
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000246 auto p =
247 std::make_shared<Connection<Adaptor, Handler, Middlewares...>>(
Ed Tanouse278c182019-03-13 16:23:37 -0700248 *ioService, handler, serverName, middlewares,
249 getCachedDateStr, timerQueue,
250 std::move(adaptorTemp.value()));
251
252 acceptor->async_accept(
253 p->socket(), [this, p](boost::system::error_code ec) {
254 if (!ec)
255 {
256 boost::asio::post(*this->ioService,
257 [p] { p->start(); });
258 }
Ed Tanouse278c182019-03-13 16:23:37 -0700259 doAccept();
260 });
Ed Tanousceac6f72018-12-02 11:58:47 -0800261 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700262 }
263
264 private:
Ed Tanous8f626352018-12-19 14:51:54 -0800265 std::shared_ptr<asio::io_context> ioService;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700266 detail::TimerQueue timerQueue;
267 std::function<std::string()> getCachedDateStr;
268 std::unique_ptr<tcp::acceptor> acceptor;
269 boost::asio::signal_set signals;
Ed Tanous271584a2019-07-09 16:24:22 -0700270 boost::asio::steady_timer tickTimer;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000271 boost::asio::steady_timer timer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700272
273 std::string dateStr;
274
275 Handler* handler;
Gunnar Millsa502de32020-07-07 20:08:24 -0500276 std::string serverName = "bmcweb";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700277
278 std::chrono::milliseconds tickInterval{};
279 std::function<void()> tickFunction;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000280 std::function<void(const boost::system::error_code& ec)> timerHandler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700281
282 std::tuple<Middlewares...>* middlewares;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700283
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700284#ifdef BMCWEB_ENABLE_SSL
Ed Tanous1abe55e2018-09-05 08:30:59 -0700285 bool useSsl{false};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700286#endif
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600287 std::shared_ptr<boost::asio::ssl::context> adaptorCtx;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700288}; // namespace crow
289} // namespace crow