ResourceWatcherFSM.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. #pragma once
  2. #include "stdafx.h"
  3. #include "SpFSM.h"
  4. #define JUS_A_TEST 0
  5. #include "FileInfo.h"
  6. #include <iostream>
  7. #include <iomanip>
  8. #include <fstream>
  9. #include <stdarg.h>
  10. #include <string>
  11. #include <vector>
  12. #include <assert.h>
  13. #include <time.h>
  14. #include <map>
  15. #include <unordered_map>
  16. #if defined(RVC_OS_LINUX)
  17. #include <sys/sysinfo.h>
  18. #include <time.h>
  19. #include <sys/types.h>
  20. #include <dirent.h>
  21. #include <sys/stat.h>
  22. #include "CommEntityUtil.hpp"
  23. #else
  24. #include <ws2tcpip.h>
  25. #include <tchar.h>
  26. #include <WinCrypt.h>
  27. #include <atlbase.h>
  28. #include <conio.h>
  29. #include "mbnapi.h"
  30. #include <comdef.h>
  31. #pragma comment(lib, "ws2_32.lib")
  32. #pragma comment(lib, "mbnapi_uuid.lib")
  33. #include "ResourceWatcher_UserCode.h"
  34. #include <time.h>
  35. #pragma comment(lib, "Gdi32.lib")
  36. #endif //RVC_OS_LINUX
  37. #include "FileSimpleComp.h"
  38. using namespace std;
  39. #define SYSVAR_FRAMEWORK_FIRST_BOOT "FirstLaunchSinceSysBoot" //框架开机后首次启动标识
  40. #define SYSVAR_FRAMEWORK_FIRST_BOOT_YES "Y"
  41. #define SYSVAR_FRAMEWORK_FIRST_BOOT_NO "N"
  42. enum EvtType
  43. {
  44. USER_EVT_RUN = EVT_USER + 1
  45. };
  46. #include "ResourceWatcher_server_g.h"
  47. #include "ResourceWatcher_msg_g.h"
  48. using namespace ResourceWatcher;
  49. #ifdef RVC_OS_LINUX
  50. #define MAX_SOFTWARE_INSTALLED_DETECT_COUNT 50
  51. #endif // RVC_OS_LINUX
  52. #define MOD_VERSION "1.0.0.0"
  53. struct CInstallInfoEx : public CInstallInfo
  54. {
  55. CInstallInfoEx(const CInstallInfo& info)
  56. :CInstallInfo(info)
  57. {
  58. /* empty */
  59. }
  60. bool operator < (const CInstallInfoEx& rhs) const
  61. {
  62. CVersion l_version(this->InstallVersion);
  63. DWORD l_timeTick = (DWORD)this->tmSwithOverDate;
  64. CVersion r_version(rhs.InstallVersion);
  65. DWORD r_timeTick = (DWORD)rhs.tmSwithOverDate;
  66. if (l_version == r_version)
  67. return l_timeTick <= r_timeTick;
  68. return l_version > r_version;
  69. }
  70. };
  71. #ifdef RVC_OS_LINUX
  72. //chenjilin hardware change
  73. typedef struct CPUInfoStruct
  74. { // 记录 /proc/stat 中的CPU信息
  75. char name[20];
  76. unsigned int user;
  77. unsigned int nice;
  78. unsigned int system;
  79. unsigned int idle;
  80. unsigned int lowait;
  81. unsigned int irq;
  82. unsigned int softirq;
  83. }CPUInfo;
  84. typedef struct ProcCPUInfoStruct {
  85. unsigned int pid;
  86. char pname[1024];
  87. unsigned long utime; //user time
  88. unsigned long stime; //kernel time
  89. unsigned long cutime; //all user time
  90. unsigned long cstime; //all dead time
  91. }ProcCPUInfo;
  92. typedef struct MemInfoStruct // 记录 /proc/meminfo 中的内存信息
  93. {
  94. char infoName1[20];
  95. unsigned long memTotal;
  96. char infoName2[20];
  97. unsigned long memFree;
  98. char infoName3[20];
  99. unsigned long memAvail;
  100. }MemInfo;
  101. //change end
  102. #else
  103. struct EntityRunInfo
  104. {
  105. bool bGetLoadOpt;
  106. bool bRestarting;
  107. int eState;
  108. int eTest;
  109. int loadOpt;
  110. int memoryHighCount;
  111. int cpuRatio;
  112. FILETIME prevSysKernel, prevSysUser;
  113. FILETIME prevProcKernel, prevProcUser;
  114. };
  115. struct CPUInforStruct {
  116. bool bInitialed;
  117. int level;
  118. CSimpleStringA csBrand;
  119. };
  120. #endif // RVC_OS_LINUX
  121. struct SystemBasicInfo
  122. {
  123. CSimpleStringA strManufacturer;
  124. CSimpleStringA strProductName;
  125. CSimpleStringA strSerialNumber;
  126. SystemBasicInfo():strManufacturer(true), strProductName(true), strSerialNumber(true) {}
  127. };
  128. const int DEFAULT_DAY_OF_BACKWARD = 90;
  129. class ResourceWatcherFSM : public FSMImpl<ResourceWatcherFSM>
  130. {
  131. public:
  132. ResourceWatcherFSM(void);
  133. ~ResourceWatcherFSM(void);
  134. enum { s0, s1, s2, s3 };
  135. BEGIN_FSM_STATE(ResourceWatcherFSM)
  136. FSM_STATE_ENTRY(s0, "Idle", s0_on_entry, s0_on_exit, s0_on_event)
  137. FSM_STATE_ENTRY(s1, "Running", s1_on_entry, s1_on_exit, s1_on_event)
  138. FSM_STATE_ENTRY(s2, "Failed", s2_on_entry, s2_on_exit, s2_on_event)
  139. FSM_STATE_ENTRY(s3, "Listerning", s3_on_entry, s3_on_exit, s3_on_event)
  140. END_FSM_STATE()
  141. BEGIN_FSM_RULE(ResourceWatcherFSM, s0)
  142. FSM_RULE_ENTRY(s0, s1, USER_EVT_RUN, 0)
  143. END_FSM_RULE()
  144. virtual ErrorCodeEnum OnInit();
  145. virtual ErrorCodeEnum OnExit();
  146. //进入首页时触发的功能在此
  147. void TriggerAtStatusChanged(bool bMStatus);
  148. void AfterInit();
  149. virtual void s0_on_entry();
  150. virtual void s0_on_exit();
  151. virtual unsigned int s0_on_event(FSMEvent* e);
  152. virtual void s1_on_entry();
  153. virtual void s1_on_exit();
  154. virtual unsigned int s1_on_event(FSMEvent* e);
  155. virtual void s2_on_entry();
  156. virtual void s2_on_exit();
  157. virtual unsigned int s2_on_event(FSMEvent* e);
  158. virtual void s3_on_entry();
  159. virtual void s3_on_exit();
  160. virtual unsigned int s3_on_event(FSMEvent* e);
  161. void SelfTest(EntityTestEnum eTestType, CSmartPointer<ITransactionContext> pTransactionContext);
  162. BOOL IsFirstRunAfterBoot() const
  163. {
  164. return m_bFirstRunAfterBoot;
  165. }
  166. #ifdef RVC_OS_LINUX
  167. void UploadMonitorSettings();
  168. bool GetMonitorInfo(CSimpleStringA& outInfo);
  169. void DetectSoftwareInstallStatus();
  170. #else
  171. void SetForceDiskCheckFlag(bool flag)
  172. {
  173. m_bNeedForceDiskCheck = flag;
  174. }
  175. #endif // RVC_OS_LINUX
  176. private:
  177. int m_cpuTop; // CPU资源消耗最高的top进程个数
  178. CSystemStaticInfo m_RvcSysinfo;
  179. typedef std::string bs_key;
  180. typedef DWORD bs_value;
  181. typedef map<bs_key, bs_value> BlackSheet;
  182. typedef BlackSheet::const_iterator const_bs_iter;
  183. typedef BlackSheet::iterator bs_iter;
  184. typedef std::pair<bs_key, bs_value> BlackSheetPair;
  185. std::map<std::string, DWORD> m_forbidDirList;
  186. CSimpleStringA m_strTerminalNo;
  187. BOOL m_bFirstRunAfterBoot;
  188. int m_diskLastWarnHour;
  189. #ifdef RVC_OS_WIN
  190. bool m_DoneDetectAutostart;
  191. //1:将VBS改成EXE,前提是VBS必须已在注册表
  192. //2:设置使用EXE自启动
  193. //3:不设置开机自启动(通过注册表)
  194. int m_forceStartupWithExeFlag;
  195. bool m_bIsVerified;
  196. vector<CSimpleStringA>m_nonSignedFiles;
  197. int m_iNonSignedTotal;
  198. vector<CSimpleStringA> m_activeEntity, m_allEntity, m_vKeyEntity, m_vQueryStateEntity;
  199. int m_restartNormal, m_restartSpecial, m_maxOsRestart, m_maxPowerRestart, m_warmLevel;
  200. DWORD m_dwIssueCount, m_dwCaptureCount;
  201. bool m_bInIssue, m_bFirstCalcCpu, m_bEverInMainPage, m_bHaveGetEntityList, m_bFWBEntityAdd;
  202. bool m_bNeedForceDiskCheck;
  203. int m_simulateTest;
  204. CSimpleStringA m_csKeyEntity;
  205. #endif // RVC_OS_LINUX
  206. int m_iVerify;
  207. int m_iNonSignedDisplay;
  208. int m_cpuHighPercent, m_memHighPercent, m_diskHighPercent;
  209. FILE_LAYOUT_ENTRY m_FileEntry;
  210. BOOL checkActiveBefore;
  211. BOOL firstMpage;
  212. public:
  213. void CheckDiskFileSpace();
  214. ErrorCodeEnum BizLinkDetect(
  215. SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req,
  216. ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx);
  217. ErrorCodeEnum CheckNetType(
  218. SpReqAnsContext<ResourceWatcherService_CheckNetType_Req,
  219. ResourceWatcherService_CheckNetType_Ans>::Pointer ctx);
  220. ErrorCodeEnum GetBizLinks(
  221. SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req,
  222. ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx);
  223. void GetSystemCPUStatus();
  224. void GetSystemMemoryStatus();
  225. void GetSystemDiskStatus();
  226. void GetDiskStatusFrom(LPCTSTR path);
  227. void HardwareInfoTimer(void* pData);
  228. void DiskCheckTimer(void* pData);
  229. void LinkDetect(int detectType, const char* url, bool& status, unsigned int& delay, int warnFlag = 1);
  230. #if defined(RVC_OS_WIN)
  231. int GetCpuUsageRatio(int pid);
  232. ULONGLONG GetDiskUsedByte();
  233. #endif // RVC_OS_WIN
  234. time_t GetPathTimeSeconds(const char* inPath);
  235. void AutoDeleteFiles();
  236. void CenterSettingDelete();
  237. BOOL DeleteVersionPackage();
  238. void DeleteVideoFiles();
  239. int ProcessFileDelete(LPCTSTR lpszPath, int& nDelSucCnt, int& nDelFailedCnt, bool delDir, unsigned int saveBackDay = 0);
  240. BOOL DetectIsFirstRunAtBoot();
  241. #ifdef RVC_OS_LINUX
  242. /** 返回操作系统当前的版本,比如1032,1010,1031*/
  243. UINT GetSystemDisplayVersion(CSimpleStringA& strVersion);
  244. void UploadSysVersionInfo(UINT& ver);
  245. string MemoryProcessStatus();
  246. long GetCPURunTime(CPUInfo* cpu);
  247. long ReadProcCPURunTime(DWORD pid, ProcCPUInfo* pInfo);
  248. const char* GetItems(const char* buffer, unsigned int item);
  249. void ConfirmWindowEffectHasBeenOpen();
  250. void switchWindowsEffect(const char* cfgFilePath, bool enable);
  251. /** 1032操作系统版本出现剪贴板和操作中心,需要将剪贴板关掉避免手势唤起
  252. * 统信的人告知可以通过删除剪贴板程序实现……JUST DO IT!
  253. */
  254. void ConfirmDDEClipboardDisable();
  255. void RecoverDDEClipboardEnable();
  256. /** 自己摸索,统信说需要操作系统定制方能解决 [Gifur@202198]*/
  257. void ConfirmNotificationCenterDisable();
  258. #else
  259. bool CopyExeToRoot(const CSimpleStringA& csVersion);
  260. bool RegOperation();
  261. bool RegRdVtmVersion(CSimpleStringA& VTMpath, CSimpleStringA& vtmVersion);
  262. bool RegWtVtmPath();
  263. bool RegWtVtmVersion();
  264. void CreateLink(const CSimpleStringA& exePath, const CSimpleStringA& csVersion);
  265. bool CreateLinkFile(const CSimpleStringA& szStartAppPath, const CSimpleStringA& szAddCmdLine,
  266. const CSimpleStringA& szDestLnkPath, const CSimpleStringA& szIconPath);
  267. bool GetVtmVersionFromActiveTxt(CSimpleStringA& csVersion);
  268. bool UpdateExe();
  269. bool GetRegValue(HKEY hKey, LPCTSTR lpcszParam, CHAR* pSzValue, DWORD* pDwSizeOfSz);
  270. void DetectAutoStartupCover();
  271. //0:explorer.exe
  272. //1:vbs
  273. //2:vtm.exe
  274. //3:startmenu lnk from RVCTermimalApplicationSetup
  275. //4:startmenu lnk from install scripts
  276. //-1:failed
  277. int DetectAutoStartupType();
  278. ErrorCodeEnum ChangeAutoStartupWithExe(bool resetVBS = false, bool resetExplorer = false);
  279. int InitialAutoStartupSetType();
  280. BOOL IsHasSetAutoStartupByFolder(CSimpleStringA& strPath, bool& scriptsType);
  281. void DetectVersionHasChangedAndWarn();
  282. void DetectDestopFileAndWarn(bool bClear, CSimpleStringA& strFileSaveList);
  283. void DetectAutoStartFileAndWarn();
  284. ErrorCodeEnum DetectVTMInstalledBySetup(BOOL& fYes);
  285. void DetectVersionHasChangedAndWarnCover()
  286. {
  287. if (!checkActiveBefore) {
  288. DetectVersionHasChangedAndWarn();
  289. checkActiveBefore = TRUE;
  290. }
  291. }
  292. void VerifySignature();
  293. void DetectAndClearDesktopFile();
  294. void VerifySignature(const CSimpleStringA& filePath);
  295. CSimpleStringA GetFileName(const CSimpleStringA& filePath);
  296. bool RetrieveDigitalSignatureInfo(const char* pFilePath);
  297. bool VerifyFirstCertificate(HCERTSTORE hStore, PCMSG_SIGNER_INFO pSignerInfo);
  298. bool VerifyOtherCertificate(PCMSG_SIGNER_INFO pSignerInfo);
  299. int IsWifiConnected();
  300. void DetectWallpaperAndWarn();
  301. #endif // RVC_OS_LINUX
  302. void AlarmSystemBasicInfo();
  303. ErrorCodeEnum CatchSystemBasicInfo(SystemBasicInfo& info);
  304. };
  305. class RunEvent : public FSMEvent
  306. {
  307. public:
  308. RunEvent() : FSMEvent(USER_EVT_RUN) {}
  309. virtual ~RunEvent() {}
  310. };
  311. #ifdef RVC_OS_LINUX
  312. struct UploadMonitorInfoTask : public ITaskSp
  313. {
  314. ResourceWatcherFSM* m_pFSM;
  315. UploadMonitorInfoTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  316. void Process()
  317. {
  318. m_pFSM->UploadMonitorSettings();
  319. }
  320. };
  321. struct DetectSoftwareInstallStatusTask : public ITaskSp
  322. {
  323. ResourceWatcherFSM* m_pFSM;
  324. DetectSoftwareInstallStatusTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  325. void Process()
  326. {
  327. m_pFSM->DetectSoftwareInstallStatus();
  328. }
  329. };
  330. #else
  331. struct CreateLinkTask :public ITaskSp
  332. {
  333. ResourceWatcherFSM* m_pFSM;
  334. CreateLinkTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  335. void Process()
  336. {
  337. m_pFSM->RegOperation();
  338. }
  339. };
  340. struct VerifyDigitSignTask : public ITaskSp
  341. {
  342. ResourceWatcherFSM* m_pFSM;
  343. VerifyDigitSignTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  344. void Process()
  345. {
  346. m_pFSM->VerifySignature();
  347. }
  348. };
  349. struct ClearDesktopFileTask : public ITaskSp
  350. {
  351. ResourceWatcherFSM* m_pFSM;
  352. ClearDesktopFileTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  353. void Process()
  354. {
  355. m_pFSM->DetectAndClearDesktopFile();
  356. m_pFSM->DetectAutoStartFileAndWarn();
  357. }
  358. };
  359. #endif // RVC_OS_LINUX
  360. //终端版本默认的自动清理策略
  361. struct AutoDeleteFilesTask : public ITaskSp
  362. {
  363. ResourceWatcherFSM* m_pFSM;
  364. AutoDeleteFilesTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  365. void Process()
  366. {
  367. m_pFSM->AutoDeleteFiles();
  368. }
  369. };
  370. //通过集中配置执行额外的清理策略
  371. struct CenterSettingDeleteTask : public ITaskSp
  372. {
  373. ResourceWatcherFSM* m_pFSM;
  374. CenterSettingDeleteTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  375. void Process()
  376. {
  377. m_pFSM->CenterSettingDelete();
  378. }
  379. };
  380. struct CheckDiskFileSpaceTask : public ITaskSp
  381. {
  382. ResourceWatcherFSM* m_pFSM;
  383. CheckDiskFileSpaceTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  384. void Process()
  385. {
  386. m_pFSM->CheckDiskFileSpace();
  387. }
  388. };