179 lines
3.4 KiB
C++
179 lines
3.4 KiB
C++
|
#include "Token.h"
|
||
|
|
||
|
#include <time.h>
|
||
|
#include <fstream>
|
||
|
|
||
|
#ifdef _WIN32
|
||
|
#include <windows.h>
|
||
|
#include <Shlobj.h>
|
||
|
#elif __linux__
|
||
|
#include <unistd.h>
|
||
|
#elif __APPLE__
|
||
|
#include <sys/sysctl.h>
|
||
|
#endif
|
||
|
|
||
|
#include "rapidjson/rapidjson.h"
|
||
|
#include "rapidjson/document.h"
|
||
|
#include "rapidjson/stringbuffer.h"
|
||
|
#include "rapidjson/writer.h"
|
||
|
|
||
|
#include "Request.h"
|
||
|
#include "Licensed.h"
|
||
|
#include "Util.h"
|
||
|
|
||
|
|
||
|
#define RAND(a, b) ((rand() % (b-a))+ a + 1)
|
||
|
constexpr char* LICENSED_FILE_NAME = "art/config.dat";
|
||
|
constexpr int32_t NERVER = 1000000;
|
||
|
|
||
|
Token::Token(const std::string& token) noexcept
|
||
|
: m_token(token){
|
||
|
}
|
||
|
|
||
|
Token::Token() noexcept {
|
||
|
}
|
||
|
|
||
|
Token::~Token() noexcept {
|
||
|
}
|
||
|
|
||
|
Token Token::FromConfig() {
|
||
|
std::string dllPath = GetDllPath();
|
||
|
std::ifstream in(dllPath + LICENSED_FILE_NAME, std::ios::in);
|
||
|
if (!in.is_open()) {
|
||
|
return Token("");
|
||
|
}
|
||
|
|
||
|
std::string token;
|
||
|
in >> token;
|
||
|
in.close();
|
||
|
|
||
|
|
||
|
Token t;
|
||
|
t.decode(token);
|
||
|
return t;
|
||
|
}
|
||
|
|
||
|
Token Token::FromServer(const std::string& token) {
|
||
|
Token t(token);
|
||
|
const std::string key = t.encode();
|
||
|
|
||
|
rapidjson::StringBuffer buffer;
|
||
|
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
|
||
|
writer.StartObject();
|
||
|
writer.Key("code");
|
||
|
writer.String(key.c_str(), key.length());
|
||
|
writer.EndObject();
|
||
|
|
||
|
const std::string body = buffer.GetString();
|
||
|
Request::Ptr request = Request::Create();
|
||
|
if (!request->Post("/token/token", body)) {
|
||
|
Token localToken = Token::FromConfig();
|
||
|
if (Token::CodeType::Nerver == localToken.GetCode()) {
|
||
|
return localToken;
|
||
|
}
|
||
|
|
||
|
Licensed::Instance().UpdateGetTokenCount(false);
|
||
|
return localToken;
|
||
|
}
|
||
|
|
||
|
static int count = 0;
|
||
|
if (!request->IsSuccess()) {
|
||
|
Token localToken = Token::FromConfig();
|
||
|
if (Token::CodeType::Nerver == localToken.GetCode()) {
|
||
|
return localToken;
|
||
|
}
|
||
|
|
||
|
Licensed::Instance().UpdateGetTokenCount(false);
|
||
|
}
|
||
|
else {
|
||
|
Licensed::Instance().UpdateGetTokenCount(true);
|
||
|
}
|
||
|
|
||
|
const std::string& responseBody = request->GetResponseBody();
|
||
|
Token serverToken;
|
||
|
if (!serverToken.DecodeFromJson(responseBody)) {
|
||
|
return Token();
|
||
|
}
|
||
|
|
||
|
return serverToken;
|
||
|
}
|
||
|
|
||
|
bool Token::SaveConfig() const {
|
||
|
std::string dllPath = GetDllPath();
|
||
|
std::ofstream out(dllPath + LICENSED_FILE_NAME, std::ios::out);
|
||
|
if (!out.is_open()) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
std::string token = encode();
|
||
|
out << token;
|
||
|
out.close();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
std::string Token::encode() const {
|
||
|
srand((unsigned)time(NULL));
|
||
|
|
||
|
int32_t rad = RAND(0, 9);
|
||
|
|
||
|
std::string ecodeStr = m_token;
|
||
|
for (int i = 0; i < ecodeStr.length(); ++i) {
|
||
|
ecodeStr[i] = ecodeStr[i] + rad;
|
||
|
}
|
||
|
|
||
|
return std::to_string(rad) + ecodeStr + std::to_string(int(m_code) + rad);
|
||
|
}
|
||
|
|
||
|
bool Token::decode(const std::string& str) {
|
||
|
if (str.empty()) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
const char* data = str.data();
|
||
|
int32_t rad = data[0] - '0';
|
||
|
if (rad < 0 || rad > 9) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
int32_t code = data[str.length() - 1] - '0' - rad;
|
||
|
if (code < 0 || code > 9) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
m_code = static_cast<CodeType>(code);
|
||
|
|
||
|
std::string token;
|
||
|
for (int i = 1; i < str.length() - 1; ++i) {
|
||
|
token += data[i] - rad;
|
||
|
}
|
||
|
m_token = token;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool Token::DecodeFromJson(const std::string& token) {
|
||
|
rapidjson::Document doc;
|
||
|
if (doc.Parse(token.c_str()).HasParseError()) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (!doc.HasMember("code")) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// int32_t code = doc["code"].GetInt();
|
||
|
|
||
|
if (!doc.HasMember("token")) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
std::string serverToken = doc["token"].GetString();
|
||
|
if (!decode(serverToken)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
//if (NERVER == code) {
|
||
|
// m_code = 1;
|
||
|
//}
|
||
|
return true;
|
||
|
}
|