From e9e31d1e843c6156b3085d562e0e36b47e1da259 Mon Sep 17 00:00:00 2001 From: Sami Littow Date: Thu, 24 Nov 2022 08:27:21 +0200 Subject: Expiry date calculation/comparison adjusted Some Ooopses corrected --- httpclient.cpp | 11 +++++------ include/commonsetup.h | 1 + include/httpclient.h | 10 ---------- licenser.cpp | 11 ++++++----- utils.cpp | 2 +- 5 files changed, 13 insertions(+), 22 deletions(-) diff --git a/httpclient.cpp b/httpclient.cpp index 83a0914..33329c6 100644 --- a/httpclient.cpp +++ b/httpclient.cpp @@ -72,7 +72,6 @@ int HttpClient::sendRequest(std::string &reply, const std::string &payload, CURL *curl = curl_easy_init(); try { if (doRequest(curl, request) != 0) { - m_lastError += "Server not reachable "; retVal = 1; } } catch(...) { @@ -97,14 +96,14 @@ int HttpClient::sendRequest(std::string &reply, const std::string &payload, int HttpClient::doRequest(CURL *curl, HttpRequest &request) { - /* send all data to this function */ + // Set all received data to be send to our callback function curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); std::string readBuffer; - /* we pass our 'chunk' struct to the callback function */ + // Pass the buffer to a callback curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); - // Set the URL + // Set the URL / headers curl_easy_setopt(curl, CURLOPT_URL, request.url.c_str()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, request.headers); if (!request.payload.empty()) { @@ -116,11 +115,11 @@ int HttpClient::doRequest(CURL *curl, HttpRequest &request) } curl_easy_setopt(curl, CURLOPT_TIMEOUT, SERVER_CONN_TIMEOUT); - /* get it! */ + // get it CURLcode res; res = curl_easy_perform(curl); - /* check for errors */ + // check for errors if (res != CURLE_OK) { std::cout << "HTTP transfer failed, URL: " << request.url << std::endl; m_lastError = curl_easy_strerror(res); diff --git a/include/commonsetup.h b/include/commonsetup.h index 1622503..f523216 100644 --- a/include/commonsetup.h +++ b/include/commonsetup.h @@ -39,3 +39,4 @@ #define LICENSE_FILE_BASE WORKING_DIR "/lic_" #define SHA256_HASH_SIZE 32 #define SECS_IN_HOUR 3600 +#define SECS_IN_DAY (SECS_IN_HOUR * 24) diff --git a/include/httpclient.h b/include/httpclient.h index a861ae6..6ee1f86 100644 --- a/include/httpclient.h +++ b/include/httpclient.h @@ -12,17 +12,7 @@ #define SERVER_CONN_TIMEOUT 10 #if _WIN32 - //#include #include "curl/curl.h" - //#pragma comment(lib, "../3rdParty/curl/lib/libcurl.lib") - //#pragma comment(lib, "ws2_32.lib") - //#pragma comment(lib, "winmm.lib") - //pragma comment(lib, "wldap32.lib") - //#include - //#include - //#include - //#include - //#include #else #include #include diff --git a/licenser.cpp b/licenser.cpp index 142164d..35059c5 100644 --- a/licenser.cpp +++ b/licenser.cpp @@ -281,9 +281,9 @@ bool Licenser::checkLicenseExpiryDate(std::string &reply, const RequestInfo &req reply = replyString[e_bad_connection]; return false; } - // Expiry date + // Expiry date. Add 1 day to expiry date to get the end actually at the beginning of the next day std::string expDate = license.get("expiry_date"); - std::time_t expEpoch = utils::stringToEpoch(expDate.c_str()); + std::time_t expEpoch = utils::stringToEpoch(expDate.c_str()) + SECS_IN_DAY; // Current date std::time_t current = std::time(0); // See if the time has expire @@ -291,14 +291,14 @@ bool Licenser::checkLicenseExpiryDate(std::string &reply, const RequestInfo &req // License expired if (request.appName == MOCWRAPPER_APP_NAME) { // For MOC, allow some leeway time - int leewayTimeLeft = expEpoch + (license.getInt("leeway_time") * SECS_IN_HOUR) - current; + int leewayTimeLeft = expEpoch + (license.getInt("leeway_hours") * SECS_IN_HOUR) - current; std::cout << "Leeway time left: " << leewayTimeLeft << std::endl; if (leewayTimeLeft > 0) { std::stringstream ss; ss << "Warning: No connection to server. " << "Leeway time left: " << std::fixed << std::setprecision(1) - << (float)leewayTimeLeft / (24 * SECS_IN_HOUR) << " days"; + << (float)leewayTimeLeft / SECS_IN_DAY << " days"; reply = ss.str(); return true; } @@ -380,9 +380,10 @@ void Licenser::doHmacHashSha256(const std::string &payload, const std::string &s } authKey = ss_result.str(); + JsonHandler pay(payload); // Print out the result - std::cout << "Message: " << payload << std::endl; + std::cout << "Message: " << pay.dump(4) << std::endl; std::cout << "Key: " << secret << std::endl; std::cout << "HMAC: " << authKey << std::endl; } diff --git a/utils.cpp b/utils.cpp index b60295b..2a8696f 100644 --- a/utils.cpp +++ b/utils.cpp @@ -83,7 +83,7 @@ std::string getUserHomeDir() const char* homedir; #if __linux__ || __APPLE__ || __MACH__ // Linux and Mac - homedir = std::getenv("PATH"); + homedir = std::getenv("HOME"); if (homedir == nullptr) { homedir = getpwuid(getuid())->pw_dir; } -- cgit v1.2.3