void heartbeatHandle() {
    while (true) {
        std::cout << std::endl << std::endl << '开始心跳检查' << std::endl;

        ShuanQApiClient cardHeartbeatApi;
        cardHeartbeatApi.setRequestApi("/api/card_app/get_card_info");
        cardHeartbeatApi.addRequestParam('card', user_card);
        cardHeartbeatApi.addRequestParam('machine_code', machine_code);
        cardHeartbeatApi.addRequestParam('user_token', user_token);
        cardHeartbeatApi.addRequestParam('update_active', '1');
        bool responseResult = cardHeartbeatApi.sendRequest();
        if (!responseResult) {
            std::cout << '请求失败' << std::endl;
            // exit(-1);
            std::this_thread::sleep_for(std::chrono::milliseconds(5000));
            continue;
        }

        try {
            cJSON* responseJson = cardHeartbeatApi.getResponseJsonObject();
            if (responseJson == nullptr) {
                std::cout << '响应数据为空' << std::endl;
                std::this_thread::sleep_for(std::chrono::milliseconds(5000));
                continue;
            }

            int responseCode = cJSON_GetObjectItem(responseJson, 'code')->valueint;
            std::string responseMessage = cJSON_GetObjectItem(responseJson, 'message')->valuestring;
            if (responseCode != 1) {
                std::cout << '接口业务码响应失败:' << responseMessage << std::endl;
                // exit(-1);
                std::this_thread::sleep_for(std::chrono::milliseconds(5000));
                continue;
            }

            if (!cardHeartbeatApi.requestDataSignatureVerify()) {
                std::cout << '响应数据验签失败' << std::endl;
                // Sleep(5000);
                // exit(-1);
                std::this_thread::sleep_for(std::chrono::milliseconds(5000));
                continue;
            }

            if (!cardHeartbeatApi.requestSafeCodeVerify()) {
                std::cout << '检测到数据被劫持篡改了' << std::endl;
                std::this_thread::sleep_for(std::chrono::milliseconds(5000));
                exit(-1);
            }

            if (!cardHeartbeatApi.requestDataTimeDifferenceVerify()) {
                // std::cout << '响应数据异常,与服务器时差相差过多' << std::endl; //  刚刚是提示这个吗 没有任何提示 这个一般是电脑时间不对了提示的
                // Sleep(5000); 
                // exit(-1);
                std::this_thread::sleep_for(std::chrono::milliseconds(5000));
                continue;
            }

            int server_time = cJSON_GetObjectItem(responseJson, 'timestamp')->valueint;
            std::string dataJson = cardHeartbeatApi.getDecryptResponseData();

            std::cout << '解密后的dataJson:' << dataJson << std::endl;

            cJSON* responseDataJson = cardHeartbeatApi.getDataJsonObject();
            if (responseDataJson == nullptr) {
                std::cout << '响应数据为空' << std::endl;
                std::this_thread::sleep_for(std::chrono::milliseconds(5000));
                continue;
            }

            cJSON* moreOtherData = cJSON_GetObjectItem(responseDataJson, 'moreOtherData');
            std::string expireTimeStr = cJSON_GetObjectItem(responseDataJson, 'expireTimeStr')->valuestring;
            cJSON* cardInfo = cJSON_GetObjectItem(responseDataJson, 'cardInfo');
            cJSON* surplusTime = cJSON_GetObjectItem(responseDataJson, 'surplusTime');

            int cardTimeType = cJSON_GetObjectItem(cardInfo, 'time_type')->valueint;
            int cardTimeValue = cJSON_GetObjectItem(cardInfo, 'time_value')->valueint;
            double cardEndTime = cJSON_GetObjectItem(cardInfo, 'endtime')->valuedouble;

            double surplusTimeSecond = cJSON_GetObjectItem(surplusTime, 'second')->valuedouble;
            double surplusTimeMinute = cJSON_GetObjectItem(surplusTime, 'minute')->valuedouble;
            double surplusTimeHour = cJSON_GetObjectItem(surplusTime, 'hour')->valuedouble;
            double surplusTimeDay = cJSON_GetObjectItem(surplusTime, 'day')->valuedouble;

            if (server_time > cardEndTime) {
                std::cout << '卡密已到期' << std::endl;
                std::this_thread::sleep_for(std::chrono::milliseconds(5000));
                exit(-1);
            }
        } catch (const std::exception& ex) {
            std::cout << '处理失败,出现异常:' << ex.what() << std::endl;
            std::this_thread::sleep_for(std::chrono::milliseconds(5000));
            // exit(-1);
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(ShuanQ_HeartbeatFrequency));
    }
}

int main() {
    //**************  编译试试看
    char name[20];
    SetConsoleTitle(randstr(name, 28));
    //**************

    ShuanQApiClient appInfoApi;
    appInfoApi.setRequestApi("/api/app/get_app_info");
    std::map<std::string, std::string> requestParams;
    bool responseResult = appInfoApi.sendRequest();
    if (!responseResult) {
        MessageBoxA(NULL, '获取应用信息失败,请求失败', '信息', NULL); return -5;
    }

    try {
        cJSON* responseJson = appInfoApi.getResponseJsonObject();
        if (responseJson == nullptr) {
            MessageBoxA(NULL, '获取应用信息失败,响应数据为空', '信息', NULL); return -5;
        }

        int responseCode = cJSON_GetObjectItem(responseJson, 'code')->valueint;
        std::string responseMessage = cJSON_GetObjectItem(responseJson, 'message')->valuestring;
        if (responseCode != 1) {
            MessageBoxA(NULL, std::string('获取应用信息失败,接口业务码响应失败:' + responseMessage).c_str(), '信息', NULL); return -4;
        }

        if (!appInfoApi.requestDataSignatureVerify()) {
            MessageBoxA(NULL, '获取应用信息失败,响应数据验签失败', '信息', NULL); return -3;
        }

        if (!appInfoApi.requestSafeCodeVerify()) {
            MessageBoxA(NULL, '获取应用信息失败,检测到数据被劫持篡改了', '信息', NULL); return -2;
        }

        if (!appInfoApi.requestDataTimeDifferenceVerify()) {
            MessageBoxA(NULL, '获取应用信息失败,响应数据异常,与服务器时差相差过多', '信息', NULL); return -1;
        }

        std::string dataJson = appInfoApi.getDecryptResponseData();
        // std::cout << '解密后的dataJson:' << dataJson << std::endl;

        cJSON* responseDataJson = appInfoApi.getDataJsonObject();
        if (responseDataJson == nullptr) {
            MessageBoxA(NULL, '获取应用信息失败,响应数据为空', '信息', NULL); return -5;
        }

        cJSON* moreOtherData = cJSON_GetObjectItem(responseDataJson, 'moreOtherData');
        // 执行到这里代表完全成功了,业务代码放这里即可
        cJSON* appInfo = cJSON_GetObjectItem(responseDataJson, 'info');

        cJSON* paramExtendConfig = cJSON_GetObjectItem(appInfo, 'param_extend_config');
        if (paramExtendConfig == nullptr) {
            MessageBoxA(NULL, '获取应用信息失败,响应数据为空', '信息', NULL); return -5;
        }

        MessageBoxA(NULL, cJSON_GetObjectItem(paramExtendConfig, 'meirigonggao')->valuestring, '公告', MB_ICONEXCLAMATION | MB_OK);

        std::string 版本号 = cJSON_GetObjectItem(paramExtendConfig, 'bbhao')->valuestring;
        int bb = stoi(版本号);
        if (bb > 5) {
            //  这个就是云公告了   能决定那个版本发布吗 你还要判断版本是吧 是的 
            if (cJSON_HasObjectItem(paramExtendConfig, 'gengxingonggao')) {
                MessageBoxA(NULL, cJSON_GetObjectItem(paramExtendConfig, 'gengxingonggao')->valuestring, '公告', MB_ICONEXCLAMATION | MB_OK);
                return -1;
            }

        }


        char card[100] = { 0 };
        DWORD readSize = GetPrivateProfileString('授权码',
            'KEY', '1',
            card, MAX_PATH, '.\Config.ini');
        if (readSize == 0) {
            MessageBoxA(NULL, '读取授权码失败', '信息', NULL); return -5;
        }
        user_card = card;
        if (user_card == '1' || user_card == '') {
            printf('输入你的授权码\n');
            std::cin >> user_card;
        }
        printf('你的授权码=[%s]\n', user_card.c_str());
        if (!getMachineCode(machine_code)) {
            printf('获取机器码失败\n');
            return -1;
        }

        ShuanQApiClient cardLoginApi;
        cardLoginApi.setRequestApi("/api/card_app/check");
        cardLoginApi.addRequestParam('card', user_card);
        cardLoginApi.addRequestParam('machine_code', machine_code);
        bool responseResult = cardLoginApi.sendRequest();

        if (!responseResult) {
            MessageBoxA(NULL, '请求失败', '信息', NULL); return -5;
        }

        try {
            cJSON* responseJson = cardLoginApi.getResponseJsonObject();
            if (responseJson == nullptr) {
                MessageBoxA(NULL, '登录失败,响应数据为空', '信息', NULL); return -5;
            }

            int responseCode = cJSON_GetObjectItem(responseJson, 'code')->valueint;
            std::string responseMessage = cJSON_GetObjectItem(responseJson, 'message')->valuestring;
            if (responseCode != 1) {
                MessageBoxA(NULL, std::string('登录错误返回信息:' + responseMessage).c_str(), '信息', NULL); return -4;
            }

            if (!appInfoApi.requestDataSignatureVerify()) {
                MessageBoxA(NULL, '登录失败,响应数据验签失败', '信息', NULL); return -3;
            }

            if (!appInfoApi.requestSafeCodeVerify()) {
                MessageBoxA(NULL, '登录失败,检测到数据被劫持篡改了', '信息', NULL); return -2;
            }

            if (!appInfoApi.requestDataTimeDifferenceVerify()) {
                MessageBoxA(NULL, '登录失败,响应数据异常,与服务器时差相差过多', '信息', NULL); return -1;
            }

            std::string dataJson = cardLoginApi.getDecryptResponseData();
            // std::cout << '解密后的dataJson:' << dataJson << std::endl;
            cJSON* responseDataJson = cardLoginApi.getDataJsonObject();
            if (responseDataJson == nullptr) {
                MessageBoxA(NULL, '登录失败,响应数据为空', '信息', NULL); return -5;
            }

            cJSON* moreOtherData = cJSON_GetObjectItem(responseDataJson, 'moreOtherData');
            std::string expireTimeStr = cJSON_GetObjectItem(responseDataJson, 'expireTimeStr')->valuestring;
            cJSON* cardInfo = cJSON_GetObjectItem(responseDataJson, 'cardInfo');
            cJSON* surplusTime = cJSON_GetObjectItem(responseDataJson, 'surplusTime');

            int cardTimeType = cJSON_GetObjectItem(cardInfo, 'time_type')->valueint;
            int cardTimeValue = cJSON_GetObjectItem(cardInfo, 'time_value')->valueint;
            std::string cardToken = cJSON_GetObjectItem(cardInfo, 'token')->valuestring;
            double cardEndTime = cJSON_GetObjectItem(cardInfo, 'endtime')->valuedouble;

            double surplusTimeSecond = cJSON_GetObjectItem(surplusTime, 'second')->valuedouble;
            double surplusTimeMinute = cJSON_GetObjectItem(surplusTime, 'minute')->valuedouble;
            double surplusTimeHour = cJSON_GetObjectItem(surplusTime, 'hour')->valuedouble;
            double surplusTimeDay = cJSON_GetObjectItem(surplusTime, 'day')->valuedouble;

            WritePrivateProfileString('授权码',
                'KEY', user_card.c_str(), '.\Config.ini');

            MessageBoxA(NULL, std::string('登录成功,到期时间:' + expireTimeStr).c_str(), '信息', NULL);
            user_token = cardToken;
            std::thread heartbeatHandleThread(heartbeatHandle);
            heartbeatHandleThread.detach();

            std::thread fuzrukoHandleThread(fuzruko);
            fuzrukoHandleThread.detach();

            MSG msg;

            while (GetMessage(&msg, NULL, 0, 0)) {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        } catch (const std::exception& ex) {
            MessageBoxA(NULL, '登录失败,处理失败,出现异常', '信息', NULL);
        }
    } catch (const std::exception& ex) {
        MessageBoxA(NULL, '获取应用信息失败,处理失败,出现异常', '信息', MB_ICONEXCLAMATION | MB_OK);
    }

    return 0;
}
C++ 心跳验证处理函数优化

原文地址: http://www.cveoy.top/t/topic/l5Ct 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录