From a54b221bbe5200bc6283a2ae540505a92fe3d915 Mon Sep 17 00:00:00 2001 From: Changhua Date: Sat, 25 Feb 2023 02:11:04 +0800 Subject: [PATCH] Add cli --- WeChatFerry.sln | 12 +-- launcher/launcher.vcxproj | 2 +- sdk/SDK.vcxproj | 2 +- sdk/sdk.cpp | 76 +++++++++++++---- spy/Spy.vcxproj | 7 +- spy/util.cpp | 2 +- spy/util.h | 1 + {sdk => wcf}/main.cpp | 6 +- wcf/wcf.vcxproj | 172 ++++++++++++++++++++++++++++++++++++++ wcf/wcf.vcxproj.filters | 51 +++++++++++ wcf/wcf.vcxproj.user | 4 + 11 files changed, 308 insertions(+), 27 deletions(-) rename {sdk => wcf}/main.cpp (81%) create mode 100644 wcf/wcf.vcxproj create mode 100644 wcf/wcf.vcxproj.filters create mode 100644 wcf/wcf.vcxproj.user diff --git a/WeChatFerry.sln b/WeChatFerry.sln index aa5cbf3..1e8e445 100644 --- a/WeChatFerry.sln +++ b/WeChatFerry.sln @@ -15,22 +15,24 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "launcher", "launcher\launch {ABFCB647-137F-478B-A73E-F0B1E3ADC215} = {ABFCB647-137F-478B-A73E-F0B1E3ADC215} EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "wcf", "wcf\wcf.vcxproj", "{02747CE0-AD9F-4812-B019-FCF9867F7514}" + ProjectSection(ProjectDependencies) = postProject + {4DE80B82-5F6A-4C4C-9D16-1574308110FA} = {4DE80B82-5F6A-4C4C-9D16-1574308110FA} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x86 = Debug|x86 Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {4DE80B82-5F6A-4C4C-9D16-1574308110FA}.Debug|x86.ActiveCfg = Debug|Win32 - {4DE80B82-5F6A-4C4C-9D16-1574308110FA}.Debug|x86.Build.0 = Debug|Win32 {4DE80B82-5F6A-4C4C-9D16-1574308110FA}.Release|x86.ActiveCfg = Release|Win32 {4DE80B82-5F6A-4C4C-9D16-1574308110FA}.Release|x86.Build.0 = Release|Win32 - {ABFCB647-137F-478B-A73E-F0B1E3ADC215}.Debug|x86.ActiveCfg = Debug|x64 {ABFCB647-137F-478B-A73E-F0B1E3ADC215}.Release|x86.ActiveCfg = Release|Win32 {ABFCB647-137F-478B-A73E-F0B1E3ADC215}.Release|x86.Build.0 = Release|Win32 - {B11ADC6F-20DA-4DEF-A8A0-60374427D4C6}.Debug|x86.ActiveCfg = Debug|x64 {B11ADC6F-20DA-4DEF-A8A0-60374427D4C6}.Release|x86.ActiveCfg = Release|Win32 {B11ADC6F-20DA-4DEF-A8A0-60374427D4C6}.Release|x86.Build.0 = Release|Win32 + {02747CE0-AD9F-4812-B019-FCF9867F7514}.Release|x86.ActiveCfg = Release|Win32 + {02747CE0-AD9F-4812-B019-FCF9867F7514}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/launcher/launcher.vcxproj b/launcher/launcher.vcxproj index 073a34f..9268296 100644 --- a/launcher/launcher.vcxproj +++ b/launcher/launcher.vcxproj @@ -85,7 +85,7 @@ main - xcopy /y $(OutDir)launcher.exe $(SolutionDir)Out + xcopy /y $(OutDir)$(TargetFileName) $(SolutionDir)Out Copy files diff --git a/sdk/SDK.vcxproj b/sdk/SDK.vcxproj index e51793b..8c6146a 100644 --- a/sdk/SDK.vcxproj +++ b/sdk/SDK.vcxproj @@ -126,7 +126,7 @@ sdk.def - xcopy /y $(OutDir)sdk.dll $(SolutionDir)Out + xcopy /y $(OutDir)$(TargetFileName) $(SolutionDir)Out Copy files diff --git a/sdk/sdk.cpp b/sdk/sdk.cpp index db33369..5a7d51c 100644 --- a/sdk/sdk.cpp +++ b/sdk/sdk.cpp @@ -8,16 +8,16 @@ #include "sdk.h" #include "util.h" -static DWORD wcPid = 0; +#define WCF_LOCK ".wcf.lock" + +static bool debugMode = false; static HANDLE wcProcess = NULL; static HMODULE spyBase = NULL; static WCHAR spyDllPath[MAX_PATH] = { 0 }; -int WxInitSDK(bool debug) +static int GetDllPath(bool debug, wchar_t *dllPath) { - int status = 0; InitLogger(); - LOG_INFO("WxInitSDK."); GetModuleFileName(GetModuleHandle(WECHATSDKDLL), spyDllPath, MAX_PATH); PathRemoveFileSpec(spyDllPath); if (debug) { @@ -31,6 +31,19 @@ int WxInitSDK(bool debug) return ERROR_FILE_NOT_FOUND; } + return 0; +} + +int WxInitSDK(bool debug) +{ + int status = 0; + DWORD wcPid = 0; + + status = GetDllPath(debug, spyDllPath); + if (status != 0) { + return status; + } + status = OpenWeChat(&wcPid); if (status != 0) { LOG_ERROR("OpenWeChat failed: {}.", status); @@ -48,21 +61,56 @@ int WxInitSDK(bool debug) LOG_ERROR("Failed to InitSpy."); return -1; } -#if 0 - do { - if (!CallDllFunc(wcProcess, spyDllPath, spyBase, "IsLogin", (DWORD *)&status)) { - LOG_ERROR("Failed to check login status."); - return -1; - } - Sleep(1000); - } while (status == 0); + +#ifdef WCF + FILE *fd = fopen(WCF_LOCK, "wb"); + if (fd == NULL) { + LOG_ERROR("Failed to open {}.", WCF_LOCK); + return -2; + } + fwrite((uint8_t *)&debug, sizeof(debug), 1, fd); + fwrite((uint8_t *)&spyBase, sizeof(spyBase), 1, fd); + fclose(fd); #endif + debugMode = debug; + LOG_INFO("WxInitSDK done."); return 0; } int WxDestroySDK() { - LOG_INFO("WxDestroySDK"); + int status = 0; +#ifdef WCF + bool debug; + DWORD pid = GetWeChatPid(); + if (pid == 0) { + LOG_ERROR("WeChat is not running."); + return status; + } + + wcProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); + if (wcProcess == NULL) { + LOG_ERROR("WeChat is not running."); + return -1; + } + + FILE *fd = fopen(WCF_LOCK, "rb"); + if (fd == NULL) { + LOG_ERROR("Failed to open {}.", WCF_LOCK); + return -2; + } + fread((uint8_t *)&debug, sizeof(debug), 1, fd); + fread((uint8_t *)&spyBase, sizeof(spyBase), 1, fd); + fclose(fd); + status = GetDllPath(debug, spyDllPath); +#else + status = GetDllPath(debugMode, spyDllPath); +#endif + + if (status != 0) { + return status; + } + if (!CallDllFunc(wcProcess, spyDllPath, spyBase, "CleanupSpy", NULL)) { LOG_ERROR("Failed to CleanupSpy."); return -1; @@ -72,6 +120,6 @@ int WxDestroySDK() LOG_ERROR("Failed to Eject DLL."); return -1; // TODO: Unify error codes } - + LOG_INFO("WxDestroySDK done."); return 0; } diff --git a/spy/Spy.vcxproj b/spy/Spy.vcxproj index 222e66f..efdd2d3 100644 --- a/spy/Spy.vcxproj +++ b/spy/Spy.vcxproj @@ -78,7 +78,7 @@ false - false + true true @@ -163,9 +163,8 @@ C:\Tools\nanopb\protoc --nanopb_out=. wcf.proto spy.def - cd $(SolutionDir) -md Out -xcopy /y $(OutDir)$(TargetFileName) Out + md $(SolutionDir)Out +xcopy /y $(OutDir)$(TargetFileName) $(SolutionDir)Out Copy spy.dll diff --git a/spy/util.cpp b/spy/util.cpp index 2ccbd2f..5604b34 100644 --- a/spy/util.cpp +++ b/spy/util.cpp @@ -147,7 +147,7 @@ int GetWeChatVersion(wchar_t *version) return ret; } -static DWORD GetWeChatPid() +DWORD GetWeChatPid() { DWORD pid = 0; HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); diff --git a/spy/util.h b/spy/util.h index 948fbf5..56eab0e 100644 --- a/spy/util.h +++ b/spy/util.h @@ -12,6 +12,7 @@ #define GET_STRING(addr) ((CHAR *)(*(DWORD *)(addr))) #define GET_WSTRING(addr) ((WCHAR *)(*(DWORD *)(addr))) +DWORD GetWeChatPid(); int OpenWeChat(DWORD *pid); int GetWeChatVersion(wchar_t *version); int GetWstringByAddress(DWORD address, wchar_t *buffer, DWORD buffer_size); diff --git a/sdk/main.cpp b/wcf/main.cpp similarity index 81% rename from sdk/main.cpp rename to wcf/main.cpp index 7a3e90a..5e1bb25 100644 --- a/sdk/main.cpp +++ b/wcf/main.cpp @@ -1,14 +1,18 @@ #include #include +#include "framework.h" + +#include "log.h" #include "sdk.h" -void help() { printf("Usage: sdk.exe start|stop [debug]"); } +void help() { LOG_INFO("Usage: wcf.exe start|stop [debug]\n"); } int main(int argc, char *argv[]) { int ret = -1; bool debug = false; + if ((argc < 2) || (argc > 3)) { help(); } else if (argc == 3) { diff --git a/wcf/wcf.vcxproj b/wcf/wcf.vcxproj new file mode 100644 index 0000000..27db5e2 --- /dev/null +++ b/wcf/wcf.vcxproj @@ -0,0 +1,172 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {02747ce0-ad9f-4812-b019-fcf9867f7514} + wcf + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + true + x86-windows-static + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;WCF;%(PreprocessorDefinitions) + true + $(SolutionDir)sdk;$(SolutionDir)spy;C:\Tools\vcpkg\installed\x86-windows-static\include + MultiThreaded + stdcpp17 + 4996 + + + Console + true + true + true + + + xcopy /y $(OutDir)$(TargetFileName) $(SolutionDir)Out + + + Copy files + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/wcf/wcf.vcxproj.filters b/wcf/wcf.vcxproj.filters new file mode 100644 index 0000000..aee99f2 --- /dev/null +++ b/wcf/wcf.vcxproj.filters @@ -0,0 +1,51 @@ + + + + + {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 + + + + + 头文件 + + + 头文件 + + + 头文件 + + + 头文件 + + + 头文件 + + + + + 源文件 + + + 源文件 + + + 源文件 + + + 源文件 + + + 源文件 + + + \ No newline at end of file diff --git a/wcf/wcf.vcxproj.user b/wcf/wcf.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/wcf/wcf.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file