实现获取服务器版本信息功能

This commit is contained in:
luoliangyi 2022-07-02 09:53:31 +08:00
parent 6f6f8306bb
commit c8df304df9
3 changed files with 156 additions and 6 deletions

View File

@ -484,7 +484,151 @@ std::string GetCurrVersion()
bool GetVersionInfoList(std::list<VersionInfo>& versionList)
{
return false;
versionList.clear();
curl_global_init(CURL_GLOBAL_ALL);
bool ret = false;
CURL* curl = curl_easy_init();
if (curl)
{
std::stringstream out;
std::string osName, archName, oemName;
#if defined(HG_CMP_MSC)
osName = "Windows";
#ifdef _WIN64
archName = "x64";
#else
archName = "x86";
#endif
#else
osName = "Linux";
archName = "unknown";
FILE *fp = popen("cat /etc/issue | cut -d\' \' -f1", "r");
if (nullptr != fp)
{
char buff[1024] = {0};
fread(buff, 1024, 1, fp);
int len = (int)strlen(buff);
for (int i = 0; i < len; ++i)
{
if (buff[i] == '\n')
buff[i] = '\0';
}
osName = buff;
pclose(fp);
}
fp = popen("arch", "r");
if (nullptr != fp)
{
char buff[1024] = {0};
fread(buff, 1024, 1, fp);
int len = (int)strlen(buff);
for (int i = 0; i < len; ++i)
{
if (buff[i] == '\n')
buff[i] = '\0';
}
archName = buff;
pclose(fp);
}
#endif
#if defined(OEM_HANWANG)
oemName = "Hanvon";
#elif defined(OEM_LISICHENG)
oemName = "Lanxum";
#else
oemName = "Huago";
#endif
std::string url = "http://up.appqy.com/api/ver?oem=" + oemName + "&cpu=" + archName + "&sys=" + osName;
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Accept-Encoding:gzip, deflate, sdch");
headers = curl_slist_append(headers, "Accept-Language:zh-CN,zh;q=0.8");
headers = curl_slist_append(headers, "Connection:keep-alive");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out);
/* Perform the request, res will get the return code */
CURLcode res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s", curl_easy_strerror(res));
else
ret = true;
curl_slist_free_all(headers);
std::string str_json = out.str(); // 返回值
printf("%s\n", str_json.c_str());
cJSON* json = cJSON_Parse(str_json.c_str());
if (nullptr != json)
{
cJSON* p = json->child;
if (nullptr != p)
{
while (nullptr != p && 0 != strcmp(p->string, "data"))
p = p->next;
if (nullptr != p && 0 == strcmp(p->string, "data") && p->type == cJSON_Array)
{
cJSON* p2 = p->child;
while (nullptr != p2)
{
VersionInfo version;
cJSON* p3 = p2->child;
while (nullptr != p3)
{
if (0 == strcmp(p3->string, "full") && p3->type == cJSON_String)
version.url = p3->valuestring;
else if (0 == strcmp(p3->string, "size") && p3->type == cJSON_String)
version.size = atoi(p3->valuestring);
else if (0 == strcmp(p3->string, "desc") && p3->type == cJSON_String)
version.desc = p3->valuestring;
else if (0 == strcmp(p3->string, "bug") && p3->type == cJSON_String)
version.bugInfo = p3->valuestring;
else if (0 == strcmp(p3->string, "md5") && p3->type == cJSON_String)
version.md5 = p3->valuestring;
else if (0 == strcmp(p3->string, "v") && p3->type == cJSON_String)
version.version = p3->valuestring;
p3 = p3->next;
}
versionList.push_back(version);
p2 = p2->next;
}
}
}
cJSON_Delete(json);
}
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return ret;
}
static std::vector<std::string> split(std::string strtem, char a)
@ -559,7 +703,7 @@ static int xferinfo2(void* p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t u
return 0;
}
bool HttpDownload(const char* url, const char* saveFilePath, HttpDownloadFunc func, void* param)
bool HttpDownload(const std::string& url, const std::string& saveFilePath, HttpDownloadFunc func, void* param)
{
curl_global_init(CURL_GLOBAL_ALL);
@ -567,10 +711,11 @@ bool HttpDownload(const char* url, const char* saveFilePath, HttpDownloadFunc fu
CURL* curl = curl_easy_init();
if (curl)
{
FILE *fp = fopen(saveFilePath, "wb"); // 打开文件,准备写入
FILE *fp = fopen(saveFilePath.c_str(), "wb"); // 打开文件,准备写入
if (nullptr != fp)
{
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteFunction);

View File

@ -14,6 +14,11 @@ struct ServerConfig
struct VersionInfo
{
VersionInfo()
{
size = 0;
}
std::string version;
std::string desc;
std::string bugInfo;
@ -46,6 +51,6 @@ bool GetVersionInfoList(std::list<VersionInfo> &versionList);
int CompareVersion(const std::string& version1, const std::string& version2);
// 下载HTTP文件
bool HttpDownload(const char* url, const char* saveFilePath, HttpDownloadFunc func, void *param);
bool HttpDownload(const std::string& url, const std::string& saveFilePath, HttpDownloadFunc func, void *param);
#endif /* __HGVERSION_H__ */

View File

@ -399,7 +399,7 @@ static bool Setup(const std::string& pkgPath)
StartupInfo.cb = sizeof(StartupInfo);
char command[256];
sprintf(command, "%s %s", Utf8ToStdString(pkgPath).c_str(), "/verysilent");
sprintf(command, "%s %s", pkgPath.c_str(), "/verysilent");
if (CreateProcessA(nullptr, command, nullptr, nullptr, FALSE, 0, nullptr, nullptr, &StartupInfo, &ProcessInfo))
{
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);