mod_ResourceWatcher.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. }
  75. else
  76. {
  77. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Terminal is starting, regist the teminalStage Event.");
  78. spEntityFunction->RegistSysVarEvent("terminalStage", this);
  79. }
  80. CSmartPointer<IConfigInfo> spCtSettingConfig;
  81. GetFunction()->OpenConfig(Config_CenterSetting, spCtSettingConfig);
  82. int procFlag = 0;
  83. int closeSecCheck = 0;
  84. #if defined(RVC_OS_WIN)
  85. spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", "ProcDetectFlag", procFlag);
  86. #else
  87. spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", "ProcDetectFlag4UOS", procFlag);
  88. #endif //RVC_OS_WIN
  89. if (procFlag > 0)
  90. {
  91. CheckProcessStatus(); //启动时检测一次进程
  92. spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", "CloseSecProcDetect", closeSecCheck);
  93. if (!closeSecCheck)
  94. {
  95. SecProcCheck(); //检测一次安全软件
  96. }
  97. int tTime = 0;
  98. spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", "ProcCheckTime", tTime);
  99. if (tTime >= 0)
  100. {
  101. if (tTime == 0) tTime = ENT_TIMERINTERVAL_PROCESS_CHECK; //没设置时间的话默认一小时间隔
  102. spEntityFunction->SetTimer(ENT_TIMERID_PROCESS_CHECK, this, tTime);
  103. }
  104. else
  105. {
  106. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invalid proccess check time:%d", tTime);
  107. }
  108. }
  109. #if defined(RVC_OS_WIN)
  110. ReadFileContent();
  111. ReadFileInfo();
  112. #endif //RVC_OS_WIN
  113. }
  114. /*ignore*/
  115. virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
  116. {
  117. pTransactionContext->SendAnswer(Error_Succeed);
  118. }
  119. /*ignore*/
  120. virtual void OnPreContinue(CSmartPointer<ITransactionContext> pTransactionContext)
  121. {
  122. pTransactionContext->SendAnswer(Error_Succeed);
  123. }
  124. /*invoke fsm.onExit(), invoked when entity will be closed*/
  125. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause, CSmartPointer<ITransactionContext>pTransactionContext)
  126. {
  127. ErrorCodeEnum errorCode = m_fsm.OnExit();
  128. pTransactionContext->SendAnswer(errorCode);
  129. }
  130. virtual void OnSelfTest(EntityTestEnum eTestType,
  131. CSmartPointer<ITransactionContext>pTransactionContext)
  132. {
  133. m_fsm.SelfTest(eTestType, pTransactionContext);
  134. }
  135. virtual bool IsService() const
  136. {
  137. return true;
  138. }
  139. virtual bool IsMultiThread() const
  140. {
  141. return false;
  142. }
  143. void GetDevInfo(SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req,
  144. ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx)
  145. {
  146. SystemBasicInfo info;
  147. ErrorCodeEnum result = m_fsm.CatchSystemBasicInfo(info);
  148. if (Error_Succeed == result) {
  149. ctx->Ans.type = info.strProductName;
  150. ctx->Ans.model = info.strManufacturer;
  151. ctx->Ans.version = info.strSerialNumber;
  152. ctx->Ans.state = 0;
  153. }
  154. ctx->Answer(result);
  155. }
  156. void BizLinkDetect(SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req,
  157. ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx)
  158. {
  159. m_fsm.BizLinkDetect(ctx);
  160. }
  161. void CheckNetType(SpReqAnsContext<ResourceWatcherService_CheckNetType_Req,
  162. ResourceWatcherService_CheckNetType_Ans>::Pointer ctx)
  163. {
  164. m_fsm.CheckNetType(ctx);
  165. }
  166. void GetBizLinks(SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req,
  167. ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx)
  168. {
  169. m_fsm.GetBizLinks(ctx);
  170. }
  171. void OperateFile(
  172. SpReqAnsContext<ResourceWatcherService_OperateFile_Req,
  173. ResourceWatcherService_OperateFile_Ans>::Pointer ctx)
  174. {
  175. ctx->Answer(Error_NotSupport);
  176. }
  177. void InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  178. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  179. void FilesClean(SpReqAnsContext<ResourceWatcherService_FilesClean_Req,
  180. ResourceWatcherService_FilesClean_Ans>::Pointer ctx);
  181. void FetchSystemSnapshot(SpReqAnsContext<ResourceWatcherService_FetchSystemSnapshot_Req, ResourceWatcherService_FetchSystemSnapshot_Ans>::Pointer ctx);
  182. void InstallSogou(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  183. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  184. void CheckIsFileExists(SpReqAnsContext<ResourceWatcherService_CheckIsFileExists_Req,
  185. ResourceWatcherService_CheckIsFileExists_Ans>::Pointer ctx);
  186. int CheckMediaResource(int iFileType, const char* pFileName);
  187. void OnSysVarEvent(const char* pszKey, const char* pszValue, const char* pszOldValue, const char* pszEntityName)
  188. {
  189. if ((_strnicmp(pszKey, "UIState", strlen("UIState")) == 0)) {
  190. m_fsm.TriggerAtStatusChanged(_strnicmp(pszValue, "M", strlen("M")) == 0);
  191. }
  192. else if ((_strnicmp(pszKey, "TerminalStage", strlen("TerminalStage")) == 0)) {
  193. if (0 == CSimpleStringA("C").Compare(pszValue, true)
  194. || 0 == CSimpleStringA("S").Compare(pszValue, true)
  195. || 0 == CSimpleStringA("M").Compare(pszValue, true)
  196. || 0 == CSimpleStringA("A").Compare(pszValue, true)
  197. )
  198. {
  199. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Status changed, teminalStage = %s.", pszValue);
  200. m_fsm.TriggerProccessUpload();
  201. }
  202. }
  203. }
  204. private:
  205. //report sogou input software install state and installed information
  206. ErrorCodeEnum ReportSogouInstallState();
  207. void OnTimeout(DWORD dwTimerID)
  208. {
  209. if (dwTimerID == ENT_TIMERID_PROCESS_CHECK) {
  210. CheckProcessStatus();
  211. }
  212. else {
  213. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Unkonwn timer id: %u", dwTimerID);
  214. }
  215. }
  216. std::string DoCheckCertainProcessStatus(const CAutoArray<CSimpleStringA>& pName);
  217. void SecProcCheck(); //检测终端安全软件应用
  218. ErrorCodeEnum UnzipPack(const char* unZipPackName);//解压文件
  219. CSimpleStringA GetFilePathWithDir(CSimpleStringA dir, CSimpleStringA fileName);
  220. std::vector<std::string> GetUserNameList(bool bExcludeRoot = false);
  221. ErrorCodeEnum RunShellScript(LPCTSTR cmdline);
  222. ErrorCodeEnum DeleteUnzipDir();//清理临时解压包文件
  223. BOOL KillProcessFromName(const CSimpleStringA& strProcessName);
  224. bool is_str_utf8(const char* str);
  225. /// <summary>
  226. /// 通过集中配置检测指定进程是否存在
  227. /// </summary>
  228. void CheckProcessStatus();
  229. #if defined(RVC_OS_WIN)
  230. void ReadFileContent();
  231. void ReadFileInfo();
  232. void ChcekDiskFileSpace();
  233. #else
  234. ErrorCodeEnum GetSogouPkgDirPath(CSimpleStringA& strPkgPath);
  235. #endif //RVC_OS_WIN
  236. private:
  237. ResourceWatcherFSM m_fsm;
  238. };
  239. struct InstallSogouTask : public ITaskSp
  240. {
  241. SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  242. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx;
  243. ResourceWatcherEntity* m_pEntity;
  244. InstallSogouTask(ResourceWatcherEntity* entity) :m_pEntity(entity) {}
  245. void Process()
  246. {
  247. m_pEntity->InstallSogou(ctx);
  248. }
  249. };