aboutsummaryrefslogtreecommitdiffstats
path: root/src/clienthandler.cpp
blob: 789176b5be4bff652ace052f9cb9b7a90df8e67c (plain)
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
/* 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;
}