928 lines
28 KiB
C++
928 lines
28 KiB
C++
#include "HGVersionImpl.hpp"
|
|
#include "../base/HGInc.h"
|
|
#include "../base/HGInfo.h"
|
|
#include "../base/HGMd5.h"
|
|
#include "../base/HGUtility.h"
|
|
#include <string>
|
|
#include <vector>
|
|
#include <list>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <algorithm>
|
|
#include "cJSON.h"
|
|
#include "curl/curl.h"
|
|
#if defined(HG_CMP_MSC)
|
|
#include <iphlpapi.h>
|
|
#endif
|
|
|
|
struct VersionInfoImpl
|
|
{
|
|
VersionInfoImpl()
|
|
{
|
|
size = 0;
|
|
}
|
|
|
|
std::string version;
|
|
std::string desc;
|
|
std::string bugInfo;
|
|
std::string url;
|
|
unsigned long long size;
|
|
std::string md5;
|
|
};
|
|
|
|
static size_t write_data(void* ptr, size_t size, size_t nmemb, void* stream)
|
|
{
|
|
std::string data((const char*)ptr, (size_t)size * nmemb);
|
|
|
|
*((std::stringstream*)stream) << data << std::endl;
|
|
|
|
return size * nmemb;
|
|
}
|
|
|
|
static HGResult GetServerConfig(HGServerConfig& config)
|
|
{
|
|
config.postCrashInfo = HGFALSE;
|
|
config.postUserBehavior = HGFALSE;
|
|
config.postDeviceInfo = HGFALSE;
|
|
|
|
HGResult ret = HGBASE_ERR_FAIL;
|
|
CURL* curl = curl_easy_init();
|
|
if (NULL != curl)
|
|
{
|
|
std::stringstream out;
|
|
|
|
std::string url = "http://up.appqy.com/api/ver/setting";
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2);
|
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
|
|
struct curl_slist* headers = NULL;
|
|
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
|
|
{
|
|
std::string str_json = out.str(); // 返回值
|
|
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "str_json=%s", str_json.c_str());
|
|
|
|
cJSON* json = cJSON_Parse(str_json.c_str());
|
|
if (NULL != json)
|
|
{
|
|
cJSON* p = json->child;
|
|
while (NULL != p)
|
|
{
|
|
if (0 == strcmp(p->string, "code") && p->type == cJSON_Number)
|
|
{
|
|
if (1 == p->valueint)
|
|
ret = HGBASE_ERR_OK;
|
|
}
|
|
else if (0 == strcmp(p->string, "data") && p->type == cJSON_Object)
|
|
{
|
|
cJSON* p2 = p->child;
|
|
while (NULL != p2)
|
|
{
|
|
if (0 == strcmp(p2->string, "report_switch"))
|
|
config.postUserBehavior = (HGBool)p2->valueint;
|
|
else if (0 == strcmp(p2->string, "collapse_switch"))
|
|
config.postCrashInfo = (HGBool)p2->valueint;
|
|
else if (0 == strcmp(p2->string, "device_info_switch"))
|
|
config.postDeviceInfo = (HGBool)p2->valueint;
|
|
|
|
p2 = p2->next;
|
|
}
|
|
}
|
|
|
|
p = p->next;
|
|
}
|
|
|
|
cJSON_Delete(json);
|
|
}
|
|
}
|
|
|
|
curl_slist_free_all(headers);
|
|
/* always cleanup */
|
|
curl_easy_cleanup(curl);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int xferinfo(void* p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
|
|
{
|
|
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "ulnow=%lld, ultotal=%lld", ulnow, ultotal);
|
|
return 0;
|
|
}
|
|
|
|
static HGResult CrashFileUpload(const std::string& crashFilePath, std::string& crashFileUrl)
|
|
{
|
|
crashFileUrl.clear();
|
|
|
|
HGResult ret = HGBASE_ERR_FAIL;
|
|
CURL* curl = curl_easy_init();
|
|
if (NULL != curl)
|
|
{
|
|
std::stringstream out;
|
|
|
|
struct curl_httppost* formpost = NULL;
|
|
struct curl_httppost* lastptr = NULL;
|
|
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "file", CURLFORM_FILE, crashFilePath.c_str(), CURLFORM_END);
|
|
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Submit", CURLFORM_END);
|
|
|
|
std::string url = "http://up.appqy.com/api/coollapse/upload";
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2);
|
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
|
|
|
|
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
|
|
{
|
|
std::string str_json = out.str(); // 返回值
|
|
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "str_json=%s", str_json.c_str());
|
|
|
|
cJSON* json = cJSON_Parse(str_json.c_str());
|
|
if (NULL != json)
|
|
{
|
|
cJSON* p = json->child;
|
|
while (NULL != p)
|
|
{
|
|
if (0 == strcmp(p->string, "code") && p->type == cJSON_Number)
|
|
{
|
|
if (1 == p->valueint)
|
|
ret = HGBASE_ERR_OK;
|
|
}
|
|
else if (0 == strcmp(p->string, "data") && p->type == cJSON_Object)
|
|
{
|
|
cJSON* p2 = p->child;
|
|
while (NULL != p2)
|
|
{
|
|
if (0 == strcmp(p2->string, "url") && p2->type == cJSON_String)
|
|
crashFileUrl = p2->valuestring;
|
|
|
|
p2 = p2->next;
|
|
}
|
|
}
|
|
|
|
p = p->next;
|
|
}
|
|
|
|
cJSON_Delete(json);
|
|
}
|
|
}
|
|
|
|
/* then cleanup the formpost chain */
|
|
curl_formfree(formpost);
|
|
/* always cleanup */
|
|
curl_easy_cleanup(curl);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static void GetMacAddrList(std::vector<std::string>& macList)
|
|
{
|
|
macList.clear();
|
|
|
|
#if defined(HG_CMP_MSC)
|
|
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
|
|
PIP_ADAPTER_INFO pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulOutBufLen);
|
|
|
|
ULONG nRet = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
|
|
if (ERROR_BUFFER_OVERFLOW == nRet)
|
|
{
|
|
free(pAdapterInfo);
|
|
pAdapterInfo = NULL;
|
|
pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulOutBufLen);
|
|
|
|
nRet = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
|
|
}
|
|
|
|
if (ERROR_SUCCESS == nRet)
|
|
{
|
|
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
|
|
while (NULL != pAdapter)
|
|
{
|
|
DWORD dwCharacteristics = 0;
|
|
|
|
HKEY hKey = NULL;
|
|
RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}", 0, KEY_ENUMERATE_SUB_KEYS, &hKey);
|
|
if (NULL != hKey)
|
|
{
|
|
DWORD dwIndex = 0;
|
|
|
|
while (1)
|
|
{
|
|
CHAR szValueName[256];
|
|
if (ERROR_SUCCESS != RegEnumKeyA(hKey, dwIndex, szValueName, 256))
|
|
{
|
|
break;
|
|
}
|
|
|
|
HKEY hKey2 = NULL;
|
|
RegOpenKeyExA(hKey, szValueName, 0, KEY_QUERY_VALUE, &hKey2);
|
|
if (NULL != hKey2)
|
|
{
|
|
DWORD dwType;
|
|
CHAR szData[256] = { 0 };
|
|
DWORD cbData = 256;
|
|
if (ERROR_SUCCESS == RegQueryValueExA(hKey2, "NetCfgInstanceId", NULL, &dwType, (LPBYTE)szData, &cbData) && REG_SZ == dwType)
|
|
{
|
|
if (0 == _stricmp(szData, pAdapter->AdapterName))
|
|
{
|
|
if (ERROR_SUCCESS == RegQueryValueExA(hKey2, "Characteristics", NULL, &dwType, (LPBYTE)szData, &cbData) && REG_DWORD == dwType)
|
|
{
|
|
dwCharacteristics = *(DWORD*)szData;
|
|
}
|
|
}
|
|
}
|
|
|
|
RegCloseKey(hKey2);
|
|
}
|
|
|
|
++dwIndex;
|
|
}
|
|
|
|
RegCloseKey(hKey);
|
|
}
|
|
|
|
if ((dwCharacteristics & 0x4))
|
|
{
|
|
std::string strMacAddr;
|
|
for (UINT i = 0; i < pAdapter->AddressLength; i++)
|
|
{
|
|
char str[10];
|
|
sprintf(str, "%02x", pAdapter->Address[i]);
|
|
strMacAddr += str;
|
|
if (i != pAdapter->AddressLength - 1)
|
|
strMacAddr += "-";
|
|
}
|
|
|
|
macList.push_back(strMacAddr);
|
|
}
|
|
|
|
pAdapter = pAdapter->Next;
|
|
}
|
|
}
|
|
|
|
free(pAdapterInfo);
|
|
pAdapterInfo = NULL;
|
|
#else
|
|
DIR* dir = opendir("/sys/class/net");
|
|
if (NULL != dir)
|
|
{
|
|
printf("opendir success\n");
|
|
|
|
struct dirent* ent;
|
|
while ((ent = readdir(dir)) != NULL)
|
|
{
|
|
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
|
|
continue;
|
|
|
|
if (strstr(ent->d_name, "docker") == ent->d_name || strstr(ent->d_name, "sit") == ent->d_name
|
|
|| strstr(ent->d_name, "ip6tnl") == ent->d_name || strcmp(ent->d_name, "lo") == 0)
|
|
continue;
|
|
|
|
printf("name %s\n", ent->d_name);
|
|
|
|
char addrPath[256];
|
|
sprintf(addrPath, "/sys/class/net/%s/address", ent->d_name);
|
|
|
|
FILE* file = fopen(addrPath, "rb");
|
|
if (NULL != file)
|
|
{
|
|
char mac[1025] = { 0 };
|
|
fread(mac, 1, 1024, file);
|
|
|
|
int len = (int)strlen(mac);
|
|
for (int i = 0; i < len; ++i)
|
|
{
|
|
if (mac[i] == ':')
|
|
mac[i] = '-';
|
|
else if (mac[i] == '\n')
|
|
mac[i] = '\0';
|
|
}
|
|
|
|
if (strlen(mac) > 0)
|
|
{
|
|
macList.push_back(mac);
|
|
}
|
|
|
|
printf("mac %s\n", mac);
|
|
fclose(file);
|
|
}
|
|
else
|
|
{
|
|
printf("fopen fail\n");
|
|
}
|
|
}
|
|
|
|
closedir(dir);
|
|
}
|
|
else
|
|
{
|
|
printf("opendir fail\n");
|
|
}
|
|
#endif
|
|
}
|
|
|
|
static std::string GetCurrVersion(const std::string& appName)
|
|
{
|
|
std::string version = "0.0.0.0";
|
|
|
|
if (appName == HGVERSION_APPNAME_SCANNER)
|
|
{
|
|
#if defined(HG_CMP_MSC)
|
|
#if defined(OEM_HANWANG)
|
|
std::string regName = "SOFTWARE\\HanvonScan";
|
|
#elif defined(OEM_LISICHENG)
|
|
std::string regName = "SOFTWARE\\LanxumScan";
|
|
#else
|
|
std::string regName = "SOFTWARE\\HuaGoScan";
|
|
#endif
|
|
|
|
HKEY hKey = NULL;
|
|
RegOpenKeyExA(HKEY_LOCAL_MACHINE, regName.c_str(), 0, KEY_QUERY_VALUE, &hKey);
|
|
if (NULL != hKey)
|
|
{
|
|
CHAR szData[MAX_PATH] = { 0 };
|
|
DWORD cbData = MAX_PATH;
|
|
if (ERROR_SUCCESS == RegQueryValueExA(hKey, "AppVersion", NULL, NULL, (LPBYTE)szData, &cbData))
|
|
{
|
|
version = szData;
|
|
}
|
|
|
|
RegCloseKey(hKey);
|
|
}
|
|
#else
|
|
#if defined(OEM_HANWANG)
|
|
std::string appName = "com.hanvonchina.hanvonscan";
|
|
#elif defined(OEM_LISICHENG)
|
|
std::string appName = "com.lanxumchina.lanxumscan";
|
|
#else
|
|
std::string appName = "com.huagaochina.huagoscan";
|
|
#endif
|
|
|
|
std::string cmd = "dpkg -l " + appName;
|
|
FILE* fp = popen(cmd.c_str(), "r");
|
|
if (NULL != fp)
|
|
{
|
|
char buff[2048] = { 0 };
|
|
fread(buff, 2048, 1, fp);
|
|
|
|
char* p = strstr(buff, appName.c_str());
|
|
if (NULL != p)
|
|
{
|
|
char* p2 = p + appName.size();
|
|
while (!isdigit(*p2) && '.' != *p2)
|
|
++p2;
|
|
|
|
int len = (int)strlen(p2);
|
|
for (int i = 0; i < len; ++i)
|
|
{
|
|
if (!isdigit(p2[i]) && '.' != p2[i])
|
|
{
|
|
p2[i] = '\0';
|
|
break;
|
|
}
|
|
}
|
|
|
|
version = p2;
|
|
}
|
|
|
|
pclose(fp);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
return version;
|
|
}
|
|
|
|
static std::string GetOSName()
|
|
{
|
|
std::string osName;
|
|
|
|
#if defined(HG_CMP_MSC)
|
|
osName = "Windows";
|
|
#else
|
|
osName = "Linux";
|
|
FILE* fp = popen("cat /etc/issue | cut -d\' \' -f1", "r");
|
|
if (NULL != 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);
|
|
}
|
|
#endif
|
|
|
|
return osName;
|
|
}
|
|
|
|
static std::string GetArchName()
|
|
{
|
|
std::string archName;
|
|
|
|
#if defined(HG_CMP_MSC)
|
|
#ifdef _WIN64
|
|
archName = "x64";
|
|
#else
|
|
archName = "x86";
|
|
#endif
|
|
#else
|
|
archName = "unknown";
|
|
FILE *fp = popen("arch", "r");
|
|
if (NULL != 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
|
|
|
|
return archName;
|
|
}
|
|
|
|
static std::string GetOemName()
|
|
{
|
|
std::string oemName;
|
|
#if defined(OEM_HANWANG)
|
|
oemName = "Hanvon";
|
|
#elif defined(OEM_LISICHENG)
|
|
oemName = "Lanxum";
|
|
#else
|
|
oemName = "Huago";
|
|
#endif
|
|
return oemName;
|
|
}
|
|
|
|
static bool Greater(const std::string& str1, const std::string& str2)
|
|
{
|
|
return str1 > str2;
|
|
}
|
|
|
|
static HGResult PostInfo(int type, const std::string& appName, const std::string& desc, const std::string& crashFileUrl,
|
|
const std::string& exceptionAddr, const std::string& feedbackInfo, const std::string& feedbackContact)
|
|
{
|
|
HGResult ret = HGBASE_ERR_FAIL;
|
|
CURL* curl = curl_easy_init();
|
|
if (NULL != curl)
|
|
{
|
|
std::stringstream out;
|
|
|
|
std::string url = "http://up.appqy.com/api/recode";
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2);
|
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
|
|
std::vector<std::string> macList;
|
|
GetMacAddrList(macList);
|
|
if (macList.empty())
|
|
macList.push_back("00-00-00-00-00-00");
|
|
|
|
std::sort(macList.begin(), macList.end(), Greater);
|
|
std::string strMacAddrTotal;
|
|
for (int i = 0; i < (int)macList.size(); ++i)
|
|
{
|
|
strMacAddrTotal += macList[i];
|
|
}
|
|
|
|
HGByte md5[16] = { 0 };
|
|
HGBase_MakeMd5((const HGByte*)strMacAddrTotal.c_str(), (HGUInt)strMacAddrTotal.size(), md5);
|
|
char md5Str[64] = { 0 };
|
|
char* pstr = md5Str;
|
|
for (int i = 0; i < 16; ++i)
|
|
{
|
|
sprintf(pstr, "%02x", md5[i]);
|
|
pstr += 2;
|
|
}
|
|
|
|
std::string version = GetCurrVersion(appName);
|
|
|
|
std::string osName = GetOSName();
|
|
std::string archName = GetArchName();
|
|
std::string oemName = GetOemName();
|
|
std::string source = osName + "-" + archName + "-" + oemName;
|
|
|
|
char json[1024];
|
|
if (1 == type || 2 == type)
|
|
{
|
|
sprintf(json, "{\"type\":%d, \"mac\":\"%s\", \"localid\":\"%s\", \"v\":\"%s\", \"ref\":\"%s\", \"desc\":\"%s\"}",
|
|
type, macList[0].c_str(), md5Str, version.c_str(), source.c_str(), desc.c_str());
|
|
}
|
|
else if (3 == type)
|
|
{
|
|
sprintf(json, "{\"type\":%d, \"mac\":\"%s\", \"localid\":\"%s\", \"v\":\"%s\", \"ref\":\"%s\", \"desc\":\"%s\", \"crashaddress\":\"%s\", \"crash_data\":\"%s\"}",
|
|
type, macList[0].c_str(), md5Str, version.c_str(), source.c_str(), desc.c_str(), exceptionAddr.c_str(), crashFileUrl.c_str());
|
|
}
|
|
else
|
|
{
|
|
assert(4 == type);
|
|
sprintf(json, "{\"type\":%d, \"mac\":\"%s\", \"localid\":\"%s\", \"v\":\"%s\", \"ref\":\"%s\", \"desc\":\"%s\", \"content\":\"%s\", \"contact\":\"%s\"}",
|
|
type, macList[0].c_str(), md5Str, version.c_str(), source.c_str(), desc.c_str(), feedbackInfo.c_str(), feedbackContact.c_str());
|
|
}
|
|
|
|
struct curl_slist* headers = NULL;
|
|
headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8");
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json);
|
|
|
|
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
|
|
{
|
|
std::string str_json = out.str(); // 返回值
|
|
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "str_json=%s", str_json.c_str());
|
|
|
|
cJSON* json = cJSON_Parse(str_json.c_str());
|
|
if (NULL != json)
|
|
{
|
|
cJSON* p = json->child;
|
|
while (NULL != p)
|
|
{
|
|
if (0 == strcmp(p->string, "code") && p->type == cJSON_Number)
|
|
{
|
|
if (1 == p->valueint)
|
|
ret = HGBASE_ERR_OK;
|
|
}
|
|
|
|
p = p->next;
|
|
}
|
|
|
|
cJSON_Delete(json);
|
|
}
|
|
}
|
|
|
|
curl_slist_free_all(headers);
|
|
/* always cleanup */
|
|
curl_easy_cleanup(curl);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static HGResult PostInstallInfo(const std::string& appName, const std::string& desc)
|
|
{
|
|
return PostInfo(1, appName, desc, "", "", "", "");
|
|
}
|
|
|
|
static HGResult PostUninstallInfo(const std::string& appName, const std::string& desc)
|
|
{
|
|
return PostInfo(2, appName, desc, "", "", "", "");
|
|
}
|
|
|
|
static HGResult PostCrashInfo(const std::string &appName, const std::string &desc, const std::string& crashFilePath, const std::string& exceptionAddr)
|
|
{
|
|
std::string crashFileUrl;
|
|
HGResult ret = CrashFileUpload(crashFilePath, crashFileUrl);
|
|
if (HGBASE_ERR_OK != ret)
|
|
{
|
|
return ret;
|
|
}
|
|
|
|
return PostInfo(3, appName, desc, crashFileUrl, exceptionAddr, "", "");
|
|
}
|
|
|
|
static HGResult PostUserFeedback(const std::string& appName, const std::string& desc, const std::string& feedback, const std::string& contact)
|
|
{
|
|
return PostInfo(4, appName, desc, "", "", feedback, contact);
|
|
}
|
|
|
|
static HGResult GetVersionList(const std::string& appName, std::list<VersionInfoImpl> &versionList)
|
|
{
|
|
versionList.clear();
|
|
|
|
HGResult ret = HGBASE_ERR_FAIL;
|
|
CURL* curl = curl_easy_init();
|
|
if (NULL != curl)
|
|
{
|
|
std::stringstream out;
|
|
|
|
std::string osName = GetOSName();
|
|
std::string archName = GetArchName();
|
|
std::string oemName = GetOemName();
|
|
|
|
std::string url = "http://up.appqy.com/api/ver?app=" + appName + "&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 = NULL;
|
|
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
|
|
{
|
|
std::string str_json = out.str(); // 返回值
|
|
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "str_json=%s", str_json.c_str());
|
|
|
|
cJSON* json = cJSON_Parse(str_json.c_str());
|
|
if (NULL != json)
|
|
{
|
|
cJSON* p = json->child;
|
|
while (NULL != p)
|
|
{
|
|
if (0 == strcmp(p->string, "code") && p->type == cJSON_Number)
|
|
{
|
|
if (1 == p->valueint)
|
|
ret = HGBASE_ERR_OK;
|
|
}
|
|
else if (0 == strcmp(p->string, "data") && p->type == cJSON_Array)
|
|
{
|
|
cJSON* p2 = p->child;
|
|
while (NULL != p2)
|
|
{
|
|
VersionInfoImpl version;
|
|
|
|
cJSON* p3 = p2->child;
|
|
while (NULL != 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 = atoll(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;
|
|
}
|
|
}
|
|
|
|
p = p->next;
|
|
}
|
|
|
|
cJSON_Delete(json);
|
|
}
|
|
}
|
|
|
|
curl_slist_free_all(headers);
|
|
/* always cleanup */
|
|
curl_easy_cleanup(curl);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static size_t curlWriteFunction(void* ptr, size_t size, size_t nmemb, FILE* stream)
|
|
{
|
|
return fwrite(ptr, size, nmemb, stream);
|
|
}
|
|
|
|
struct myprogress
|
|
{
|
|
HGHttpDownloadFunc func;
|
|
void* param;
|
|
};
|
|
|
|
static int xferinfo2(void* p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
|
|
{
|
|
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "ulnow=%lld, ultotal=%lld", ulnow, ultotal);
|
|
|
|
struct myprogress* myp = (struct myprogress*)p;
|
|
if (NULL != myp->func)
|
|
return myp->func(dltotal, dlnow, myp->param);
|
|
return 0;
|
|
}
|
|
|
|
static HGResult HttpDownload(const std::string& url, const std::string& saveFilePath, HGHttpDownloadFunc func, void* param)
|
|
{
|
|
HGResult ret = HGBASE_ERR_FAIL;
|
|
CURL* curl = curl_easy_init();
|
|
if (NULL != curl)
|
|
{
|
|
FILE* fp = fopen(saveFilePath.c_str(), "wb"); // 打开文件,准备写入
|
|
if (NULL != fp)
|
|
{
|
|
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);
|
|
|
|
struct myprogress prog;
|
|
prog.func = func;
|
|
prog.param = param;
|
|
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &prog);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo2);
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
|
|
|
|
/* 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 = HGBASE_ERR_OK;
|
|
|
|
fclose(fp);
|
|
}
|
|
|
|
curl_easy_cleanup(curl);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
|
|
HGVersionMgrImpl::HGVersionMgrImpl()
|
|
{
|
|
m_serverCfg.postCrashInfo = HGFALSE;
|
|
m_serverCfg.postUserBehavior = HGFALSE;
|
|
m_serverCfg.postDeviceInfo = HGFALSE;
|
|
m_thread = NULL;
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
::GetServerConfig(m_serverCfg);
|
|
}
|
|
|
|
HGVersionMgrImpl::~HGVersionMgrImpl()
|
|
{
|
|
curl_global_cleanup();
|
|
}
|
|
|
|
HGResult HGVersionMgrImpl::GetServerConfig(HGServerConfig* config)
|
|
{
|
|
if (NULL == config)
|
|
{
|
|
return HGBASE_ERR_INVALIDARG;
|
|
}
|
|
|
|
memcpy(config, &m_serverCfg, sizeof(HGServerConfig));
|
|
return HGBASE_ERR_OK;
|
|
}
|
|
|
|
HGResult HGVersionMgrImpl::PostInstallInfo(const HGChar* appName, const HGChar* desc)
|
|
{
|
|
return ::PostInstallInfo(appName, desc);
|
|
}
|
|
|
|
HGResult HGVersionMgrImpl::PostUninstallInfo(const HGChar* appName, const HGChar* desc)
|
|
{
|
|
return ::PostUninstallInfo(appName, desc);
|
|
}
|
|
|
|
HGResult HGVersionMgrImpl::PostCrashInfo(const HGChar* appName, const HGChar* desc, const HGChar* crashFilePath, const HGChar* exceptionAddr)
|
|
{
|
|
return ::PostCrashInfo(appName, desc, crashFilePath, exceptionAddr);
|
|
}
|
|
|
|
HGResult HGVersionMgrImpl::PostUserFeedback(const HGChar* appName, const HGChar* desc, const HGChar* feedback, const HGChar* contact)
|
|
{
|
|
return ::PostUserFeedback(appName, desc, feedback, contact);
|
|
}
|
|
|
|
HGResult HGVersionMgrImpl::GetVersionList(const HGChar* appName, HGVersionInfo** info, HGUInt* count)
|
|
{
|
|
if (NULL == info || NULL == count)
|
|
{
|
|
return HGBASE_ERR_INVALIDARG;
|
|
}
|
|
|
|
std::list<VersionInfoImpl> versionList;
|
|
HGResult ret = ::GetVersionList(appName, versionList);
|
|
if (HGBASE_ERR_OK != ret)
|
|
{
|
|
return ret;
|
|
}
|
|
|
|
if (versionList.empty())
|
|
{
|
|
return HGBASE_ERR_FAIL;
|
|
}
|
|
|
|
HGUInt count2 = (HGUInt)versionList.size();
|
|
HGVersionInfo *info2 = new HGVersionInfo[count2];
|
|
|
|
HGUInt index = 0;
|
|
std::list<VersionInfoImpl>::const_iterator iter;
|
|
for (iter = versionList.begin(); iter != versionList.end(); ++iter)
|
|
{
|
|
const VersionInfoImpl& ver = *iter;
|
|
info2[index].version = new HGChar[ver.version.size() + 1];
|
|
strcpy(info2[index].version, ver.version.c_str());
|
|
info2[index].desc = new HGChar[ver.desc.size() + 1];
|
|
strcpy(info2[index].desc, ver.desc.c_str());
|
|
info2[index].bugInfo = new HGChar[ver.bugInfo.size() + 1];
|
|
strcpy(info2[index].bugInfo, ver.bugInfo.c_str());
|
|
info2[index].url = new HGChar[ver.url.size() + 1];
|
|
strcpy(info2[index].url, ver.url.c_str());
|
|
info2[index].size = ver.size;
|
|
info2[index].md5 = new HGChar[ver.md5.size() + 1];
|
|
strcpy(info2[index].md5, ver.md5.c_str());
|
|
|
|
++index;
|
|
}
|
|
|
|
*info = info2;
|
|
*count = count2;
|
|
return HGBASE_ERR_OK;
|
|
}
|
|
|
|
HGResult HGVersionMgrImpl::HttpDownload(const HGChar* url, const HGChar* saveFilePath, HGHttpDownloadFunc func, HGPointer param)
|
|
{
|
|
return ::HttpDownload(url, saveFilePath, func, param);
|
|
}
|
|
|
|
HGResult HGVersionMgrImpl::ReleaseVersionList(HGVersionInfo* info, HGUInt count)
|
|
{
|
|
if (NULL == info || 0 == count)
|
|
{
|
|
return HGBASE_ERR_INVALIDARG;
|
|
}
|
|
|
|
for (HGUInt i = 0; i < count; ++i)
|
|
{
|
|
delete[] info[i].version;
|
|
info[i].version = NULL;
|
|
delete[] info[i].desc;
|
|
info[i].desc = NULL;
|
|
delete[] info[i].bugInfo;
|
|
info[i].bugInfo = NULL;
|
|
delete[] info[i].url;
|
|
info[i].url = NULL;
|
|
delete[] info[i].md5;
|
|
info[i].md5 = NULL;
|
|
}
|
|
|
|
delete[] info;
|
|
return HGBASE_ERR_OK;
|
|
}
|
|
|
|
HGResult HGVersionMgrImpl::GetCurrVersion(const HGChar* appName, HGChar* version, HGUInt maxLen)
|
|
{
|
|
if (NULL == version || 0 == maxLen)
|
|
{
|
|
return HGBASE_ERR_INVALIDARG;
|
|
}
|
|
|
|
std::string ver = ::GetCurrVersion(appName);
|
|
if (maxLen < ver.size() + 1)
|
|
{
|
|
return HGBASE_ERR_FAIL;
|
|
}
|
|
|
|
strcpy(version, ver.c_str());
|
|
return HGBASE_ERR_OK;
|
|
} |