-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client.cpp
More file actions
156 lines (126 loc) · 4.53 KB
/
Copy pathhttp_client.cpp
File metadata and controls
156 lines (126 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "shieldcore/http_client.hpp"
#include <curl/curl.h>
#include <chrono>
#include <iomanip>
#include <memory>
#include <random>
#include <sstream>
#include <string>
namespace shieldcore {
std::string generateNonce() {
std::random_device randomDevice;
std::mt19937 generator(randomDevice());
std::uniform_int_distribution<int> distribution(0, 255);
std::ostringstream stream;
stream << std::hex << std::setfill('0');
for (int index = 0; index < 16; ++index) {
stream << std::setw(2) << distribution(generator);
}
return stream.str();
}
long long getCurrentTimestamp() {
return std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
}
namespace {
size_t writeCallback(char* contents, size_t size, size_t count, void* userData) {
auto* response = static_cast<std::string*>(userData);
response->append(contents, size * count);
return size * count;
}
bool curlGlobalInit() {
static const bool initialized = []() {
return curl_global_init(CURL_GLOBAL_DEFAULT) == CURLE_OK;
}();
return initialized;
}
} // namespace
bool validateWithBackend(const std::string& baseUrl,
const std::string& pluginId,
const std::string& hwid,
bool& isValid,
std::string& error) {
isValid = false;
if (!curlGlobalInit()) {
error = "failed to initialize libcurl";
return false;
}
CURL* raw = curl_easy_init();
if (!raw) {
error = "failed to create curl handle";
return false;
}
std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> curl(raw, curl_easy_cleanup);
std::string response;
const std::string requestBody = "{\"plugin_id\":\"" + pluginId + "\",\"hwid\":\"" + hwid + "\"}";
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl.get(), CURLOPT_URL, (baseUrl + "/validate").c_str());
curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl.get(), CURLOPT_POST, 1L);
curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, requestBody.c_str());
curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDSIZE, static_cast<long>(requestBody.size()));
curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &response);
CURLcode code = curl_easy_perform(curl.get());
curl_slist_free_all(headers);
if (code != CURLE_OK) {
error = curl_easy_strerror(code);
return false;
}
if (response.find("valid") != std::string::npos) {
isValid = true;
return true;
}
if (response.find("invalid") != std::string::npos) {
isValid = false;
return true;
}
error = "unexpected backend response: " + response;
return false;
}
bool activateLicense(const std::string& baseUrl,
const LicenseValidationRequest& request,
LicenseValidationResponse& response,
std::string& error) {
response = {};
if (!curlGlobalInit()) {
error = "failed to initialize libcurl";
return false;
}
CURL* raw = curl_easy_init();
if (!raw) {
error = "failed to create curl handle";
return false;
}
std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> curl(raw, curl_easy_cleanup);
std::string responseBody;
const std::string requestBody =
"{\"license_key\":\"" + request.licenseKey +
"\",\"hwid\":\"" + request.hwid +
"\",\"nonce\":\"" + request.nonce +
"\",\"timestamp\":" + std::to_string(request.timestamp) + "}";
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl.get(), CURLOPT_URL, (baseUrl + "/activate").c_str());
curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl.get(), CURLOPT_POST, 1L);
curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, requestBody.c_str());
curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDSIZE, static_cast<long>(requestBody.size()));
curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &responseBody);
CURLcode code = curl_easy_perform(curl.get());
curl_slist_free_all(headers);
if (code != CURLE_OK) {
error = curl_easy_strerror(code);
return false;
}
if (responseBody.find("valid") != std::string::npos) {
response.valid = true;
}
if (responseBody.find("revoked") != std::string::npos) {
response.isRevoked = true;
}
return true;
}
} // namespace shieldcore