Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame^] | 1 | #include <webassets.hpp> |
| 2 | #include <web_kvm.hpp> |
| 3 | #include "ssl_key_handler.hpp" |
| 4 | |
| 5 | #include "app_type.hpp" |
| 6 | |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 7 | #include "crow/app.h" |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 8 | #include "crow/ci_map.h" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 9 | #include "crow/common.h" |
| 10 | #include "crow/dumb_timer_queue.h" |
| 11 | #include "crow/http_connection.h" |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 12 | #include "crow/http_parser_merged.h" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 13 | #include "crow/http_request.h" |
| 14 | #include "crow/http_response.h" |
| 15 | #include "crow/http_server.h" |
| 16 | #include "crow/json.h" |
| 17 | #include "crow/logging.h" |
| 18 | #include "crow/middleware.h" |
| 19 | #include "crow/middleware_context.h" |
| 20 | #include "crow/mustache.h" |
| 21 | #include "crow/parser.h" |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 22 | #include "crow/query_string.h" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 23 | #include "crow/routing.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 24 | #include "crow/settings.h" |
| 25 | #include "crow/socket_adaptors.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 26 | #include "crow/utility.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 27 | #include "crow/websocket.h" |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 28 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 29 | |
Ed Tanous | 5f34a9c | 2017-02-28 12:35:13 -0800 | [diff] [blame] | 30 | #include "color_cout_g3_sink.hpp" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 31 | #include "token_authorization_middleware.hpp" |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 32 | #include "webassets.hpp" |
Ed Tanous | f927347 | 2017-02-28 16:05:13 -0800 | [diff] [blame] | 33 | |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame^] | 34 | |
| 35 | #include <boost/asio.hpp> |
| 36 | #include <boost/endian/arithmetic.hpp> |
| 37 | |
| 38 | |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 39 | #include <iostream> |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 40 | #include <memory> |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 41 | #include <string> |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 42 | #include <unordered_set> |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 43 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 44 | int main(int argc, char** argv) { |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 45 | auto worker(g3::LogWorker::createLogWorker()); |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 46 | std::string logger_name("bmcweb"); |
| 47 | std::string folder("/tmp/"); |
| 48 | auto handle = worker->addDefaultLogger(logger_name, folder); |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 49 | g3::initializeLogging(worker.get()); |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 50 | auto sink_handle = worker->addSink(std::make_unique<crow::ColorCoutSink>(), |
| 51 | &crow::ColorCoutSink::ReceiveLogMessage); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 52 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 53 | std::string ssl_pem_file("server.pem"); |
| 54 | ensuressl::ensure_openssl_key_present_and_valid(ssl_pem_file); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 55 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 56 | BmcAppType app; |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame^] | 57 | |
Ed Tanous | 9992332 | 2017-03-03 14:21:24 -0800 | [diff] [blame] | 58 | crow::webassets::request_routes(app); |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 59 | crow::kvm::request_routes(app); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 60 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 61 | crow::logger::setLogLevel(crow::LogLevel::INFO); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 62 | |
Ed Tanous | 9b65f1f | 2017-03-07 15:17:13 -0800 | [diff] [blame] | 63 | CROW_ROUTE(app, "/routes") |
| 64 | ([&app]() { |
| 65 | crow::json::wvalue routes; |
| 66 | |
| 67 | routes["routes"] = app.get_rules(); |
| 68 | return routes; |
| 69 | }); |
| 70 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 71 | CROW_ROUTE(app, "/login") |
| 72 | .methods("POST"_method)([&](const crow::request& req) { |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame^] | 73 | crow::json::wvalue x; |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 74 | auto auth_token = |
| 75 | app.get_context<crow::TokenAuthorizationMiddleware>(req).auth_token; |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame^] | 76 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 77 | x["token"] = auth_token; |
| 78 | |
| 79 | return x; |
| 80 | }); |
| 81 | |
| 82 | CROW_ROUTE(app, "/logout") |
Ed Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame^] | 83 | .methods("GET"_method, "POST"_method)([&](const crow::request& req) { |
| 84 | |
| 85 | app.get_context<crow::TokenAuthorizationMiddleware>(req).auth_token = ""; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 86 | // Do nothing. Credentials have already been cleared by middleware. |
| 87 | return 200; |
| 88 | }); |
| 89 | |
| 90 | CROW_ROUTE(app, "/systeminfo") |
| 91 | ([]() { |
| 92 | |
| 93 | crow::json::wvalue j; |
| 94 | j["device_id"] = 0x7B; |
| 95 | j["device_provides_sdrs"] = true; |
| 96 | j["device_revision"] = true; |
| 97 | j["device_available"] = true; |
| 98 | j["firmware_revision"] = "0.68"; |
| 99 | |
| 100 | j["ipmi_revision"] = "2.0"; |
| 101 | j["supports_chassis_device"] = true; |
| 102 | j["supports_bridge"] = true; |
| 103 | j["supports_ipmb_event_generator"] = true; |
| 104 | j["supports_ipmb_event_receiver"] = true; |
| 105 | j["supports_fru_inventory_device"] = true; |
| 106 | j["supports_sel_device"] = true; |
| 107 | j["supports_sdr_repository_device"] = true; |
| 108 | j["supports_sensor_device"] = true; |
| 109 | |
| 110 | j["firmware_aux_revision"] = "0.60.foobar"; |
| 111 | |
| 112 | return j; |
| 113 | }); |
| 114 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 115 | CROW_ROUTE(app, "/ipmiws") |
| 116 | .websocket() |
| 117 | .onopen([&](crow::websocket::connection& conn) { |
| 118 | |
| 119 | }) |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 120 | .onclose( |
| 121 | [&](crow::websocket::connection& conn, const std::string& reason) { |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 122 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 123 | }) |
| 124 | .onmessage([&](crow::websocket::connection& conn, const std::string& data, |
| 125 | bool is_binary) { |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 126 | boost::asio::io_service io_service; |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 127 | using boost::asio::ip::udp; |
| 128 | udp::resolver resolver(io_service); |
| 129 | udp::resolver::query query(udp::v4(), "10.243.48.31", "623"); |
| 130 | udp::endpoint receiver_endpoint = *resolver.resolve(query); |
| 131 | |
| 132 | udp::socket socket(io_service); |
| 133 | socket.open(udp::v4()); |
| 134 | |
| 135 | socket.send_to(boost::asio::buffer(data), receiver_endpoint); |
| 136 | |
| 137 | std::array<char, 255> recv_buf; |
| 138 | |
| 139 | udp::endpoint sender_endpoint; |
| 140 | size_t len = |
| 141 | socket.receive_from(boost::asio::buffer(recv_buf), sender_endpoint); |
| 142 | // TODO(ed) THis is ugly. Find a way to not make a copy (ie, use |
| 143 | // std::string::data()) |
| 144 | std::string str(std::begin(recv_buf), std::end(recv_buf)); |
| 145 | LOG(DEBUG) << "Got " << str << "back \n"; |
| 146 | conn.send_binary(str); |
| 147 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 148 | }); |
| 149 | |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 150 | app.port(18080) |
| 151 | //.ssl(std::move(ensuressl::get_ssl_context(ssl_pem_file))) |
| 152 | .run(); |
Ed Tanous | 0fdddb1 | 2017-02-28 11:06:34 -0800 | [diff] [blame] | 153 | } |