mod_ResourceWatcher.h 10 KB

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