feat(shell): 壳源码入库(引导/经典/DuiLib)+ 禁用调试日志落盘
This commit is contained in:
@@ -0,0 +1,403 @@
|
||||
// shell10.cpp —— NIM DuiLib 版 Windows 验证壳(多皮肤,含透明 PNG 皮肤)
|
||||
//
|
||||
// 对外接口与老壳完全一致:
|
||||
// int VerifyMain(void *oep, const char *cfgjson);
|
||||
// int VerifyShellVersion(void); // 10
|
||||
//
|
||||
// 皮肤分两类:
|
||||
// XML 皮肤(dark / light / industrial)—— 纯属性绘制,零图片
|
||||
// PNG 皮肤(nebula / onyx / aurora)—— 半透明玻璃卡片 + 发光 + 星尘,整窗按像素 alpha
|
||||
// 所有皮肤资源在启动时释放到 %TEMP%\vfytheme\<theme>\,进程结束自动清理。
|
||||
#include <windows.h>
|
||||
#include <winhttp.h>
|
||||
#include <shlobj.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "UIlib.h"
|
||||
|
||||
#pragma comment(lib, "winhttp.lib")
|
||||
|
||||
using namespace ui;
|
||||
|
||||
/* ============================== cfg ============================== */
|
||||
static char g_cfg[4096];
|
||||
static char g_api[512];
|
||||
static char g_name[128] = "\xE6\xBC\x94\xE7\xA4\xBA\xE7\xA8\x8B\xE5\xBA\x8F"; // 演示程序
|
||||
static char g_theme[32] = "dark";
|
||||
static char g_device[64] = "";
|
||||
static int g_deviceAuto = 1;
|
||||
|
||||
static int json_str(const char *src, const char *key, char *out, int cap) {
|
||||
char pat[64]; snprintf(pat, sizeof(pat), "\"%s\"", key);
|
||||
const char *p = strstr(src, pat);
|
||||
if (!p) return 0;
|
||||
p = strchr(p + strlen(pat), ':');
|
||||
if (!p) return 0;
|
||||
while (*p && *p != '"') p++;
|
||||
if (!*p) return 0;
|
||||
p++;
|
||||
int i = 0;
|
||||
while (*p && *p != '"' && i < cap - 1) out[i++] = *p++;
|
||||
out[i] = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int json_int(const char *src, const char *key, int def) {
|
||||
char pat[64]; snprintf(pat, sizeof(pat), "\"%s\"", key);
|
||||
const char *p = strstr(src, pat);
|
||||
if (!p) return def;
|
||||
p = strchr(p + strlen(pat), ':');
|
||||
if (!p) return def;
|
||||
return atoi(p + 1);
|
||||
}
|
||||
|
||||
static void make_device_id(char *out, int cap); // 定义在后面
|
||||
|
||||
static void load_cfg(const char *json) {
|
||||
snprintf(g_cfg, sizeof(g_cfg), "%s", json ? json : "");
|
||||
json_str(g_cfg, "api", g_api, sizeof(g_api));
|
||||
json_str(g_cfg, "name", g_name, sizeof(g_name));
|
||||
g_deviceAuto = json_int(g_cfg, "device_auto", 1);
|
||||
if (!json_str(g_cfg, "device", g_device, sizeof(g_device)) || !g_device[0]) {
|
||||
if (g_deviceAuto) make_device_id(g_device, sizeof(g_device));
|
||||
else snprintf(g_device, sizeof(g_device), "%s", "UNKNOWN-DEVICE");
|
||||
}
|
||||
if (!json_str(g_cfg, "theme", g_theme, sizeof(g_theme))) snprintf(g_theme, sizeof(g_theme), "dark");
|
||||
}
|
||||
|
||||
|
||||
static void LOGF(const char *fmt, ...) {
|
||||
FILE *f = fopen("C:\\vfy10.log", "a");
|
||||
if (!f) return;
|
||||
va_list ap; va_start(ap, fmt); vfprintf(f, fmt, ap); va_end(ap);
|
||||
fputc('\n', f); fclose(f);
|
||||
}
|
||||
|
||||
/* ============================ 工具 ============================ */
|
||||
static std::wstring U8(const char *s) {
|
||||
if (!s) return L"";
|
||||
int n = ::MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0);
|
||||
std::wstring w(n > 0 ? n - 1 : 0, L'\0');
|
||||
if (n > 1) ::MultiByteToWideChar(CP_UTF8, 0, s, -1, &w[0], n);
|
||||
return w;
|
||||
}
|
||||
|
||||
static std::string W2U8(const std::wstring &w) {
|
||||
if (w.empty()) return std::string();
|
||||
int n = ::WideCharToMultiByte(CP_UTF8, 0, w.c_str(), -1, NULL, 0, NULL, NULL);
|
||||
std::string s(n > 0 ? n - 1 : 0, '\0');
|
||||
if (n > 1) ::WideCharToMultiByte(CP_UTF8, 0, w.c_str(), -1, &s[0], n, NULL, NULL);
|
||||
return s;
|
||||
}
|
||||
|
||||
// MachineGuid + 系统盘卷序列号 → 稳定机器码
|
||||
static void make_device_id(char *out, int cap) {
|
||||
wchar_t guid[128] = {0};
|
||||
HKEY hk = NULL;
|
||||
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Cryptography",
|
||||
0, KEY_READ | KEY_WOW64_64KEY, &hk) == ERROR_SUCCESS) {
|
||||
DWORD sz = sizeof(guid) - sizeof(wchar_t), type = 0;
|
||||
RegQueryValueExW(hk, L"MachineGuid", NULL, &type, (LPBYTE)guid, &sz);
|
||||
RegCloseKey(hk);
|
||||
}
|
||||
DWORD serial = 0;
|
||||
GetVolumeInformationW(L"C:\\", NULL, 0, &serial, NULL, NULL, NULL, 0);
|
||||
unsigned int h1 = 2166136261u, h2 = 3735928559u;
|
||||
for (const wchar_t *p = guid; *p; p++) {
|
||||
h1 = (h1 ^ (unsigned int)(*p & 0xFF)) * 16777619u;
|
||||
h2 = (h2 ^ (unsigned int)(*p >> 8)) * 16777619u;
|
||||
}
|
||||
h1 = (h1 ^ serial) * 16777619u;
|
||||
h2 = (h2 ^ (serial >> 16)) * 16777619u;
|
||||
snprintf(out, cap, "%04X-%04X-%04X", h1 & 0xFFFF, (h1 >> 16) & 0xFFFF, h2 & 0xFFFF);
|
||||
}
|
||||
|
||||
/* ======================= 皮肤资源(内嵌) ======================= */
|
||||
// 每个皮肤 = global.xml + <theme>.xml + 可选 PNG。启动时写到临时目录。
|
||||
#include "skins_data.h" // 由 gen_skins.py 生成:SkinFile / kSkins[] / kThemeList
|
||||
|
||||
static std::wstring g_skinRoot;
|
||||
|
||||
static bool EnsureDir(const std::wstring &p) {
|
||||
return ::CreateDirectoryW(p.c_str(), NULL) != 0 || ::GetLastError() == ERROR_ALREADY_EXISTS;
|
||||
}
|
||||
|
||||
// 递归建目录:%TEMP%\vfytheme\<theme> 这类多层路径要一层层来
|
||||
static void MakeDirs(const std::wstring &path) {
|
||||
std::wstring cur;
|
||||
for (size_t i = 0; i < path.size(); i++) {
|
||||
cur += path[i];
|
||||
if (path[i] == L'\\' && cur.size() > 3) EnsureDir(cur);
|
||||
}
|
||||
EnsureDir(path);
|
||||
}
|
||||
|
||||
static void WriteFileBytes(const std::wstring &path, const void *data, unsigned int size) {
|
||||
HANDLE h = ::CreateFileW(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (h == INVALID_HANDLE_VALUE) return;
|
||||
DWORD wrote = 0;
|
||||
::WriteFile(h, data, size, &wrote, NULL);
|
||||
::CloseHandle(h);
|
||||
}
|
||||
|
||||
// 把某套皮肤的 XML/PNG 释放到临时目录 vfytheme/<theme>/
|
||||
static bool StageSkin(const char *theme) {
|
||||
wchar_t tmp[MAX_PATH] = {0};
|
||||
DWORD n = ::GetTempPathW(MAX_PATH, tmp);
|
||||
std::wstring base;
|
||||
if (n == 0 || tmp[0] == 0) {
|
||||
// 没有 TEMP:退到 exe 所在目录
|
||||
wchar_t exe[MAX_PATH] = {0};
|
||||
::GetModuleFileNameW(NULL, exe, MAX_PATH);
|
||||
base.assign(exe);
|
||||
size_t pos = base.find_last_of(L"\\");
|
||||
base = (pos != std::wstring::npos) ? base.substr(0, pos + 1) : std::wstring(L".\\");
|
||||
} else {
|
||||
base.assign(tmp); // 已经带尾部反斜杠
|
||||
}
|
||||
g_skinRoot = base + L"vfytheme";
|
||||
MakeDirs(g_skinRoot);
|
||||
|
||||
std::wstring dir = g_skinRoot + L"\\" + U8(theme);
|
||||
MakeDirs(dir);
|
||||
|
||||
bool any = false;
|
||||
for (int i = 0; kSkins[i].name != NULL; i++) {
|
||||
std::wstring fn = U8(kSkins[i].name);
|
||||
// 只释放本皮肤 + 公共 global.xml
|
||||
std::string nm = kSkins[i].name;
|
||||
if (nm != "global.xml" && nm.compare(0, strlen(theme), theme) != 0) continue;
|
||||
WriteFileBytes(dir + L"\\" + fn, kSkins[i].data, kSkins[i].size);
|
||||
any = true;
|
||||
}
|
||||
return any;
|
||||
}
|
||||
|
||||
static void CleanupSkins() {
|
||||
if (g_skinRoot.empty()) return;
|
||||
// %TEMP% 每次启动会重置同名文件,这里只删文件不删目录,留一点余量
|
||||
std::wstring pat = g_skinRoot + L"\\*";
|
||||
WIN32_FIND_DATAW fd;
|
||||
HANDLE h = ::FindFirstFileW(pat.c_str(), &fd);
|
||||
if (h != INVALID_HANDLE_VALUE) {
|
||||
do {
|
||||
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
||||
std::wstring sub = g_skinRoot + L"\\" + fd.cFileName + L"\\*";
|
||||
WIN32_FIND_DATAW fd2; HANDLE h2 = ::FindFirstFileW(sub.c_str(), &fd2);
|
||||
if (h2 != INVALID_HANDLE_VALUE) {
|
||||
do {
|
||||
::DeleteFileW((g_skinRoot + L"\\" + fd.cFileName + L"\\" + fd2.cFileName).c_str());
|
||||
} while (::FindNextFileW(h2, &fd2));
|
||||
::FindClose(h2);
|
||||
}
|
||||
::RemoveDirectoryW((g_skinRoot + L"\\" + fd.cFileName).c_str());
|
||||
}
|
||||
} while (::FindNextFileW(h, &fd));
|
||||
::FindClose(h);
|
||||
}
|
||||
::RemoveDirectoryW(g_skinRoot.c_str());
|
||||
}
|
||||
|
||||
/* ============================ 登录 ============================ */
|
||||
static int api_login(const char *code, char *msg, int cap) {
|
||||
msg[0] = 0;
|
||||
if (!g_api[0]) { snprintf(msg, cap, "cfg 里没有 api 地址"); return 0; }
|
||||
|
||||
char scheme[16] = "http://";
|
||||
const char *rest = g_api;
|
||||
if (!strncmp(g_api, "https://", 8)) { snprintf(scheme, sizeof(scheme), "https://"); rest = g_api + 8; }
|
||||
else if (!strncmp(g_api, "http://", 7)) rest = g_api + 7;
|
||||
|
||||
char host[256] = {0}, path[512] = "/";
|
||||
const char *slash = strchr(rest, '/');
|
||||
if (slash) {
|
||||
int hl = (int)(slash - rest);
|
||||
if (hl > 255) hl = 255;
|
||||
memcpy(host, rest, hl);
|
||||
snprintf(path, sizeof(path), "%s", slash);
|
||||
} else {
|
||||
snprintf(host, sizeof(host), "%s", rest);
|
||||
}
|
||||
unsigned short port = (scheme[4] == 's') ? 443 : 80;
|
||||
char *colon = strrchr(host, ':');
|
||||
if (colon) { *colon = 0; port = (unsigned short)atoi(colon + 1); }
|
||||
|
||||
char body[1024];
|
||||
snprintf(body, sizeof(body), "{\"code\":\"%s\",\"device\":\"%s\"}", code, g_device);
|
||||
|
||||
std::wstring wHost = U8(host), wPath = U8(path);
|
||||
HINTERNET hS = WinHttpOpen(L"vfy/10.0", WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0);
|
||||
if (!hS) { snprintf(msg, cap, "网络初始化失败"); return 0; }
|
||||
HINTERNET hC = WinHttpConnect(hS, wHost.c_str(), port, 0);
|
||||
HINTERNET hR = hC ? WinHttpOpenRequest(hC, L"POST", wPath.c_str(), NULL, NULL, NULL, 0) : NULL;
|
||||
int ok = 0;
|
||||
if (hR) {
|
||||
WinHttpSendRequest(hR, L"Content-Type: application/json\r\n", (DWORD)-1,
|
||||
(LPVOID)body, (DWORD)strlen(body), (DWORD)strlen(body), 0);
|
||||
WinHttpReceiveResponse(hR, NULL);
|
||||
char resp[2048] = {0};
|
||||
DWORD cl = 0;
|
||||
WinHttpReadData(hR, resp, sizeof(resp) - 1, &cl);
|
||||
resp[cl < sizeof(resp) ? cl : sizeof(resp) - 1] = 0;
|
||||
if (strstr(resp, "\"ok\":true") || strstr(resp, "\"code\":200")) ok = 1;
|
||||
if (!json_str(resp, "message", msg, cap)) {
|
||||
if (ok) snprintf(msg, cap, "验证成功");
|
||||
else if (strstr(resp, "DEVICE_LIMIT")) snprintf(msg, cap, "设备数量已达上限");
|
||||
else snprintf(msg, cap, "卡密无效或已过期");
|
||||
}
|
||||
} else {
|
||||
snprintf(msg, cap, "无法连接验证服务器");
|
||||
}
|
||||
if (hR) WinHttpCloseHandle(hR);
|
||||
if (hC) WinHttpCloseHandle(hC);
|
||||
WinHttpCloseHandle(hS);
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* ============================ 窗口 ============================ */
|
||||
typedef void (*PFN_OEP)(void);
|
||||
static PFN_OEP g_oep = NULL;
|
||||
static int g_logged_in = 0;
|
||||
|
||||
class VerifyWnd : public WindowImplBase {
|
||||
public:
|
||||
explicit VerifyWnd(const std::string &theme) : theme_(theme) {}
|
||||
|
||||
std::wstring GetSkinFolder() override { return U8(theme_.c_str()); }
|
||||
std::wstring GetSkinFile() override { return U8((theme_ + ".xml").c_str()); }
|
||||
std::wstring GetWindowClassName() const override { return L"VfyNimWnd"; }
|
||||
|
||||
// WindowImplBase::OnCreate 会用 GetStyle() 覆盖创建时的样式(默认是有边框的主窗口),
|
||||
// 验证窗必须是无边框弹窗,否则系统标题栏会盖在皮肤上。
|
||||
LONG GetStyle() override { return WS_POPUP | WS_CLIPCHILDREN; }
|
||||
|
||||
void InitWindow() override {
|
||||
if (Label *p = dynamic_cast<Label *>(FindControl(L"lblApp")))
|
||||
p->SetText(U8(g_name));
|
||||
if (Label *p = dynamic_cast<Label *>(FindControl(L"lblTip"))) {
|
||||
std::wstring t = L"设备指纹:";
|
||||
t += U8(g_device);
|
||||
p->SetText(t);
|
||||
}
|
||||
if (Button *b = dynamic_cast<Button *>(FindControl(L"btnLogin")))
|
||||
b->AttachClick([this](EventArgs *) { DoLogin(); return true; });
|
||||
if (Button *b = dynamic_cast<Button *>(FindControl(L"btnClose")))
|
||||
b->AttachClick([this](EventArgs *) { ::PostQuitMessage(0); return true; });
|
||||
if (Button *b = dynamic_cast<Button *>(FindControl(L"btnQuit")))
|
||||
b->AttachClick([this](EventArgs *) { ::PostQuitMessage(0); return true; });
|
||||
if (RichEdit *e = dynamic_cast<RichEdit *>(FindControl(L"editCard")))
|
||||
e->SetFocus();
|
||||
}
|
||||
|
||||
void DoLogin() {
|
||||
RichEdit *edit = dynamic_cast<RichEdit *>(FindControl(L"editCard"));
|
||||
Label *tip = dynamic_cast<Label *>(FindControl(L"lblTip"));
|
||||
Button *btn = dynamic_cast<Button *>(FindControl(L"btnLogin"));
|
||||
if (!edit || !tip) return;
|
||||
std::wstring code = edit->GetText();
|
||||
if (code.empty()) { tip->SetText(L"请输入卡密"); return; }
|
||||
if (btn) btn->SetText(L"验证中…");
|
||||
tip->SetText(L"正在连接验证服务器…");
|
||||
|
||||
char msg[512] = {0};
|
||||
std::string codeU8 = W2U8(code);
|
||||
int ok = api_login(codeU8.c_str(), msg, sizeof(msg));
|
||||
if (ok) {
|
||||
g_logged_in = 1;
|
||||
tip->SetText(L"验证成功,正在启动程序…");
|
||||
::SetTimer(m_hWnd, 1, 700, NULL);
|
||||
} else {
|
||||
tip->SetText(U8(msg));
|
||||
if (btn) btn->SetText(L"登 录");
|
||||
}
|
||||
}
|
||||
|
||||
LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) override {
|
||||
if (uMsg == WM_TIMER && wParam == 1) {
|
||||
::KillTimer(m_hWnd, 1);
|
||||
::ShowWindow(m_hWnd, SW_HIDE);
|
||||
if (g_oep) g_oep();
|
||||
::PostQuitMessage(0);
|
||||
return 0;
|
||||
}
|
||||
return WindowImplBase::HandleMessage(uMsg, wParam, lParam);
|
||||
}
|
||||
|
||||
private:
|
||||
std::string theme_;
|
||||
};
|
||||
|
||||
static int RunShell(void *oep) {
|
||||
LOGF("RunShell enter theme=%s", g_theme);
|
||||
g_oep = (PFN_OEP)oep;
|
||||
|
||||
std::string theme = g_theme;
|
||||
bool png = (theme == "nebula" || theme == "onyx" || theme == "aurora");
|
||||
if (!StageSkin(theme.c_str())) {
|
||||
::MessageBoxW(NULL, L"皮肤资源释放失败", L"验证", MB_ICONERROR);
|
||||
return 6;
|
||||
}
|
||||
|
||||
LOGF("skins staged at %ls", g_skinRoot.c_str());
|
||||
GlobalManager::Startup(g_skinRoot + L"\\", CreateControlCallback(), false, L"", L"");
|
||||
LOGF("GlobalManager started");
|
||||
|
||||
int w = 470, h = 330;
|
||||
if (theme == "industrial") { w = 520; h = 350; }
|
||||
if (png) { w = 520; h = 356; }
|
||||
|
||||
VerifyWnd *pwnd = new VerifyWnd(theme);
|
||||
LOGF("window obj created");
|
||||
// isLayeredWindow=true:整窗按像素 alpha,PNG 皮肤的圆角/发光才不会被裁成方块
|
||||
HWND hwnd = pwnd->Create(NULL, L"软件授权验证",
|
||||
WS_POPUP | WS_CLIPCHILDREN,
|
||||
WS_EX_TOPMOST | WS_EX_APPWINDOW,
|
||||
png, UiRect(0, 0, w, h));
|
||||
LOGF("Create returned %p", (void*)hwnd);
|
||||
if (hwnd == NULL) {
|
||||
::MessageBoxW(NULL, L"窗口创建失败", L"验证", MB_ICONERROR);
|
||||
GlobalManager::Shutdown();
|
||||
CleanupSkins();
|
||||
delete pwnd;
|
||||
return 6;
|
||||
}
|
||||
pwnd->CenterWindow();
|
||||
pwnd->ShowWindow(true, true);
|
||||
|
||||
MSG msg;
|
||||
while (::GetMessage(&msg, NULL, 0, 0)) {
|
||||
if (!pwnd->TranslateAccelerator(&msg)) {
|
||||
::TranslateMessage(&msg);
|
||||
::DispatchMessage(&msg);
|
||||
}
|
||||
}
|
||||
|
||||
GlobalManager::Shutdown();
|
||||
CleanupSkins();
|
||||
return g_logged_in ? 0 : 6;
|
||||
}
|
||||
|
||||
/* ============================ 导出 ============================ */
|
||||
#ifdef SHELL_EXE
|
||||
int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR lpCmdLine, int) {
|
||||
const char *cfg = (lpCmdLine && lpCmdLine[0]) ? lpCmdLine : getenv("VFY_CFG");
|
||||
if (!cfg || !cfg[0]) cfg = "{\"theme\":\"nebula\"}";
|
||||
load_cfg(cfg);
|
||||
return RunShell(NULL);
|
||||
}
|
||||
#else
|
||||
extern "C" __declspec(dllexport) int VerifyMain(void *oep, const char *cfgjson) {
|
||||
load_cfg(cfgjson);
|
||||
return RunShell(oep);
|
||||
}
|
||||
extern "C" __declspec(dllexport) int VerifyShellVersion(void) { return 10; }
|
||||
BOOL WINAPI DllMain(HINSTANCE h, DWORD reason, LPVOID) {
|
||||
if (reason == DLL_PROCESS_ATTACH) DisableThreadLibraryCalls(h);
|
||||
return TRUE;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user