Make port configurable

This commit is contained in:
Changhua 2023-04-08 22:52:35 +08:00
parent dd02c4b6ff
commit b643f92681
8 changed files with 33 additions and 35 deletions

View File

@ -107,7 +107,8 @@ namespace launcher {
private: System::Void Start_Click(System::Object^ sender, System::EventArgs^ e) { private: System::Void Start_Click(System::Object^ sender, System::EventArgs^ e) {
this->Start->Enabled = false; this->Start->Enabled = false;
this->Stop->Enabled = true; this->Stop->Enabled = true;
WxInitSDK(true, "tcp://0.0.0.0:10086"); int port = 10086;
WxInitSDK(true, port);
} }
private: System::Void Stop_Click(System::Object^ sender, System::EventArgs^ e) { private: System::Void Stop_Click(System::Object^ sender, System::EventArgs^ e) {
this->Stop->Enabled = false; this->Stop->Enabled = false;

View File

@ -34,12 +34,8 @@ static int GetDllPath(bool debug, wchar_t *dllPath)
return 0; return 0;
} }
int WxInitSDK(bool debug, const char *url) int WxInitSDK(bool debug, int port)
{ {
if (url == NULL) {
return -1;
}
int status = 0; int status = 0;
DWORD wcPid = 0; DWORD wcPid = 0;
@ -60,15 +56,8 @@ int WxInitSDK(bool debug, const char *url)
LOG_ERROR("Failed to Inject DLL into WeChat."); LOG_ERROR("Failed to Inject DLL into WeChat.");
return -1; return -1;
} }
size_t urlLen = strlen(url) + 1;
LPVOID urlAddr = VirtualAllocEx(wcProcess, NULL, urlLen, MEM_COMMIT, PAGE_READWRITE);
if (urlAddr == NULL) {
LOG_ERROR("Failed to Alloc Memory.");
return NULL;
}
WriteProcessMemory(wcProcess, urlAddr, url, urlLen, NULL);
if (!CallDllFunc(wcProcess, spyDllPath, spyBase, "InitSpy", urlAddr, NULL)) { if (!CallDllFunc(wcProcess, spyDllPath, spyBase, "InitSpy", (LPVOID)port, NULL)) {
LOG_ERROR("Failed to InitSpy."); LOG_ERROR("Failed to InitSpy.");
return -1; return -1;
} }

View File

@ -1,4 +1,4 @@
#pragma once #pragma once
int WxInitSDK(bool debug, const char *url); int WxInitSDK(bool debug, int port);
int WxDestroySDK(); int WxDestroySDK();

View File

@ -31,9 +31,9 @@
#include "user_info.h" #include "user_info.h"
#include "util.h" #include "util.h"
#define URL_SIZE 20
#define BASE_URL "tcp://0.0.0.0"
#define G_BUF_SIZE (16 * 1024 * 1024) #define G_BUF_SIZE (16 * 1024 * 1024)
#define CMD_URL "tcp://0.0.0.0:10086"
#define MSG_URL "tcp://0.0.0.0:10087"
extern int IsLogin(void); // Defined in spy.cpp extern int IsLogin(void); // Defined in spy.cpp
@ -42,6 +42,7 @@ mutex gMutex;
condition_variable gCV; condition_variable gCV;
queue<WxMsg_t> gMsgQueue; queue<WxMsg_t> gMsgQueue;
static int lport = 0;
static DWORD lThreadId = 0; static DWORD lThreadId = 0;
static bool lIsRunning = false; static bool lIsRunning = false;
static nng_socket sock; static nng_socket sock;
@ -297,7 +298,9 @@ static void PushMessage()
pb_ostream_t stream = pb_ostream_from_buffer(buffer, G_BUF_SIZE); pb_ostream_t stream = pb_ostream_from_buffer(buffer, G_BUF_SIZE);
char *url = (char *)MSG_URL; char url[URL_SIZE + 1] = { 0 };
sprintf_s(url, URL_SIZE, "%s:%d", BASE_URL, lport + 1);
LOG_ERROR("URL: {}", url);
if ((rv = nng_pair1_open(&msg_sock)) != 0) { if ((rv = nng_pair1_open(&msg_sock)) != 0) {
LOG_ERROR("nng_pair0_open error {}", nng_strerror(rv)); LOG_ERROR("nng_pair0_open error {}", nng_strerror(rv));
return; return;
@ -560,9 +563,11 @@ static bool dispatcher(uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len
return ret; return ret;
} }
static int RunServer(LPVOID url) static int RunServer()
{ {
int rv = 0; int rv = 0;
char url[URL_SIZE + 1] = { 0 };
sprintf_s(url, URL_SIZE, "%s:%d", BASE_URL, lport);
if ((rv = nng_pair1_open(&sock)) != 0) { if ((rv = nng_pair1_open(&sock)) != 0) {
LOG_ERROR("nng_pair0_open error {}", nng_strerror(rv)); LOG_ERROR("nng_pair0_open error {}", nng_strerror(rv));
return rv; return rv;
@ -612,13 +617,15 @@ static int RunServer(LPVOID url)
return rv; return rv;
} }
int RpcStartServer(const char *url) int RpcStartServer(int port)
{ {
if (lIsRunning) { if (lIsRunning) {
return 0; return 0;
} }
HANDLE rpcThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)RunServer, (LPVOID)url, NULL, &lThreadId); lport = port;
HANDLE rpcThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)RunServer, NULL, NULL, &lThreadId);
if (rpcThread != 0) { if (rpcThread != 0) {
CloseHandle(rpcThread); CloseHandle(rpcThread);
} }

View File

@ -6,5 +6,5 @@
#define SPY_API __declspec(dllimport) #define SPY_API __declspec(dllimport)
#endif #endif
int RpcStartServer(const char *url); int RpcStartServer(int port);
int RpcStopServer(); int RpcStopServer();

View File

@ -7,7 +7,7 @@
WxCalls_t g_WxCalls = { 0 }; WxCalls_t g_WxCalls = { 0 };
DWORD g_WeChatWinDllAddr = 0; DWORD g_WeChatWinDllAddr = 0;
void InitSpy(const char *url) void InitSpy(int port)
{ {
wchar_t version[16] = { 0 }; wchar_t version[16] = { 0 };
InitLogger(); InitLogger();
@ -28,13 +28,9 @@ void InitSpy(const char *url)
return; return;
} }
RpcStartServer(url); RpcStartServer(port);
} }
void CleanupSpy() void CleanupSpy() { RpcStopServer(); }
{
RpcStopServer();
// FreeLibraryAndExitThread(hModule, 0);
}
int IsLogin(void) { return (int)GET_DWORD(g_WeChatWinDllAddr + g_WxCalls.login); } int IsLogin(void) { return (int)GET_DWORD(g_WeChatWinDllAddr + g_WxCalls.login); }

View File

@ -2,6 +2,5 @@
#include "framework.h" #include "framework.h"
void InitSpy(); void InitSpy(int port);
void CleanupSpy(); void CleanupSpy();
int IsLogin(void);

View File

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "framework.h" #include "framework.h"
@ -6,7 +7,10 @@
#include "log.h" #include "log.h"
#include "sdk.h" #include "sdk.h"
void help() { LOG_INFO("Usage: \n启动: wcf.exe start url [debug]\n关闭: wcf.exe stop"); } void help()
{
LOG_INFO("\nUsage: \n启动: wcf.exe start port [debug]\n关闭: wcf.exe stop\nport: 命令端口, 消息端口为命令端口+1\n");
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -16,11 +20,13 @@ int main(int argc, char *argv[])
if ((argc < 2) || (argc > 4)) { if ((argc < 2) || (argc > 4)) {
help(); help();
} else if (argc == 4) { } else if (argc == 4) {
debug = (strcmp(argv[2], "debug") == 0); debug = (strcmp(argv[3], "debug") == 0);
} }
if (strcmp(argv[1], "start") == 0) { if (strcmp(argv[1], "start") == 0) {
ret = WxInitSDK(debug, argv[2]); int port = strtol(argv[2], NULL, 10);
ret = WxInitSDK(debug, port);
} else if (strcmp(argv[1], "stop") == 0) { } else if (strcmp(argv[1], "stop") == 0) {
ret = WxDestroySDK(); ret = WxDestroySDK();
} else { } else {