blob: 0099274a8272f698f56a233bcee39cdca32321fa [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 Tanous52cc1122020-07-18 13:51:21 -070030template <typename Handler, typename Adaptor = boost::asio::ip::tcp::socket>
Ed Tanous1abe55e2018-09-05 08:30:59 -070031class Server
32{
33 public:
Ed Tanous23a21a12020-07-25 04:45:05 +000034 Server(Handler* handlerIn, std::unique_ptr<tcp::acceptor>&& acceptor,
Jason M. Billse3e29612019-09-13 08:05:13 -070035 std::shared_ptr<boost::asio::ssl::context> adaptor_ctx,
Ed Tanous8f626352018-12-19 14:51:54 -080036 std::shared_ptr<boost::asio::io_context> io =
37 std::make_shared<boost::asio::io_context>()) :
Ed Tanous1abe55e2018-09-05 08:30:59 -070038 ioService(std::move(io)),
Marri Devender Rao5968cae2019-01-21 10:27:12 -060039 acceptor(std::move(acceptor)),
40 signals(*ioService, SIGINT, SIGTERM, SIGHUP), tickTimer(*ioService),
Ed Tanous23a21a12020-07-25 04:45:05 +000041 timer(*ioService), handler(handlerIn), adaptorCtx(adaptor_ctx)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050042 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070043
Ed Tanous1abe55e2018-09-05 08:30:59 -070044 Server(Handler* handler, const std::string& bindaddr, uint16_t port,
Jason M. Billse3e29612019-09-13 08:05:13 -070045 std::shared_ptr<boost::asio::ssl::context> adaptor_ctx,
Ed Tanous8f626352018-12-19 14:51:54 -080046 std::shared_ptr<boost::asio::io_context> io =
47 std::make_shared<boost::asio::io_context>()) :
Ed Tanous1abe55e2018-09-05 08:30:59 -070048 Server(handler,
Ed Tanous9e6e1b22018-03-16 13:08:50 -070049 std::make_unique<tcp::acceptor>(
Ed Tanouse278c182019-03-13 16:23:37 -070050 *io, tcp::endpoint(boost::asio::ip::make_address(bindaddr),
51 port)),
Ed Tanous52cc1122020-07-18 13:51:21 -070052 adaptor_ctx, io)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050053 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070054
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 Server(Handler* handler, int existing_socket,
Jason M. Billse3e29612019-09-13 08:05:13 -070056 std::shared_ptr<boost::asio::ssl::context> adaptor_ctx,
Ed Tanous8f626352018-12-19 14:51:54 -080057 std::shared_ptr<boost::asio::io_context> io =
58 std::make_shared<boost::asio::io_context>()) :
Ed Tanous1abe55e2018-09-05 08:30:59 -070059 Server(handler,
60 std::make_unique<tcp::acceptor>(*io, boost::asio::ip::tcp::v6(),
61 existing_socket),
Ed Tanous52cc1122020-07-18 13:51:21 -070062 adaptor_ctx, 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 void setTickFunction(std::chrono::milliseconds d, std::function<void()> f)
66 {
67 tickInterval = d;
68 tickFunction = f;
69 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070070
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 void onTick()
72 {
73 tickFunction();
Ed Tanous271584a2019-07-09 16:24:22 -070074 tickTimer.expires_after(
75 std::chrono::milliseconds(tickInterval.count()));
Ed Tanous1abe55e2018-09-05 08:30:59 -070076 tickTimer.async_wait([this](const boost::system::error_code& ec) {
77 if (ec)
78 {
79 return;
80 }
81 onTick();
82 });
83 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070084
Ed Tanous1abe55e2018-09-05 08:30:59 -070085 void updateDateStr()
86 {
Ed Tanous99131cd2019-10-24 11:12:47 -070087 time_t lastTimeT = time(nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -070088 tm myTm{};
Ed Tanous7045c8d2017-04-03 10:04:37 -070089
Ed Tanous1abe55e2018-09-05 08:30:59 -070090 gmtime_r(&lastTimeT, &myTm);
Ed Tanous271584a2019-07-09 16:24:22 -070091
Ed Tanous1abe55e2018-09-05 08:30:59 -070092 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 Tanous23a21a12020-07-25 04:45:05 +000096 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070097
Ed Tanous1abe55e2018-09-05 08:30:59 -070098 void run()
99 {
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600100 loadCertificate();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700101 updateDateStr();
Ed Tanous7045c8d2017-04-03 10:04:37 -0700102
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 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 Tanous9e6e1b22018-03-16 13:08:50 -0700114
Ed Tanous271584a2019-07-09 16:24:22 -0700115 timer.expires_after(std::chrono::seconds(1));
Ed Tanous7045c8d2017-04-03 10:04:37 -0700116
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000117 timerHandler = [this](const boost::system::error_code& ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118 if (ec)
119 {
120 return;
121 }
122 timerQueue.process();
Ed Tanous271584a2019-07-09 16:24:22 -0700123 timer.expires_after(std::chrono::seconds(1));
Marri Devender Rao92e07bf2019-04-04 01:33:34 -0500124 timer.async_wait(timerHandler);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700125 };
Marri Devender Rao92e07bf2019-04-04 01:33:34 -0500126 timer.async_wait(timerHandler);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700127
Ed Tanous1abe55e2018-09-05 08:30:59 -0700128 if (tickFunction && tickInterval.count() > 0)
129 {
Ed Tanous271584a2019-07-09 16:24:22 -0700130 tickTimer.expires_after(
131 std::chrono::milliseconds(tickInterval.count()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700132 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 Rao5968cae2019-01-21 10:27:12 -0600143 startAsyncWaitForSignal();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700144 doAccept();
145 }
146
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600147 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 Lukwinski7d0120b2019-10-15 09:12:45 +0200190 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 Rao5968cae2019-01-21 10:27:12 -0600198 this->startAsyncWaitForSignal();
199 }
200 else
201 {
202 stop();
203 }
204 }
205 });
206 }
207
Ed Tanous1abe55e2018-09-05 08:30:59 -0700208 void stop()
209 {
210 ioService->stop();
211 }
212
213 void doAccept()
214 {
Ed Tanousceac6f72018-12-02 11:58:47 -0800215 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 Tanous52cc1122020-07-18 13:51:21 -0700221 auto p = std::make_shared<Connection<Adaptor, Handler>>(
222 *ioService, handler, serverName, getCachedDateStr, timerQueue,
223 std::move(adaptorTemp.value()));
Ed Tanouse278c182019-03-13 16:23:37 -0700224
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 Tanouse278c182019-03-13 16:23:37 -0700233 doAccept();
234 });
Ed Tanousceac6f72018-12-02 11:58:47 -0800235 }
236 else
237 {
238 adaptorTemp = Adaptor(*ioService);
Ed Tanous52cc1122020-07-18 13:51:21 -0700239 auto p = std::make_shared<Connection<Adaptor, Handler>>(
240 *ioService, handler, serverName, getCachedDateStr, timerQueue,
241 std::move(adaptorTemp.value()));
Ed Tanouse278c182019-03-13 16:23:37 -0700242
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 Tanouse278c182019-03-13 16:23:37 -0700250 doAccept();
251 });
Ed Tanousceac6f72018-12-02 11:58:47 -0800252 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700253 }
254
255 private:
Ed Tanous8f626352018-12-19 14:51:54 -0800256 std::shared_ptr<asio::io_context> ioService;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700257 detail::TimerQueue timerQueue;
258 std::function<std::string()> getCachedDateStr;
259 std::unique_ptr<tcp::acceptor> acceptor;
260 boost::asio::signal_set signals;
Ed Tanous271584a2019-07-09 16:24:22 -0700261 boost::asio::steady_timer tickTimer;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000262 boost::asio::steady_timer timer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700263
264 std::string dateStr;
265
266 Handler* handler;
Gunnar Millsa502de32020-07-07 20:08:24 -0500267 std::string serverName = "bmcweb";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700268
269 std::chrono::milliseconds tickInterval{};
270 std::function<void()> tickFunction;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000271 std::function<void(const boost::system::error_code& ec)> timerHandler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700272
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700273#ifdef BMCWEB_ENABLE_SSL
Ed Tanous1abe55e2018-09-05 08:30:59 -0700274 bool useSsl{false};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700275#endif
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600276 std::shared_ptr<boost::asio::ssl::context> adaptorCtx;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700277}; // namespace crow
278} // namespace crow