ResourceWatcherFSM.h 12 KB

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