This commit is contained in:
hugy 2023-03-31 21:20:57 +08:00
parent 61c9e6f8c1
commit 384aade1a8
9 changed files with 190 additions and 5 deletions

View File

@ -14,7 +14,7 @@ file(GLOB CPP_FILES ${PROJECT_SOURCE_DIR}/src/*.cc ${PROJECT_SOURCE_DIR}/src/*
include_directories(${VCPKG_INSTALLED_DIR}/x86-windows/include) include_directories(${VCPKG_INSTALLED_DIR}/x86-windows/include)
# add_subdirectory(3rd) # add_subdirectory(3rd)
add_subdirectory(source) # add_subdirectory(source)
find_package(nlohmann_json CONFIG REQUIRED) find_package(nlohmann_json CONFIG REQUIRED)
find_package(unofficial-mongoose CONFIG REQUIRED) find_package(unofficial-mongoose CONFIG REQUIRED)

View File

@ -31,7 +31,7 @@
using namespace std; using namespace std;
using namespace nlohmann; using namespace nlohmann;
#define STR2INT(str) (is_digit(str) ? stoi(str) : 0) // #define STR2INT(str) (is_digit(str) ? stoi(str) : 0)
#define WS2LW(wstr) (LPWSTR) wstr.c_str() #define WS2LW(wstr) (LPWSTR) wstr.c_str()
static bool kHttpRuning = false; static bool kHttpRuning = false;

View File

@ -188,3 +188,16 @@ void Hex2Bytes(const std::string &hex, BYTE *bytes) {
bytes[i] = n; bytes[i] = n;
} }
} }
bool IsDigit(string str) {
if (str.length() == 0) {
return false;
}
for (auto it : str) {
if (it < '0' || it > '9') {
return false;
}
}
return true;
}

View File

@ -2,7 +2,7 @@
#define COMMON_H_ #define COMMON_H_
#include <string> #include <string>
#define READ_WSTRING(addr, offset) ((*(DWORD *)(addr + offset + 0x4) == 0) ? std::wstring(L"") : std::wstring((wchar_t *)(*(DWORD *)(addr + offset)), *(DWORD *)(addr + offset + 0x4))) #define READ_WSTRING(addr, offset) ((*(DWORD *)(addr + offset + 0x4) == 0) ? std::wstring(L"") : std::wstring((wchar_t *)(*(DWORD *)(addr + offset)), *(DWORD *)(addr + offset + 0x4)))
#define STR2INT(str) (IsDigit(str) ? stoi(str) : 0)
/// @brief utf8 转换成unicode /// @brief utf8 转换成unicode
/// @param buffer utf8 /// @param buffer utf8
/// @return unicode /// @return unicode
@ -63,6 +63,8 @@ std::string Bytes2Hex(const BYTE *bytes, const int length);
void Hex2Bytes(const std::string &hex, BYTE *bytes); void Hex2Bytes(const std::string &hex, BYTE *bytes);
bool IsDigit(std::string str);
template <typename T1, typename T2> template <typename T1, typename T2>
std::vector<T1> split(T1 str, T2 letter) { std::vector<T1> split(T1 str, T2 letter) {
vector<T1> arr; vector<T1> arr;

View File

@ -1,12 +1,14 @@
#include "pch.h" #include "pch.h"
#include "api.h" #include "api.h"
#include "common.h" #include "common.h"
#include "wxhelper.h"
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,
LPVOID lpReserved) { LPVOID lpReserved) {
switch (ul_reason_for_call) { switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH: { case DLL_PROCESS_ATTACH: {
http_start(19088); // http_start(19088);
wxhelper::WXHelper::GetInstance().http_start(19088);
break; break;
} }
case DLL_THREAD_ATTACH: { case DLL_THREAD_ATTACH: {
@ -17,7 +19,8 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,
} }
case DLL_PROCESS_DETACH: { case DLL_PROCESS_DETACH: {
CloseConsole(); CloseConsole();
http_close(); // http_close();
wxhelper::WXHelper::GetInstance().http_close();
break; break;
} }
} }

86
src/http_server.cc Normal file
View File

@ -0,0 +1,86 @@
#include "http_server.h"
#include <nlohmann/json.hpp>
#include "api_route.h"
#include "common.h"
#include "pch.h"
#pragma comment(lib, "ws2_32.lib")
using namespace std;
using namespace nlohmann;
namespace wxhelper {
HttpServer& HttpServer::GetInstance() {
static HttpServer p;
return p;
}
void HttpServer::Init(int port) {
port_ = port;
running_ = false;
http_handler_ = new HttpHandler();
mg_mgr_init(&mgr_);
}
HttpServer::~HttpServer() {
mg_mgr_free(&mgr_);
delete http_handler_;
}
bool HttpServer::HttpStart() {
if (running_) {
return true;
}
#ifdef _DEBUG
CreateConsole();
#endif
running_ = true;
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)StartHttpServer, NULL,
NULL, 0);
return false;
}
void HttpServer::StartHttpServer() {
string lsten_addr = "http://0.0.0.0:" + to_string(GetInstance().port_);
mg_http_listen(&GetInstance().mgr_, lsten_addr.c_str(), EventHandler,
&GetInstance().mgr_);
for (;;) mg_mgr_poll(&GetInstance().mgr_, 1000);
}
void HttpServer::EventHandler(struct mg_connection *c,
int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_OPEN) {
// c->is_hexdumping = 1;
} else if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *)ev_data;
if (mg_http_match_uri(hm, "/websocket")) {
mg_ws_upgrade(c, hm, NULL);
} else if (mg_http_match_uri(hm, "/api/")) {
GetInstance().HandleHttpRequest(c,hm);
} else {
mg_http_reply(c, 500, NULL, "%s", "Invalid URI");
}
} else if (ev == MG_EV_WS_MSG) {
GetInstance().HandleWebsocketRequest(c,ev_data);
}
(void)fn_data;
}
void HttpServer::HandleHttpRequest(struct mg_connection *c,
void *ev_data) {
http_handler_->HandlerRequest(c,ev_data);
}
void HttpServer::HandleWebsocketRequest(struct mg_connection *c,
void *ev_data) {
// Got websocket frame. Received data is wm->data. Echo it back!
struct mg_ws_message *wm = (struct mg_ws_message *)ev_data;
mg_ws_send(c, wm->data.ptr, wm->data.len, WEBSOCKET_OP_TEXT);
}
bool HttpServer::HttpClose() { return false; }
} // namespace wxhelper

36
src/http_server.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef WXHELPER_HTTP_SERVER_H_
#define WXHELPER_HTTP_SERVER_H_
#include <mongoose.h>
#include "http_handler.h"
namespace wxhelper {
class HttpServer {
public:
static HttpServer &GetInstance();
bool HttpStart();
bool HttpClose();
void Init(int port);
private:
HttpServer(){}
HttpServer(const HttpServer &) = delete;
HttpServer &operator=(const HttpServer &) = delete;
~HttpServer();
static void StartHttpServer();
static void EventHandler(struct mg_connection *c, int ev, void *ev_data, void *fn_data);
void HandleHttpRequest(struct mg_connection *c, void *ev_data);
void HandleWebsocketRequest(struct mg_connection *c, void *ev_data);
private:
int port_;
bool running_;
struct mg_mgr mgr_;
HttpHandler* http_handler_;
};
} // namespace wxhelper
#endif

24
src/wxhelper.cc Normal file
View File

@ -0,0 +1,24 @@
#include "wxhelper.h"
#include <mongoose.h>
#include "pch.h"
namespace wxhelper {
WXHelper& WXHelper::GetInstance() {
static WXHelper p;
return p;
}
int WXHelper::http_start(int port) {
HttpServer::GetInstance().Init(port);
bool ret = HttpServer::GetInstance().HttpStart();
return ret == true ? 1 : 0;
}
int WXHelper::http_close() {
bool ret = HttpServer::GetInstance().HttpClose();
return ret == true ? 1 : 0;
}
} // namespace wxhelper

21
src/wxhelper.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef WXHELPER_WXHELPER_H_
#define WXHELPER_WXHELPER_H_
#include "http_server.h"
namespace wxhelper {
class WXHelper {
public:
static WXHelper &GetInstance();
int http_start(int port);
int http_close();
private:
WXHelper(){};
WXHelper(const WXHelper &) = delete;
WXHelper &operator=(const WXHelper &) = delete;
~WXHelper(){};
};
} // namespace wxhelper
#endif