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