mod_ResourceWatcher.h 9.4 KB

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