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