mod_ResourceWatcher.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #pragma once
  2. #include "modVer.h"
  3. #include "ResourceWatcherFSM.h"
  4. #include "UOSTools.hpp"
  5. #include "fileutil.h"
  6. #include "UpgradeManager_msg_g.h"
  7. class ResourceWatcherEntity;
  8. #define ENT_TIMERID_PROCESS_CHECK 66
  9. #define ENT_TIMERINTERVAL_PROCESS_CHECK 60 * 60 * 1000 //进程检测间隔默认一小时
  10. #define ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
  11. #if defined(RVC_OS_WIN)
  12. #define SECNAME_PROCESSDETECT "ProcDetectFlag"
  13. #else
  14. #define SECNAME_PROCESSDETECT "ProcDetectFlag4UOS"
  15. #endif //RVC_OS_WIN
  16. const int AbsolutePath = 0;
  17. const int AudioDefaultPath = 1;
  18. const int VideoDefaultPath = 2;
  19. class ResourceWatcherServiceSession : public ResourceWatcherService_ServerSessionBase
  20. {
  21. public:
  22. ResourceWatcherServiceSession(ResourceWatcherEntity* pEntity) : m_pEntity(pEntity) {}
  23. virtual ~ResourceWatcherServiceSession() {}
  24. virtual void Handle_GetDevInfo(SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req,
  25. ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx);
  26. virtual void Handle_OperateFile(SpReqAnsContext<ResourceWatcherService_OperateFile_Req, ResourceWatcherService_OperateFile_Ans>::Pointer ctx);
  27. virtual void Handle_BizLinkDetect(SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req, ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx);
  28. virtual void Handle_CheckNetType(SpReqAnsContext<ResourceWatcherService_CheckNetType_Req, ResourceWatcherService_CheckNetType_Ans>::Pointer ctx);
  29. virtual void Handle_GetBizLinks(SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req, ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx);
  30. virtual void Handle_InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req, ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  31. virtual void Handle_FilesClean(SpReqAnsContext<ResourceWatcherService_FilesClean_Req, ResourceWatcherService_FilesClean_Ans>::Pointer ctx);
  32. virtual void Handle_FetchSystemSnapshot(SpReqAnsContext<ResourceWatcherService_FetchSystemSnapshot_Req, ResourceWatcherService_FetchSystemSnapshot_Ans>::Pointer ctx);
  33. virtual void Handle_CheckIsFileExists(SpReqAnsContext<ResourceWatcherService_CheckIsFileExists_Req, ResourceWatcherService_CheckIsFileExists_Ans>::Pointer ctx);
  34. private:
  35. ResourceWatcherEntity* m_pEntity;
  36. };
  37. class ResourceWatcherEntity : public CEntityBase, public ITimerListener, public ISysVarListener
  38. {
  39. public:
  40. ResourceWatcherEntity(){}
  41. virtual ~ResourceWatcherEntity() {}
  42. virtual const char* GetEntityName() const { return "ResourceWatcher"; }
  43. const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
  44. virtual CServerSessionBase* OnNewSession(const char*, const char*)
  45. {
  46. return new ResourceWatcherServiceSession(this);
  47. }
  48. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,
  49. CSmartPointer<ITransactionContext> pTransactionContext)
  50. {
  51. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Version %s; Complied at: %s %s", MOD_VERSION, __DATE__, __TIME__);
  52. ErrorCodeEnum errorCode = Error_Succeed;
  53. errorCode = m_fsm.Init(this);
  54. pTransactionContext->SendAnswer(errorCode);
  55. }
  56. void OnStarted()
  57. {
  58. m_fsm.AfterInit();
  59. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to check Sogou input install state...");
  60. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  61. ReportSogouInstallState(); //实体启动时检测搜狗输入法安装状态
  62. CSimpleStringA uiState;
  63. spEntityFunction->GetSysVar("UIState", uiState);
  64. if (uiState[0] == 'M') {
  65. m_fsm.TriggerAtStatusChanged(true);
  66. }
  67. spEntityFunction->RegistSysVarEvent("UIState", this);
  68. CSimpleStringA terminalStage;
  69. spEntityFunction->GetSysVar("TerminalStage", terminalStage);
  70. if (
  71. 0 == CSimpleStringA("C").Compare(terminalStage, true)
  72. || 0 == CSimpleStringA("S").Compare(terminalStage, true)
  73. || 0 == CSimpleStringA("M").Compare(terminalStage, true)
  74. || 0 == CSimpleStringA("A").Compare(terminalStage, true)
  75. )
  76. {
  77. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Terminal has been lastStage(%s) before ResourceWatcher start.", terminalStage);
  78. m_fsm.TriggerProccessUpload();
  79. if (!(0 == CSimpleStringA("A").Compare(terminalStage, true))) //非首页,已达终态
  80. {
  81. m_fsm.TriggerFileListUpload();
  82. }
  83. }
  84. else
  85. {
  86. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Terminal is starting, regist the teminalStage Event.");
  87. spEntityFunction->RegistSysVarEvent("TerminalStage", this);
  88. }
  89. CSmartPointer<IConfigInfo> spCtSettingConfig;
  90. GetFunction()->OpenConfig(Config_CenterSetting, spCtSettingConfig);
  91. int procFlag = 0;
  92. int closeSecCheck = 0;
  93. spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", SECNAME_PROCESSDETECT, procFlag);
  94. if (procFlag > 0)
  95. {
  96. CheckProcessStatus(); //启动时检测一次进程
  97. spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", "CloseSecProcDetect", closeSecCheck);
  98. if (!closeSecCheck)
  99. {
  100. SecProcCheck(); //检测一次安全软件
  101. }
  102. int tTime = 0;
  103. spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", "ProcCheckTime", tTime);
  104. if (tTime >= 0)
  105. {
  106. if (tTime == 0) tTime = ENT_TIMERINTERVAL_PROCESS_CHECK; //没设置时间的话默认一小时间隔
  107. spEntityFunction->SetTimer(ENT_TIMERID_PROCESS_CHECK, this, tTime);
  108. }
  109. else
  110. {
  111. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invalid proccess check time:%d", tTime);
  112. }
  113. }
  114. #if defined(RVC_OS_WIN)
  115. ReadFileContent();
  116. ReadFileInfo();
  117. #endif //RVC_OS_WIN
  118. }
  119. /*ignore*/
  120. virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
  121. {
  122. pTransactionContext->SendAnswer(Error_Succeed);
  123. }
  124. /*ignore*/
  125. virtual void OnPreContinue(CSmartPointer<ITransactionContext> pTransactionContext)
  126. {
  127. pTransactionContext->SendAnswer(Error_Succeed);
  128. }
  129. /*invoke fsm.onExit(), invoked when entity will be closed*/
  130. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause, CSmartPointer<ITransactionContext>pTransactionContext)
  131. {
  132. ErrorCodeEnum errorCode = m_fsm.OnExit();
  133. pTransactionContext->SendAnswer(errorCode);
  134. }
  135. virtual void OnSelfTest(EntityTestEnum eTestType,
  136. CSmartPointer<ITransactionContext>pTransactionContext)
  137. {
  138. m_fsm.SelfTest(eTestType, pTransactionContext);
  139. }
  140. virtual bool IsService() const
  141. {
  142. return true;
  143. }
  144. virtual bool IsMultiThread() const
  145. {
  146. return false;
  147. }
  148. void GetDevInfo(SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req,
  149. ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx)
  150. {
  151. SystemBasicInfo info;
  152. ErrorCodeEnum result = m_fsm.CatchSystemBasicInfo(info);
  153. if (Error_Succeed == result) {
  154. ctx->Ans.type = info.strProductName;
  155. ctx->Ans.model = info.strManufacturer;
  156. ctx->Ans.version = info.strSerialNumber;
  157. ctx->Ans.state = 0;
  158. }
  159. ctx->Answer(result);
  160. }
  161. void BizLinkDetect(SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req,
  162. ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx)
  163. {
  164. m_fsm.BizLinkDetect(ctx);
  165. }
  166. void CheckNetType(SpReqAnsContext<ResourceWatcherService_CheckNetType_Req,
  167. ResourceWatcherService_CheckNetType_Ans>::Pointer ctx)
  168. {
  169. m_fsm.CheckNetType(ctx);
  170. }
  171. void GetBizLinks(SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req,
  172. ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx)
  173. {
  174. m_fsm.GetBizLinks(ctx);
  175. }
  176. void OperateFile(
  177. SpReqAnsContext<ResourceWatcherService_OperateFile_Req,
  178. ResourceWatcherService_OperateFile_Ans>::Pointer ctx)
  179. {
  180. ctx->Answer(Error_NotSupport);
  181. }
  182. void InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  183. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  184. void FilesClean(SpReqAnsContext<ResourceWatcherService_FilesClean_Req,
  185. ResourceWatcherService_FilesClean_Ans>::Pointer ctx);
  186. void FetchSystemSnapshot(SpReqAnsContext<ResourceWatcherService_FetchSystemSnapshot_Req, ResourceWatcherService_FetchSystemSnapshot_Ans>::Pointer ctx);
  187. void InstallSogou(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  188. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  189. void CheckIsFileExists(SpReqAnsContext<ResourceWatcherService_CheckIsFileExists_Req,
  190. ResourceWatcherService_CheckIsFileExists_Ans>::Pointer ctx);
  191. int CheckMediaResource(int iFileType, const char* pFileName);
  192. void OnSysVarEvent(const char* pszKey, const char* pszValue, const char* pszOldValue, const char* pszEntityName)
  193. {
  194. if ((_strnicmp(pszKey, "UIState", strlen("UIState")) == 0)) {
  195. m_fsm.TriggerAtStatusChanged(_strnicmp(pszValue, "M", strlen("M")) == 0);
  196. }
  197. else if ((_strnicmp(pszKey, "TerminalStage", strlen("TerminalStage")) == 0)) {
  198. if (0 == CSimpleStringA("C").Compare(pszValue, true)
  199. || 0 == CSimpleStringA("S").Compare(pszValue, true)
  200. || 0 == CSimpleStringA("M").Compare(pszValue, true)
  201. || 0 == CSimpleStringA("A").Compare(pszValue, true)
  202. )
  203. {
  204. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Status changed, teminalStage = %s.", pszValue);
  205. m_fsm.TriggerProccessUpload();
  206. if (!(0 == CSimpleStringA("A").Compare(pszValue, true))) //非通过准入的情况下,直接开始文件列表获取
  207. {
  208. m_fsm.TriggerFileListUpload();
  209. }
  210. }
  211. }
  212. }
  213. private:
  214. //report sogou input software install state and installed information
  215. ErrorCodeEnum ReportSogouInstallState();
  216. void OnTimeout(DWORD dwTimerID)
  217. {
  218. if (dwTimerID == ENT_TIMERID_PROCESS_CHECK) {
  219. CheckProcessStatus();
  220. }
  221. else {
  222. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Unkonwn timer id: %u", dwTimerID);
  223. }
  224. }
  225. std::string DoCheckCertainProcessStatus(const CAutoArray<CSimpleStringA>& pName);
  226. void SecProcCheck(); //检测终端安全软件应用
  227. ErrorCodeEnum UnzipPack(const char* unZipPackName);//解压文件
  228. CSimpleStringA GetFilePathWithDir(CSimpleStringA dir, CSimpleStringA fileName);
  229. std::vector<std::string> GetUserNameList(bool bExcludeRoot = false);
  230. ErrorCodeEnum RunShellScript(LPCTSTR cmdline);
  231. ErrorCodeEnum DeleteUnzipDir();//清理临时解压包文件
  232. bool is_str_utf8(const char* str);
  233. /// <summary>
  234. /// 通过集中配置检测指定进程是否存在
  235. /// </summary>
  236. void CheckProcessStatus();
  237. #if defined(RVC_OS_WIN)
  238. void ReadFileContent();
  239. void ReadFileInfo();
  240. void ChcekDiskFileSpace();
  241. #else
  242. ErrorCodeEnum GetSogouPkgDirPath(CSimpleStringA& strPkgPath);
  243. #endif //RVC_OS_WIN
  244. private:
  245. ResourceWatcherFSM m_fsm;
  246. };
  247. struct InstallSogouTask : public ITaskSp
  248. {
  249. SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  250. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx;
  251. ResourceWatcherEntity* m_pEntity;
  252. InstallSogouTask(ResourceWatcherEntity* entity) :m_pEntity(entity) {}
  253. void Process()
  254. {
  255. m_pEntity->InstallSogou(ctx);
  256. }
  257. };