GrpcPrint/PrintC/Registration/Registration.cpp
2024-05-06 10:49:15 +08:00

172 lines
4.2 KiB
C++

#include "Registration.h"
#include <fstream>
//#include "../config/ConfigManager.h"
#include "../utils/TimeHelper.h"
#include "../external/QRCode/QrCode.hpp"
#include <vector>
Registration::Registration()
:m_TrailYear(0)
,m_TrailMonth(13)
,m_TrailDay(32)
{
}
Registration::~Registration()
{
}
void Registration::Init()
{
m_key.InitKey();
}
Registration::RegType Registration::CheckReg(time_t time)
{
//if (ConfigManager::Instance()->GetMachineCfg()->m_serial == m_key.get_key()) {
// if (ConfigManager::Instance()->GetMachineCfg()->m_ExpriedTime == 0) {
// return REG_SUCCESS;
// }
// else if ((time > ConfigManager::Instance()->GetMachineCfg()->m_lastShutdownTime) &&
// (time < ConfigManager::Instance()->GetMachineCfg()->m_ExpriedTime)) {
// return REG_TRIAL;
// }
//}
return REG_FAIL;
}
GLuint Registration::GetSN(char* strcode)
{
uint8_t* ptr = m_key.get_crypt();
std::vector<unsigned char> qrcode;
std::vector<uint8_t> buf;
int flag = 0;
for (int i = 0; i < 48; i++) {
buf.push_back(ptr[i]);
if (i!=47 && ((i + 1) % 16 == 0)) {
sprintf_s(strcode + flag,4, "%02X\n", ptr[i]);
flag += 3;
}
else {
sprintf_s(strcode + flag, 3, "%02X", ptr[i]);
flag += 2;
}
}
qrcodegen::QrCode qr0 = qrcodegen::QrCode::encodeBinary(buf, qrcodegen::QrCode::Ecc::MEDIUM);
for (int y = 0; y < qr0.getSize(); y++) {
for (int x = 0; x < qr0.getSize(); x++) {
if (qr0.getModule(x, y)) {
qrcode.push_back(0);
qrcode.push_back(0);
qrcode.push_back(0);
}
else {
qrcode.push_back(0xFF);
qrcode.push_back(0xFF);
qrcode.push_back(0xFF);
}
}
}
GLuint tex;
glGenTextures(1, &tex);
if (tex > 0) {
glBindTexture(GL_TEXTURE_2D, tex);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, qr0.getSize(), qr0.getSize(), 0, GL_RGB, GL_UNSIGNED_BYTE, &qrcode[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
}
return tex;
}
Registration::RegType Registration::CheckRegKey(std::string reg_file)
{
std::ifstream in_file(reg_file, std::ios::in|ios::binary);
if (!in_file.is_open())
return REG_FAIL;
char reg_key[64];
in_file.read(reg_key, 64);
in_file.close();
for (int i = 0; i < 4; i++)
m_key.aes_decrypt((uint8_t *)&reg_key[i * 16]);
string str_reg_key = std::string(reg_key, 32);
if (str_reg_key == m_key.get_key())
{
if (reg_key[32] == 'N')
{
//ConfigManager::Instance()->GetMachineCfg()->m_serial = str_reg_key;
//ConfigManager::Instance()->GetMachineCfg()->m_ExpriedTime = 0;
//ConfigManager::Instance()->SaveMachineConfig();
return REG_SUCCESS;
}
else if (reg_key[32] == 'D')
{
time_t expried_time = TimeHelper::Str2Time(&reg_key[33]);
time_t now = TimeHelper::Str2Time(TimeHelper::GetStrNow());
if (expried_time <= now)
return REG_FAIL;
else
{
//ConfigManager::Instance()->GetMachineCfg()->m_serial = str_reg_key;
//ConfigManager::Instance()->GetMachineCfg()->m_ExpriedTime = expried_time;
//ConfigManager::Instance()->SaveMachineConfig();
return REG_TRIAL;
}
}
else
{
return REG_FAIL;
}
#if 0
else if (key[32] == 'P')
{
int expried_mon = atoi((char*)&key[33]);
if (expried_mon < 0)
return REG_FAIL;
else
{
string now = TimeHelper::GetStrNow();
int year, month, day, hour, minute, second;
sscanf_s(now.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
month += expried_mon;
if (month > 12)
{
year++;
month -= 12;
}
if (month == 2)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
if (day > 29)
day = 29;
}
else if (day > 28)
day = 28;
}
string expried_time = std::to_string(year) + "-" + std::to_string(month) + "-" + std::to_string(day) + " 23:59:59";
ConfigManager::Instance()->GetMachineCfg()->m_ExpriedTime = TimeHelper::Str2Time(expried_time);
ConfigManager::Instance()->SaveMachineConfig();
return REG_TRIAL;
}
}
#endif
}
else
return REG_FAIL;
}