From c8d74f21605edacaec8e2c015d185fcd3db3146a Mon Sep 17 00:00:00 2001 From: Xander Frangos <33106561+xanderfrangos@users.noreply.github.com> Date: Fri, 19 Jan 2024 21:59:35 -0500 Subject: [PATCH] API to unpair clients --- src/confighttp.cpp | 32 ++++++++++++++++++++++++++++++++ src/nvhttp.cpp | 23 +++++++++++++++++++++++ src/nvhttp.h | 2 ++ 3 files changed, 57 insertions(+) diff --git a/src/confighttp.cpp b/src/confighttp.cpp index 44765d7ab3..29b7b01090 100644 --- a/src/confighttp.cpp +++ b/src/confighttp.cpp @@ -701,6 +701,37 @@ namespace confighttp { outputTree.put("status", true); } + void + unpairClient(resp_https_t response, req_https_t request) { + if (!authenticate(response, request)) return; + + print_req(request); + + std::stringstream ss; + ss << request->content.rdbuf(); + + pt::ptree inputTree, outputTree; + + auto g = util::fail_guard([&]() { + std::ostringstream data; + pt::write_json(data, outputTree); + response->write(data.str()); + }); + + try { + // TODO: Input Validation + pt::read_json(ss, inputTree); + std::string uniqueid = inputTree.get("uniqueid"); + outputTree.put("status", nvhttp::unpair_client(uniqueid)); + } + catch (std::exception &e) { + BOOST_LOG(warning) << "UnpairClient: "sv << e.what(); + outputTree.put("status", false); + outputTree.put("error", e.what()); + return; + } + } + void listClients(resp_https_t response, req_https_t request) { if (!authenticate(response, request)) return; @@ -769,6 +800,7 @@ namespace confighttp { server.resource["^/api/apps/([0-9]+)$"]["DELETE"] = deleteApp; server.resource["^/api/clients/unpair$"]["POST"] = unpairAll; server.resource["^/api/clients/list$"]["GET"] = listClients; + server.resource["^/api/clients/unpair-single$"]["POST"] = unpairClient; server.resource["^/api/apps/close$"]["POST"] = closeApp; server.resource["^/api/covers/upload$"]["POST"] = uploadCover; server.resource["^/images/sunshine.ico$"]["GET"] = getFaviconImage; diff --git a/src/nvhttp.cpp b/src/nvhttp.cpp index 382038b868..1ac44cbe50 100644 --- a/src/nvhttp.cpp +++ b/src/nvhttp.cpp @@ -1153,4 +1153,27 @@ namespace nvhttp { map_id_client.clear(); save_state(); } + + /** + * @brief Remove all single client. + * + * EXAMPLES: + * ```cpp + * nvhttp::unpair_client("4D7BB2DD-5704-A405-B41C-891A022932E1"); + * ``` + */ + bool + unpair_client(std::string uniqueID) { + for (auto &[_, client] : map_id_client) { + for (auto it = client.named_certs.begin(); it != client.named_certs.end();) + { + if ((*it).uniqueID == uniqueID) + it = client.named_certs.erase(it); + else + ++it; + } + } + save_state(); + return true; + } } // namespace nvhttp diff --git a/src/nvhttp.h b/src/nvhttp.h index 11df8fc6f5..f150bae14e 100644 --- a/src/nvhttp.h +++ b/src/nvhttp.h @@ -47,6 +47,8 @@ namespace nvhttp { start(); bool pin(std::string pin, std::string name); + bool + unpair_client(std::string uniqueid); boost::property_tree::ptree get_all_clients(); void