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. #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. bool GetSysActiveStatus(CSimpleStringA& outInfo);
  170. void DetectSoftwareInstallStatus();
  171. #else
  172. void SetForceDiskCheckFlag(bool flag)
  173. {
  174. m_bNeedForceDiskCheck = flag;
  175. }
  176. #endif // RVC_OS_LINUX
  177. private:
  178. int m_cpuTop; // CPU资源消耗最高的top进程个数
  179. CSystemStaticInfo m_RvcSysinfo;
  180. typedef std::string bs_key;
  181. typedef DWORD bs_value;
  182. typedef map<bs_key, bs_value> BlackSheet;
  183. typedef BlackSheet::const_iterator const_bs_iter;
  184. typedef BlackSheet::iterator bs_iter;
  185. typedef std::pair<bs_key, bs_value> BlackSheetPair;
  186. std::map<std::string, DWORD> m_forbidDirList;
  187. CSimpleStringA m_strTerminalNo;
  188. BOOL m_bFirstRunAfterBoot;
  189. int m_diskLastWarnHour;
  190. #ifdef RVC_OS_WIN
  191. bool m_DoneDetectAutostart;
  192. //1:将VBS改成EXE,前提是VBS必须已在注册表
  193. //2:设置使用EXE自启动
  194. //3:不设置开机自启动(通过注册表)
  195. int m_forceStartupWithExeFlag;
  196. bool m_bIsVerified;
  197. vector<CSimpleStringA>m_nonSignedFiles;
  198. int m_iNonSignedTotal;
  199. vector<CSimpleStringA> m_activeEntity, m_allEntity, m_vKeyEntity, m_vQueryStateEntity;
  200. int m_restartNormal, m_restartSpecial, m_maxOsRestart, m_maxPowerRestart, m_warmLevel;
  201. DWORD m_dwIssueCount, m_dwCaptureCount;
  202. bool m_bInIssue, m_bFirstCalcCpu, m_bEverInMainPage, m_bHaveGetEntityList, m_bFWBEntityAdd;
  203. bool m_bNeedForceDiskCheck;
  204. int m_simulateTest;
  205. CSimpleStringA m_csKeyEntity;
  206. #endif // RVC_OS_LINUX
  207. int m_iVerify;
  208. int m_iNonSignedDisplay;
  209. int m_cpuHighPercent, m_memHighPercent, m_diskHighPercent;
  210. FILE_LAYOUT_ENTRY m_FileEntry;
  211. BOOL checkActiveBefore;
  212. BOOL firstMpage;
  213. public:
  214. void CheckDiskFileSpace();
  215. ErrorCodeEnum BizLinkDetect(
  216. SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req,
  217. ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx);
  218. ErrorCodeEnum CheckNetType(
  219. SpReqAnsContext<ResourceWatcherService_CheckNetType_Req,
  220. ResourceWatcherService_CheckNetType_Ans>::Pointer ctx);
  221. ErrorCodeEnum GetBizLinks(
  222. SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req,
  223. ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx);
  224. void GetSystemCPUStatus();
  225. void GetSystemMemoryStatus();
  226. void GetSystemDiskStatus();
  227. void GetDiskStatusFrom(LPCTSTR path);
  228. void HardwareInfoTimer(void* pData);
  229. void DiskCheckTimer(void* pData);
  230. void LinkDetect(int detectType, const char* url, bool& status, unsigned int& delay, int warnFlag = 1);
  231. #if defined(RVC_OS_WIN)
  232. int GetCpuUsageRatio(int pid);
  233. ULONGLONG GetDiskUsedByte();
  234. #endif // RVC_OS_WIN
  235. time_t GetPathTimeSeconds(const char* inPath);
  236. void AutoDeleteFiles();
  237. void CenterSettingDelete();
  238. BOOL DeleteVersionPackage();
  239. void DeleteVideoFiles();
  240. int ProcessFileDelete(LPCTSTR lpszPath, int& nDelSucCnt, int& nDelFailedCnt, bool delDir, unsigned int saveBackDay = 0);
  241. BOOL DetectIsFirstRunAtBoot();
  242. #ifdef RVC_OS_LINUX
  243. /** 返回操作系统当前的版本,比如1032,1010,1031*/
  244. UINT GetSystemDisplayVersion(CSimpleStringA& strVersion);
  245. void UploadSysVersionInfo(UINT& ver);
  246. string MemoryProcessStatus();
  247. long GetCPURunTime(CPUInfo* cpu);
  248. long ReadProcCPURunTime(DWORD pid, ProcCPUInfo* pInfo);
  249. const char* GetItems(const char* buffer, unsigned int item);
  250. void ConfirmWindowEffectHasBeenOpen();
  251. void switchWindowsEffect(const char* cfgFilePath, bool enable);
  252. /** 1032操作系统版本出现剪贴板和操作中心,需要将剪贴板关掉避免手势唤起
  253. * 统信的人告知可以通过删除剪贴板程序实现……JUST DO IT!
  254. */
  255. void ConfirmDDEClipboardDisable();
  256. void RecoverDDEClipboardEnable();
  257. /** 自己摸索,统信说需要操作系统定制方能解决 [Gifur@202198]*/
  258. void ConfirmNotificationCenterDisable();
  259. #else
  260. bool CopyExeToRoot(const CSimpleStringA& csVersion);
  261. bool RegOperation4LnkFile();
  262. bool RegRdVtmVersion(CSimpleStringA& VTMpath, CSimpleStringA& vtmVersion, CSimpleStringA& verPath);
  263. bool RegWtVtmPath(const CSimpleStringA& vtmExtPath, const CSimpleStringA& rootPath);
  264. bool RegWtVtmVersion(const CSimpleStringA& vtmVer);
  265. void CreateLink(const CSimpleStringA& exePath, const CSimpleStringA& csVersion);
  266. bool CreateLinkFile(const CSimpleStringA& szStartAppPath, const CSimpleStringA& szAddCmdLine,
  267. const CSimpleStringA& szDestLnkPath, const CSimpleStringA& szIconPath);
  268. bool GetVtmVersionFromActiveTxt(CSimpleStringA& csVersion);
  269. bool UpdateExe();
  270. bool GetRegValue(HKEY hKey, LPCTSTR lpcszParam, CHAR* pSzValue, DWORD* pDwSizeOfSz);
  271. void DetectAutoStartupCover();
  272. //0:explorer.exe
  273. //1:vbs
  274. //2:vtm.exe
  275. //3:startmenu lnk from RVCTermimalApplicationSetup
  276. //4:startmenu lnk from install scripts
  277. //-1:failed
  278. int DetectAutoStartupType();
  279. ErrorCodeEnum ChangeAutoStartupWithExe(bool resetVBS = false, bool resetExplorer = false);
  280. int InitialAutoStartupSetType();
  281. BOOL IsHasSetAutoStartupByFolder(CSimpleStringA& strPath, bool& scriptsType);
  282. void DetectVersionHasChangedAndWarn();
  283. void DetectDestopFileAndWarn(bool bClear, CSimpleStringA& strFileSaveList);
  284. void DetectAutoStartFileAndWarn();
  285. ErrorCodeEnum DetectVTMInstalledBySetup(BOOL& fYes);
  286. void DetectVersionHasChangedAndWarnCover()
  287. {
  288. if (!checkActiveBefore) {
  289. DetectVersionHasChangedAndWarn();
  290. checkActiveBefore = TRUE;
  291. }
  292. }
  293. void VerifySignature();
  294. void DetectAndClearDesktopFile();
  295. void VerifySignature(const CSimpleStringA& filePath);
  296. CSimpleStringA GetFileName(const CSimpleStringA& filePath);
  297. bool RetrieveDigitalSignatureInfo(const char* pFilePath);
  298. bool VerifyFirstCertificate(HCERTSTORE hStore, PCMSG_SIGNER_INFO pSignerInfo);
  299. bool VerifyOtherCertificate(PCMSG_SIGNER_INFO pSignerInfo);
  300. int IsWifiConnected();
  301. void DetectWallpaperAndWarn();
  302. #endif // RVC_OS_LINUX
  303. void AlarmSystemBasicInfo();
  304. ErrorCodeEnum CatchSystemBasicInfo(SystemBasicInfo& info);
  305. };
  306. class RunEvent : public FSMEvent
  307. {
  308. public:
  309. RunEvent() : FSMEvent(USER_EVT_RUN) {}
  310. virtual ~RunEvent() {}
  311. };
  312. #ifdef RVC_OS_LINUX
  313. struct UploadMonitorInfoTask : public ITaskSp
  314. {
  315. ResourceWatcherFSM* m_pFSM;
  316. UploadMonitorInfoTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  317. void Process()
  318. {
  319. m_pFSM->UploadMonitorSettings();
  320. }
  321. };
  322. struct DetectSoftwareInstallStatusTask : public ITaskSp
  323. {
  324. ResourceWatcherFSM* m_pFSM;
  325. DetectSoftwareInstallStatusTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  326. void Process()
  327. {
  328. m_pFSM->DetectSoftwareInstallStatus();
  329. }
  330. };
  331. #else
  332. struct CreateLinkTask :public ITaskSp
  333. {
  334. ResourceWatcherFSM* m_pFSM;
  335. CreateLinkTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  336. void Process()
  337. {
  338. m_pFSM->RegOperation4LnkFile();
  339. }
  340. };
  341. struct VerifyDigitSignTask : public ITaskSp
  342. {
  343. ResourceWatcherFSM* m_pFSM;
  344. VerifyDigitSignTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  345. void Process()
  346. {
  347. m_pFSM->VerifySignature();
  348. }
  349. };
  350. struct ClearDesktopFileTask : public ITaskSp
  351. {
  352. ResourceWatcherFSM* m_pFSM;
  353. ClearDesktopFileTask(ResourceWatcherFSM* pFSM) : m_pFSM(pFSM) {}
  354. void Process()
  355. {
  356. m_pFSM->RegOperation4LnkFile();
  357. m_pFSM->DetectAndClearDesktopFile();
  358. m_pFSM->DetectAutoStartFileAndWarn();
  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. };