From 2941b4548e00fd8205c628763f885719afccc21e Mon Sep 17 00:00:00 2001 From: Peter Repukat Date: Sun, 29 Jan 2023 13:51:23 +0100 Subject: [PATCH] Add CEFInject lib and add PoC --- .gitmodules | 3 + CEFInjectLib/CEFInject.cpp | 142 +++++++++++++++++++ CEFInjectLib/CEFInject.h | 32 +++++ CEFInjectLib/CEFInjectLib.vcxproj | 158 ++++++++++++++++++++++ CEFInjectLib/CEFInjectLib.vcxproj.filters | 27 ++++ GlosSI.sln | 13 ++ GlosSITarget/SteamTarget.cpp | 4 + deps/easywsclient | 1 + 8 files changed, 380 insertions(+) create mode 100644 CEFInjectLib/CEFInject.cpp create mode 100644 CEFInjectLib/CEFInject.h create mode 100644 CEFInjectLib/CEFInjectLib.vcxproj create mode 100644 CEFInjectLib/CEFInjectLib.vcxproj.filters create mode 160000 deps/easywsclient diff --git a/.gitmodules b/.gitmodules index ae61cba..4e86e86 100644 --- a/.gitmodules +++ b/.gitmodules @@ -37,3 +37,6 @@ [submodule "deps/cpp-httplib"] path = deps/cpp-httplib url = https://github.com/yhirose/cpp-httplib +[submodule "deps/easywsclient"] + path = deps/easywsclient + url = https://github.com/dhbaird/easywsclient diff --git a/CEFInjectLib/CEFInject.cpp b/CEFInjectLib/CEFInject.cpp new file mode 100644 index 0000000..c3a579d --- /dev/null +++ b/CEFInjectLib/CEFInject.cpp @@ -0,0 +1,142 @@ +/* +Copyright 2021-2023 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 "CEFInject.h" + + +#include + +#define _SSIZE_T_DEFINED +#include // seems like a hack to me, but eh + +#include +#include + +namespace nlohmann { + template <> + struct adl_serializer { + static void to_json(json& j, const std::wstring& str) { + j = std::wstring_convert>().to_bytes(str); + } + + static void from_json(const json& j, std::wstring& str) { + str = std::wstring_convert>().from_bytes(j.get()); + } + }; +} + +namespace CEFInject +{ + namespace internal + { + httplib::Client GetHttpClient(uint16_t port) + { + httplib::Client cli("localhost", port); + cli.set_connection_timeout(1000); + return cli; + } + + static uint32_t msg_id = 0; + + } + bool CEFDebugAvailable(uint16_t port) + { + auto cli = internal::GetHttpClient(port); + cli.set_connection_timeout(500); + cli.set_read_timeout(500); + if (auto res = cli.Get("/json")) { + if (res->status == 200) { + return true; + } + } + return false; + } + + std::vector AvailableTabs(uint16_t port) + { + std::vector tabs; + auto cli = internal::GetHttpClient(port); + if (auto res = cli.Get("/json")) { + if (res->status == 200) { + const auto json = nlohmann::json::parse(res->body); + for (const auto& j : json) { + tabs.push_back(j["title"].get()); + } + } + } + return tabs; + } + + + std::string InjectJs(const std::wstring& tabname, const std::wstring& js, uint16_t port) + { + auto cli = internal::GetHttpClient(port); + if (auto res = cli.Get("/json")) { + if (res->status == 200) { + const auto json = nlohmann::json::parse(res->body); + for (const auto& tab : json) { + if (tab["title"].get().starts_with(tabname)) { + + +#ifdef _WIN32 + INT rc; + WSADATA wsaData; + + rc = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (rc) { + printf("WSAStartup Failed.\n"); + return {}; + } +#endif + + std::shared_ptr ws{ + easywsclient::WebSocket::from_url(tab["webSocketDebuggerUrl"].get()) + }; + if (ws) + { + ws->send( + nlohmann::json{ + {"id", internal::msg_id++}, + {"method", "Runtime.evaluate"}, + {"params", { + {"userGesture", true}, + {"expression", js} + }} + }.dump()); + std::string res; + while (ws->getReadyState() != easywsclient::WebSocket::CLOSED) { + ws->poll(); + ws->dispatch([&ws, &res](const std::string& message) { + res = message; + }); + ws->close(); + } +#ifdef _WIN32 + WSACleanup(); +#endif + return res; + } +#ifdef _WIN32 + WSACleanup(); +#endif + return {}; + } + } + } + } + } + +} \ No newline at end of file diff --git a/CEFInjectLib/CEFInject.h b/CEFInjectLib/CEFInject.h new file mode 100644 index 0000000..82f4510 --- /dev/null +++ b/CEFInjectLib/CEFInject.h @@ -0,0 +1,32 @@ +/* +Copyright 2021-2023 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 + + +namespace CEFInject +{ + namespace internal { + httplib::Client GetHttpClient(uint16_t port); + } + + bool CEFDebugAvailable(uint16_t port = 8080); + std::vector AvailableTabs(uint16_t port = 8080); + std::string InjectJs(const std::wstring& tabname, const std::wstring& js, uint16_t port = 8080); + +} \ No newline at end of file diff --git a/CEFInjectLib/CEFInjectLib.vcxproj b/CEFInjectLib/CEFInjectLib.vcxproj new file mode 100644 index 0000000..f39c501 --- /dev/null +++ b/CEFInjectLib/CEFInjectLib.vcxproj @@ -0,0 +1,158 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + 16.0 + Win32Proj + {74fba967-ab7e-43ea-b561-3f4821954b3b} + CEFInjectLib + 10.0 + + + + StaticLibrary + true + v143 + Unicode + + + StaticLibrary + false + v143 + true + Unicode + + + StaticLibrary + true + v143 + Unicode + + + StaticLibrary + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + ..\deps\cpp-httplib;..\deps\json\include;..\deps\easywsclient;$(IncludePath) + + + ..\deps\cpp-httplib;..\deps\json\include;..\deps\easywsclient;$(IncludePath) + + + + Level3 + true + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + Use + pch.h + + + + + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + Use + pch.h + + + + + true + true + true + + + + + Level3 + true + _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + NotUsing + pch.h + stdcpp20 + + + + + true + + + + + Level3 + true + true + true + _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + NotUsing + pch.h + stdcpp20 + + + + + true + true + true + + + + + + \ No newline at end of file diff --git a/CEFInjectLib/CEFInjectLib.vcxproj.filters b/CEFInjectLib/CEFInjectLib.vcxproj.filters new file mode 100644 index 0000000..70e20e3 --- /dev/null +++ b/CEFInjectLib/CEFInjectLib.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + + + Source Files + + + \ No newline at end of file diff --git a/GlosSI.sln b/GlosSI.sln index 4be7ecb..8102453 100644 --- a/GlosSI.sln +++ b/GlosSI.sln @@ -4,6 +4,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 VisualStudioVersion = 17.3.32922.545 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "GlosSITarget", "GlosSITarget\GlosSITarget.vcxproj", "{076E263E-0687-4435-836E-8F4EF6668843}" + ProjectSection(ProjectDependencies) = postProject + {74FBA967-AB7E-43EA-B561-3F4821954B3B} = {74FBA967-AB7E-43EA-B561-3F4821954B3B} + EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "GlosSIConfig", "GlosSIConfig\GlosSIConfig.vcxproj", "{4B42920B-3CC6-475F-A5B3-441337968483}" EndProject @@ -11,6 +14,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UWPOverlayEnablerDLL", "UWP EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "GlosSIWatchdog", "GlosSIWatchdog\GlosSIWatchdog.vcxproj", "{BF273B90-CB69-43C8-9AF6-F3256DAFD41E}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CEFInjectLib", "CEFInjectLib\CEFInjectLib.vcxproj", "{74FBA967-AB7E-43EA-B561-3F4821954B3B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -49,6 +54,14 @@ Global {BF273B90-CB69-43C8-9AF6-F3256DAFD41E}.Release|x64.Build.0 = Release|x64 {BF273B90-CB69-43C8-9AF6-F3256DAFD41E}.Release|x86.ActiveCfg = Release|Win32 {BF273B90-CB69-43C8-9AF6-F3256DAFD41E}.Release|x86.Build.0 = Release|Win32 + {74FBA967-AB7E-43EA-B561-3F4821954B3B}.Debug|x64.ActiveCfg = Debug|x64 + {74FBA967-AB7E-43EA-B561-3F4821954B3B}.Debug|x64.Build.0 = Debug|x64 + {74FBA967-AB7E-43EA-B561-3F4821954B3B}.Debug|x86.ActiveCfg = Debug|Win32 + {74FBA967-AB7E-43EA-B561-3F4821954B3B}.Debug|x86.Build.0 = Debug|Win32 + {74FBA967-AB7E-43EA-B561-3F4821954B3B}.Release|x64.ActiveCfg = Release|x64 + {74FBA967-AB7E-43EA-B561-3F4821954B3B}.Release|x64.Build.0 = Release|x64 + {74FBA967-AB7E-43EA-B561-3F4821954B3B}.Release|x86.ActiveCfg = Release|Win32 + {74FBA967-AB7E-43EA-B561-3F4821954B3B}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/GlosSITarget/SteamTarget.cpp b/GlosSITarget/SteamTarget.cpp index 16e62f9..4767e1c 100644 --- a/GlosSITarget/SteamTarget.cpp +++ b/GlosSITarget/SteamTarget.cpp @@ -30,6 +30,7 @@ limitations under the License. #include #endif +#include SteamTarget::SteamTarget() : window_( @@ -61,6 +62,9 @@ SteamTarget::SteamTarget() int SteamTarget::run() { + + // CEFInject::InjectJs(L"Steam Shared Context presented by Valve", L"console.log('FUCK YEAH!')"); + if (!SteamOverlayDetector::IsSteamInjected()) { spdlog::warn("Steam-overlay not detected. Showing GlosSI-overlay!\n\ Application will not function!"); diff --git a/deps/easywsclient b/deps/easywsclient new file mode 160000 index 0000000..afc1d8c --- /dev/null +++ b/deps/easywsclient @@ -0,0 +1 @@ +Subproject commit afc1d8cfc584e0f1f4a77e8c0ce3e979d9fe7ce2