mod_ResourceWatcher.h 16 KB

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