diff --git a/GlosSITarget/AppLauncher.cpp b/GlosSITarget/AppLauncher.cpp index 1e81d6a..7cf9179 100644 --- a/GlosSITarget/AppLauncher.cpp +++ b/GlosSITarget/AppLauncher.cpp @@ -176,6 +176,20 @@ std::vector AppLauncher::launchedPids() return res; } +void AppLauncher::addPids(const std::vector& pids) +{ + pid_mutex_.lock(); + for (const auto pid : pids) { + if (pid > 0 && std::ranges::find(pids_, pid) == pids_.end()) { + if (Settings::common.extendedLogging) { + spdlog::debug("Added PID {} via API", pid); + } + pids_.push_back(pid); + } + } + pid_mutex_.unlock(); +} + #ifdef _WIN32 bool AppLauncher::IsProcessRunning(DWORD pid) { diff --git a/GlosSITarget/AppLauncher.h b/GlosSITarget/AppLauncher.h index 84563b9..df4032e 100644 --- a/GlosSITarget/AppLauncher.h +++ b/GlosSITarget/AppLauncher.h @@ -38,6 +38,7 @@ class AppLauncher { void close(); std::vector launchedPids(); + void addPids(const std::vector& pids); private: std::function shutdown_; diff --git a/GlosSITarget/HttpServer.cpp b/GlosSITarget/HttpServer.cpp index 85466e1..c3acd0d 100644 --- a/GlosSITarget/HttpServer.cpp +++ b/GlosSITarget/HttpServer.cpp @@ -32,6 +32,36 @@ void HttpServer::run() res.set_content(j.dump(), "text/json"); }); + server_.Post("/launched-pids", [this](const httplib::Request& req, httplib::Response& res) { + try { + const nlohmann::json postbody = nlohmann::json::parse(req.body); + app_launcher_.addPids(postbody.get>()); + } catch (std::exception& e) { + res.status = 401; + res.set_content(nlohmann::json{ + {"code", 401}, + {"name", "Bad Request"}, + {"message", e.what()}, + } + .dump(), + "text/json"); + return; + } + catch (...) { + res.status = 500; + res.set_content(nlohmann::json{ + {"code", 500}, + {"name", "Internal Server Error"}, + {"message", "Unknown Error"}, + } + .dump(), + "text/json"); + return; + } + const nlohmann::json j = app_launcher_.launchedPids(); + res.set_content(j.dump(), "text/json"); + }); + server_.Get("/settings", [this](const httplib::Request& req, httplib::Response& res) { res.set_content(Settings::toJson().dump(), "text/json"); });