#include "Registration.h" #include #include "../config/ConfigManager.h" #include "../utils/TimeHelper.h" #include "../external/QRCode/QrCode.hpp" #include 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::GetInstance()->GetMachineCfg()->m_serial->GetValueStr() == m_key.get_key()) { if (ConfigManager::GetInstance()->GetMachineCfg()->m_ExpriedTime == 0) { return REG_SUCCESS; } else if ((time > ConfigManager::GetInstance()->GetMachineCfg()->m_lastShutdownTime->GetValue()) && (time < ConfigManager::GetInstance()->GetMachineCfg()->m_ExpriedTime->GetValue())) { return REG_TRIAL; } } return REG_FAIL; } GLuint Registration::GetSN(char* strcode) { uint8_t* ptr = m_key.get_crypt(); std::vector qrcode; std::vector 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 *)®_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::GetInstance()->GetMachineCfg()->m_serial->SetValue(str_reg_key); ConfigManager::GetInstance()->GetMachineCfg()->m_ExpriedTime = 0; ConfigManager::GetInstance()->SaveMachineConfig(); return REG_SUCCESS; } else if (reg_key[32] == 'D') { time_t expried_time = TimeHelper::Str2Time(®_key[33]); time_t now = TimeHelper::Str2Time(TimeHelper::GetStrNow()); if (expried_time <= now) return REG_FAIL; else { ConfigManager::GetInstance()->GetMachineCfg()->m_serial->SetValue(str_reg_key); ConfigManager::GetInstance()->GetMachineCfg()->m_ExpriedTime->SetValue(expried_time); ConfigManager::GetInstance()->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::GetInstance()->GetMachineCfg()->m_ExpriedTime = TimeHelper::Str2Time(expried_time); ConfigManager::GetInstance()->SaveMachineConfig(); return REG_TRIAL; } } #endif } else return REG_FAIL; } void Registration::CallFunc(const ReadData& rd, ::stream::ResponseAny** response) { char code[120]{ 0 }; stream::RegResponce result; REGFUNC regF = (REGFUNC)stoi(rd.nameKey); switch (regF) { case CHECKREG: result.set_data(CheckReg(stoll(rd.strValue))); (*response)->mutable_data()->PackFrom(result); printf("CHECKREG is respond...\n"); break; case GETSN: sprintf_s(code, 120, "%s", rd.strValue.c_str()); result.set_data((int)GetSN(code)); (*response)->mutable_data()->PackFrom(result); printf("GETSN is respond...\n"); break; case CHECKREGKEY: result.set_data(CheckRegKey(rd.strValue)); (*response)->mutable_data()->PackFrom(result); printf("CHECKREGKEY is respond...\n"); break; default:break; } }