mod_ResourceWatcher.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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_CHECK_SOGOU_INPUT_INSTALLED_STATE 1
  9. #define ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_INSTALLED_STATE 60 * 1000
  10. #define ENT_TIMERID_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE 3
  11. #define ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE 30 * 1000
  12. #define ENT_TIMERID_PROCESS_CHECK 66
  13. #define ENT_TIMERINTERVAL_PROCESS_CHECK 60 * 60 * 1000 //进程检测间隔默认一小时
  14. #define ENCODING (X509_ASN_ENCODING | PKCS_7_ASN_ENCODING)
  15. const int AbsolutePath = 0;
  16. const int AudioDefaultPath = 1;
  17. const int VideoDefaultPath = 2;
  18. class ResourceWatcherServiceSession : public ResourceWatcherService_ServerSessionBase
  19. {
  20. public:
  21. ResourceWatcherServiceSession(ResourceWatcherEntity* pEntity) : m_pEntity(pEntity) {}
  22. virtual ~ResourceWatcherServiceSession() {}
  23. virtual void Handle_GetDevInfo(SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req,
  24. ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx);
  25. virtual void Handle_OperateFile(SpReqAnsContext<ResourceWatcherService_OperateFile_Req, ResourceWatcherService_OperateFile_Ans>::Pointer ctx);
  26. virtual void Handle_BizLinkDetect(SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req, ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx);
  27. virtual void Handle_CheckNetType(SpReqAnsContext<ResourceWatcherService_CheckNetType_Req, ResourceWatcherService_CheckNetType_Ans>::Pointer ctx);
  28. virtual void Handle_GetBizLinks(SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req, ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx);
  29. virtual void Handle_GetThirdPartyInstallState(SpReqAnsContext<ResourceWatcherService_GetThirdPartyInstallState_Req, ResourceWatcherService_GetThirdPartyInstallState_Ans>::Pointer ctx);
  30. virtual void Handle_InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req, ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  31. virtual void Handle_UninstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req, ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx);
  32. virtual void Handle_RestartThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_RestartThirdPartyProgram_Req, ResourceWatcherService_RestartThirdPartyProgram_Ans>::Pointer ctx);
  33. virtual void Handle_ProcessDetectThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_ProcessDetectThirdPartyProgram_Req, ResourceWatcherService_ProcessDetectThirdPartyProgram_Ans>::Pointer ctx);
  34. virtual void Handle_FilesClean(SpReqAnsContext<ResourceWatcherService_FilesClean_Req, ResourceWatcherService_FilesClean_Ans>::Pointer ctx);
  35. virtual void Handle_FetchSystemSnapshot(SpReqAnsContext<ResourceWatcherService_FetchSystemSnapshot_Req, ResourceWatcherService_FetchSystemSnapshot_Ans>::Pointer ctx);
  36. virtual void Handle_CheckIsFileExists(SpReqAnsContext<ResourceWatcherService_CheckIsFileExists_Req, ResourceWatcherService_CheckIsFileExists_Ans>::Pointer ctx);
  37. private:
  38. ResourceWatcherEntity* m_pEntity;
  39. };
  40. class ResourceWatcherEntity : public CEntityBase, public IBroadcastListener, public ITimerListener, public ISysVarListener
  41. {
  42. public:
  43. ResourceWatcherEntity(){}
  44. virtual ~ResourceWatcherEntity() {}
  45. virtual const char* GetEntityName() const { return "ResourceWatcher"; }
  46. const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
  47. virtual CServerSessionBase* OnNewSession(const char*, const char*)
  48. {
  49. return new ResourceWatcherServiceSession(this);
  50. }
  51. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,
  52. CSmartPointer<ITransactionContext> pTransactionContext)
  53. {
  54. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Version %s; Complied at: %s %s", MOD_VERSION, __DATE__, __TIME__);
  55. ErrorCodeEnum errorCode = Error_Succeed;
  56. errorCode = m_fsm.Init(this);
  57. if (errorCode == Error_Succeed) {
  58. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  59. CSystemStaticInfo staticInfo;
  60. spEntityFunction->GetSystemStaticInfo(staticInfo);
  61. updateShowFlag = false;
  62. newestSogouInstall = false;
  63. lastUpgradeInstallTime = 0; //上次升级安装时间
  64. lastUpgradeSwitchTime = 0; //上次升级切换时间
  65. lastSogouChangeEndTime = 0; //上一次切换结束判定的时间
  66. lastSogouChangeWarn = ""; //上一次搜狗告警信息
  67. sogouProcessRun = 0; //搜狗进程是否启动
  68. errorCode = spEntityFunction->SubscribeBroadcast("UpgradeManager", NULL, this, m_uuidUpgradeStateEventListener);
  69. if (errorCode != Error_Succeed)
  70. {
  71. LOG_TRACE("Subscribe UpgradeManager evt failed 0x%x", errorCode);
  72. pTransactionContext->SendAnswer(errorCode);
  73. return;
  74. }
  75. }
  76. pTransactionContext->SendAnswer(errorCode);
  77. }
  78. void OnStarted()
  79. {
  80. m_fsm.AfterInit();
  81. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  82. spEntityFunction->SetTimer(ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE,
  83. this, ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_INSTALLED_STATE);
  84. CSimpleStringA uiState;
  85. spEntityFunction->GetSysVar("UIState", uiState);
  86. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("NETWORK_PROBE Timer GetUIState %s", (LPCTSTR)uiState);
  87. if (uiState[0] == 'M') {
  88. m_fsm.TriggerAtStatusChanged(true);
  89. }
  90. spEntityFunction->RegistSysVarEvent("UIState", this);
  91. CSmartPointer<IConfigInfo> spCtSettingConfig;
  92. GetFunction()->OpenConfig(Config_CenterSetting, spCtSettingConfig);
  93. int procFlag = 0;
  94. int closeSecCheck = 0;
  95. #if defined(RVC_OS_WIN)
  96. spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", "ProcDetectFlag", procFlag);
  97. #else
  98. spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", "ProcDetectFlag4UOS", procFlag);
  99. #endif //RVC_OS_WIN
  100. if (procFlag > 0)
  101. {
  102. CheckProcessStatus(); //启动时检测一次进程
  103. spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", "CloseSecProcDetect", closeSecCheck);
  104. if (!closeSecCheck)
  105. {
  106. SecProcCheck(); //检测一次安全软件
  107. }
  108. int tTime = 0;
  109. spCtSettingConfig->ReadConfigValueInt("ResourceWatcher", "ProcCheckTime", tTime);
  110. if (tTime >= 0)
  111. {
  112. if (tTime == 0) tTime = ENT_TIMERINTERVAL_PROCESS_CHECK; //没设置时间的话默认一小时间隔
  113. spEntityFunction->SetTimer(ENT_TIMERID_PROCESS_CHECK, this, tTime);
  114. }
  115. else
  116. {
  117. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invalid proccess check time:%d", tTime);
  118. }
  119. }
  120. #if defined(RVC_OS_WIN)
  121. ReadFileContent();
  122. ReadFileInfo();
  123. #endif //RVC_OS_WIN
  124. }
  125. /*ignore*/
  126. virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
  127. {
  128. OnTimeout(ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE);
  129. pTransactionContext->SendAnswer(Error_Succeed);
  130. }
  131. /*ignore*/
  132. virtual void OnPreContinue(CSmartPointer<ITransactionContext> pTransactionContext)
  133. {
  134. pTransactionContext->SendAnswer(Error_Succeed);
  135. }
  136. /*invoke fsm.onExit(), invoked when entity will be closed*/
  137. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause, CSmartPointer<ITransactionContext>pTransactionContext)
  138. {
  139. ErrorCodeEnum errorCode = m_fsm.OnExit();
  140. pTransactionContext->SendAnswer(errorCode);
  141. }
  142. virtual void OnSelfTest(EntityTestEnum eTestType,
  143. CSmartPointer<ITransactionContext>pTransactionContext)
  144. {
  145. m_fsm.SelfTest(eTestType, pTransactionContext);
  146. }
  147. virtual bool IsService() const
  148. {
  149. return true;
  150. }
  151. virtual bool IsMultiThread() const
  152. {
  153. return false;
  154. }
  155. void GetDevInfo(SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req,
  156. ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx)
  157. {
  158. LOG_FUNCTION();
  159. SystemBasicInfo info;
  160. ErrorCodeEnum result = m_fsm.CatchSystemBasicInfo(info);
  161. if (Error_Succeed == result) {
  162. ctx->Ans.type = info.strProductName;
  163. ctx->Ans.model = info.strManufacturer;
  164. ctx->Ans.version = info.strSerialNumber;
  165. ctx->Ans.state = 0;
  166. }
  167. ctx->Answer(result);
  168. }
  169. void BizLinkDetect(SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req,
  170. ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx)
  171. {
  172. m_fsm.BizLinkDetect(ctx);
  173. }
  174. void CheckNetType(SpReqAnsContext<ResourceWatcherService_CheckNetType_Req,
  175. ResourceWatcherService_CheckNetType_Ans>::Pointer ctx)
  176. {
  177. m_fsm.CheckNetType(ctx);
  178. }
  179. void GetBizLinks(SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req,
  180. ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx)
  181. {
  182. m_fsm.GetBizLinks(ctx);
  183. }
  184. void OperateFile(
  185. SpReqAnsContext<ResourceWatcherService_OperateFile_Req,
  186. ResourceWatcherService_OperateFile_Ans>::Pointer ctx)
  187. {
  188. ctx->Answer(Error_NotSupport);
  189. }
  190. void GetThirdPartyInstallState(SpReqAnsContext<ResourceWatcherService_GetThirdPartyInstallState_Req,
  191. ResourceWatcherService_GetThirdPartyInstallState_Ans>::Pointer ctx);
  192. void InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  193. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  194. void UninstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req,
  195. ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx);
  196. void RestartThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_RestartThirdPartyProgram_Req,
  197. ResourceWatcherService_RestartThirdPartyProgram_Ans>::Pointer ctx);
  198. void ProcessDetectThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_ProcessDetectThirdPartyProgram_Req,
  199. ResourceWatcherService_ProcessDetectThirdPartyProgram_Ans>::Pointer ctx);
  200. void FilesClean(SpReqAnsContext<ResourceWatcherService_FilesClean_Req,
  201. ResourceWatcherService_FilesClean_Ans>::Pointer ctx);
  202. void FetchSystemSnapshot(SpReqAnsContext<ResourceWatcherService_FetchSystemSnapshot_Req, ResourceWatcherService_FetchSystemSnapshot_Ans>::Pointer ctx);
  203. void InstallSogou(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  204. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  205. void UninstallSogou(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req,
  206. ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx);
  207. void CheckIsFileExists(SpReqAnsContext<ResourceWatcherService_CheckIsFileExists_Req,
  208. ResourceWatcherService_CheckIsFileExists_Ans>::Pointer ctx);
  209. int CheckMediaResource(int iFileType, const char* pFileName);
  210. #if defined(_MSC_VER)
  211. SP_BEGIN_MSG_DISPATCH_MAP(ResourceWatcherEntity)
  212. SP_BEGIN_ENTITY_MSG("UpgradeManager")
  213. SP_MSG_HANDLE_NS(UpgradeManager, UpgradeStateEvent, OnUpgradeStateEvent)
  214. SP_END_ENTITY_MSG()
  215. SP_END_MSG_DISPATCH_MAP()
  216. #else
  217. virtual void OnBroadcastEvent(CUUID SubID, const char* pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, CAutoBuffer Buffer) \
  218. {
  219. if (!pszEntityName) {
  220. LOG_TRACE("pszEntityName cannot empty!");
  221. }
  222. else if (_stricmp("UpgradeManager", pszEntityName) == 0) {
  223. switch (dwMessageId) {
  224. case eMsg_UpgradeStateEvent:
  225. if (eMsgSig_UpgradeStateEvent == dwMessageSignature) {
  226. UpgradeManager::UpgradeStateEvent t;
  227. ErrorCodeEnum Error = SpBuffer2Object(Buffer, t);
  228. if (Error == Error_Succeed)
  229. OnUpgradeStateEvent(pszEntityName, dwMessageId, dwMessageSignature, t);
  230. }
  231. else {
  232. LOG_TRACE("%s signature mismatched!", "UpgradeStateEvent");
  233. }
  234. break;
  235. default:
  236. LOG_TRACE("msg id %d ignored!", dwMessageId);
  237. break;
  238. }
  239. }
  240. else {
  241. LOG_TRACE("ignore pszEntityName");
  242. }
  243. }
  244. #endif //_MSC_VER
  245. void OnSysVarEvent(const char* pszKey, const char* pszValue, const char* pszOldValue, const char* pszEntityName)
  246. {
  247. if ((_strnicmp(pszKey, "UIState", strlen("UIState")) == 0)) {
  248. m_fsm.TriggerAtStatusChanged(_strnicmp(pszValue, "M", strlen("M")) == 0);
  249. }
  250. }
  251. private:
  252. void OnUpgradeStateEvent(const char* pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, UpgradeManager::UpgradeStateEvent& evt);
  253. //report sogou input software install state and installed information
  254. ErrorCodeEnum ReportSogouInstallState();
  255. void OnTimeout(DWORD dwTimerID)
  256. {
  257. if (dwTimerID == ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE) {
  258. CSimpleStringA strState;
  259. auto err = GetFunction()->GetSysVar("UIState", strState);
  260. if (err == Error_Succeed && strState == "M") {
  261. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to check Sogou input install state...");
  262. if (Error_Succeed == ReportSogouInstallState()) {//如果安装了搜狗输入法,就去除检测搜狗输入法安装的定时器,并设置进程检测的定时器。
  263. GetFunction()->KillTimer(ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE);
  264. GetFunction()->SetTimer(ENT_TIMERID_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE,
  265. this, ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE);
  266. }
  267. }
  268. else {
  269. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Get UIState result: %s, %s", SpStrError(err), strState.GetData());
  270. }
  271. }
  272. else if (dwTimerID == ENT_TIMERID_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE) {
  273. DoCheckSogouProcessStatus();
  274. }
  275. else if (dwTimerID == ENT_TIMERID_PROCESS_CHECK) {
  276. CheckProcessStatus();
  277. }
  278. else {
  279. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Unkonwn timer id: %u", dwTimerID);
  280. }
  281. }
  282. void DoCheckSogouProcessStatus();
  283. std::string DoCheckCertainProcessStatus(const CAutoArray<CSimpleStringA>& pName);
  284. void SecProcCheck(); //检测终端安全软件应用
  285. ErrorCodeEnum UnzipPack(const char* unZipPackName);//解压文件
  286. CSimpleStringA GetFilePathWithDir(CSimpleStringA dir, CSimpleStringA fileName);
  287. std::vector<std::string> GetUserNameList(bool bExcludeRoot = false);
  288. ErrorCodeEnum RunShellScript(LPCTSTR cmdline);
  289. ErrorCodeEnum DeleteUnzipDir();//清理临时解压包文件
  290. BOOL KillProcessFromName(const CSimpleStringA& strProcessName);
  291. bool is_str_utf8(const char* str);
  292. /// <summary>
  293. /// 通过集中配置检测指定进程是否存在
  294. /// </summary>
  295. void CheckProcessStatus();
  296. #if defined(RVC_OS_WIN)
  297. void ReadFileContent();
  298. void ReadFileInfo();
  299. void ChcekDiskFileSpace();
  300. #else
  301. ErrorCodeEnum GetSogouPkgDirPath(CSimpleStringA& strPkgPath);
  302. #endif //RVC_OS_WIN
  303. private:
  304. ResourceWatcherFSM m_fsm;
  305. CUUID m_uuidUpgradeStateEventListener;
  306. BOOL updateShowFlag;
  307. BOOL newestSogouInstall; //最近一次安装的标志
  308. time_t lastUpgradeInstallTime; //上次升级安装时间
  309. time_t lastUpgradeSwitchTime; //上次升级切换时间
  310. time_t lastSogouChangeEndTime; //上一次进程切换时间
  311. CSimpleStringA lastSogouChangeWarn; //上一次搜狗告警信息
  312. BOOL isSogouUpdateChange;
  313. int sogouProcessRun;
  314. vector<CSimpleStringA> sogouChangeWarn;
  315. vector<time_t> sogouChangeTime;
  316. };
  317. struct InstallSogouTask : public ITaskSp
  318. {
  319. SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req,
  320. ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx;
  321. ResourceWatcherEntity* m_pEntity;
  322. InstallSogouTask(ResourceWatcherEntity* entity) :m_pEntity(entity) {}
  323. void Process()
  324. {
  325. m_pEntity->InstallSogou(ctx);
  326. }
  327. };
  328. struct UninstallSogouTask : public ITaskSp
  329. {
  330. SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req,
  331. ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx;
  332. ResourceWatcherEntity* m_pEntity;
  333. UninstallSogouTask(ResourceWatcherEntity* entity) :m_pEntity(entity) {}
  334. void Process()
  335. {
  336. m_pEntity->UninstallSogou(ctx);
  337. }
  338. };