#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <json-c/json.h>
#define API_KEY "your-api-key-here"
#define API_URL "http://your-domain.com/api/license/auth"
#define PRODUCT "MyCApp"
#define VERSION "1.0.0"
struct APIResponse {
char *data;
size_t size;
};
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, struct APIResponse *response) {
size_t realsize = size * nmemb;
char *ptr = realloc(response->data, response->size + realsize + 1);
if (ptr == NULL) return 0;
response->data = ptr;
memcpy(&(response->data[response->size]), contents, realsize);
response->size += realsize;
response->data[response->size] = 0;
return realsize;
}
void get_hwid(char* hwid, size_t size) {
FILE *fp = popen("cat /proc/cpuinfo | grep 'serial' | head -n1 | cut -d: -f2 | tr -d ' \t\n'", "r");
if (fp != NULL) {
if (fgets(hwid, size, fp) != NULL) {
hwid[strcspn(hwid, "\n")] = 0;
}
pclose(fp);
}
if (strlen(hwid) == 0) {
snprintf(hwid, size, "fallback-hwid-%ld", time(NULL) % 1000000);
}
}
void get_local_ip(char* ip, size_t size) {
FILE *fp = popen("hostname -I | awk '{print $1}'", "r");
if (fp != NULL) {
if (fgets(ip, size, fp) != NULL) {
ip[strcspn(ip, "\n")] = 0;
}
pclose(fp);
}
if (strlen(ip) == 0) {
strncpy(ip, "127.0.0.1", size - 1);
}
}
int authenticate_license(const char* license_key) {
CURL *curl;
CURLcode res;
struct APIResponse response = {0};
struct curl_slist *headers = NULL;
char auth_header[512];
char json_data[1024];
char hwid[64] = {0};
char ip[16] = {0};
int success = 0;
get_hwid(hwid, sizeof(hwid));
get_local_ip(ip, sizeof(ip));
curl = curl_easy_init();
if (!curl) return 0;
snprintf(auth_header, sizeof(auth_header), "Authorization: %s", API_KEY);
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, auth_header);
snprintf(json_data, sizeof(json_data),
"{\"data\":{\"product\":\"%s\",\"version\":\"%s\",\"licensekey\":\"%s\",\"ip\":\"%s\",\"hwid\":\"%s\"}}",
PRODUCT, VERSION, license_key, ip, hwid);
curl_easy_setopt(curl, CURLOPT_URL, API_URL);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_data);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);
res = curl_easy_perform(curl);
if (res == CURLE_OK) {
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
if (response_code == 200) {
json_object *root = json_tokener_parse(response.data);
json_object *status_obj;
if (json_object_object_get_ex(root, "status_overview", &status_obj)) {
const char *status = json_object_get_string(status_obj);
if (strcmp(status, "success") == 0) {
success = 1;
printf("License authenticated successfully!\n");
json_object *username_obj;
if (json_object_object_get_ex(root, "discord_username", &username_obj)) {
printf("User: %s\n", json_object_get_string(username_obj));
}
}
}
json_object_put(root);
}
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
if (response.data) free(response.data);
return success;
}
int main() {
char license_key[64];
curl_global_init(CURL_GLOBAL_DEFAULT);
printf("Enter license key: ");
if (fgets(license_key, sizeof(license_key), stdin)) {
license_key[strcspn(license_key, "\n")] = 0;
if (authenticate_license(license_key)) {
printf("Starting application...\n");
} else {
printf("Authentication failed!\n");
return 1;
}
}
curl_global_cleanup();
return 0;
}