admin
2025-07-17 6cd92a169cbc0db35042f243a09d976fd3e1445c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
 
#include "common/pch.h"
#include<codecvt>
#include<list>
#include<string>
#include"StringUtil.h"
#include"ProjectBuildNetworkApi.h"
#include "JsonUtil.h"
#include <chrono>
#include <ctime>
#include <random>
#include "md5.h"
#include <sstream>
#include <iostream>
#include <iomanip>
#include <codecvt>
#include <map>
#include <curl/curl.h>
 
 
string ProjectBuildNetworkApi::get_rquest_id()
{
    string requestIdStr;
    std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
    // ½«µ±Ç°Ê±¼äµãת»»Îªtime_tÀàÐÍ
    std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
    requestIdStr.append("_").append(std::to_string(currentTime)).append("_");
    // Éú³ÉËæ»úÊý
    std::random_device rd;
    std::mt19937 rng(rd());
    std::uniform_int_distribution<int> intDist(1, 100000); // Éú³É·¶Î§ÔÚ1-100Ö®¼äµÄÕûÊý
    int randomInt = intDist(rng);
    requestIdStr.append(std::to_string(randomInt));
    return requestIdStr;
}
 
string ProjectBuildNetworkApi::get_sign(rapidjson::Document& document)
{
    //¼ÆËãÇ©Ãû
    std::list<string> strList;
    for (rapidjson::Value::ConstMemberIterator itr = document.MemberBegin(); itr != document.MemberEnd(); ++itr) {
        std::cout << "¼ü: " << itr->name.GetString() << ", Öµ: ";
        if (itr->value.IsObject() || itr->value.IsArray()) {
            //Èç¹ûÊǶÔÏó
            rapidjson::StringBuffer buffer;
            rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
            itr->value.Accept(writer);
            std::string jsonString = buffer.GetString();
            strList.push_back(string(itr->name.GetString()).append("=").append(jsonString));
        }
        else {
            if (itr->value.IsString()) {
                strList.push_back(string(itr->name.GetString()).append("=").append(itr->value.GetString()));
            }
            else     if (itr->value.IsInt()) {
                strList.push_back(string(itr->name.GetString()).append("=").append(std::to_string(itr->value.GetInt())));
            }
        }
 
    }
 
    strList.sort();
    strList.push_back("%Yeshi2014@#.");
    string src = "";
    int index = 0;
    for (std::list<string>::iterator el = strList.begin(); el != strList.end(); el++) {
        src.append(*el);
        index++;
        if (index != strList.size()) {
            src.append("&");
        }
    }
    //MD5
    //cout << "¼ÓÃÜǰ×Ö·û´®£º" <<src << endl;
    string md5 = MD5(src).toStr();
    return md5;
}
 
 
std::string ProjectBuildNetworkApi::load_request_data(std::string data)
{
    rapidjson::Document document;
    document.Parse(data.c_str());
    string requestIdStr = (document["type"].IsInt() ? std::to_string(document["type"].GetInt()) : document["type"].GetString()).append(get_rquest_id());
    rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
 
    // Ìí¼Ó request_id ×Ö¶Î
    rapidjson::Value requestId(rapidjson::kStringType);
    requestId.SetString(requestIdStr.c_str(), allocator);
    document.AddMember("request_id", requestId, allocator);
    // Ìí¼ÓÇ©Ãû£¬sign
    string sign = get_sign(document);
    rapidjson::Value signV(rapidjson::kStringType);
    signV.SetString(sign.c_str(), allocator);
    document.AddMember("sign", signV, allocator);
 
    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
    document.Accept(writer);
 
    std::string jsonString = buffer.GetString();
    //·¢ËÍÊý¾Ý
    std::stringstream ss;
    ss << std::setw(8) << std::setfill('0') << jsonString.length();
    std::string length_str = string("##").append(ss.str());
    std::string fstr = length_str.append(jsonString);
    return fstr;
}
 
 
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}
 
 
std::string ProjectBuildNetworkApi::base_request_get(string ip, int port,string path, map<string,string>  params)
{
    CURL* curl = curl_easy_init();
    std::string response;
    if (curl) {
        string url = "http://";
        url.append(ip).append(":").append(to_string(port)).append(path);
        int index = 0;
        if (!params.empty()) {
            url.append("?");
            for (auto it = params.begin(); it != params.end(); ++it) {
                url.append(it->first).append("=");
                char* output = curl_easy_escape(curl, it->second.c_str(), it->second.length());
                url.append(output);
                if (index != params.size() - 1) {
                    url.append("&");
                }
                index++;
            }
        }
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());  // ÉèÖÃURL
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); // ÉèÖûص÷
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);// ´«µÝ½ÓÊÕ»º³åÇø
        curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 600L);// Á¬½Ó³¬Ê± 600 Ãë
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 600L);       // ´«Êä×ܳ¬Ê± 600 Ãë
        CURLcode res = curl_easy_perform(curl);  // Ö´ÐÐÇëÇó
        if (res != CURLE_OK) {
            std::cerr << "cURL failed: " << curl_easy_strerror(res) << std::endl;
            throw;
        }
        else {
            long response_code;
            curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
            std::cout << "Response code: " << response_code << std::endl;
            // ´òÓ¡ÏìÓ¦ÄÚÈÝ
            std::cout << "Response body:\n" << response << std::endl;
        }
 
        curl_easy_cleanup(curl);  // ÇåÀí×ÊÔ´
        return response;
    }
    throw;
}
 
 
string ProjectBuildNetworkApi::build(string ip, int port, string dir, string script, string output)
{
    map<string, string> params;
    params["dir"] = dir;
    params["script"] = script;
    params["output"] = output;
    return base_request_get(ip, port, "/build",params);
}