diff --git a/CMakeLists.txt b/CMakeLists.txt index adcb78d..85f13b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) # add_subdirectory(3rd) -add_subdirectory(source) +# add_subdirectory(source) find_package(nlohmann_json CONFIG REQUIRED) find_package(unofficial-mongoose CONFIG REQUIRED) diff --git a/src/api.cc b/src/api.cc index a4785fe..d935d56 100644 --- a/src/api.cc +++ b/src/api.cc @@ -31,7 +31,7 @@ using namespace std; 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() static bool kHttpRuning = false; diff --git a/src/common.cc b/src/common.cc index 36a31dd..5a1e218 100644 --- a/src/common.cc +++ b/src/common.cc @@ -188,3 +188,16 @@ void Hex2Bytes(const std::string &hex, BYTE *bytes) { 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; +} \ No newline at end of file diff --git a/src/common.h b/src/common.h index 0cd0f4f..482ce56 100644 --- a/src/common.h +++ b/src/common.h @@ -2,7 +2,7 @@ #define COMMON_H_ #include #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 /// @param buffer utf8 /// @return unicode @@ -63,6 +63,8 @@ std::string Bytes2Hex(const BYTE *bytes, const int length); void Hex2Bytes(const std::string &hex, BYTE *bytes); +bool IsDigit(std::string str); + template std::vector split(T1 str, T2 letter) { vector arr; diff --git a/src/dllMain.cc b/src/dllMain.cc index 40a1cef..57880ea 100644 --- a/src/dllMain.cc +++ b/src/dllMain.cc @@ -1,12 +1,14 @@ #include "pch.h" #include "api.h" #include "common.h" +#include "wxhelper.h" BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: { - http_start(19088); + // http_start(19088); + wxhelper::WXHelper::GetInstance().http_start(19088); break; } case DLL_THREAD_ATTACH: { @@ -17,7 +19,8 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, } case DLL_PROCESS_DETACH: { CloseConsole(); - http_close(); + // http_close(); + wxhelper::WXHelper::GetInstance().http_close(); break; } } diff --git a/src/http_server.cc b/src/http_server.cc new file mode 100644 index 0000000..9e57bc1 --- /dev/null +++ b/src/http_server.cc @@ -0,0 +1,86 @@ +#include "http_server.h" + +#include + +#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 \ No newline at end of file diff --git a/src/http_server.h b/src/http_server.h new file mode 100644 index 0000000..89df650 --- /dev/null +++ b/src/http_server.h @@ -0,0 +1,36 @@ +#ifndef WXHELPER_HTTP_SERVER_H_ +#define WXHELPER_HTTP_SERVER_H_ + +#include +#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 diff --git a/src/wxhelper.cc b/src/wxhelper.cc new file mode 100644 index 0000000..73e9397 --- /dev/null +++ b/src/wxhelper.cc @@ -0,0 +1,24 @@ +#include "wxhelper.h" + +#include + +#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 \ No newline at end of file diff --git a/src/wxhelper.h b/src/wxhelper.h new file mode 100644 index 0000000..d0f60be --- /dev/null +++ b/src/wxhelper.h @@ -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 \ No newline at end of file