Update: drop Qt dependency, rewrite as pure Win32 using v142 toolset. Replace Update.ui/qrc and miniz-based Zip with Api/Crypto/Http/Json/Unzip/UpdateWindow modules. CrownClient/CrownServer: update Verify and main sources, retarget x64 configs to v142 (VS2019) with SDK 10.0.19041.0, fix VMProtect SDK paths.
903 lines
22 KiB
C++
903 lines
22 KiB
C++
#include "Verify.h"
|
||
#include "Global.h"
|
||
|
||
#pragma comment(lib, "bcrypt.lib")
|
||
|
||
#include <windows.h>
|
||
#include <bcrypt.h>
|
||
#include <QSysInfo>
|
||
#include <QTimer>
|
||
#include <QUrl>
|
||
|
||
// 一次网络请求最多等多久,超时就放弃,别把心跳线程卡死
|
||
#define VERIFY_REQUEST_TIMEOUT 15000
|
||
|
||
/// <summary>
|
||
/// 构造函数
|
||
/// </summary>
|
||
/// <param name="Url">verify 站点地址,如 https://gendan.xyz</param>
|
||
/// <param name="AppId">后台「应用 -> AppKey」</param>
|
||
/// <param name="CommKey">后台「应用 -> EncryptKey」</param>
|
||
/// <param name="Verison">客户端版本</param>
|
||
Verify::Verify(QString Url, QString AppId, QString CommKey, QString Verison)
|
||
{
|
||
VMProtectBegin("Verify_Init");
|
||
|
||
// 获取随机Key
|
||
this->m_randomeKey = this->GetRandom(32);
|
||
|
||
m_appId = this->ParamEncrypt(AppId);
|
||
m_commKey = this->ParamEncrypt(CommKey);
|
||
m_webApi = this->ParamEncrypt(Url);
|
||
m_machineCode = this->GetCPUID();
|
||
m_version = Verison;
|
||
// 同一台机器多开时用实例ID区分,服务端拿它做会话粒度
|
||
m_instanceId = QUuid::createUuid().toString(QUuid::WithoutBraces);
|
||
|
||
m_permanent = false;
|
||
m_balance = 0;
|
||
|
||
//qDebug() << "验证网址:" << Url;
|
||
//qDebug() << "应用AppKey:" << AppId;
|
||
//qDebug() << "软件版本:" << Verison;
|
||
//qDebug() << "机器码:" << this->GetCPUID();
|
||
VMProtectEnd();
|
||
}
|
||
|
||
Verify::~Verify()
|
||
{
|
||
|
||
}
|
||
|
||
|
||
// 获取计算机的CPU的ID
|
||
QString Verify::GetCPUID()
|
||
{
|
||
QString strCpuId = "";
|
||
unsigned int dwBuf[4] = { 0 };
|
||
unsigned long long ret = 0;
|
||
|
||
__cpuid((int*)(void*)dwBuf, 1);
|
||
|
||
ret = dwBuf[3];
|
||
ret = ret << 32;
|
||
|
||
QString str0 = QString::number(dwBuf[3], 16).toUpper();
|
||
QString str0_1 = str0.rightJustified(8, '0');
|
||
QString str1 = QString::number(dwBuf[0], 16).toUpper();
|
||
QString str1_1 = str1.rightJustified(8, '0');
|
||
strCpuId = str0_1 + str1_1;
|
||
|
||
return strCpuId;
|
||
}
|
||
|
||
// 取随机文本
|
||
QString Verify::GetRandom(int Length)
|
||
{
|
||
srand(QDateTime::currentMSecsSinceEpoch());
|
||
QString String;
|
||
const char ch[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||
int index = 0;
|
||
for (int i = 0; i < Length; ++i)
|
||
{
|
||
index = rand() % strlen(ch);
|
||
String += ch[index];
|
||
}
|
||
return String;
|
||
}
|
||
|
||
// 取随机数
|
||
int Verify::GetRandom(int Min, int Max)
|
||
{
|
||
srand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
|
||
int Random = rand();
|
||
int Number = Random % (Max - Min + 1) + Min;
|
||
return Number;
|
||
}
|
||
|
||
// 计算CRC32
|
||
unsigned int Verify::Crc32(const void* Buffer, unsigned short Length)
|
||
{
|
||
|
||
int i, j;
|
||
unsigned int CRC32;
|
||
|
||
CRC32 = 0xFFFFFFFF;
|
||
for (i = 0; i < Length; i++)
|
||
{
|
||
for (j = 0; j < 8; j++)
|
||
{
|
||
if (CRC32 & 1) {
|
||
CRC32 = (CRC32 >> 1) ^ 0xEDB88320;
|
||
}
|
||
else
|
||
{
|
||
CRC32 = (CRC32 >> 1);
|
||
}
|
||
if (((unsigned char*)Buffer)[i] & (1 << j))
|
||
{
|
||
CRC32 ^= 0xEDB88320;
|
||
}
|
||
}
|
||
}
|
||
CRC32 = ~CRC32;
|
||
return (CRC32);
|
||
}
|
||
|
||
// RC4加密
|
||
QByteArray Verify::RC4(QByteArray Data, QString Key)
|
||
{
|
||
QByteArray m, k, result;
|
||
m.resize(256);
|
||
k.resize(256);
|
||
result.resize(Data.length());
|
||
if (Data.length() < 1)
|
||
{
|
||
return "";
|
||
}
|
||
for (int i = 0; i < 256; i++)
|
||
{
|
||
m[i] = i;
|
||
}
|
||
|
||
if (Key.length() > 0)
|
||
{
|
||
int j = 0;
|
||
for (int i = 0; i < 256; i++)
|
||
{
|
||
k[i] = Key.toUtf8()[j];
|
||
j++;
|
||
if (j > Key.length() - 1)
|
||
{
|
||
j = 0;
|
||
}
|
||
}
|
||
j = 0;
|
||
for (int i = 0; i < 256; i++)
|
||
{
|
||
j = (j + m[i] + k[i]) & 255;
|
||
char temp = m[i];
|
||
m[i] = m[j];
|
||
m[j] = temp;
|
||
}
|
||
}
|
||
int i = 0, j = 0;
|
||
for (int x = 0; x < Data.length(); x++)
|
||
{
|
||
i = (i + 1) & 255;
|
||
j = (j + m[i]) & 255;
|
||
|
||
char temp = m[i];
|
||
m[i] = m[j];
|
||
m[j] = temp;
|
||
result[x] = Data[x] ^ m[(m[i] + m[j]) & 255];
|
||
}
|
||
//qDebug() << "RC4:" << result.toHex();
|
||
return result;
|
||
}
|
||
|
||
// 获取MD5
|
||
QString Verify::Md5(QByteArray Data)
|
||
{
|
||
QCryptographicHash hash(QCryptographicHash::Md5);
|
||
hash.addData(Data);
|
||
QString md5 = hash.result().toHex();
|
||
return md5;
|
||
}
|
||
|
||
QString Verify::ParamEncrypt(QString Param)
|
||
{
|
||
VMProtectBegin("Verify_Enc");
|
||
if (Param.isEmpty())
|
||
{
|
||
return "";
|
||
}
|
||
// 转为字节数组
|
||
QByteArray paramByteArray = Param.toUtf8();
|
||
// 根据字节数组指针计算CRC32
|
||
unsigned int ParamCrc32 = this->Crc32(paramByteArray.data(), Param.size());
|
||
// 参数 + CRC32
|
||
paramByteArray.append(((char*)&ParamCrc32)[0]);
|
||
paramByteArray.append(((char*)&ParamCrc32)[1]);
|
||
paramByteArray.append(((char*)&ParamCrc32)[2]);
|
||
paramByteArray.append(((char*)&ParamCrc32)[3]);
|
||
|
||
int Key = this->GetRandom(1, 65535);
|
||
QByteArray keyByteArray;
|
||
keyByteArray.append(((char*)&Key)[0]);
|
||
keyByteArray.append(((char*)&Key)[1]);
|
||
keyByteArray.append(((char*)&Key)[2]);
|
||
keyByteArray.append(((char*)&Key)[3]);
|
||
|
||
int passwordCRC32 = this->Crc32(m_randomeKey.toUtf8().data(), m_randomeKey.size());
|
||
keyByteArray.append(((char*)&passwordCRC32)[0]);
|
||
keyByteArray.append(((char*)&passwordCRC32)[1]);
|
||
keyByteArray.append(((char*)&passwordCRC32)[2]);
|
||
keyByteArray.append(((char*)&passwordCRC32)[3]);
|
||
|
||
// MD5加密
|
||
QCryptographicHash hash(QCryptographicHash::Md5);
|
||
hash.addData(keyByteArray);
|
||
// 转换为十六进制字符串
|
||
QString md5 = hash.result().toHex();
|
||
int offset = 0;
|
||
for (int i = 0; i < paramByteArray.length(); i++)
|
||
{
|
||
// 防止数组越界
|
||
if (offset > md5.length() - 1)
|
||
{
|
||
offset = 0;
|
||
}
|
||
|
||
int location = md5.mid(offset, 1).toInt(nullptr, 16) + 4;
|
||
offset++;
|
||
if (location > paramByteArray.length() - 1)
|
||
{
|
||
continue;
|
||
}
|
||
char TempA = paramByteArray[paramByteArray.size() - 1 - i];
|
||
char TempB = paramByteArray[location];
|
||
paramByteArray[paramByteArray.size() - 1 - i] = TempB;
|
||
paramByteArray[location] = TempA;
|
||
}
|
||
paramByteArray += ((char*)&offset)[0];
|
||
paramByteArray += ((char*)&offset)[1];
|
||
paramByteArray += ((char*)&Key)[0];
|
||
paramByteArray += ((char*)&Key)[1];
|
||
paramByteArray += ((char*)&Key)[2];
|
||
|
||
// XOR
|
||
for (unsigned int i = 0; i < paramByteArray.length(); i++)
|
||
{
|
||
paramByteArray[i] = paramByteArray[i] ^ ((char*)&Key)[3];
|
||
}
|
||
paramByteArray.append(((char*)&Key)[3]);
|
||
|
||
return paramByteArray.toHex();
|
||
VMProtectEnd();
|
||
|
||
}
|
||
|
||
QString Verify::ParamDecrypt(QString Param)
|
||
{
|
||
VMProtectBegin("Verify_Dec");
|
||
if (Param.isEmpty())
|
||
{
|
||
return "";
|
||
}
|
||
QByteArray paramByteArray = QByteArray::fromHex(Param.toUtf8());
|
||
char xorKey = paramByteArray[paramByteArray.size() - 1];
|
||
|
||
QByteArray UnHashData = NULL;
|
||
for (unsigned int i = 0; i < paramByteArray.size() - 1; i++)
|
||
{
|
||
UnHashData += paramByteArray[i] ^ xorKey;
|
||
}
|
||
QByteArray md5ByteArray;
|
||
|
||
// 根据字节数组指针计算CRC32
|
||
int passwordCrc32 = this->Crc32(m_randomeKey.toUtf8().data(), m_randomeKey.size());
|
||
// 参数 + CRC32
|
||
md5ByteArray.append(UnHashData[UnHashData.size() - 3]);
|
||
md5ByteArray.append(UnHashData[UnHashData.size() - 2]);
|
||
md5ByteArray.append(UnHashData[UnHashData.size() - 1]);
|
||
//qDebug() << "RValue:" << md5ByteArray.toHex();
|
||
md5ByteArray.append(xorKey);
|
||
md5ByteArray.append(((char*)&passwordCrc32)[0]);
|
||
md5ByteArray.append(((char*)&passwordCrc32)[1]);
|
||
md5ByteArray.append(((char*)&passwordCrc32)[2]);
|
||
md5ByteArray.append(((char*)&passwordCrc32)[3]);
|
||
|
||
QCryptographicHash hash(QCryptographicHash::Md5);
|
||
hash.addData(md5ByteArray);
|
||
// 转换为十六进制字符串
|
||
QString md5 = hash.result().toHex();
|
||
short last;
|
||
((char*)&last)[0] = UnHashData[UnHashData.length() - 5];
|
||
((char*)&last)[1] = UnHashData[UnHashData.length() - 4];
|
||
|
||
UnHashData = UnHashData.mid(0, UnHashData.length() - 5);
|
||
|
||
int offset = 0;
|
||
int _last = last;
|
||
bool error = false;
|
||
for (int i = 0; i < UnHashData.length(); i++)
|
||
{
|
||
if (UnHashData.length() < _last - 1)
|
||
{
|
||
break;
|
||
}
|
||
if (offset > md5.size() - 1)
|
||
{
|
||
offset = 0;
|
||
}
|
||
if (last - offset - 1 < 0)
|
||
{
|
||
last = md5.size();
|
||
offset = 0;
|
||
}
|
||
int location = md5.mid(last - offset - 1, 1).toInt(nullptr, 16) + 4;
|
||
offset = offset + 1;
|
||
if (location > UnHashData.size() - 1)
|
||
{
|
||
continue;
|
||
}
|
||
char TempA = UnHashData[i];
|
||
char TempB = UnHashData[location];
|
||
UnHashData[i] = TempB;
|
||
UnHashData[location] = TempA;
|
||
}
|
||
|
||
int crc32;
|
||
((char*)&crc32)[0] = UnHashData[UnHashData.length() - 4];
|
||
((char*)&crc32)[1] = UnHashData[UnHashData.length() - 3];
|
||
((char*)&crc32)[2] = UnHashData[UnHashData.length() - 2];
|
||
((char*)&crc32)[3] = UnHashData[UnHashData.length() - 1];
|
||
UnHashData = UnHashData.mid(0, UnHashData.length() - 4);
|
||
if (crc32 != this->Crc32(UnHashData.data(), UnHashData.length()))
|
||
{
|
||
return "";
|
||
}
|
||
|
||
return UnHashData;
|
||
VMProtectEnd();
|
||
}
|
||
|
||
// 后端 crypto.padKey 的等价实现:不足 32 字节循环补齐,超过 32 字节直接截断
|
||
static QByteArray PadAesKey(const QByteArray& Secret)
|
||
{
|
||
QByteArray key(32, '\0');
|
||
if (Secret.isEmpty())
|
||
{
|
||
return key;
|
||
}
|
||
// 后端只有 16/24/32 三种长度会原样使用,这里统一补到 32,跟它 padKey(32) 的结果一致
|
||
if (Secret.size() != 16 && Secret.size() != 24 && Secret.size() != 32)
|
||
{
|
||
for (int i = 0; i < key.size(); ++i)
|
||
{
|
||
key[i] = Secret[i % Secret.size()];
|
||
}
|
||
return key;
|
||
}
|
||
return Secret;
|
||
}
|
||
|
||
// 打开一个 GCM 模式的 AES 算法句柄
|
||
static BCRYPT_ALG_HANDLE OpenAesGcm()
|
||
{
|
||
BCRYPT_ALG_HANDLE alg = nullptr;
|
||
if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&alg, BCRYPT_AES_ALGORITHM, nullptr, 0)))
|
||
{
|
||
return nullptr;
|
||
}
|
||
ULONG modeLength = (ULONG)((wcslen(BCRYPT_CHAIN_MODE_GCM) + 1) * sizeof(wchar_t));
|
||
if (!BCRYPT_SUCCESS(BCryptSetProperty(alg, BCRYPT_CHAINING_MODE,
|
||
(PUCHAR)BCRYPT_CHAIN_MODE_GCM, modeLength, 0)))
|
||
{
|
||
BCryptCloseAlgorithmProvider(alg, 0);
|
||
return nullptr;
|
||
}
|
||
return alg;
|
||
}
|
||
|
||
// AES-256-GCM,输出 nonce(12) || 密文 || tag(16)
|
||
QByteArray Verify::AesGcmEncrypt(const QByteArray& Plain, bool& Ok)
|
||
{
|
||
VMProtectBegin("Verify_AesEnc");
|
||
Ok = false;
|
||
|
||
QByteArray key = PadAesKey(ParamDecrypt(m_commKey).toUtf8());
|
||
BCRYPT_ALG_HANDLE alg = OpenAesGcm();
|
||
if (alg == nullptr)
|
||
{
|
||
VMProtectEnd();
|
||
return QByteArray();
|
||
}
|
||
|
||
BCRYPT_KEY_HANDLE keyHandle = nullptr;
|
||
if (!BCRYPT_SUCCESS(BCryptGenerateSymmetricKey(alg, &keyHandle, nullptr, 0,
|
||
(PUCHAR)key.data(), (ULONG)key.size(), 0)))
|
||
{
|
||
BCryptCloseAlgorithmProvider(alg, 0);
|
||
VMProtectEnd();
|
||
return QByteArray();
|
||
}
|
||
|
||
const int nonceSize = 12;
|
||
const int tagSize = 16;
|
||
QByteArray nonce(nonceSize, '\0');
|
||
BCryptGenRandom(nullptr, (PUCHAR)nonce.data(), (ULONG)nonce.size(), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
|
||
|
||
QByteArray cipher(Plain.size(), '\0');
|
||
QByteArray tag(tagSize, '\0');
|
||
|
||
BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO info;
|
||
BCRYPT_INIT_AUTH_MODE_INFO(info);
|
||
info.pbNonce = (PUCHAR)nonce.data();
|
||
info.cbNonce = (ULONG)nonce.size();
|
||
info.pbTag = (PUCHAR)tag.data();
|
||
info.cbTag = (ULONG)tag.size();
|
||
|
||
ULONG written = 0;
|
||
NTSTATUS status = BCryptEncrypt(keyHandle,
|
||
(PUCHAR)Plain.data(), (ULONG)Plain.size(),
|
||
&info, nullptr, 0,
|
||
(PUCHAR)cipher.data(), (ULONG)cipher.size(), &written, 0);
|
||
|
||
BCryptDestroyKey(keyHandle);
|
||
BCryptCloseAlgorithmProvider(alg, 0);
|
||
|
||
if (!BCRYPT_SUCCESS(status) || (int)written != Plain.size())
|
||
{
|
||
VMProtectEnd();
|
||
return QByteArray();
|
||
}
|
||
|
||
Ok = true;
|
||
VMProtectEnd();
|
||
return nonce + cipher + tag;
|
||
}
|
||
|
||
// AES-256-GCM,输入 nonce(12) || 密文 || tag(16),tag 校验不过直接失败
|
||
QByteArray Verify::AesGcmDecrypt(const QByteArray& Cipher, bool& Ok)
|
||
{
|
||
VMProtectBegin("Verify_AesDec");
|
||
Ok = false;
|
||
|
||
const int nonceSize = 12;
|
||
const int tagSize = 16;
|
||
if (Cipher.size() < nonceSize + tagSize)
|
||
{
|
||
VMProtectEnd();
|
||
return QByteArray();
|
||
}
|
||
|
||
QByteArray key = PadAesKey(ParamDecrypt(m_commKey).toUtf8());
|
||
BCRYPT_ALG_HANDLE alg = OpenAesGcm();
|
||
if (alg == nullptr)
|
||
{
|
||
VMProtectEnd();
|
||
return QByteArray();
|
||
}
|
||
|
||
BCRYPT_KEY_HANDLE keyHandle = nullptr;
|
||
if (!BCRYPT_SUCCESS(BCryptGenerateSymmetricKey(alg, &keyHandle, nullptr, 0,
|
||
(PUCHAR)key.data(), (ULONG)key.size(), 0)))
|
||
{
|
||
BCryptCloseAlgorithmProvider(alg, 0);
|
||
VMProtectEnd();
|
||
return QByteArray();
|
||
}
|
||
|
||
QByteArray nonce = Cipher.left(nonceSize);
|
||
QByteArray tag = Cipher.right(tagSize);
|
||
QByteArray body = Cipher.mid(nonceSize, Cipher.size() - nonceSize - tagSize);
|
||
|
||
BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO info;
|
||
BCRYPT_INIT_AUTH_MODE_INFO(info);
|
||
info.pbNonce = (PUCHAR)nonce.data();
|
||
info.cbNonce = (ULONG)nonce.size();
|
||
info.pbTag = (PUCHAR)tag.data();
|
||
info.cbTag = (ULONG)tag.size();
|
||
|
||
QByteArray plain(body.size(), '\0');
|
||
ULONG written = 0;
|
||
NTSTATUS status = BCryptDecrypt(keyHandle,
|
||
(PUCHAR)body.data(), (ULONG)body.size(),
|
||
&info, nullptr, 0,
|
||
(PUCHAR)plain.data(), (ULONG)plain.size(), &written, 0);
|
||
|
||
BCryptDestroyKey(keyHandle);
|
||
BCryptCloseAlgorithmProvider(alg, 0);
|
||
|
||
if (!BCRYPT_SUCCESS(status))
|
||
{
|
||
VMProtectEnd();
|
||
return QByteArray();
|
||
}
|
||
|
||
Ok = true;
|
||
VMProtectEnd();
|
||
return plain;
|
||
}
|
||
|
||
QString Verify::EncryptBody(const QString& Plain, bool& Ok)
|
||
{
|
||
Ok = true;
|
||
#if VERIFY_ENCRYPT_TYPE == 2
|
||
QByteArray cipher = AesGcmEncrypt(Plain.toUtf8(), Ok);
|
||
return Ok ? QString::fromLatin1(cipher.toBase64()) : QString();
|
||
#elif VERIFY_ENCRYPT_TYPE == 1
|
||
return QString::fromLatin1(RC4(Plain.toUtf8(), ParamDecrypt(m_commKey)).toBase64());
|
||
#else
|
||
return Plain;
|
||
#endif
|
||
}
|
||
|
||
QString Verify::DecryptBody(const QString& Cipher, bool& Ok)
|
||
{
|
||
Ok = true;
|
||
#if VERIFY_ENCRYPT_TYPE == 2
|
||
QByteArray plain = AesGcmDecrypt(QByteArray::fromBase64(Cipher.toLatin1()), Ok);
|
||
return Ok ? QString::fromUtf8(plain) : QString();
|
||
#elif VERIFY_ENCRYPT_TYPE == 1
|
||
return QString::fromUtf8(RC4(QByteArray::fromBase64(Cipher.toLatin1()), ParamDecrypt(m_commKey)));
|
||
#else
|
||
return Cipher;
|
||
#endif
|
||
}
|
||
|
||
// 发一个 /api/v1/app/{appKey} 请求
|
||
// 请求体加密后放进 {"data": "..."},响应是 {"data": "..."},解密出来才是 {code,message,data}
|
||
bool Verify::Request(const QString& Path, const QJsonObject& Body, QJsonValue& Data,
|
||
QString& Result, bool WithToken, const QString& Method)
|
||
{
|
||
VMProtectBegin("Verify_Send");
|
||
|
||
if (WithToken && m_token.isEmpty())
|
||
{
|
||
Result = "未登录";
|
||
VMProtectEnd();
|
||
return false;
|
||
}
|
||
|
||
bool ok = false;
|
||
QString encrypted = EncryptBody(QString::fromUtf8(QJsonDocument(Body).toJson(QJsonDocument::Compact)), ok);
|
||
if (!ok)
|
||
{
|
||
Result = "请求加密失败";
|
||
VMProtectEnd();
|
||
return false;
|
||
}
|
||
|
||
QJsonObject envelope;
|
||
envelope.insert("data", encrypted);
|
||
|
||
QNetworkAccessManager networkAccessManager;
|
||
QNetworkRequest request;
|
||
request.setUrl(QUrl(ParamDecrypt(m_webApi) + "/api/v1/app/" + ParamDecrypt(m_appId) + Path));
|
||
// 服务端只在 Content-Type 非空时才解密请求体,这个头不能省
|
||
request.setRawHeader("Content-Type", "application/json");
|
||
if (WithToken)
|
||
{
|
||
request.setRawHeader("Authorization", ("Bearer " + m_token).toUtf8());
|
||
}
|
||
|
||
QNetworkReply* reply = (Method == "GET")
|
||
? networkAccessManager.get(request)
|
||
: networkAccessManager.post(request, QJsonDocument(envelope).toJson(QJsonDocument::Compact));
|
||
|
||
QTimer timer;
|
||
timer.setSingleShot(true);
|
||
QEventLoop eventLoop;
|
||
connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
|
||
connect(&networkAccessManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
|
||
timer.start(VERIFY_REQUEST_TIMEOUT);
|
||
eventLoop.exec();
|
||
|
||
if (!reply->isFinished())
|
||
{
|
||
reply->abort();
|
||
reply->deleteLater();
|
||
Result = "请求超时";
|
||
VMProtectEnd();
|
||
return false;
|
||
}
|
||
|
||
if (reply->error() != QNetworkReply::NoError)
|
||
{
|
||
Result = "网络错误: " + reply->errorString();
|
||
reply->deleteLater();
|
||
VMProtectEnd();
|
||
return false;
|
||
}
|
||
|
||
QByteArray responseData = reply->readAll();
|
||
reply->deleteLater();
|
||
|
||
QJsonDocument wrapped = QJsonDocument::fromJson(responseData);
|
||
if (!wrapped.isObject())
|
||
{
|
||
Result = "服务器返回格式错误";
|
||
VMProtectEnd();
|
||
return false;
|
||
}
|
||
|
||
// 应用不存在 / 应用被禁用这类错误在加密中间件之前就返回了,没有 data 信封,是明文
|
||
QJsonValue payload = wrapped.object().value("data");
|
||
QString plain;
|
||
if (payload.isString())
|
||
{
|
||
plain = DecryptBody(payload.toString(), ok);
|
||
if (!ok)
|
||
{
|
||
Result = "响应解密失败";
|
||
VMProtectEnd();
|
||
return false;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
plain = QString::fromUtf8(responseData);
|
||
}
|
||
|
||
QJsonDocument response = QJsonDocument::fromJson(plain.toUtf8());
|
||
if (!response.isObject())
|
||
{
|
||
Result = "响应解密失败";
|
||
VMProtectEnd();
|
||
return false;
|
||
}
|
||
|
||
QJsonObject jsonObject = response.object();
|
||
int code = jsonObject.value("code").toVariant().toInt();
|
||
Result = jsonObject.value("message").toVariant().toString();
|
||
if (code != 200)
|
||
{
|
||
if (Result.isEmpty())
|
||
{
|
||
Result = QString("请求失败(%1)").arg(code);
|
||
}
|
||
VMProtectEnd();
|
||
return false;
|
||
}
|
||
|
||
Data = jsonObject.value("data");
|
||
VMProtectEnd();
|
||
return true;
|
||
}
|
||
|
||
// 登录和心跳返回同一套计费字段:
|
||
// Balance == -1 -> is_permanent
|
||
// billing_type 订阅 -> expiry_at + expiry_timestamp
|
||
// 其他非免费计费 -> balance
|
||
void Verify::ParseBilling(const QJsonObject& Data)
|
||
{
|
||
m_permanent = false;
|
||
m_balance = 0;
|
||
|
||
if (Data.value("is_permanent").toVariant().toBool())
|
||
{
|
||
m_billingMode = "permanent";
|
||
m_permanent = true;
|
||
m_dueTime = QDateTime(VERIFY_NO_EXPIRY_DATE, QTime(23, 59, 59));
|
||
return;
|
||
}
|
||
|
||
if (Data.contains("expiry_at") || Data.contains("expiry_timestamp"))
|
||
{
|
||
m_billingMode = "subscription";
|
||
// 必须用 expiry_timestamp。服务器按 UTC 格式化 expiry_at,直接拿这个字符串当本地时间
|
||
// 解析会早 8 个小时,用户会被提前踢下线。expiry_at 只在拿不到时间戳时才兜底
|
||
qint64 stamp = Data.value("expiry_timestamp").toVariant().toLongLong();
|
||
if (stamp > 0)
|
||
{
|
||
m_dueTime = QDateTime::fromSecsSinceEpoch(stamp);
|
||
}
|
||
else
|
||
{
|
||
m_dueTime = QDateTime::fromString(Data.value("expiry_at").toVariant().toString(), "yyyy-MM-dd HH:mm:ss");
|
||
}
|
||
return;
|
||
}
|
||
|
||
if (Data.contains("balance"))
|
||
{
|
||
m_billingMode = "balance";
|
||
m_balance = Data.value("balance").toVariant().toDouble();
|
||
m_dueTime = QDateTime(VERIFY_NO_EXPIRY_DATE, QTime(23, 59, 59));
|
||
return;
|
||
}
|
||
|
||
// 免费应用没有计费字段,同样不存在到期一说
|
||
m_billingMode = "free";
|
||
m_dueTime = QDateTime(VERIFY_NO_EXPIRY_DATE, QTime(23, 59, 59));
|
||
}
|
||
|
||
bool Verify::Login(QString Username, QString Password, QString& result)
|
||
{
|
||
VMProtectBegin("Verify_Login");
|
||
|
||
QJsonObject JsonObject;
|
||
JsonObject.insert("username", Username);
|
||
JsonObject.insert("password", Password);
|
||
// 后台把 device_id 当必填项,用机器码顶上,和旧协议的 hardwareCode 一个作用
|
||
JsonObject.insert("device_id", m_machineCode);
|
||
JsonObject.insert("device_name", QSysInfo::machineHostName());
|
||
JsonObject.insert("device_type", "windows");
|
||
JsonObject.insert("instance_id", m_instanceId);
|
||
|
||
QJsonValue data;
|
||
if (!Request("/login", JsonObject, data, result, false))
|
||
{
|
||
VMProtectEnd();
|
||
return false;
|
||
}
|
||
|
||
QJsonObject object = data.toObject();
|
||
m_token = object.value("token").toVariant().toString();
|
||
if (m_token.isEmpty())
|
||
{
|
||
result = "登录失败:服务端没有返回令牌";
|
||
VMProtectEnd();
|
||
return false;
|
||
}
|
||
|
||
m_username = Username;
|
||
m_password = Password;
|
||
ParseBilling(object);
|
||
|
||
VMProtectEnd();
|
||
return true;
|
||
}
|
||
|
||
bool Verify::Heartbeat(QString& result)
|
||
{
|
||
VMProtectBegin("Verify_Heartbeat");
|
||
|
||
QJsonObject JsonObject;
|
||
// 心跳的身份信息全在 JWT 里,请求体只需要实例ID
|
||
JsonObject.insert("instance_id", m_instanceId);
|
||
|
||
QJsonValue data;
|
||
if (!Request("/heartbeat", JsonObject, data, result, true))
|
||
{
|
||
VMProtectEnd();
|
||
return false;
|
||
}
|
||
|
||
ParseBilling(data.toObject());
|
||
|
||
VMProtectEnd();
|
||
return true;
|
||
}
|
||
|
||
bool Verify::checkUpdate()
|
||
{
|
||
VMProtectBegin("Verify_Check");
|
||
|
||
QString result;
|
||
QJsonValue data;
|
||
if (!Request("/check-update?version=" + QUrl::toPercentEncoding(m_version),
|
||
QJsonObject(), data, result, false, "GET"))
|
||
{
|
||
// 拿不到更新信息就当是最新,别拦着主程序启动
|
||
VMProtectEnd();
|
||
return true;
|
||
}
|
||
|
||
QJsonObject object = data.toObject();
|
||
QString latest = object.value("latest_version").toVariant().toString();
|
||
if (!latest.isEmpty())
|
||
{
|
||
VMProtectEnd();
|
||
return latest == m_version;
|
||
}
|
||
|
||
VMProtectEnd();
|
||
return !object.value("has_update").toVariant().toBool();
|
||
}
|
||
|
||
QString Verify::Username()
|
||
{
|
||
return m_username;
|
||
}
|
||
|
||
QDateTime Verify::GetDueTime()
|
||
{
|
||
return m_dueTime;
|
||
}
|
||
|
||
bool Verify::IsPermanent()
|
||
{
|
||
return m_permanent;
|
||
}
|
||
|
||
double Verify::Balance()
|
||
{
|
||
return m_balance;
|
||
}
|
||
|
||
QString Verify::BillingMode()
|
||
{
|
||
return m_billingMode;
|
||
}
|
||
|
||
QString Verify::DueTimeText()
|
||
{
|
||
if (m_billingMode.isEmpty())
|
||
{
|
||
return "未获取";
|
||
}
|
||
if (m_billingMode == "permanent")
|
||
{
|
||
return "永久授权";
|
||
}
|
||
if (m_billingMode == "balance")
|
||
{
|
||
return QString("剩余点数: %1").arg(m_balance, 0, 'g', 10);
|
||
}
|
||
if (m_billingMode == "free")
|
||
{
|
||
return "无到期限制";
|
||
}
|
||
return m_dueTime.toString("yyyy-MM-dd HH:mm:ss");
|
||
}
|
||
|
||
bool Verify::ChangePassword(QString OldPassword, QString NewPassword, QString& result)
|
||
{
|
||
VMProtectBegin("Verify_ChangePwd");
|
||
|
||
QJsonObject JsonObject;
|
||
JsonObject.insert("username", m_username);
|
||
JsonObject.insert("old_password", OldPassword);
|
||
JsonObject.insert("new_password", NewPassword);
|
||
|
||
QJsonValue data;
|
||
bool ok = Request("/change-password", JsonObject, data, result, false);
|
||
if (ok)
|
||
{
|
||
m_password = NewPassword;
|
||
}
|
||
|
||
VMProtectEnd();
|
||
return ok;
|
||
}
|
||
|
||
bool Verify::Devices(QJsonArray& list, QString& result)
|
||
{
|
||
VMProtectBegin("Verify_Devices");
|
||
|
||
QJsonValue data;
|
||
if (!Request("/devices", QJsonObject(), data, result, true, "GET"))
|
||
{
|
||
VMProtectEnd();
|
||
return false;
|
||
}
|
||
|
||
list = data.toArray();
|
||
|
||
VMProtectEnd();
|
||
return true;
|
||
}
|
||
|
||
bool Verify::UnbindDevice(QString DeviceId, QString& result)
|
||
{
|
||
VMProtectBegin("Verify_Unbind");
|
||
|
||
QJsonObject JsonObject;
|
||
JsonObject.insert("device_id", DeviceId);
|
||
|
||
QJsonValue data;
|
||
bool ok = Request("/unbind-device", JsonObject, data, result, true);
|
||
|
||
VMProtectEnd();
|
||
return ok;
|
||
}
|
||
|
||
bool Verify::push_msg(QString type, QString app, QString account, QMap<QString, QString> order)
|
||
{
|
||
VMProtectBegin("Verify_Push");
|
||
|
||
QJsonObject JsonObject;
|
||
JsonObject.insert("type", type);
|
||
JsonObject.insert("app", app);
|
||
JsonObject.insert("account", account);
|
||
JsonObject.insert("time", QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
|
||
|
||
QJsonObject orderJson;
|
||
for (auto it = order.begin(); it != order.end(); ++it) {
|
||
orderJson.insert(it.key(), it.value());
|
||
}
|
||
JsonObject.insert("order", orderJson);
|
||
|
||
// 后台没有专门的推送接口,写进云端变量记录:POST /variables/{key}/records
|
||
QString result;
|
||
QJsonValue data;
|
||
bool ok = Request("/variables/" VERIFY_PUSH_VARIABLE_KEY "/records", JsonObject, data, result, true);
|
||
|
||
VMProtectEnd();
|
||
return ok;
|
||
}
|