Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 2 | #include "http_request.hpp" |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 3 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 4 | #include <async_resp.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 5 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 6 | #include <boost/asio/buffer.hpp> |
Ed Tanous | 1b0044b | 2018-08-03 14:30:05 -0700 | [diff] [blame] | 7 | #include <boost/beast/websocket.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 8 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 9 | #include <array> |
| 10 | #include <functional> |
Ed Tanous | 1b0044b | 2018-08-03 14:30:05 -0700 | [diff] [blame] | 11 | |
| 12 | #ifdef BMCWEB_ENABLE_SSL |
| 13 | #include <boost/beast/websocket/ssl.hpp> |
| 14 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 15 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 16 | namespace crow |
| 17 | { |
| 18 | namespace websocket |
| 19 | { |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 20 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 21 | struct Connection : std::enable_shared_from_this<Connection> |
| 22 | { |
| 23 | public: |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 24 | explicit Connection(const crow::Request& reqIn) : |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 25 | req(reqIn.req), userdataPtr(nullptr) |
| 26 | {} |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 27 | |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 28 | explicit Connection(const crow::Request& reqIn, std::string user) : |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 29 | req(reqIn.req), userName{std::move(user)}, userdataPtr(nullptr) |
| 30 | {} |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 31 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 32 | virtual void sendBinary(const std::string_view msg) = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | virtual void sendBinary(std::string&& msg) = 0; |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 34 | virtual void sendText(const std::string_view msg) = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 35 | virtual void sendText(std::string&& msg) = 0; |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 36 | virtual void close(const std::string_view msg = "quit") = 0; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 37 | virtual boost::asio::io_context& getIoContext() = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 38 | virtual ~Connection() = default; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 39 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | void userdata(void* u) |
| 41 | { |
| 42 | userdataPtr = u; |
| 43 | } |
| 44 | void* userdata() |
| 45 | { |
| 46 | return userdataPtr; |
| 47 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 48 | |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 49 | const std::string& getUserName() const |
| 50 | { |
| 51 | return userName; |
| 52 | } |
| 53 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 54 | boost::beast::http::request<boost::beast::http::string_body> req; |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 55 | crow::Response res; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 56 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 57 | private: |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 58 | std::string userName{}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 59 | void* userdataPtr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 62 | template <typename Adaptor> |
| 63 | class ConnectionImpl : public Connection |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | { |
| 65 | public: |
| 66 | ConnectionImpl( |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 67 | const crow::Request& reqIn, Adaptor adaptorIn, |
Gunnar Mills | ccd584f | 2021-11-16 11:36:33 -0600 | [diff] [blame] | 68 | std::function<void(Connection&, std::shared_ptr<bmcweb::AsyncResp>)> |
| 69 | openHandler, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | std::function<void(Connection&, const std::string&, bool)> |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 71 | messageHandler, |
| 72 | std::function<void(Connection&, const std::string&)> closeHandler, |
| 73 | std::function<void(Connection&)> errorHandler) : |
Przemyslaw Czarnowski | 250b0eb | 2020-02-24 10:23:56 +0100 | [diff] [blame] | 74 | Connection(reqIn, reqIn.session->username), |
Adriana Kobylak | 69664cf | 2019-03-12 10:49:48 -0500 | [diff] [blame] | 75 | ws(std::move(adaptorIn)), inString(), inBuffer(inString, 131088), |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 76 | openHandler(std::move(openHandler)), |
| 77 | messageHandler(std::move(messageHandler)), |
| 78 | closeHandler(std::move(closeHandler)), |
| 79 | errorHandler(std::move(errorHandler)), session(reqIn.session) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 80 | { |
dhineskumare | 02bdd96 | 2021-07-08 16:06:49 +0530 | [diff] [blame] | 81 | /* Turn on the timeouts on websocket stream to server role */ |
| 82 | ws.set_option(boost::beast::websocket::stream_base::timeout::suggested( |
| 83 | boost::beast::role_type::server)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 84 | BMCWEB_LOG_DEBUG << "Creating new connection " << this; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 85 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 86 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 87 | boost::asio::io_context& getIoContext() override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 88 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 89 | return static_cast<boost::asio::io_context&>( |
| 90 | ws.get_executor().context()); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 91 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 92 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 93 | void start() |
| 94 | { |
| 95 | BMCWEB_LOG_DEBUG << "starting connection " << this; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 96 | |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 97 | using bf = boost::beast::http::field; |
| 98 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 99 | std::string_view protocol = req[bf::sec_websocket_protocol]; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 100 | |
Ed Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 101 | ws.set_option(boost::beast::websocket::stream_base::decorator( |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 102 | [session{session}, protocol{std::string(protocol)}]( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 103 | boost::beast::websocket::response_type& m) { |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 104 | |
| 105 | #ifndef BMCWEB_INSECURE_DISABLE_CSRF_PREVENTION |
Ed Tanous | a90daf1 | 2021-01-15 14:18:01 -0800 | [diff] [blame] | 106 | if (session != nullptr) |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 107 | { |
Ed Tanous | a90daf1 | 2021-01-15 14:18:01 -0800 | [diff] [blame] | 108 | // use protocol for csrf checking |
| 109 | if (session->cookieAuth && |
| 110 | !crow::utility::constantTimeStringCompare( |
| 111 | protocol, session->csrfToken)) |
| 112 | { |
| 113 | BMCWEB_LOG_ERROR << "Websocket CSRF error"; |
| 114 | m.result(boost::beast::http::status::unauthorized); |
| 115 | return; |
| 116 | } |
James Feist | f8aa3d2 | 2020-04-08 18:32:33 -0700 | [diff] [blame] | 117 | } |
| 118 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 119 | if (!protocol.empty()) |
| 120 | { |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 121 | m.insert(bf::sec_websocket_protocol, protocol); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 122 | } |
Ed Tanous | fe5b216 | 2019-05-22 14:28:16 -0700 | [diff] [blame] | 123 | |
| 124 | m.insert(bf::strict_transport_security, "max-age=31536000; " |
| 125 | "includeSubdomains; " |
| 126 | "preload"); |
| 127 | m.insert(bf::pragma, "no-cache"); |
| 128 | m.insert(bf::cache_control, "no-Store,no-Cache"); |
| 129 | m.insert("Content-Security-Policy", "default-src 'self'"); |
| 130 | m.insert("X-XSS-Protection", "1; " |
| 131 | "mode=block"); |
| 132 | m.insert("X-Content-Type-Options", "nosniff"); |
Ed Tanous | d4d77e3 | 2020-08-18 00:07:28 -0700 | [diff] [blame] | 133 | })); |
| 134 | |
| 135 | // Perform the websocket upgrade |
| 136 | ws.async_accept(req, [this, self(shared_from_this())]( |
| 137 | boost::system::error_code ec) { |
| 138 | if (ec) |
| 139 | { |
| 140 | BMCWEB_LOG_ERROR << "Error in ws.async_accept " << ec; |
| 141 | return; |
| 142 | } |
| 143 | acceptDone(); |
| 144 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 145 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 146 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 147 | void sendBinary(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 148 | { |
| 149 | ws.binary(true); |
| 150 | outBuffer.emplace_back(msg); |
| 151 | doWrite(); |
| 152 | } |
| 153 | |
| 154 | void sendBinary(std::string&& msg) override |
| 155 | { |
| 156 | ws.binary(true); |
| 157 | outBuffer.emplace_back(std::move(msg)); |
| 158 | doWrite(); |
| 159 | } |
| 160 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 161 | void sendText(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 162 | { |
| 163 | ws.text(true); |
| 164 | outBuffer.emplace_back(msg); |
| 165 | doWrite(); |
| 166 | } |
| 167 | |
| 168 | void sendText(std::string&& msg) override |
| 169 | { |
| 170 | ws.text(true); |
| 171 | outBuffer.emplace_back(std::move(msg)); |
| 172 | doWrite(); |
| 173 | } |
| 174 | |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 175 | void close(const std::string_view msg) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 176 | { |
| 177 | ws.async_close( |
Wludzik, Jozef | f6a0d63 | 2020-07-16 15:16:02 +0200 | [diff] [blame] | 178 | {boost::beast::websocket::close_code::normal, msg}, |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 179 | [self(shared_from_this())](boost::system::error_code ec) { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 180 | if (ec == boost::asio::error::operation_aborted) |
| 181 | { |
| 182 | return; |
| 183 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 184 | if (ec) |
| 185 | { |
| 186 | BMCWEB_LOG_ERROR << "Error closing websocket " << ec; |
| 187 | return; |
| 188 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 189 | }); |
| 190 | } |
| 191 | |
| 192 | void acceptDone() |
| 193 | { |
| 194 | BMCWEB_LOG_DEBUG << "Websocket accepted connection"; |
| 195 | |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame^] | 196 | auto asyncResp = std::make_shared<bmcweb::AsyncResp>( |
| 197 | res, [this, self(shared_from_this())]() { doRead(); }); |
Gunnar Mills | ccd584f | 2021-11-16 11:36:33 -0600 | [diff] [blame] | 198 | |
| 199 | asyncResp->res.result(boost::beast::http::status::ok); |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 200 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 201 | if (openHandler) |
| 202 | { |
Gunnar Mills | ccd584f | 2021-11-16 11:36:33 -0600 | [diff] [blame] | 203 | openHandler(*this, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 204 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void doRead() |
| 208 | { |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 209 | ws.async_read(inBuffer, |
| 210 | [this, self(shared_from_this())]( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 211 | boost::beast::error_code ec, std::size_t bytesRead) { |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 212 | if (ec) |
| 213 | { |
| 214 | if (ec != boost::beast::websocket::error::closed) |
| 215 | { |
| 216 | BMCWEB_LOG_ERROR << "doRead error " << ec; |
| 217 | } |
| 218 | if (closeHandler) |
| 219 | { |
| 220 | std::string_view reason = ws.reason().reason; |
| 221 | closeHandler(*this, std::string(reason)); |
| 222 | } |
| 223 | return; |
| 224 | } |
| 225 | if (messageHandler) |
| 226 | { |
| 227 | messageHandler(*this, inString, ws.got_text()); |
| 228 | } |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 229 | inBuffer.consume(bytesRead); |
Adriana Kobylak | ae29b8c | 2019-04-24 11:19:18 -0500 | [diff] [blame] | 230 | inString.clear(); |
| 231 | doRead(); |
| 232 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void doWrite() |
| 236 | { |
| 237 | // If we're already doing a write, ignore the request, it will be picked |
| 238 | // up when the current write is complete |
| 239 | if (doingWrite) |
| 240 | { |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | if (outBuffer.empty()) |
| 245 | { |
| 246 | // Done for now |
| 247 | return; |
| 248 | } |
| 249 | doingWrite = true; |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 250 | ws.async_write(boost::asio::buffer(outBuffer.front()), |
| 251 | [this, self(shared_from_this())]( |
| 252 | boost::beast::error_code ec, std::size_t) { |
| 253 | doingWrite = false; |
| 254 | outBuffer.erase(outBuffer.begin()); |
| 255 | if (ec == boost::beast::websocket::error::closed) |
| 256 | { |
| 257 | // Do nothing here. doRead handler will call the |
| 258 | // closeHandler. |
| 259 | close("Write error"); |
| 260 | return; |
| 261 | } |
| 262 | if (ec) |
| 263 | { |
| 264 | BMCWEB_LOG_ERROR << "Error in ws.async_write " |
| 265 | << ec; |
| 266 | return; |
| 267 | } |
| 268 | doWrite(); |
| 269 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | private: |
Ed Tanous | 2aee6ca | 2021-02-01 09:52:17 -0800 | [diff] [blame] | 273 | boost::beast::websocket::stream<Adaptor, false> ws; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 274 | |
Ed Tanous | 609145a | 2018-09-05 16:27:36 -0700 | [diff] [blame] | 275 | std::string inString; |
| 276 | boost::asio::dynamic_string_buffer<std::string::value_type, |
| 277 | std::string::traits_type, |
| 278 | std::string::allocator_type> |
| 279 | inBuffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 280 | std::vector<std::string> outBuffer; |
| 281 | bool doingWrite = false; |
| 282 | |
Gunnar Mills | ccd584f | 2021-11-16 11:36:33 -0600 | [diff] [blame] | 283 | std::function<void(Connection&, std::shared_ptr<bmcweb::AsyncResp>)> |
| 284 | openHandler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 285 | std::function<void(Connection&, const std::string&, bool)> messageHandler; |
| 286 | std::function<void(Connection&, const std::string&)> closeHandler; |
| 287 | std::function<void(Connection&)> errorHandler; |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 288 | std::shared_ptr<persistent_data::UserSession> session; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 289 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 290 | } // namespace websocket |
| 291 | } // namespace crow |