blob: 422b3a71c9d2cdf462950c7e8c9b2ea6facf31e0 [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 Tanouscb13a392020-07-25 19:02:03 +000034 Server(Handler* handlerIn, std::unique_ptr<tcp::acceptor>&& acceptorIn,
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)),
Ed Tanouscb13a392020-07-25 19:02:03 +000039 acceptor(std::move(acceptorIn)),
Ed Tanousb74e4402020-09-09 20:26:26 -070040 signals(*ioService, SIGINT, SIGTERM, SIGHUP), timer(*ioService),
41 handler(handlerIn), adaptorCtx(adaptor_ctx)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050042 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070043
Ed Tanouscb13a392020-07-25 19:02:03 +000044 Server(Handler* handlerIn, 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 Tanouscb13a392020-07-25 19:02:03 +000048 Server(handlerIn,
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 Tanouscb13a392020-07-25 19:02:03 +000055 Server(Handler* handlerIn, 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 Tanouscb13a392020-07-25 19:02:03 +000059 Server(handlerIn,
Ed Tanous1abe55e2018-09-05 08:30:59 -070060 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 updateDateStr()
66 {
Ed Tanous99131cd2019-10-24 11:12:47 -070067 time_t lastTimeT = time(nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -070068 tm myTm{};
Ed Tanous7045c8d2017-04-03 10:04:37 -070069
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 gmtime_r(&lastTimeT, &myTm);
Ed Tanous271584a2019-07-09 16:24:22 -070071
Ed Tanous1abe55e2018-09-05 08:30:59 -070072 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 Tanous23a21a12020-07-25 04:45:05 +000076 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070077
Ed Tanous1abe55e2018-09-05 08:30:59 -070078 void run()
79 {
Marri Devender Rao5968cae2019-01-21 10:27:12 -060080 loadCertificate();
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 updateDateStr();
Ed Tanous7045c8d2017-04-03 10:04:37 -070082
Ed Tanous1abe55e2018-09-05 08:30:59 -070083 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 Tanous9e6e1b22018-03-16 13:08:50 -070094
Ed Tanous271584a2019-07-09 16:24:22 -070095 timer.expires_after(std::chrono::seconds(1));
Ed Tanous7045c8d2017-04-03 10:04:37 -070096
Jan Sowinskiee52ae12020-01-09 16:28:32 +000097 timerHandler = [this](const boost::system::error_code& ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -070098 if (ec)
99 {
100 return;
101 }
102 timerQueue.process();
Ed Tanous271584a2019-07-09 16:24:22 -0700103 timer.expires_after(std::chrono::seconds(1));
Marri Devender Rao92e07bf2019-04-04 01:33:34 -0500104 timer.async_wait(timerHandler);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700105 };
Marri Devender Rao92e07bf2019-04-04 01:33:34 -0500106 timer.async_wait(timerHandler);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700107
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 BMCWEB_LOG_INFO << serverName << " server is running, local endpoint "
109 << acceptor->local_endpoint();
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600110 startAsyncWaitForSignal();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 doAccept();
112 }
113
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600114 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 Tanouscb13a392020-07-25 19:02:03 +0000157 boost::system::error_code ec2;
158 acceptor->cancel(ec2);
159 if (ec2)
Zbigniew Lukwinski7d0120b2019-10-15 09:12:45 +0200160 {
161 BMCWEB_LOG_ERROR
162 << "Error while canceling async operations:"
Ed Tanouscb13a392020-07-25 19:02:03 +0000163 << ec2.message();
Zbigniew Lukwinski7d0120b2019-10-15 09:12:45 +0200164 }
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600165 this->startAsyncWaitForSignal();
166 }
167 else
168 {
169 stop();
170 }
171 }
172 });
173 }
174
Ed Tanous1abe55e2018-09-05 08:30:59 -0700175 void stop()
176 {
177 ioService->stop();
178 }
179
180 void doAccept()
181 {
Ed Tanousceac6f72018-12-02 11:58:47 -0800182 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 Tanous52cc1122020-07-18 13:51:21 -0700188 auto p = std::make_shared<Connection<Adaptor, Handler>>(
Ed Tanouscb13a392020-07-25 19:02:03 +0000189 handler, serverName, getCachedDateStr, timerQueue,
Ed Tanous52cc1122020-07-18 13:51:21 -0700190 std::move(adaptorTemp.value()));
Ed Tanouse278c182019-03-13 16:23:37 -0700191
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 Tanouse278c182019-03-13 16:23:37 -0700200 doAccept();
201 });
Ed Tanousceac6f72018-12-02 11:58:47 -0800202 }
203 else
204 {
205 adaptorTemp = Adaptor(*ioService);
Ed Tanous52cc1122020-07-18 13:51:21 -0700206 auto p = std::make_shared<Connection<Adaptor, Handler>>(
Ed Tanouscb13a392020-07-25 19:02:03 +0000207 handler, serverName, getCachedDateStr, timerQueue,
Ed Tanous52cc1122020-07-18 13:51:21 -0700208 std::move(adaptorTemp.value()));
Ed Tanouse278c182019-03-13 16:23:37 -0700209
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 Tanouse278c182019-03-13 16:23:37 -0700217 doAccept();
218 });
Ed Tanousceac6f72018-12-02 11:58:47 -0800219 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700220 }
221
222 private:
Ed Tanous8f626352018-12-19 14:51:54 -0800223 std::shared_ptr<asio::io_context> ioService;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700224 detail::TimerQueue timerQueue;
225 std::function<std::string()> getCachedDateStr;
226 std::unique_ptr<tcp::acceptor> acceptor;
227 boost::asio::signal_set signals;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000228 boost::asio::steady_timer timer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700229
230 std::string dateStr;
231
232 Handler* handler;
Gunnar Millsa502de32020-07-07 20:08:24 -0500233 std::string serverName = "bmcweb";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700234
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000235 std::function<void(const boost::system::error_code& ec)> timerHandler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700236
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700237#ifdef BMCWEB_ENABLE_SSL
Ed Tanous1abe55e2018-09-05 08:30:59 -0700238 bool useSsl{false};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700239#endif
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600240 std::shared_ptr<boost::asio::ssl::context> adaptorCtx;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700241}; // namespace crow
242} // namespace crow