From c9c6a324327bad1993c98c146359b45e18e34d3b Mon Sep 17 00:00:00 2001 From: Peter Repukat Date: Mon, 10 Oct 2022 00:28:28 +0200 Subject: [PATCH] GlosSITarget: Add HttpServer: Get launched pids --- .gitmodules | 3 + GlosSITarget/AppLauncher.cpp | 21 ++++++- GlosSITarget/AppLauncher.h | 5 ++ GlosSITarget/GlosSITarget.vcxproj | 6 +- GlosSITarget/GlosSITarget.vcxproj.filters | 6 ++ GlosSITarget/HidHide.cpp | 8 ++- GlosSITarget/HidHide.h | 4 ++ GlosSITarget/HttpServer.cpp | 47 ++++++++++++++++ GlosSITarget/HttpServer.h | 37 ++++++++++++ GlosSITarget/InputRedirector.h | 1 + GlosSITarget/Resource.rc | 68 +++++++++++++++++++++-- GlosSITarget/SteamTarget.cpp | 12 +++- GlosSITarget/SteamTarget.h | 4 ++ GlosSITarget/main.cpp | 3 + deps/cpp-httplib | 1 + 15 files changed, 214 insertions(+), 12 deletions(-) create mode 100644 GlosSITarget/HttpServer.cpp create mode 100644 GlosSITarget/HttpServer.h create mode 160000 deps/cpp-httplib diff --git a/.gitmodules b/.gitmodules index e54a7a8..d488b86 100644 --- a/.gitmodules +++ b/.gitmodules @@ -34,3 +34,6 @@ [submodule "deps/Shortcuts_VDF"] path = deps/Shortcuts_VDF url = git@github.com:Alia5/Shortcuts_VDF.git +[submodule "deps/cpp-httplib"] + path = deps/cpp-httplib + url = git@github.com:yhirose/cpp-httplib.git diff --git a/GlosSITarget/AppLauncher.cpp b/GlosSITarget/AppLauncher.cpp index 92ebe32..37ffdc1 100644 --- a/GlosSITarget/AppLauncher.cpp +++ b/GlosSITarget/AppLauncher.cpp @@ -23,6 +23,8 @@ limitations under the License. #include #include #include +#include + #pragma comment(lib, "Shell32.lib") #endif @@ -67,6 +69,7 @@ void AppLauncher::launchApp(const std::wstring& path, const std::wstring& args) void AppLauncher::update() { if (process_check_clock_.getElapsedTime().asMilliseconds() > 250) { + pid_mutex_.lock(); #ifdef _WIN32 if (!pids_.empty() && pids_[0] > 0) { if (Settings::launch.waitForChildProcs) { @@ -98,6 +101,7 @@ void AppLauncher::update() } getProcessHwnds(); #endif + pid_mutex_.unlock(); process_check_clock_.restart(); } } @@ -112,6 +116,16 @@ void AppLauncher::close() #endif } +std::vector AppLauncher::launchedPids() +{ + pid_mutex_.lock(); + std::vector res = pids_; + std::ranges::copy(pids_.begin(), pids_.end(), + std::back_inserter(res)); + pid_mutex_.unlock(); + return res; +} + #ifdef _WIN32 bool AppLauncher::IsProcessRunning(DWORD pid) { @@ -213,7 +227,9 @@ void AppLauncher::launchWin32App(const std::wstring& path, const std::wstring& a // spdlog::info(L"Started Program: \"{}\" in directory: \"{}\"", native_seps_path, launch_dir); spdlog::info(L"Started Program: \"{}\"; PID: {}", native_seps_path, process_info.dwProcessId); if (!watchdog) { + pid_mutex_.lock(); pids_.push_back(process_info.dwProcessId); + pid_mutex_.unlock(); } } else { @@ -243,7 +259,7 @@ void AppLauncher::launchUWPApp(const LPCWSTR package_full_name, const std::wstri if (!SUCCEEDED(result)) { spdlog::warn("CoAllowSetForegroundWindow failed. Code: {}", result); } - + pid_mutex_.lock(); pids_.push_back(0); // Launch the app result = sp_app_activation_manager->ActivateApplication( @@ -258,6 +274,7 @@ void AppLauncher::launchUWPApp(const LPCWSTR package_full_name, const std::wstri else { spdlog::info(L"Launched UWP Package \"{}\"; PID: {}", package_full_name, pids_[0]); } + pid_mutex_.unlock(); } else { spdlog::error("CoCreateInstance failed: Code {}", result); @@ -298,8 +315,10 @@ void AppLauncher::launchURL(const std::wstring& url, const std::wstring& args, c if (execute_info.hProcess != nullptr) { if (const auto pid = GetProcessId(execute_info.hProcess); pid > 0) { + pid_mutex_.lock(); pids_.push_back(pid); spdlog::trace("Launched URL; PID: {}", pid); + pid_mutex_.unlock(); return; } } diff --git a/GlosSITarget/AppLauncher.h b/GlosSITarget/AppLauncher.h index 231a0bf..7203028 100644 --- a/GlosSITarget/AppLauncher.h +++ b/GlosSITarget/AppLauncher.h @@ -17,9 +17,11 @@ limitations under the License. #ifdef _WIN32 #define NOMINMAX +#define WIN32_LEAN_AND_MEAN #include #endif #include +#include #include #include #include @@ -33,10 +35,13 @@ class AppLauncher { void launchApp(const std::wstring& path, const std::wstring& args = L""); void update(); void close(); + + std::vector launchedPids(); private: std::function shutdown_; sf::Clock process_check_clock_; + std::mutex pid_mutex_; #ifdef _WIN32 static bool IsProcessRunning(DWORD pid); diff --git a/GlosSITarget/GlosSITarget.vcxproj b/GlosSITarget/GlosSITarget.vcxproj index 1dd6096..4970649 100644 --- a/GlosSITarget/GlosSITarget.vcxproj +++ b/GlosSITarget/GlosSITarget.vcxproj @@ -80,7 +80,7 @@ true - ..\deps\SFML\include;..\deps\WinReg;..\deps\spdlog\include;..\deps\ValveFileVDF;..\deps\subhook;..\deps\ViGEmClient\include;..\deps\imgui;..\deps\imgui-sfml;..\deps\json\include;..\deps\traypp\tray\include;$(ExternalIncludePath) + ..\deps\SFML\include;..\deps\WinReg;..\deps\spdlog\include;..\deps\ValveFileVDF;..\deps\subhook;..\deps\ViGEmClient\include;..\deps\imgui;..\deps\imgui-sfml;..\deps\json\include;..\deps\traypp\tray\include;..\deps\cpp-httplib;$(ExternalIncludePath) ..\deps\SFML\out\Debug\lib\Debug;..\deps\ViGEmClient\lib\debug\x64;$(LibraryPath) false true @@ -88,7 +88,7 @@ false - ..\deps\SFML\include;..\deps\WinReg;..\deps\spdlog\include;..\deps\ValveFileVDF;..\deps\subhook;..\deps\ViGEmClient\include;..\deps\imgui;..\deps\imgui-sfml;..\deps\json\include;..\deps\traypp\tray\include;$(ExternalIncludePath) + ..\deps\SFML\include;..\deps\WinReg;..\deps\spdlog\include;..\deps\ValveFileVDF;..\deps\subhook;..\deps\ViGEmClient\include;..\deps\imgui;..\deps\imgui-sfml;..\deps\json\include;..\deps\traypp\tray\include;..\deps\cpp-httplib;$(ExternalIncludePath) ..\deps\SFML\out\Release\lib\RelWithDebInfo;..\deps\ViGEmClient\lib\release\x64;$(LibraryPath) ResourceCompile true @@ -188,6 +188,7 @@ + @@ -204,6 +205,7 @@ + diff --git a/GlosSITarget/GlosSITarget.vcxproj.filters b/GlosSITarget/GlosSITarget.vcxproj.filters index df4cc29..b2bcd12 100644 --- a/GlosSITarget/GlosSITarget.vcxproj.filters +++ b/GlosSITarget/GlosSITarget.vcxproj.filters @@ -117,6 +117,9 @@ Source Files + + Source Files + @@ -185,6 +188,9 @@ Header Files + + Header Files + diff --git a/GlosSITarget/HidHide.cpp b/GlosSITarget/HidHide.cpp index 82798d4..6667828 100644 --- a/GlosSITarget/HidHide.cpp +++ b/GlosSITarget/HidHide.cpp @@ -24,11 +24,10 @@ limitations under the License. #include #include +#include // Device configuration related #include -#include -// #ifndef WATCHDOG #include "Overlay.h" #endif @@ -38,9 +37,14 @@ limitations under the License. #include #include #include +#include +#include + #include "UnhookUtil.h" +#pragma comment(lib, "Setupapi.lib") + // {D61CA365-5AF4-4486-998B-9DB4734C6CA3}add the XUSB class GUID as it is missing in the public interfaces DEFINE_GUID(GUID_DEVCLASS_XUSBCLASS, 0xD61CA365, 0x5AF4, 0x4486, 0x99, 0x8B, 0x9D, 0xB4, 0x73, 0x4C, 0x6C, 0xA3); // {EC87F1E3-C13B-4100-B5F7-8B84D54260CB} add the XUSB interface class GUID as it is missing in the public interfaces diff --git a/GlosSITarget/HidHide.h b/GlosSITarget/HidHide.h index 72095b0..4fa1b27 100644 --- a/GlosSITarget/HidHide.h +++ b/GlosSITarget/HidHide.h @@ -15,7 +15,10 @@ limitations under the License. */ #pragma once #define NOMINMAX +#define WIN32_LEAN_AND_MEAN #include +#include +#include #include @@ -25,6 +28,7 @@ limitations under the License. #include #include #include + #ifndef WATCHDOG #include #endif diff --git a/GlosSITarget/HttpServer.cpp b/GlosSITarget/HttpServer.cpp new file mode 100644 index 0000000..3d52343 --- /dev/null +++ b/GlosSITarget/HttpServer.cpp @@ -0,0 +1,47 @@ +/* +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. +*/ +#include "HttpServer.h" + +#include +#include + +#include "AppLauncher.h" + +HttpServer::HttpServer(AppLauncher& app_launcher) : app_launcher_(app_launcher) +{} + +void HttpServer::run() +{ + server_.Get("/launched-pids", [this](const httplib::Request& req, httplib::Response& res) { + const nlohmann::json j = app_launcher_.launchedPids(); + res.set_content(j.dump(), "text/json"); + }); + + server_thread_ = std::thread([this]() { + if (!server_.listen("0.0.0.0", port_)) { + spdlog::error("Couldn't start http-server"); + return; + } + spdlog::debug("Started http-server on port"); + }); +} + +void HttpServer::stop() +{ + server_.stop(); + if (server_thread_.joinable()) + server_thread_.join(); +} diff --git a/GlosSITarget/HttpServer.h b/GlosSITarget/HttpServer.h new file mode 100644 index 0000000..b23bff5 --- /dev/null +++ b/GlosSITarget/HttpServer.h @@ -0,0 +1,37 @@ +/* +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. +*/ +#pragma once +#include + +#include + +class AppLauncher; + +class HttpServer { + + public: + explicit HttpServer(AppLauncher& app_launcher); + + void run(); + void stop(); + + private: + httplib::Server server_; + std::thread server_thread_; + uint16_t port_ = 8756; + + AppLauncher& app_launcher_; +}; \ No newline at end of file diff --git a/GlosSITarget/InputRedirector.h b/GlosSITarget/InputRedirector.h index 875b155..0e74653 100644 --- a/GlosSITarget/InputRedirector.h +++ b/GlosSITarget/InputRedirector.h @@ -17,6 +17,7 @@ limitations under the License. #include #ifdef _WIN32 #define NOMINMAX +#define WIN32_LEAN_AND_MEAN #include #include #include diff --git a/GlosSITarget/Resource.rc b/GlosSITarget/Resource.rc index 3b1b5a1..ccb5d81 100644 --- a/GlosSITarget/Resource.rc +++ b/GlosSITarget/Resource.rc @@ -51,8 +51,8 @@ END // VS_VERSION_INFO VERSIONINFO - FILEVERSION 0,0,9,1040000008386 - PRODUCTVERSION 0,0,9,1040000008386 + FILEVERSION 0,1,0,203000008020 + PRODUCTVERSION 0,1,0,203000008020 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x1L @@ -69,12 +69,12 @@ BEGIN BEGIN VALUE "CompanyName", "Peter Repukat - FlatspotSoftware" VALUE "FileDescription", "GlosSI - SteamTarget" - VALUE "FileVersion", "0.0.9.1-40-gdae8386" + VALUE "FileVersion", "0.1.0.2-3-gcea802b" VALUE "InternalName", "GlosSITarget" VALUE "LegalCopyright", "Copyright (C) 2021-2022 Peter Repukat - FlatspotSoftware" VALUE "OriginalFilename", "GlosSITarget.exe" VALUE "ProductName", "GlosSI" - VALUE "ProductVersion", "0.0.9.1-40-gdae8386" + VALUE "ProductVersion", "0.1.0.2-3-gcea802b" END END BLOCK "VarFileInfo" @@ -160,6 +160,66 @@ IDI_ICON1 ICON "..\\GlosSI_Icon.ico" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/GlosSITarget/SteamTarget.cpp b/GlosSITarget/SteamTarget.cpp index 0192256..4c11e07 100644 --- a/GlosSITarget/SteamTarget.cpp +++ b/GlosSITarget/SteamTarget.cpp @@ -30,6 +30,7 @@ limitations under the License. #include #endif + SteamTarget::SteamTarget() : window_( [this] { run_ = false; }, @@ -44,13 +45,15 @@ SteamTarget::SteamTarget() launcher_(force_config_hwnds_, [this] { delayed_shutdown_ = true; delay_shutdown_clock_.restart(); - }) + }), + server_(launcher_) { target_window_handle_ = window_.getSystemHandle(); #ifdef _WIN32 if (Settings::cli.no_uwp_overlay) { UWPOverlayEnabler::AddUwpOverlayOvWidget(); - } else { + } + else { UWPOverlayEnabler::EnableUwpOverlay(); } #endif @@ -81,7 +84,7 @@ Application will not function!"); std::wstring watchDogPath(buff); watchDogPath = watchDogPath.substr(0, 1 + watchDogPath.find_last_of(L'\\')) + L"GlosSIWatchdog.dll"; - DllInjector::injectDllInto(watchDogPath, L"explorer.exe"); + DllInjector::injectDllInto(watchDogPath, L"explorer.exe"); } #endif } @@ -124,6 +127,8 @@ Application will not function!"); run_ = false; }}); + server_.run(); + while (run_) { detector_.update(); overlayHotkeyWorkaround(); @@ -140,6 +145,7 @@ Application will not function!"); } tray.exit(); + server_.stop(); #ifdef _WIN32 input_redirector_.stop(); hidhide_.disableHidHide(); diff --git a/GlosSITarget/SteamTarget.h b/GlosSITarget/SteamTarget.h index ccff866..ff39ff1 100644 --- a/GlosSITarget/SteamTarget.h +++ b/GlosSITarget/SteamTarget.h @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #pragma once +#define WIN32_LEAN_AND_MEAN #include "SteamOverlayDetector.h" @@ -27,6 +28,8 @@ limitations under the License. #include "AppLauncher.h" #include "Overlay.h" +#include "HttpServer.h" + #include @@ -81,6 +84,7 @@ class SteamTarget { std::weak_ptr overlay_; SteamOverlayDetector detector_; AppLauncher launcher_; + HttpServer server_; WindowHandle last_foreground_window_ = nullptr; static inline WindowHandle target_window_handle_ = nullptr; diff --git a/GlosSITarget/main.cpp b/GlosSITarget/main.cpp index c2ae528..65bbc06 100644 --- a/GlosSITarget/main.cpp +++ b/GlosSITarget/main.cpp @@ -14,11 +14,14 @@ See the License for the specific language governing permissions and limitations under the License. */ #ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN #define NOMINMAX #include #include #include +#include #endif +#include #include #include diff --git a/deps/cpp-httplib b/deps/cpp-httplib new file mode 160000 index 0000000..cae5a8b --- /dev/null +++ b/deps/cpp-httplib @@ -0,0 +1 @@ +Subproject commit cae5a8be1c9ef8399f45b89f6fc55afb122808cb