aboutsummaryrefslogtreecommitdiffstats
path: root/src/clienthandler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/clienthandler.cpp')
-rw-r--r--src/clienthandler.cpp120
1 files changed, 120 insertions, 0 deletions
diff --git a/src/clienthandler.cpp b/src/clienthandler.cpp
new file mode 100644
index 0000000..789176b
--- /dev/null
+++ b/src/clienthandler.cpp
@@ -0,0 +1,120 @@
+/* Copyright (C) 2022 The Qt Company Ltd.
+ *
+ * SPDX-License-Identifier: GPL-3.0-only WITH Qt-GPL-exception-1.0
+*/
+
+#include "clienthandler.h"
+
+bool ClientHandler::checkLicenseExpiryTime(std::string &reply)
+{
+ std::cout << "Offline - checking validity from license file " << m_request.licenseFile << std::endl;
+ // Open the license file
+ std::string data;
+ if (utils::readFile(data, m_request.licenseFile) != 0) {
+ std::cout << "No license file - rejecting license " << m_request.licenseFile << std::endl;
+ reply = replyString[e_bad_connection];
+ return false;
+ }
+ JsonHandler license(data);
+ // Check license status
+ if (license.get("status") != "true") {
+ std::cout << "License status = false: Rejecting license\n";
+ reply = replyString[e_bad_connection];
+ return false;
+ }
+ // 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()) + SECS_IN_DAY;
+ // Current date
+ std::time_t current = std::time(0);
+ // See if the time has expire
+ if (current > expEpoch) {
+ // Store the leeway time if applicable
+ if(license.get("leeway_hours") != "") {
+ m_license.leeway_hours = license.getInt("leeway_hours");
+ m_license.current_timestamp = current;
+ m_license.expiry_epoch = expEpoch;
+ }
+ return false;
+ }
+ std::cout << "License granted, expires at " << expDate << std::endl;
+ reply = replyString[e_license_granted];
+ return true;
+}
+
+int ClientHandler::parseRequest() {
+ // First find out the command (and operation for longterm case)
+ std::string cmd = utils::trimStr(params[0]);
+ m_request.type = RequestType::no_request;
+ if (cmd == LICENSE_REQUEST_CMD) {
+ m_request.type = RequestType::license_request;
+ } else if (cmd == LONGTERM_REQUEST_CMD) {
+ m_request.type = RequestType::long_term_request;
+ // find either 'add' or 'remove'
+ std::string op = utils::trimStr(cmd.substr(8, cmd.length() -1));
+ if (op != OP_ADD_RESERVATION && op != OP_REMOVE_RESERVATION) {
+ std::cout << "Invalid longterm operation: " << op;
+ return e_bad_request;
+ }
+ m_request.operation = op;
+ } else if (cmd == SERVER_VERSION_CMD) {
+ m_request.type = RequestType::server_version;
+ buildRequestJson();
+ return 0;
+ } else if (cmd == DAEMON_VERSION_CMD) {
+ m_request.type = RequestType::daemon_version;
+ } else if (cmd == RESERVATION_QUERY_CMD) {
+ m_request.type = RequestType::reservation_query;
+ } else {
+ std::cout << "Invalid command: " << cmd << std::endl;
+ return e_bad_request;
+ }
+ // Then cycle through parameters. Start from [1] because [0] is the command
+ for (int i = 1; i < params.size(); i++) {
+ std::string item = params[i];
+ char arg = (char)item[0];
+ std::string val = utils::trimStr(item.substr(1, item.length() - 1));
+ if (val.length() == 0) {
+ std::cout << "No value for argument: -" << arg << std::endl;
+ return e_bad_request;
+ }
+ if (arg == 'a') m_request.appName = utils::strToLower(val);
+ else if (arg == 'v') m_request.appVersion = val;
+ else if (arg == 'u') m_request.userId = val;
+ else if (arg == 'i') m_request.licenseId = val;
+ else if (arg == 'p') m_request.parentReservationId = val;
+ else if (arg == 'r') m_request.runnerType = val;
+ else if (arg == 'e') m_request.email = val;
+ else if (arg == 'l') m_request.serverAddr = val;
+ else {
+ std::cout << "Invalid argument: -" << arg << std::endl;
+ return e_bad_request;
+ }
+ }
+ std::stringstream ss;
+ ss << WORKING_DIR << DIR_SEPARATOR << LICENSE_FILE_PREFIX;
+ ss << m_request.userId << "_" << m_request.licenseId << LICENSE_FILE_EXTENSION;
+ m_request.licenseFile += ss.str();
+ buildRequestJson();
+ return 0;
+}
+
+bool ClientHandler::checkLeewayTime(std::string &reply)
+{
+ // Leeway time granted?
+ if (m_license.leeway_hours == 0) {
+ return false;
+ }
+ // License expired and offline: Allow some leeway time
+ int leewayTimeLeft = m_license.expiry_epoch + (m_license.leeway_hours * SECS_IN_HOUR) - m_license.current_timestamp;
+ if (leewayTimeLeft > 0) {
+ std::stringstream ss;
+ ss << replyString[e_no_conn_leeway]
+ << std::fixed << std::setprecision(1)
+ << (float)leewayTimeLeft / SECS_IN_DAY << " days";
+ reply = ss.str();
+ return true;
+ }
+ return false;
+}
+