Files
verify/shell/duilib/shell9_v5.cpp
T

931 lines
40 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// shell9.cpp —— DuiLib 版 Windows 验证壳(多套结构不同的皮肤)
//
// 与平台验证方式严格对齐(/api/v1/app/<appKey>/login):
// POST {username, password, device_id, device_name, device_type, instance_id}
// 即「账号(或卡密)+ 密码 + 设备绑定」,不是只填一个卡密框。
//
// 对外接口不变:
// int VerifyMain(void *oep, const char *cfgjson);
// int VerifyShellVersion(void); // 9
// const char *VerifyShellThemes(void); // 主题清单
#include <windows.h>
#include <winhttp.h>
#include <shlobj.h>
#include <shellapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include "UIlib.h"
#include "Utils/WinImplBase.h"
#pragma comment(lib, "winhttp.lib")
using namespace DuiLib;
/* ============================== 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 char g_instance[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_bool(const char *src, const char *key, int def) {
char pat[128];
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;
p++;
while (*p == ' ' || *p == '\t') p++;
if (!strncmp(p, "true", 4)) return 1;
if (!strncmp(p, "false", 5)) return 0;
return atoi(p) ? 1 : 0;
}
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);
}
// 后台开关:从 /info 拉下来的界面配置
static int g_allowRegister = 1, g_regVerify = 0, g_allowReset = 0;
static int g_maxDevices = 1, g_trialDays = 0, g_enableTrial = 0;
static char g_resetMethod[16] = "email", g_billing[16] = "free";
static int g_infoLoaded = 0;
static void make_machine_id(char *out, int cap);
static void load_cfg(const char *json);
/* ============================ 工具 ============================ */
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_machine_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);
}
static std::wstring AccountIniPath() {
wchar_t app[MAX_PATH] = {0};
::SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, app);
std::wstring dir = std::wstring(app) + L"\\vfy";
::CreateDirectoryW(dir.c_str(), NULL);
return dir + L"\\account.ini";
}
static void SaveAccount(const std::wstring &user, bool remember) {
std::wstring p = AccountIniPath();
::WritePrivateProfileStringW(L"login", L"remember", remember ? L"1" : L"0", p.c_str());
if (remember) ::WritePrivateProfileStringW(L"login", L"user", user.c_str(), p.c_str());
}
static std::wstring LoadAccount(bool &remember) {
std::wstring p = AccountIniPath();
wchar_t buf[256] = {0};
::GetPrivateProfileStringW(L"login", L"remember", L"0", buf, 256, p.c_str());
remember = (buf[0] == L'1');
wchar_t user[256] = {0};
::GetPrivateProfileStringW(L"login", L"user", L"", user, 256, p.c_str());
return remember ? std::wstring(user) : std::wstring();
}
/* --------------------------- 皮肤 ---------------------------
* 7 套在「结构」上彼此不同,字段集统一按平台验证方式:
* 账号(账号或卡密)/ 密码 / 记住账号 / 设备信息 / 登录 / 状态行 / 注册·卡密充值·忘记密码
* 注意:老 DuiLib 的 Edit 是「真 EDIT 子窗口」,必须用 nativebkcolor 指定它自己的底色,
* 否则聚焦时会出现刺眼的白条(这是它构造里写死 0xFFFFFFFF 导致的)。
*/
#include "skins_v4.h"
static const SkinDef *skin_for(const char *theme) {
for (int i = 0; kSkins[i].name; i++)
if (!strcmp(theme, kSkins[i].name)) return &kSkins[i];
return &kSkins[0];
}
/* ---------------------------- 接口请求 ---------------------------- */
// 平台接口:POST /api/v1/app/<appKey>/login
// {"username","password","device_id","device_name","device_type","instance_id"}
static int api_login(const char *user, const char *pass, char *msg, int cap) {
msg[0] = 0;
if (!g_api[0]) { snprintf(msg, cap, "配置缺少验证地址"); return 0; }
const char *rest = g_api;
if (!strncmp(g_api, "https://", 8)) rest = g_api + 8;
else if (!strncmp(g_api, "http://", 7)) rest = g_api + 7;
bool https = (rest != g_api && g_api[4] == 's');
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 = https ? 443 : 80;
char *colon = strrchr(host, ':');
if (colon) { *colon = 0; port = (unsigned short)atoi(colon + 1); }
char cname[256] = {0}; DWORD cn = sizeof(cname);
::GetComputerNameA(cname, &cn);
char body[2048];
snprintf(body, sizeof(body),
"{\"username\":\"%s\",\"password\":\"%s\",\"device_id\":\"%s\","
"\"device_name\":\"%s\",\"device_type\":\"windows\",\"instance_id\":\"%s\"}",
user, pass, g_device, cname, g_instance);
std::wstring wHost = U8(host), wPath = U8(path);
HINTERNET hS = WinHttpOpen(L"vfy/9.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[4096] = {0};
DWORD cl = 0, total = 0;
while (WinHttpReadData(hR, resp + total, sizeof(resp) - 1 - total, &cl) && cl > 0) {
total += cl;
if (total >= sizeof(resp) - 1) break;
}
resp[total] = 0;
if (strstr(resp, "\"code\":0") || strstr(resp, "\"code\":200") || strstr(resp, "\"success\":true")) ok = 1;
if (!json_str(resp, "message", msg, cap) || !msg[0]) {
if (strstr(resp, "DEVICE_LIMIT")) snprintf(msg, cap, "设备数量已达上限,请先在平台下线一台设备");
else if (strstr(resp, "IP_LIMIT")) snprintf(msg, cap, "IP 绑定已达上限,请解绑后再试");
else if (ok) snprintf(msg, cap, "登录成功");
else snprintf(msg, cap, "账号或密码错误");
}
} else {
snprintf(msg, cap, "无法连接验证服务器");
}
if (hR) WinHttpCloseHandle(hR);
if (hC) WinHttpCloseHandle(hC);
WinHttpCloseHandle(hS);
return ok;
}
// 注册:POST /api/v1/app/<appKey>/register
static int api_register(const char *user, const char *email, const char *code,
const char *pass, char *msg, int cap) {
msg[0] = 0;
if (!g_api[0]) { snprintf(msg, cap, "配置缺少验证地址"); return 0; }
std::string reg = g_api;
size_t pos = reg.rfind("/login");
if (pos != std::string::npos) reg = reg.substr(0, pos) + "/register";
const char *rest = reg.c_str();
bool https = false;
if (!strncmp(rest, "https://", 8)) { rest += 8; https = true; }
else if (!strncmp(rest, "http://", 7)) rest += 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 = https ? 443 : 80;
char *colon = strrchr(host, ':');
if (colon) { *colon = 0; port = (unsigned short)atoi(colon + 1); }
char cname[256] = {0}; DWORD cn = sizeof(cname);
::GetComputerNameA(cname, &cn);
char body[2048];
snprintf(body, sizeof(body),
"{\"username\":\"%s\",\"email\":\"%s\",\"email_code\":\"%s\",\"password\":\"%s\","
"\"device_id\":\"%s\",\"device_name\":\"%s\",\"device_type\":\"windows\",\"instance_id\":\"%s\"}",
user, email, code, pass, g_device, cname, g_instance);
std::wstring wHost = U8(host), wPath = U8(path);
HINTERNET hS = WinHttpOpen(L"vfy/9.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[4096] = {0}; DWORD cl = 0, total = 0;
while (WinHttpReadData(hR, resp + total, sizeof(resp) - 1 - total, &cl) && cl > 0) {
total += cl; if (total >= sizeof(resp) - 1) break;
}
resp[total] = 0;
if (strstr(resp, "\"code\":0") || strstr(resp, "\"code\":200") || strstr(resp, "\"success\":true")) ok = 1;
if (!json_str(resp, "message", msg, cap) || !msg[0])
snprintf(msg, cap, ok ? "注册成功,正在登录…" : "注册失败,请检查邮箱验证码");
} else {
snprintf(msg, cap, "无法连接验证服务器");
}
if (hR) WinHttpCloseHandle(hR);
if (hC) WinHttpCloseHandle(hC);
WinHttpCloseHandle(hS);
return ok;
}
// 发送邮箱验证码:POST /api/v1/app/<appKey>/send-email-code
static int api_send_code(const char *email, char *msg, int cap) {
msg[0] = 0;
std::string u = g_api;
size_t pos = u.rfind("/login");
if (pos != std::string::npos) u = u.substr(0, pos) + "/send-email-code";
const char *rest = u.c_str();
bool https = false;
if (!strncmp(rest, "https://", 8)) { rest += 8; https = true; }
else if (!strncmp(rest, "http://", 7)) rest += 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 = https ? 443 : 80;
char *colon = strrchr(host, ':');
if (colon) { *colon = 0; port = (unsigned short)atoi(colon + 1); }
char body[512];
snprintf(body, sizeof(body), "{\"email\":\"%s\"}", email);
std::wstring wHost = U8(host), wPath = U8(path);
HINTERNET hS = WinHttpOpen(L"vfy/9.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, "\"code\":0") || strstr(resp, "\"code\":200") || strstr(resp, "\"success\":true")) ok = 1;
if (!json_str(resp, "message", msg, cap) || !msg[0])
snprintf(msg, cap, ok ? "验证码已发送" : "发送失败");
} else {
snprintf(msg, cap, "无法连接验证服务器");
}
if (hR) WinHttpCloseHandle(hR);
if (hC) WinHttpCloseHandle(hC);
WinHttpCloseHandle(hS);
return ok;
}
// 通用 POST:把 JSON 发到 api 的某个兄弟路径(/login → /recharge 之类)
static int api_post_sibling(const char *sibling, const char *body, char *msg, int cap) {
msg[0] = 0;
std::string u = g_api;
size_t pos = u.rfind("/login");
if (pos != std::string::npos) u = u.substr(0, pos) + sibling;
const char *rest = u.c_str();
bool https = false;
if (!strncmp(rest, "https://", 8)) { rest += 8; https = true; }
else if (!strncmp(rest, "http://", 7)) rest += 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 = https ? 443 : 80;
char *colon = strrchr(host, ':');
if (colon) { *colon = 0; port = (unsigned short)atoi(colon + 1); }
std::wstring wHost = U8(host), wPath = U8(path);
HINTERNET hS = WinHttpOpen(L"vfy/9.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[4096] = {0}; DWORD cl = 0, total = 0;
while (WinHttpReadData(hR, resp + total, sizeof(resp) - 1 - total, &cl) && cl > 0) {
total += cl; if (total >= sizeof(resp) - 1) break;
}
resp[total] = 0;
if (strstr(resp, "\"code\":0") || strstr(resp, "\"code\":200") || strstr(resp, "\"success\":true")) ok = 1;
if (!json_str(resp, "message", msg, cap) || !msg[0]) {
if (ok) snprintf(msg, cap, "操作成功");
else snprintf(msg, cap, "操作失败");
}
} else {
snprintf(msg, cap, "无法连接验证服务器");
}
if (hR) WinHttpCloseHandle(hR);
if (hC) WinHttpCloseHandle(hC);
WinHttpCloseHandle(hS);
return ok;
}
// GET 兄弟路径(只读,用于拉 /info 拿到界面开关)
static int api_get_sibling(const char *sibling, char *resp, int cap) {
resp[0] = 0; cap--;
std::string u = g_api;
size_t pos = u.rfind("/login");
if (pos != std::string::npos) u = u.substr(0, pos) + sibling;
const char *rest = u.c_str();
bool https = false;
if (!strncmp(rest, "https://", 8)) { rest += 8; https = true; }
else if (!strncmp(rest, "http://", 7)) rest += 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 = https ? 443 : 80;
char *colon = strrchr(host, ':');
if (colon) { *colon = 0; port = (unsigned short)atoi(colon + 1); }
std::wstring wHost = U8(host), wPath = U8(path);
HINTERNET hS = WinHttpOpen(L"vfy/9.0", WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0);
if (!hS) return 0;
HINTERNET hC = WinHttpConnect(hS, wHost.c_str(), port, 0);
HINTERNET hR = hC ? WinHttpOpenRequest(hC, L"GET", wPath.c_str(), NULL, NULL, NULL, 0) : NULL;
int ok = 0;
if (hR) {
WinHttpSendRequest(hR, WINHTTP_NO_ADDITIONAL_HEADERS, 0, NULL, 0, 0, 0);
WinHttpReceiveResponse(hR, NULL);
DWORD cl = 0, total = 0;
while (WinHttpReadData(hR, resp + total, cap - total, &cl) && cl > 0) {
total += cl;
if ((int)total >= cap) break;
}
resp[total] = 0;
ok = total > 0;
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;
// 从 cfg 的 api 里抽出站点根地址(去掉 /api/v1/... 部分)
static std::wstring SiteRoot() {
std::string a(g_api);
size_t p = a.find("/api/");
if (p != std::string::npos) a = a.substr(0, p);
return U8(a.c_str());
}
static bool g_remember = false;
static int g_codeLeft = 0; // 验证码倒计时(秒)
static int g_tab = 0; // 0=登录 1=注册 2=充值 3=重置
// 面板内容高度 = 所有子控件固定高度之和(面板 height=auto,pos 不可靠)
static int PanelContentHeight(CControlUI *panel) {
CContainerUI *box = dynamic_cast<CContainerUI *>(panel);
if (!box) return 0;
int n = box->GetCount(), sum = 0;
for (int i = 0; i < n; i++) {
CControlUI *c = box->GetItemAt(i);
if (!c) continue;
int h = c->GetFixedHeight();
if (h <= 0) { RECT pr = c->GetPos(); h = pr.bottom - pr.top; }
if (h > 0) sum += h;
}
return sum;
}
class CVerifyWnd : public CWindowWnd, public INotifyUI {
public:
explicit CVerifyWnd(const char *skin) : m_skin(skin) {}
LPCTSTR GetWindowClassName() const override { return _T("VfyDuiWnd"); }
UINT GetClassStyle() const override { return CS_DBLCLKS; }
void Notify(TNotifyUI &msg) override {
if (msg.sType == _T("click")) {
CControlUI *p = msg.pSender;
LPCTSTR nm = p ? p->GetName().GetData() : NULL;
if (!nm) return;
if (!_tcsicmp(nm, _T("btnClose")) || !_tcsicmp(nm, _T("btnQuit"))) { ::PostQuitMessage(0); return; }
if (!_tcsicmp(nm, _T("btnLogin"))) { DoLogin(); return; }
if (!_tcsicmp(nm, _T("tabLogin"))) { SwitchTab(0); return; }
if (!_tcsicmp(nm, _T("tabRegister"))) { SwitchTab(1); return; }
if (!_tcsicmp(nm, _T("tabCard"))) { SwitchTab(2); return; }
if (!_tcsicmp(nm, _T("tabReset"))) { SwitchTab(3); return; }
if (!_tcsicmp(nm, _T("btnRecharge"))) { DoRecharge(); return; }
if (!_tcsicmp(nm, _T("btnReset"))) { DoReset(); return; }
if (!_tcsicmp(nm, _T("btnSendCode2"))) { DoSendResetCode(); return; }
if (!_tcsicmp(nm, _T("btnRegister"))) { DoRegister(); return; }
if (!_tcsicmp(nm, _T("btnSendCode"))) { DoSendCode(); return; }
if (!_tcsicmp(nm, _T("btnLinkReg")) || !_tcsicmp(nm, _T("btnLinkCard")) ||
!_tcsicmp(nm, _T("btnLinkForgot"))) {
std::wstring url = SiteRoot();
// 平台只有 /login 与 /register 是公开页:注册直开,其余引导到登录页
url += _tcsicmp(nm, _T("btnLinkReg")) == 0 ? L"/register" : L"/login";
::ShellExecuteW(NULL, L"open", url.c_str(), NULL, NULL, SW_SHOWNORMAL);
return;
}
if (!_tcsicmp(nm, _T("btnRemember"))) {
g_remember = !g_remember;
UpdateRememberButton();
return;
}
} else if (msg.sType == _T("return")) {
DoLogin();
} else if (msg.sType == _T("setfocus") || msg.sType == _T("killfocus")) {
CControlUI *p = msg.pSender;
if (!p) return;
LPCTSTR nm = p->GetName().GetData();
if (!nm || (_tcsicmp(nm, _T("editUser")) && _tcsicmp(nm, _T("editPass")))) return;
bool focus = !_tcsicmp(msg.sType.GetData(), _T("setfocus"));
if (CEditUI *e = static_cast<CEditUI *>(p)) {
DWORD bg = e->GetBkColor();
// 聚焦时把原生 EDIT 窗口底色提亮一档(纯 XML 画不出描边,用亮度做反馈)
if (focus) {
BYTE r = GetRValue(bg) + 10 > 255 ? 255 : GetRValue(bg) + 10;
BYTE g = GetGValue(bg) + 10 > 255 ? 255 : GetGValue(bg) + 10;
BYTE b = GetBValue(bg) + 14 > 255 ? 255 : GetBValue(bg) + 14;
e->SetNativeEditBkColor(RGB(r, g, b));
} else {
e->SetNativeEditBkColor(bg);
}
e->Invalidate();
}
}
}
// ---------------- 四个 tab:登录 / 注册 / 卡密充值 / 忘记密码 ----------------
void SwitchTab(int tab) {
int old = g_tab;
g_tab = tab;
const char *panels[4] = { "panelLogin", "panelRegister", "panelCard", "panelReset" };
for (int i = 0; i < 4; i++) {
if (CControlUI *p = m_pm.FindControl(U8(panels[i]).c_str()))
p->SetVisible(i == tab);
}
UpdateTabStyle();
ResizeForTab(tab, old);
}
// 面板高度不同:切换后按新面板高度改窗口高度,避免大片空白或内容被切
void ResizeForTab(int tab, int old) {
if (m_chromeH <= 0) return;
const char *panels[4] = { "panelLogin", "panelRegister", "panelCard", "panelReset" };
CControlUI *p = m_pm.FindControl(U8(panels[tab]).c_str());
if (!p) return;
int h = p->GetFixedHeight(); // 面板现在都是固定高度,直接取
if (h <= 0) h = PanelContentHeight(p);
if (h <= 0) return;
if (h <= 0) return;
int newH = m_chromeH + h + 8;
if (m_iMinH > 0 && newH < m_iMinH) newH = m_iMinH;
::SetWindowPos(m_hWnd, NULL, 0, 0, m_iWidth, newH, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
}
// 每套皮肤的 tab 表达方式不同,这里按主题分别上色
void UpdateTabStyle() {
const char *names[4] = { "tabLogin", "tabRegister", "tabCard", "tabReset" };
CButtonUI *btns[4] = { NULL, NULL, NULL, NULL };
for (int i = 0; i < 4; i++)
btns[i] = static_cast<CButtonUI *>(m_pm.FindControl(U8(names[i]).c_str()));
if (!btns[0]) return;
std::string th(g_theme);
DWORD actBg = 0, idleBg = 0, actFg = 0, idleFg = 0;
bool bgMode = true;
if (th == "dark") { bgMode = false; actFg = 0xFF4EA3E8; idleFg = 0xFF85858F; }
else if (th == "light") { actBg = 0xFF1B6FD6; idleBg = 0xFFEBEFF6; actFg = 0xFFFFFFFF; idleFg = 0xFF5B6270; }
else if (th == "split") { bgMode = false; actFg = 0xFF4CC2FF; idleFg = 0xFF7A8794; }
else if (th == "card") { actBg = 0xFFFFFFFF; idleBg = 0xFFE8E6FF; actFg = 0xFF3B2BD6; idleFg = 0xFF5B56A8; }
else if (th == "industrial") { actBg = 0xFF1A5A8C; idleBg = 0x00000000; actFg = 0xFFFFFFFF; idleFg = 0xFF9FC8E6; }
else if (th == "terminal") { bgMode = false; actFg = 0xFF7FE39B; idleFg = 0xFF4E7A5C; }
else if (th == "compact") { actBg = 0xFF2E9E6B; idleBg = 0xFF161C22; actFg = 0xFFFFFFFF; idleFg = 0xFF9FB3C8; }
for (int i = 0; i < 4; i++) {
if (!btns[i]) continue;
bool on = (i == g_tab);
if (bgMode) btns[i]->SetBkColor(on ? actBg : idleBg);
btns[i]->SetTextColor(on ? actFg : idleFg);
btns[i]->Invalidate();
}
// dark 皮肤用下划线指示条
const char *lines[4] = { "lineLogin", "lineRegister", "lineCard", "lineReset" };
for (int i = 0; i < 4; i++) {
if (CControlUI *l = m_pm.FindControl(U8(lines[i]).c_str()))
l->SetBkColor(i == g_tab ? 0xFF4EA3E8 : 0x00000000);
}
}
void DoRecharge() {
CEditUI *u = static_cast<CEditUI *>(m_pm.FindControl(_T("cardUser")));
CEditUI *k = static_cast<CEditUI *>(m_pm.FindControl(_T("cardKey")));
CLabelUI *tip = static_cast<CLabelUI *>(m_pm.FindControl(_T("cardTip")));
if (!u || !k || !tip) return;
std::wstring user = u->GetText().GetData(), key = k->GetText().GetData();
if (user.empty()) { tip->SetText(L"请输入用户名"); return; }
if (key.empty()) { tip->SetText(L"请输入卡密"); return; }
char body[1024];
snprintf(body, sizeof(body), "{\"username\":\"%s\",\"card_key\":\"%s\",\"device_id\":\"%s\"}",
W2U8(user).c_str(), W2U8(key).c_str(), g_device);
tip->SetText(L"正在提交充值…");
char msg[1024] = {0};
if (api_post_sibling("/recharge", body, msg, sizeof(msg))) {
tip->SetText(U8(msg).c_str());
if (CEditUI *lu = static_cast<CEditUI *>(m_pm.FindControl(_T("editUser")))) lu->SetText(user.c_str());
} else {
tip->SetText(U8(msg).c_str());
}
}
void DoReset() {
CEditUI *e = static_cast<CEditUI *>(m_pm.FindControl(_T("resetEmail")));
CEditUI *c = static_cast<CEditUI *>(m_pm.FindControl(_T("resetCode")));
CEditUI *p = static_cast<CEditUI *>(m_pm.FindControl(_T("resetPass")));
CLabelUI *tip = static_cast<CLabelUI *>(m_pm.FindControl(_T("resetTip")));
if (!e || !c || !p || !tip) return;
std::wstring email = e->GetText().GetData(), code = c->GetText().GetData(), pass = p->GetText().GetData();
if (email.empty()) { tip->SetText(L"请输入邮箱"); return; }
if (code.empty()) { tip->SetText(L"请输入验证码"); return; }
if (pass.size() < 6) { tip->SetText(L"新密码至少 6 位"); return; }
char body[1024];
snprintf(body, sizeof(body), "{\"email\":\"%s\",\"code\":\"%s\",\"password\":\"%s\"}",
W2U8(email).c_str(), W2U8(code).c_str(), W2U8(pass).c_str());
tip->SetText(L"正在重置…");
char msg[1024] = {0};
int ok = api_post_sibling("/reset-password", body, msg, sizeof(msg));
tip->SetText(U8(msg).c_str());
if (ok) {
if (CEditUI *lp = static_cast<CEditUI *>(m_pm.FindControl(_T("editPass")))) lp->SetText(pass.c_str());
SwitchTab(0);
}
}
void DoSendResetCode() {
CEditUI *e = static_cast<CEditUI *>(m_pm.FindControl(_T("resetEmail")));
CLabelUI *tip = static_cast<CLabelUI *>(m_pm.FindControl(_T("resetTip")));
if (!e || !tip) return;
std::wstring email = e->GetText().GetData();
if (email.empty()) { tip->SetText(L"请先填邮箱"); return; }
char msg[512] = {0};
api_send_code(W2U8(email).c_str(), msg, sizeof(msg));
tip->SetText(U8(msg).c_str());
}
void DoRegister() {
CEditUI *u = static_cast<CEditUI *>(m_pm.FindControl(_T("regUser")));
CEditUI *e = static_cast<CEditUI *>(m_pm.FindControl(_T("regEmail")));
CEditUI *c = static_cast<CEditUI *>(m_pm.FindControl(_T("regCode")));
CEditUI *p1 = static_cast<CEditUI *>(m_pm.FindControl(_T("regPass")));
CEditUI *p2 = static_cast<CEditUI *>(m_pm.FindControl(_T("regPass2")));
CLabelUI *tip = static_cast<CLabelUI *>(m_pm.FindControl(_T("regTip")));
if (!u || !e || !c || !p1 || !p2 || !tip) return;
std::wstring user = u->GetText().GetData(), email = e->GetText().GetData();
std::wstring code = c->GetText().GetData(), pass = p1->GetText().GetData();
std::wstring pass2 = p2->GetText().GetData();
if (user.empty()) { tip->SetText(L"请输入用户名"); return; }
if (email.empty()) { tip->SetText(L"请输入邮箱"); return; }
if (code.empty()) { tip->SetText(L"请输入邮箱验证码"); return; }
if (pass.size() < 6) { tip->SetText(L"密码至少 6 位"); return; }
if (pass != pass2) { tip->SetText(L"两次输入的密码不一致"); return; }
tip->SetText(L"正在提交注册…");
char msg[1024] = {0};
int ok = api_register(W2U8(user).c_str(), W2U8(email).c_str(), W2U8(code).c_str(),
W2U8(pass).c_str(), msg, sizeof(msg));
if (!ok) { tip->SetText(U8(msg).c_str()); return; }
// 注册成功直接登录
if (CEditUI *lu = static_cast<CEditUI *>(m_pm.FindControl(_T("editUser")))) lu->SetText(user.c_str());
if (CEditUI *lp = static_cast<CEditUI *>(m_pm.FindControl(_T("editPass")))) lp->SetText(pass.c_str());
SwitchTab(0);
DoLogin();
}
// ---------------- 后台开关 → 界面 ----------------
void FetchAppInfo() {
char buf[8192] = {0};
if (!api_get_sibling("/info", buf, sizeof(buf))) return;
char v[256];
if (json_str(buf, "name", v, sizeof(v)) && v[0])
snprintf(g_name, sizeof(g_name), "%s", v);
if (json_str(buf, "billing_type", v, sizeof(v)) && v[0])
snprintf(g_billing, sizeof(g_billing), "%s", v);
if (json_str(buf, "password_reset_method", v, sizeof(v)) && v[0])
snprintf(g_resetMethod, sizeof(g_resetMethod), "%s", v);
g_allowRegister = json_bool(buf, "allow_register", 1);
g_regVerify = json_bool(buf, "enable_register_verify", 0);
g_allowReset = json_bool(buf, "enable_password_reset", 0);
g_enableTrial = json_bool(buf, "enable_trial", 0);
g_maxDevices = json_int(buf, "max_devices", 1);
g_trialDays = json_int(buf, "trial_days", 0);
g_infoLoaded = 1;
ApplyAppInfo();
}
void ApplyAppInfo() {
if (!g_infoLoaded) return;
// 应用名以服务器为准
if (CLabelUI *p = static_cast<CLabelUI *>(m_pm.FindControl(_T("lblApp"))))
p->SetText(U8(g_name).c_str());
// 注册:后台关掉就整条 tab 拿掉
if (!g_allowRegister) {
HideByName("tabRegister"); HideByName("panelRegister");
if (CEditUI *b = static_cast<CEditUI *>(m_pm.FindControl(_T("regTip")))) (void)b;
if (CLabelUI *t = static_cast<CLabelUI *>(m_pm.FindControl(_T("regTip"))))
{ t->SetTextColor(0xFF8A8F99); t->SetText(L"本应用未开放注册"); }
} else if (!g_regVerify) {
// 不需要邮箱验证:把「邮箱验证码」整行收起来,注册只要账号+密码
HideByName("lblRegCode"); HideByName("regCode"); HideByName("btnSendCode");
if (CLabelUI *t = static_cast<CLabelUI *>(m_pm.FindControl(_T("regTip"))))
{ t->SetTextColor(0xFF8A8F99); t->SetText(L"本应用注册无需邮箱验证"); }
}
// 忘记密码:后台没启用就是 0 个入口,别放个点了报错的按钮
if (!g_allowReset) {
HideByName("tabReset"); HideByName("panelReset");
} else if (CLabelUI *t = static_cast<CLabelUI *>(m_pm.FindControl(_T("resetTip")))) {
t->SetTextColor(0xFF8A8F99);
t->SetText(strcmp(g_resetMethod, "email") ? L"验证码将发送到绑定手机" : L"验证码将发送到注册邮箱");
}
// 免费/试用模式没有余额概念,卡密充值不出现
if (!strcmp(g_billing, "free")) {
HideByName("tabCard"); HideByName("panelCard");
}
// 当前 tab 若已被隐藏,退回登录
if (g_tab == 1 && (!g_allowRegister || 0)) SwitchTab(0);
if (g_tab == 3 && !g_allowReset) SwitchTab(0);
if (g_tab == 2 && !strcmp(g_billing, "free")) SwitchTab(0);
}
void HideByName(const char *name) {
if (CControlUI *c = m_pm.FindControl(U8(name).c_str())) c->SetVisible(false);
}
// ---------------- 验证码倒计时 ----------------
void StartCodeCountdown() {
g_codeLeft = 60;
::SetTimer(m_hWnd, 2, 1000, NULL);
UpdateCodeButtons();
}
void UpdateCodeButtons() {
char label[32];
if (g_codeLeft > 0) snprintf(label, sizeof(label), "%ds", g_codeLeft);
const char *names[2] = { "btnSendCode", "btnSendCode2" };
for (int i = 0; i < 2; i++) {
CButtonUI *b = static_cast<CButtonUI *>(m_pm.FindControl(U8(names[i]).c_str()));
if (!b) continue;
b->SetText(g_codeLeft > 0 ? U8(label).c_str() : L"发送验证码");
b->SetEnabled(g_codeLeft <= 0);
}
}
void DoSendCode() {
CEditUI *e = static_cast<CEditUI *>(m_pm.FindControl(_T("regEmail")));
CLabelUI *tip = static_cast<CLabelUI *>(m_pm.FindControl(_T("regTip")));
if (!e || !tip) return;
std::wstring email = e->GetText().GetData();
if (email.empty()) { tip->SetText(L"请先填邮箱"); return; }
char msg[512] = {0};
if (api_send_code(W2U8(email).c_str(), msg, sizeof(msg))) StartCodeCountdown();
tip->SetText(U8(msg).c_str());
}
// 自绘勾选框:选中时方框填充主色(老 DuiLib 没有勾选图片资源,只能用色块表达)
void UpdateRememberButton() {
CLabelUI *box = static_cast<CLabelUI *>(m_pm.FindControl(_T("chkBox")));
if (!box) return;
box->SetBkColor(g_remember ? 0xFF2C7BE5 : 0x00000000);
box->SetBorderColor(g_remember ? 0xFF2C7BE5 : 0xFF5A5A66);
box->Invalidate();
}
void DoLogin() {
CEditUI *pUser = static_cast<CEditUI *>(m_pm.FindControl(_T("editUser")));
CEditUI *pPass = static_cast<CEditUI *>(m_pm.FindControl(_T("editPass")));
CLabelUI *pTip = static_cast<CLabelUI *>(m_pm.FindControl(_T("lblTip")));
CButtonUI *pBtn = static_cast<CButtonUI *>(m_pm.FindControl(_T("btnLogin")));
if (!pUser || !pPass || !pTip) return;
std::wstring user = pUser->GetText().GetData();
std::wstring pass = pPass->GetText().GetData();
if (user.empty()) { pTip->SetText(L"请输入账号或卡密"); return; }
if (pass.empty()) { pTip->SetText(L"请输入密码"); return; }
SaveAccount(user, g_remember);
if (pBtn) { pBtn->SetText(L"验证中…"); pBtn->SetEnabled(false); }
pTip->SetText(L"正在连接验证服务器…");
char msg[1024] = {0};
int ok = api_login(W2U8(user).c_str(), W2U8(pass).c_str(), msg, sizeof(msg));
if (ok) {
g_logged_in = 1;
pTip->SetText(L"验证成功,正在启动程序…");
::SetTimer(m_hWnd, 1, 600, NULL);
} else {
std::wstring tipText = U8(msg);
if (tipText.find(L"设备") != std::wstring::npos && tipText.find(L"上限") != std::wstring::npos) {
wchar_t extra[128];
swprintf(extra, 128, L"(本应用上限 %d 台,可在官网「我的设备」解绑后重试)", g_maxDevices);
tipText += extra;
}
pTip->SetText(tipText.c_str());
if (pBtn) { pBtn->SetText(L"登 录"); pBtn->SetEnabled(true); }
}
}
LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) override {
if (uMsg == WM_CREATE) {
m_pm.Init(m_hWnd);
CDialogBuilder builder;
std::wstring xml = U8(m_skin);
CControlUI *pRoot = builder.Create(xml.c_str(), (UINT)0, NULL, &m_pm);
if (!pRoot) { ::MessageBoxW(m_hWnd, L"界面加载失败", L"验证", MB_ICONERROR); return 0; }
m_pm.AttachDialog(pRoot);
m_pm.AddNotifier(this);
if (CLabelUI *p = static_cast<CLabelUI *>(m_pm.FindControl(_T("lblApp"))))
p->SetText(U8(g_name).c_str());
std::wstring devText = L"设备 ";
devText += U8(g_device);
if (CLabelUI *p = static_cast<CLabelUI *>(m_pm.FindControl(_T("lblDevice"))))
p->SetText(devText.c_str());
bool remember = false;
std::wstring saved = LoadAccount(remember);
g_remember = remember;
UpdateRememberButton();
RECT rc = {0};
::GetClientRect(m_hWnd, &rc);
m_iWidth = rc.right - rc.left;
CControlUI *lp = m_pm.FindControl(_T("panelLogin"));
int loginH = lp ? lp->GetFixedHeight() : 0;
if (loginH > 0) m_chromeH = (rc.bottom - rc.top) - loginH;
SwitchTab(0);
FetchAppInfo(); // 按后台设置裁剪界面(注册/重置/充值 tab、注册验证码)
if (CEditUI *pUser = static_cast<CEditUI *>(m_pm.FindControl(_T("editUser")))) {
if (!saved.empty()) pUser->SetText(saved.c_str());
pUser->SetFocus();
}
return 0;
}
if (uMsg == WM_TIMER && wParam == 2) {
if (g_codeLeft > 0) g_codeLeft--;
if (g_codeLeft <= 0) { g_codeLeft = 0; ::KillTimer(m_hWnd, 2); }
UpdateCodeButtons();
return 0;
}
if (uMsg == WM_TIMER && wParam == 1) {
::KillTimer(m_hWnd, 1);
::ShowWindow(m_hWnd, SW_HIDE);
if (g_oep) g_oep();
::PostQuitMessage(0);
return 0;
}
if (uMsg == WM_DESTROY) { ::PostQuitMessage(0); return 0; }
LRESULT lr = 0;
if (m_pm.MessageHandler(uMsg, wParam, lParam, lr)) return lr;
return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
}
CPaintManagerUI m_pm;
const char *m_skin;
int m_chromeH = 0; // 除面板外的固定高度(标题栏 + tab 行 + 页脚)
int m_iWidth = 0;
int m_iMinH = 380;
};
static int RunShell(void *oep) {
g_oep = (PFN_OEP)oep;
const SkinDef *sk = skin_for(g_theme);
CPaintManagerUI::SetInstance(::GetModuleHandle(NULL));
::CoInitialize(NULL);
CVerifyWnd wnd(sk->xml);
UINT style = WS_POPUP | WS_CLIPCHILDREN;
UINT exstyle = WS_EX_TOPMOST | WS_EX_APPWINDOW;
wnd.Create(NULL, L"软件授权验证", style, exstyle, 0, 0, sk->w, sk->h);
wnd.CenterWindow();
::ShowWindow(wnd, SW_SHOW);
CPaintManagerUI::MessageLoop();
::CoUninitialize();
return g_logged_in ? 0 : 6;
}
/* ---------------------------- 对外接口 ---------------------------- */
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);
char mid[64] = {0};
make_machine_id(mid, sizeof(mid));
snprintf(g_instance, sizeof(g_instance), "%s", mid);
if (!json_str(g_cfg, "device", g_device, sizeof(g_device)) || !g_device[0]) {
if (g_deviceAuto) snprintf(g_device, sizeof(g_device), "%s", mid);
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");
}
#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\":\"dark\"}";
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 9; }
extern "C" __declspec(dllexport) const char *VerifyShellThemes(void) {
return "dark,light,split,card,industrial,terminal,compact";
}
BOOL WINAPI DllMain(HINSTANCE h, DWORD reason, LPVOID) {
if (reason == DLL_PROCESS_ATTACH) DisableThreadLibraryCalls(h);
return TRUE;
}
#endif