MixpanelScript.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "MixpanelScript.h"
  2. #define _WIN32_DCOM
  3. #include <iostream>
  4. #include <comdef.h>
  5. #include <Wbemidl.h>
  6. #pragma comment(lib, "wbemuuid.lib")
  7. void MixpanelScript::PostData(std::string EventName, std::string DistinctId, std::string InsertId, int error)
  8. {
  9. CURL* curl;
  10. CURLcode res;
  11. time_t systemtime;
  12. time(&systemtime);
  13. nlohmann::json data = { {{"event", EventName},
  14. {"properties",
  15. {{"time", systemtime},
  16. {"distinct_id", DistinctId},
  17. {"error", error},
  18. {"$insert_id",
  19. InsertId}}}} };
  20. std::string data_str = data.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);
  21. curl_global_init(CURL_GLOBAL_ALL);
  22. curl = curl_easy_init();
  23. if (curl) {
  24. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
  25. // 设置请求URL和参数
  26. curl_easy_setopt(curl, CURLOPT_URL,
  27. "https://api.mixpanel.com/import?strict=1");
  28. // 设置身份验证(填API_Secret)
  29. curl_easy_setopt(curl, CURLOPT_USERPWD, "b1074a764fc7926acbc978d13ad663d7");
  30. // 设置请求头
  31. struct curl_slist* headers = NULL;
  32. headers = curl_slist_append(headers, "accept: application/json");
  33. headers = curl_slist_append(headers, "Content-Type: application/json");
  34. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  35. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data_str.c_str());
  36. // 发送请求
  37. res = curl_easy_perform(curl);
  38. if (res != CURLE_OK) {
  39. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  40. curl_easy_strerror(res));
  41. }
  42. // 回收资源
  43. curl_easy_cleanup(curl);
  44. curl_slist_free_all(headers);
  45. }
  46. curl_global_cleanup();
  47. }
  48. std::string MixpanelScript::GetGUID()
  49. {
  50. std::string guidString="";
  51. GUID uuid; // 用于存储GUID的变量
  52. // 生成GUID
  53. if (UuidCreate(&uuid) != RPC_S_OK) {
  54. std::cerr << "Error generating GUID" << std::endl;
  55. return guidString;
  56. }
  57. // 将GUID转换为自字符串
  58. RPC_CSTR szUuid = NULL;
  59. if (UuidToStringA(&uuid, &szUuid) != RPC_S_OK) {
  60. std::cerr << "Error converting GUID to string" << std::endl;
  61. return guidString;
  62. }
  63. // 释放内存
  64. RpcStringFreeA(&szUuid);
  65. guidString = GuidToString(uuid);
  66. return guidString;
  67. }
  68. std::string MixpanelScript::GetCPUID()
  69. {
  70. std::string wmi = "XXXXXXXXXXXXXXXX";
  71. HRESULT hres;
  72. hres = CoInitializeEx(0, COINIT_MULTITHREADED);
  73. if (FAILED(hres))
  74. {
  75. return "XXXXXXXXXXXXXXXX";
  76. }
  77. hres = CoInitializeSecurity(
  78. NULL,
  79. -1, // COM authentication
  80. NULL, // Authentication services
  81. NULL, // Reserved
  82. RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
  83. RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
  84. NULL, // Authentication info
  85. EOAC_NONE, // Additional capabilities
  86. NULL // Reserved
  87. );
  88. if (FAILED(hres))
  89. {
  90. CoUninitialize();
  91. return "XXXXXXXXXXXXXXXX";
  92. }
  93. IWbemLocator* pLoc = NULL;
  94. hres = CoCreateInstance(
  95. CLSID_WbemLocator,
  96. 0,
  97. CLSCTX_INPROC_SERVER,
  98. IID_IWbemLocator, (LPVOID*)&pLoc);
  99. if (FAILED(hres))
  100. {
  101. CoUninitialize();
  102. return "XXXXXXXXXXXXXXXX"; // Program has failed.
  103. }
  104. IWbemServices* pSvc = NULL;
  105. hres = pLoc->ConnectServer(
  106. _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
  107. NULL, // User name. NULL = current user
  108. NULL, // User password. NULL = current
  109. 0, // Locale. NULL indicates current
  110. NULL, // Security flags.
  111. 0, // Authority (for example, Kerberos)
  112. 0, // Context object
  113. &pSvc // pointer to IWbemServices proxy
  114. );
  115. if (FAILED(hres))
  116. {
  117. pLoc->Release();
  118. CoUninitialize();
  119. return "XXXXXXXXXXXXXXXX"; // Program has failed.
  120. }
  121. hres = CoSetProxyBlanket(
  122. pSvc, // Indicates the proxy to set
  123. RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
  124. RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
  125. NULL, // Server principal name
  126. RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
  127. RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
  128. NULL, // client identity
  129. EOAC_NONE // proxy capabilities
  130. );
  131. if (FAILED(hres))
  132. {
  133. pSvc->Release();
  134. pLoc->Release();
  135. CoUninitialize();
  136. return "XXXXXXXXXXXXXXXX"; // Program has failed.
  137. }
  138. IEnumWbemClassObject* pEnumerator = NULL;
  139. hres = pSvc->ExecQuery(
  140. bstr_t("WQL"),
  141. bstr_t("SELECT * FROM Win32_Processor"),
  142. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  143. NULL,
  144. &pEnumerator);
  145. if (FAILED(hres))
  146. {
  147. pSvc->Release();
  148. pLoc->Release();
  149. CoUninitialize();
  150. return "XXXXXXXXXXXXXXXX"; // Program has failed.
  151. }
  152. IWbemClassObject* pclsObj = NULL;
  153. ULONG uReturn = 0;
  154. while (pEnumerator)
  155. {
  156. HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
  157. &pclsObj, &uReturn);
  158. if (0 == uReturn)
  159. {
  160. break;
  161. }
  162. VARIANT vtProp;
  163. VariantInit(&vtProp);
  164. // Get the value of the Name property
  165. hr = pclsObj->Get(L"ProcessorId", 0, &vtProp, 0, 0);
  166. _bstr_t bstr_t(vtProp.bstrVal);
  167. wmi= bstr_t;
  168. VariantClear(&vtProp);
  169. pclsObj->Release();
  170. }
  171. pSvc->Release();
  172. pLoc->Release();
  173. pEnumerator->Release();
  174. CoUninitialize();
  175. return wmi;
  176. }
  177. std::string MixpanelScript::GuidToString(const GUID& guid)
  178. {
  179. std::ostringstream oss;
  180. oss << std::hex << std::setw(8) << std::setfill('0') << guid.Data1
  181. << std::setw(4) << std::setfill('0') << guid.Data2
  182. << std::setw(4) << std::setfill('0') << guid.Data3
  183. << std::setw(2) << std::setfill('0') << static_cast<int>(guid.Data4[0])
  184. << std::setw(2) << std::setfill('0') << static_cast<int>(guid.Data4[1])
  185. << std::setw(2) << std::setfill('0') << static_cast<int>(guid.Data4[2])
  186. << std::setw(2) << std::setfill('0') << static_cast<int>(guid.Data4[3])
  187. << std::setw(2) << std::setfill('0') << static_cast<int>(guid.Data4[4])
  188. << std::setw(2) << std::setfill('0') << static_cast<int>(guid.Data4[5])
  189. << std::setw(2) << std::setfill('0') << static_cast<int>(guid.Data4[6])
  190. << std::setw(2) << std::setfill('0') << static_cast<int>(guid.Data4[7]);
  191. return oss.str();
  192. }