2022-10-02 00:29:37 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021-2022 Peter Repukat - FlatspotSoftware
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-10-10 18:27:59 +00:00
|
|
|
#include <httplib.h>
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2022-10-02 00:29:37 +00:00
|
|
|
#define NOMINMAX
|
|
|
|
#include <Windows.h>
|
2022-10-04 14:47:21 +00:00
|
|
|
#include <ShlObj.h>
|
2022-10-02 00:29:37 +00:00
|
|
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
|
|
|
#include <spdlog/sinks/basic_file_sink.h>
|
|
|
|
#include <spdlog/sinks/stdout_color_sinks.h>
|
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
2022-10-10 18:27:59 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2022-10-02 00:29:37 +00:00
|
|
|
#include "../version.hpp"
|
2022-10-10 19:08:22 +00:00
|
|
|
#include "../GlosSITarget/Settings.h"
|
2022-10-02 00:29:37 +00:00
|
|
|
#include "../GlosSITarget/HidHide.h"
|
2022-10-11 18:16:14 +00:00
|
|
|
#include "../GlosSITarget/util.h"
|
2022-10-02 00:29:37 +00:00
|
|
|
|
2022-10-10 18:27:59 +00:00
|
|
|
bool IsProcessRunning(DWORD pid)
|
|
|
|
{
|
|
|
|
const HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, pid);
|
|
|
|
if (process == nullptr)
|
|
|
|
return false;
|
|
|
|
const DWORD ret = WaitForSingleObject(process, 0);
|
|
|
|
CloseHandle(process);
|
|
|
|
return ret == WAIT_TIMEOUT;
|
|
|
|
}
|
2022-10-02 00:29:37 +00:00
|
|
|
|
2022-10-10 19:21:48 +00:00
|
|
|
void fetchSettings(httplib::Client& http_client, int retried_count = 0) {
|
|
|
|
http_client.set_connection_timeout(1 + (retried_count > 0 ? 2 : 0));
|
|
|
|
|
|
|
|
auto http_res = http_client.Get("/settings");
|
|
|
|
if (http_res.error() == httplib::Error::Success && http_res->status == 200)
|
|
|
|
{
|
|
|
|
const auto json = nlohmann::json::parse(http_res->body);
|
|
|
|
spdlog::debug("Received settings from GlosSITarget: {}", json.dump());
|
|
|
|
Settings::Parse(json);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
spdlog::error("Couldn't get settings from GlosSITarget. Error: {}", (int)http_res.error());
|
|
|
|
if (retried_count < 2)
|
|
|
|
{
|
|
|
|
spdlog::info("Retrying... {}", retried_count);
|
|
|
|
fetchSettings(http_client, retried_count + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 19:53:04 +00:00
|
|
|
DWORD WINAPI watchdog(HMODULE hModule)
|
|
|
|
{
|
2022-10-04 14:47:21 +00:00
|
|
|
wchar_t* localAppDataFolder;
|
|
|
|
std::filesystem::path configDirPath;
|
|
|
|
if (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, NULL, &localAppDataFolder) != S_OK) {
|
|
|
|
configDirPath = std::filesystem::temp_directory_path().parent_path().parent_path().parent_path();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
configDirPath = std::filesystem::path(localAppDataFolder).parent_path();
|
|
|
|
}
|
2022-10-02 00:29:37 +00:00
|
|
|
|
|
|
|
configDirPath /= "Roaming";
|
|
|
|
configDirPath /= "GlosSI";
|
|
|
|
if (!std::filesystem::exists(configDirPath))
|
|
|
|
std::filesystem::create_directories(configDirPath);
|
|
|
|
|
|
|
|
auto logPath = configDirPath;
|
|
|
|
logPath /= "GlosSIWatchdog.log";
|
|
|
|
const auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(logPath.wstring(), true);
|
|
|
|
std::vector<spdlog::sink_ptr> sinks{ file_sink };
|
|
|
|
auto logger = std::make_shared<spdlog::logger>("log", sinks.begin(), sinks.end());
|
|
|
|
logger->set_level(spdlog::level::trace);
|
|
|
|
logger->flush_on(spdlog::level::trace);
|
|
|
|
spdlog::set_default_logger(logger);
|
|
|
|
|
|
|
|
spdlog::info("GlosSIWatchdog loaded");
|
|
|
|
spdlog::info("Version: {}", version::VERSION_STR);
|
|
|
|
|
|
|
|
auto glossi_hwnd = FindWindowA(nullptr, "GlosSITarget");
|
|
|
|
if (!glossi_hwnd)
|
|
|
|
{
|
|
|
|
spdlog::error("Couldn't find GlosSITarget window. Exiting...");
|
2022-10-10 17:58:36 +00:00
|
|
|
FreeLibraryAndExitThread(hModule, 1);
|
2022-10-02 00:29:37 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
spdlog::debug("Found GlosSITarget window; Starting watch loop");
|
|
|
|
|
2022-10-10 18:27:59 +00:00
|
|
|
httplib::Client http_client("http://localhost:8756");
|
2022-10-10 19:08:22 +00:00
|
|
|
|
2022-10-10 19:21:48 +00:00
|
|
|
fetchSettings(http_client);
|
2022-10-10 19:08:22 +00:00
|
|
|
|
|
|
|
std::vector<DWORD> pids;
|
2022-10-04 19:53:04 +00:00
|
|
|
while (glossi_hwnd)
|
|
|
|
{
|
2022-10-10 18:27:59 +00:00
|
|
|
http_client.set_connection_timeout(120);
|
2022-10-10 19:21:48 +00:00
|
|
|
const auto http_res = http_client.Get("/launched-pids");
|
2022-10-10 18:27:59 +00:00
|
|
|
if (http_res.error() == httplib::Error::Success && http_res->status == 200)
|
|
|
|
{
|
2022-10-10 19:08:22 +00:00
|
|
|
const auto json = nlohmann::json::parse(http_res->body);
|
|
|
|
if (Settings::common.extendedLogging)
|
|
|
|
{
|
|
|
|
spdlog::trace("Received pids: {}", json.dump());
|
|
|
|
}
|
|
|
|
pids = json.get<std::vector<DWORD>>();
|
2022-10-10 19:21:48 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-10-10 18:27:59 +00:00
|
|
|
spdlog::error("Couldn't fetch launched PIDs: {}", (int)http_res.error());
|
|
|
|
}
|
|
|
|
|
2022-10-04 19:53:04 +00:00
|
|
|
glossi_hwnd = FindWindowA(nullptr, "GlosSITarget");
|
2022-10-10 18:27:59 +00:00
|
|
|
|
|
|
|
Sleep(333);
|
2022-10-04 19:53:04 +00:00
|
|
|
}
|
2022-10-10 18:27:59 +00:00
|
|
|
spdlog::info("GlosSITarget was closed. Resetting HidHide state...");
|
2022-10-02 00:29:37 +00:00
|
|
|
HidHide hidhide;
|
|
|
|
hidhide.disableHidHide();
|
2022-10-10 18:27:59 +00:00
|
|
|
|
2022-10-10 19:08:22 +00:00
|
|
|
if (Settings::launch.closeOnExit)
|
2022-10-10 18:27:59 +00:00
|
|
|
{
|
2022-10-10 19:08:22 +00:00
|
|
|
spdlog::info("Closing launched processes");
|
|
|
|
|
|
|
|
for (const auto pid : pids)
|
2022-10-10 18:27:59 +00:00
|
|
|
{
|
2022-10-10 19:08:22 +00:00
|
|
|
if (Settings::common.extendedLogging)
|
|
|
|
{
|
|
|
|
spdlog::debug("Checking if process {} is running", pid);
|
|
|
|
}
|
|
|
|
if (IsProcessRunning(pid))
|
|
|
|
{
|
2022-10-11 18:16:14 +00:00
|
|
|
glossi_util::KillProcess(pid);
|
2022-10-10 19:21:48 +00:00
|
|
|
}
|
|
|
|
else
|
2022-10-10 18:27:59 +00:00
|
|
|
{
|
2022-10-10 19:08:22 +00:00
|
|
|
if (Settings::common.extendedLogging)
|
2022-10-10 18:27:59 +00:00
|
|
|
{
|
2022-10-10 19:08:22 +00:00
|
|
|
spdlog::debug("Process {} is not running", pid);
|
2022-10-10 18:27:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 19:53:04 +00:00
|
|
|
spdlog::info("Unloading Watchdog...");
|
|
|
|
FreeLibraryAndExitThread(hModule, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL APIENTRY DllMain(HMODULE hModule,
|
|
|
|
DWORD ul_reason_for_call,
|
|
|
|
LPVOID lpReserved
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
|
|
|
|
{
|
|
|
|
CloseHandle(CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)watchdog, hModule, 0, nullptr));
|
|
|
|
}
|
|
|
|
else if (ul_reason_for_call == DLL_PROCESS_DETACH) {
|
|
|
|
}
|
|
|
|
return TRUE;
|
2022-10-02 00:29:37 +00:00
|
|
|
|
2022-10-10 19:21:48 +00:00
|
|
|
return 0;
|
2022-10-02 00:29:37 +00:00
|
|
|
}
|