Skip to content

Commit c8d74f2

Browse files
committed
API to unpair clients
1 parent 4b9f9d0 commit c8d74f2

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/confighttp.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,37 @@ namespace confighttp {
701701
outputTree.put("status", true);
702702
}
703703

704+
void
705+
unpairClient(resp_https_t response, req_https_t request) {
706+
if (!authenticate(response, request)) return;
707+
708+
print_req(request);
709+
710+
std::stringstream ss;
711+
ss << request->content.rdbuf();
712+
713+
pt::ptree inputTree, outputTree;
714+
715+
auto g = util::fail_guard([&]() {
716+
std::ostringstream data;
717+
pt::write_json(data, outputTree);
718+
response->write(data.str());
719+
});
720+
721+
try {
722+
// TODO: Input Validation
723+
pt::read_json(ss, inputTree);
724+
std::string uniqueid = inputTree.get<std::string>("uniqueid");
725+
outputTree.put("status", nvhttp::unpair_client(uniqueid));
726+
}
727+
catch (std::exception &e) {
728+
BOOST_LOG(warning) << "UnpairClient: "sv << e.what();
729+
outputTree.put("status", false);
730+
outputTree.put("error", e.what());
731+
return;
732+
}
733+
}
734+
704735
void
705736
listClients(resp_https_t response, req_https_t request) {
706737
if (!authenticate(response, request)) return;
@@ -769,6 +800,7 @@ namespace confighttp {
769800
server.resource["^/api/apps/([0-9]+)$"]["DELETE"] = deleteApp;
770801
server.resource["^/api/clients/unpair$"]["POST"] = unpairAll;
771802
server.resource["^/api/clients/list$"]["GET"] = listClients;
803+
server.resource["^/api/clients/unpair-single$"]["POST"] = unpairClient;
772804
server.resource["^/api/apps/close$"]["POST"] = closeApp;
773805
server.resource["^/api/covers/upload$"]["POST"] = uploadCover;
774806
server.resource["^/images/sunshine.ico$"]["GET"] = getFaviconImage;

src/nvhttp.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,4 +1153,27 @@ namespace nvhttp {
11531153
map_id_client.clear();
11541154
save_state();
11551155
}
1156+
1157+
/**
1158+
* @brief Remove all single client.
1159+
*
1160+
* EXAMPLES:
1161+
* ```cpp
1162+
* nvhttp::unpair_client("4D7BB2DD-5704-A405-B41C-891A022932E1");
1163+
* ```
1164+
*/
1165+
bool
1166+
unpair_client(std::string uniqueID) {
1167+
for (auto &[_, client] : map_id_client) {
1168+
for (auto it = client.named_certs.begin(); it != client.named_certs.end();)
1169+
{
1170+
if ((*it).uniqueID == uniqueID)
1171+
it = client.named_certs.erase(it);
1172+
else
1173+
++it;
1174+
}
1175+
}
1176+
save_state();
1177+
return true;
1178+
}
11561179
} // namespace nvhttp

src/nvhttp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ namespace nvhttp {
4747
start();
4848
bool
4949
pin(std::string pin, std::string name);
50+
bool
51+
unpair_client(std::string uniqueid);
5052
boost::property_tree::ptree
5153
get_all_clients();
5254
void

0 commit comments

Comments
 (0)