增加安装和卸载日志上报功能

This commit is contained in:
luoliangyi 2022-06-30 16:42:46 +08:00
parent e6e239f529
commit 35bacd19ce
11 changed files with 357 additions and 26 deletions

View File

@ -1,16 +1,155 @@
#include "HGUpgrade.h"
#include "base/HGDef.h"
#include "base/HGInc.h"
#include "base/HGUtility.h"
#include "base/HGMd5.h"
#include "curl/curl.h"
#include <iostream>
#include <sstream>
#include <HGString.h>
bool PostInstallInfo()
static std::string GetCurrVersion()
{
return false;
std::string version = "0.0.0.0";
#if defined(HG_CMP_MSC)
HKEY hKey = NULL;
RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\HuaGoScan", 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
// TODO
#endif
return version;
}
bool PostUninstallInfo()
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
return false;
std::string data((const char*) ptr, (size_t) size * nmemb);
*((std::stringstream*) stream) << data << std::endl;
return size * nmemb;
}
bool Upgrade(const std::string& pkgPath)
// type:1表示安装 2表示卸载 src:来源 desc: 说明
static bool PostInfo(int type, const std::string &src, const std::string &desc)
{
return false;
}
curl_global_init(CURL_GLOBAL_ALL);
bool ret = false;
CURL* curl = curl_easy_init();
if (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());
HGByte mac[256];
HGUInt macCount = 0;
HGBase_GetMACAddress(mac, 256, &macCount);
char macStr[64] = {0};
sprintf(macStr, "%02x-%02x-%02x-%02x-%02x-%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
HGByte md5[16] = {0};
HGBase_MakeMd5(mac, macCount * 6, 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();
char json[1024];
sprintf(json, "{\"type\":%d, \"mac\":\"%s\", \"localid\":\"%s\", \"v\":\"%s\", \"ref\":\"%s\", \"desc\":\"%s\"}",
type, macStr, md5Str, version.c_str(), src.c_str(), desc.c_str());
struct curl_slist* headers = nullptr;
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
ret = true;
curl_slist_free_all(headers);
std::string str_json = out.str(); // 返回值
printf("%s\n", str_json.c_str());
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return ret;
}
bool PostInstallInfo(const std::string &src, const std::string &desc)
{
return PostInfo(1, src, desc);
}
bool PostUninstallInfo(const std::string &src, const std::string &desc)
{
return PostInfo(2, src, desc);
}
static bool Setup(const std::string& pkgPath)
{
bool ret = false;
#if defined(HG_CMP_MSC)
PROCESS_INFORMATION ProcessInfo;
STARTUPINFOA StartupInfo;
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof(StartupInfo);
char command[256];
sprintf(command, "%s %s", Utf8ToStdString(pkgPath).c_str(), "/verysilent");
if (CreateProcessA(NULL, command, NULL, NULL, FALSE, 0, NULL, NULL, &StartupInfo, &ProcessInfo))
{
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
ret = true;
}
#else
// TODO
#endif
return ret;
}
bool Upgrade(const std::string& pkgPath, const std::string &src, const std::string &desc)
{
bool ret = Setup(pkgPath);
if (ret)
PostInfo(1, src, desc);
return ret;
}

View File

@ -4,12 +4,12 @@
#include <string>
// 上传安装日志
bool PostInstallInfo();
bool PostInstallInfo(const std::string &src, const std::string &desc);
// 上传卸载日志
bool PostUninstallInfo();
bool PostUninstallInfo(const std::string &src, const std::string &desc);
// 升级安装, 使用之前的安装路径
bool Upgrade(const std::string& pkgPath);
bool Upgrade(const std::string& pkgPath, const std::string &src, const std::string &desc);
#endif /* __HGUPGRADE_H__ */
#endif /* __HGUPGRADE_H__ */

View File

@ -1,11 +1,44 @@
#include "mainwindow.h"
#include <QApplication>
#include "HGUpgrade.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
int type = 0;
std::string src;
std::string desc;
std::string pkgPath;
for (int i = 1; i < argc; i++)
{
char* z = argv[i];
if (0 == strcmp(z, "-type=postinstallinfo"))
type = 1;
else if (0 == strcmp(z, "-type=postuninstallinfo"))
type = 2;
else if (0 == strcmp(z, "-type=upgrade"))
type = 3;
else if (z == strstr(z, "-pkgpath="))
pkgPath = z + strlen("-pkgpath=");
else if (z == strstr(z, "-src="))
src = z + strlen("-src=");
else if (z == strstr(z, "-desc="))
desc = z + strlen("-desc=");
}
if (1 == type)
PostInstallInfo(src, desc);
else if (2 == type)
PostUninstallInfo(src, desc);
else if (3 == type && !pkgPath.empty())
{
QApplication a(argc, argv);
MainWindow w(pkgPath, src, desc);
w.show();
a.exec();
}
return 0;
}

View File

@ -1,15 +1,39 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "HGUpgrade.h"
MainWindow::MainWindow(QWidget *parent)
MainWindow::MainWindow(const std::string& pkgPath, const std::string& src, const std::string& desc, QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_pkgPath(pkgPath)
, m_src(src)
, m_desc(desc)
, m_thread(nullptr)
{
ui->setupUi(this);
setWindowTitle(tr("Installation in progress, please wait..."));
setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint);
connect(this, SIGNAL(closeWnd()), this, SLOT(close()), Qt::QueuedConnection);
HGBase_OpenThread(ThreadFunc, this, &m_thread);
}
MainWindow::~MainWindow()
{
HGBase_CloseThread(m_thread);
m_thread = NULL;
delete ui;
}
void MainWindow::ThreadFunc(HGThread thread, HGPointer param)
{
(void)thread;
MainWindow* p = (MainWindow*)param;
Upgrade(p->m_pkgPath, p->m_src, p->m_desc);
emit p->closeWnd();
}

View File

@ -2,6 +2,7 @@
#define MAINWINDOW_H
#include <QMainWindow>
#include "base/HGThread.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
@ -12,10 +13,21 @@ class MainWindow : public QMainWindow
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
MainWindow(const std::string &pkgPath, const std::string& src, const std::string& desc, QWidget *parent = nullptr);
~MainWindow();
signals:
void closeWnd();
private:
static void ThreadFunc(HGThread thread, HGPointer param);
private:
Ui::MainWindow *ui;
std::string m_pkgPath;
std::string m_src;
std::string m_desc;
HGThread m_thread;
};
#endif // MAINWINDOW_H

View File

@ -24,21 +24,27 @@ TARGET = HuaGoScanUpgrade
win32 {
DEFINES += _CRT_SECURE_NO_WARNINGS
DEFINES += CURL_STATICLIB
INCLUDEPATH += $$PWD/../../../third_party/libcurl/windows/include
LIBS += -lIphlpapi -lwldap32 -lws2_32
contains(QT_ARCH, i386) {
CONFIG(release, debug|release) {
LIBS += -L../../../third_party/libcurl/windows/lib/x86 -llibcurl
DESTDIR = ../../../../release/win/x86/Release/
}
CONFIG(debug, debug|release) {
LIBS += -L../../../third_party/libcurl/windows/lib/x86 -llibcurld
}
}
else {
CONFIG(release, debug|release) {
LIBS += -L../../../third_party/libcurl/windows/lib/x64 -llibcurl
DESTDIR = ../../../../release/win/x64/Release/
}
CONFIG(debug, debug|release) {
LIBS += -L../../../third_party/libcurl/windows/lib/x64 -llibcurld
}
}
}
@ -113,11 +119,19 @@ INCLUDEPATH += $$PWD/../../../modules/
SOURCES += \
../../../app/upgrade/HGUpgrade.cpp \
../../../app/upgrade/main.cpp \
../../../app/upgrade/mainwindow.cpp
../../../app/upgrade/mainwindow.cpp \
../../../modules/base/HGMd5.cpp \
../../../modules/base/HGUtility.cpp \
../../../modules/base/HGThread.cpp \
../../../utility/HGString.cpp
HEADERS += \
../../../app/upgrade/HGUpgrade.h \
../../../app/upgrade/mainwindow.h
../../../app/upgrade/mainwindow.h \
../../../modules/base/HGMd5.h \
../../../modules/base/HGUtility.h \
../../../modules/base/HGThread.h \
../../../utility/HGString.h
FORMS += \
../../../app/upgrade/mainwindow.ui

View File

@ -62,6 +62,7 @@ HGBase_GetFileName
HGBase_GetFilePrefix
HGBase_GetFileSuffix
HGBase_StandardiseFileName
HGBase_GetMACAddress
HGBase_SetProfileInt
HGBase_SetProfileString

View File

@ -145,7 +145,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<ModuleDefinitionFile>HGBase.def</ModuleDefinitionFile>
<AdditionalDependencies>gdiplus.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>gdiplus.lib;Iphlpapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>
@ -171,7 +171,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<ModuleDefinitionFile>HGBase.def</ModuleDefinitionFile>
<AdditionalDependencies>gdiplus.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>gdiplus.lib;Iphlpapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalOptions>/LTCG %(AdditionalOptions)</AdditionalOptions>
</Link>
<PostBuildEvent>
@ -196,7 +196,7 @@ copy $(OutDir)HGBase.dll $(SolutionDir)..\..\..\release\win\x86\Release\</Comman
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<ModuleDefinitionFile>HGBase.def</ModuleDefinitionFile>
<AdditionalDependencies>gdiplus.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>gdiplus.lib;Iphlpapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>
@ -222,7 +222,7 @@ copy $(OutDir)HGBase.dll $(SolutionDir)..\..\..\release\win\x86\Release\</Comman
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<ModuleDefinitionFile>HGBase.def</ModuleDefinitionFile>
<AdditionalDependencies>gdiplus.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>gdiplus.lib;Iphlpapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalOptions>/LTCG %(AdditionalOptions)</AdditionalOptions>
</Link>
<PostBuildEvent>

View File

@ -111,6 +111,7 @@
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Iphlpapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -130,6 +131,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalOptions>/LTCG %(AdditionalOptions)</AdditionalOptions>
<AdditionalDependencies>Iphlpapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>copy $(OutDir)upload.cgi $(SolutionDir)..\..\..\release\win\x86\Release\</Command>
@ -147,6 +149,7 @@
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>Iphlpapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -166,6 +169,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalOptions>/LTCG %(AdditionalOptions)</AdditionalOptions>
<AdditionalDependencies>Iphlpapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>copy $(OutDir)upload.cgi $(SolutionDir)..\..\..\release\win\x64\Release\</Command>

View File

@ -3,6 +3,7 @@
#if defined(HG_CMP_MSC)
#include <shlobj.h>
#include <atlstr.h>
#include <iphlpapi.h>
#else
#include "uuid/uuid.h"
#endif
@ -578,3 +579,103 @@ HGResult HGAPI HGBase_StandardiseFileName(const HGChar* fileName, HGChar* result
strcpy(result, stdFileName.c_str());
return HGBASE_ERR_OK;
}
HGResult HGAPI HGBase_GetMACAddress(HGByte *mac, HGUInt maxLen, HGUInt* macCount)
{
if (NULL == mac || 0 == maxLen || NULL == macCount)
{
return HGBASE_ERR_INVALIDARG;
}
*macCount = 0;
HGByte* p = mac;
HGUInt len = maxLen;
#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)
{
if (6 != pAdapter->AddressLength)
{
pAdapter = pAdapter->Next;
continue;
}
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) && (len >= pAdapter->AddressLength))
{
memcpy(p, pAdapter->Address, pAdapter->AddressLength);
p += pAdapter->AddressLength;
len -= pAdapter->AddressLength;
++(*macCount);
}
pAdapter = pAdapter->Next;
}
}
free(pAdapterInfo);
pAdapterInfo = NULL;
#else
// TODO
#endif
return HGBASE_ERR_OK;
}

View File

@ -77,4 +77,7 @@ HGEXPORT HGResult HGAPI HGBase_GetFileSuffix(const HGChar *fileName, HGChar* suf
/* 将文件名标准化 */
HGEXPORT HGResult HGAPI HGBase_StandardiseFileName(const HGChar* fileName, HGChar *result, HGUInt maxLen);
#endif /* __HGUTILITY_H__ */
/* 获取MAC地址 */
HGEXPORT HGResult HGAPI HGBase_GetMACAddress(HGByte *mac, HGUInt maxLen, HGUInt *macCount);
#endif /* __HGUTILITY_H__ */