Skip to main content

⚡ C++ Code Examples

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <curl/curl.h>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

#define API_KEY "your-api-key-here"
#define API_URL "http://your-domain.com/api/license/auth"
#define PRODUCT "MyCppApp"
#define VERSION "1.0.0"

class TKIAuth {
private:
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response) {
size_t total_size = size * nmemb;
response->append(static_cast<char*>(contents), total_size);
return total_size;
}

static std::string getHWID() {
std::string hwid;

// Try to get CPU serial
std::ifstream cpuinfo("/proc/cpuinfo");
std::string line;
while (std::getline(cpuinfo, line)) {
if (line.find("Serial") != std::string::npos) {
size_t pos = line.find(':');
if (pos != std::string::npos) {
hwid = line.substr(pos + 1);
// Remove whitespace
hwid.erase(0, hwid.find_first_not_of(" \t"));
hwid.erase(hwid.find_last_not_of(" \t") + 1);
break;
}
}
}

// Fallback HWID
if (hwid.empty()) {
hwid = "cpp-fallback-" + std::to_string(time(nullptr) % 1000000);
}

return hwid;
}

static std::string getLocalIP() {
FILE* fp = popen("hostname -I | awk '{print $1}'", "r");
if (!fp) return "127.0.0.1";

char buffer[128];
std::string ip;
if (fgets(buffer, sizeof(buffer), fp)) {
ip = buffer;
ip.erase(ip.find_last_not_of(" \n\r\t") + 1);
}
pclose(fp);

return ip.empty() ? "127.0.0.1" : ip;
}

public:
static bool authenticate(const std::string& license_key) {
CURL* curl = curl_easy_init();
if (!curl) return false;

std::string response_body;
struct curl_slist* headers = nullptr;

std::string hwid = getHWID();
std::string ip = getLocalIP();

json payload = {
{"data", {
{"product", PRODUCT},
{"version", VERSION},
{"licensekey", license_key},
{"ip", ip},
{"hwid", hwid}
}}
};

std::string json_str = payload.dump();
std::string auth_header = "Authorization: " + std::string(API_KEY);

headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, auth_header.c_str());

curl_easy_setopt(curl, CURLOPT_URL, API_URL);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_str.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_body);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);

CURLcode res = curl_easy_perform(curl);
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);

curl_slist_free_all(headers);
curl_easy_cleanup(curl);

if (res == CURLE_OK && response_code == 200) {
try {
json response_json = json::parse(response_body);
if (response_json.value("status_overview", "") == "success") {
std::cout << "✓ License authenticated!" << std::endl;
std::cout << "User: " << response_json.value("discord_username", "Unknown") << std::endl;
std::cout << "Expires: " << response_json.value("expire_date", "Unknown") << std::endl;
return true;
}
} catch (const json::parse_error& e) {
std::cerr << "JSON parse error: " << e.what() << std::endl;
}
}

std::cout << "✗ Authentication failed (HTTP " << response_code << ")" << std::endl;
return false;
}
};

int main() {
curl_global_init(CURL_GLOBAL_DEFAULT);

std::string license_key;
std::cout << "Enter license key: ";
std::getline(std::cin, license_key);

if (TKIAuth::authenticate(license_key)) {
std::cout << "Starting application..." << std::endl;
// Your application logic here
} else {
std::cout << "Authentication failed!" << std::endl;
curl_global_cleanup();
return 1;
}

curl_global_cleanup();
return 0;
}

Compilation​

g++ -o app main.cpp -lcurl -std=c++17