CModTools.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  1. #include "stdafx.h"
  2. #include "uuid4.h"
  3. #if (defined _WIN32 || defined _WIN64)
  4. #include "processControl.h"
  5. #define SPLIT_SLASH_STR "\\"
  6. #else
  7. #include <mutex>
  8. #include <thread>
  9. #include "path.h"
  10. #include "fileutil.h"
  11. #include <boost/process.hpp>
  12. #include <boost/thread.hpp>
  13. #include "CommEntityUtil.hpp"
  14. #include <SDL2/SDL.h>
  15. //#include <winpr/wnd.h>
  16. #define HWND_TOP (0)
  17. #define HWND_BOTTOM (1)
  18. #define HWND_TOPMOST (-1)
  19. #define HWND_NOTOPMOST (-2)
  20. #endif
  21. #include "baseEx.h"
  22. #include "CModTools.h"
  23. #include "SpIni.h"
  24. #include <vector>
  25. #include <list>
  26. #include <memory>
  27. #include <iostream>
  28. #include "../mod_upload/Upload_client_g.h"
  29. #include "mod_chromium.h"
  30. #include <boost/thread/mutex.hpp>
  31. #include "EventCode.h"
  32. #include "url_encoder.h"
  33. #include "SpUtility.h"
  34. void SystemRunTest(const std::string& systemCmd);
  35. extern std::pair<bool, std::string> DetectActiveHttp(std::vector<std::string> urlArr);
  36. auto GetCefHead = [](CEntityBase* curEntity)->CSimpleStringA {
  37. CSimpleStringA strChromiumPath, basePath;
  38. curEntity->GetFunction()->GetPath("Base", basePath);
  39. strChromiumPath.Append(basePath);
  40. #if defined(_MSC_VER)
  41. strChromiumPath.Append(CSimpleStringA(SPLIT_SLASH_STR) + "bin" + SPLIT_SLASH_STR + "Chromium" + SPLIT_SLASH_STR);
  42. strChromiumPath.Append(CEFCLIENT_NAME);
  43. #else
  44. strChromiumPath.Append(CSimpleStringA(SPLIT_SLASH_STR) + "res" + SPLIT_SLASH_STR + "RunScript" + SPLIT_SLASH_STR);
  45. strChromiumPath.Append("startMixBrower.sh");
  46. #endif //_MSC_VER
  47. return strChromiumPath;
  48. };
  49. auto detectUrls = [](std::string urls) -> std::string {
  50. std::vector<std::string> urlArr;
  51. urlArr.emplace_back(urls);
  52. auto checkRet = DetectActiveHttp(urlArr);
  53. LogWarn(Severity_Low, Error_Debug, LOG_SLV_CHROMIUM_URLCHECK,
  54. CSimpleStringA::Format("check chromium url %d, %s", checkRet.first, checkRet.first ? checkRet.second.c_str() : urls.c_str()));
  55. if (checkRet.first)
  56. return checkRet.second.c_str();
  57. else
  58. return urlArr[0];
  59. };
  60. namespace Chromium {
  61. boost::mutex g_mutexFreerdp, m_guardInterruptLock;
  62. void CModTools::lockGuard()
  63. {
  64. m_guardInterruptLock.lock();
  65. }
  66. void CModTools::unlockGuard()
  67. {
  68. m_guardInterruptLock.unlock();
  69. }
  70. void CModTools::InitCModTools(CEntityBase* pEntity) {
  71. this->m_pEntity = pEntity;
  72. m_UseUOSBrowser = 0;
  73. m_isGuardMainBrowser = false;
  74. CSmartPointer<IConfigInfo> spConfig;
  75. ErrorCodeEnum Error = m_pEntity->GetFunction()->OpenConfig(Config_Root, spConfig);
  76. if (Error == Error_Succeed)
  77. {
  78. Error = spConfig->ReadConfigValue("Terminal", "MachineType", ConfigManager::getInstance().m_strMachineType);
  79. if (Error == Error_Succeed)
  80. {
  81. Error = spConfig->ReadConfigValue("Terminal", "Site", ConfigManager::getInstance().m_strSite);
  82. if (Error != Error_Succeed)
  83. {
  84. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ReadConfigValue, get Site value failed");
  85. }
  86. }
  87. else
  88. {
  89. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ReadConfigValue, get MachineType value failed");
  90. }
  91. }
  92. auto configRet = ReadCenterConfigStr();
  93. ConfigManager::getInstance().m_UserMgrUrlStr = std::get<0>(configRet);
  94. ConfigManager::getInstance().m_UserMgrUrlNoSidebarStr = std::get<1>(configRet);
  95. ConfigManager::getInstance().m_UserMgrUrlNoSidebarMutiStr = std::get<2>(configRet);
  96. ConfigManager::getInstance().m_UserMgrUrlFultureStr = std::get<3>(configRet);
  97. ConfigManager::getInstance().m_UserMgrAdStr = std::get<4>(configRet);
  98. notifyExist = breakdownExist = m_isAdOpen = false;
  99. m_magicStr = uuid4_generate(8);
  100. ConfigManager::getInstance().m_withMin = false;
  101. #if defined(RVC_OS_LINUX)
  102. InitBrowserUseMode();
  103. #endif //RVC_OS_LINUX
  104. SetSpecialPageParam("", "", "", 0);
  105. }
  106. bool CModTools::killAllChromium()
  107. {
  108. #if (defined _WIN32 || defined _WIN64)
  109. const char* killCmd = "taskkill /F /IM cefclient.exe /T";
  110. SYSTEM_ON(killCmd);
  111. return true;
  112. #else
  113. do
  114. {
  115. boost::process::child child_process("sudo killall -9 uosbrowser");
  116. child_process.wait();
  117. } while (false);
  118. do
  119. {
  120. boost::process::child child_process("sudo killall -9 cefclient");
  121. child_process.wait();
  122. } while (false);
  123. do {
  124. boost::process::child child_process("sudo killall -9 browser");
  125. child_process.wait();
  126. } while (false);
  127. breakdownExist = m_isAdOpen = notifyExist = false;
  128. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("run killAllChromium success ex");
  129. return true;
  130. #endif
  131. }
  132. void CModTools::killAllChromiumByThread(int seconds)
  133. {
  134. #if (defined _WIN32 || defined _WIN64)
  135. #else
  136. auto cur = std::async([]() {
  137. CModTools::get_mutable_instance().killAllChromium();
  138. });
  139. std::future_status a1 = cur.wait_for(std::chrono::seconds(seconds));
  140. if (a1 == std::future_status::deferred)
  141. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("killAllChromiumByThread run delay");
  142. else
  143. {
  144. if (a1 == std::future_status::ready)
  145. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("killAllChromiumByThread run end");
  146. else
  147. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("killAllChromiumByThread do not end, over than %d seconds", seconds);
  148. }
  149. #endif
  150. }
  151. bool CModTools::killChromiumByName(std::string name)
  152. {
  153. return true;
  154. }
  155. std::pair<bool, std::string> CModTools::getAdUrl()
  156. {
  157. auto AdUrl = ConfigManager::getInstance().m_UserMgrAdStr;
  158. std::string strUrl = AdUrl.GetData();
  159. //auto strUrl = detectUrls(AdUrl.GetData());
  160. return strUrl.length() > 0 ? std::make_pair(true, strUrl) : std::make_pair(false, strUrl);
  161. }
  162. std::pair<bool, std::string> CModTools::getErrUrl(ERR_PAGE_REASON errType)
  163. {
  164. CSimpleStringA errPagePath;
  165. m_pEntity->GetFunction()->GetPath("Base", errPagePath);
  166. errPagePath.Append(SPLIT_SLASH_STR).Append("res").Append(SPLIT_SLASH_STR);
  167. errPagePath.Append("ManagerDesktop").Append(SPLIT_SLASH_STR);
  168. if (errType._to_integral() == ERR_PAGE_REASON::breakdown)
  169. errPagePath.Append("entityCheck.html");
  170. else if (errType._to_integral() == ERR_PAGE_REASON::warnPrompt)
  171. errPagePath.Append("serverNoAccess.html");
  172. else if (errType._to_integral() == ERR_PAGE_REASON::audioErr)
  173. errPagePath.Append("audioErr.html");
  174. else if (errType._to_integral() == ERR_PAGE_REASON::CardStoreIsBusy)
  175. errPagePath.Append("cardStore.html");
  176. else if (errType._to_integral() == ERR_PAGE_REASON::main)
  177. errPagePath.Append("homePageErr.html");
  178. else if (errType._to_integral() == ERR_PAGE_REASON::startup)
  179. errPagePath.Append("startPage.html");
  180. else if (errType._to_integral() == ERR_PAGE_REASON::redirect)
  181. errPagePath.Append("redirect.html");
  182. else
  183. errPagePath.Append("entityCheck.html");
  184. return std::make_pair(true, errPagePath.GetData());
  185. }
  186. std::pair<bool, std::string> CModTools::GenerateErrPage(ERR_PAGE_REASON errType, std::tuple < std::string, std::string> norParam, std::tuple<std::string, std::string, std::string, DWORD, DWORD> exParam)
  187. {
  188. auto generate_url_prefix = []() -> CSimpleStringA {
  189. #if defined(RVC_OS_WIN)
  190. return CSimpleStringA("");
  191. #else
  192. return CSimpleStringA("\\");
  193. #endif
  194. };
  195. auto errUrlRet = getErrUrl(errType);
  196. CSimpleStringA errPagePathBase = errUrlRet.second.c_str();
  197. CSimpleStringA errPageParams(true);
  198. CSimpleStringA needGuardian = "Y";
  199. auto rc = this->m_pEntity->GetFunction()->GetSysVar("NeedGuardian", needGuardian);
  200. bool showRebootTime = true;
  201. if (rc == Error_Succeed && needGuardian.Compare("Y", false))
  202. showRebootTime = false;
  203. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("NeedGuardian:%s", needGuardian.GetData());
  204. switch (errType)
  205. {
  206. case ERR_PAGE_REASON::CardStoreIsBusy:
  207. break;
  208. case ERR_PAGE_REASON::breakdown:
  209. case ERR_PAGE_REASON::warnPrompt:
  210. case ERR_PAGE_REASON::audioErr:
  211. {
  212. errPageParams.Append(CSimpleStringA("showlimit=1"));
  213. if (ConfigManager::getInstance().m_withMin) {
  214. errPageParams.Append(CSimpleStringA(generate_url_prefix())).Append(CSimpleStringA("&showmin=1"));
  215. }
  216. }
  217. break;
  218. default:
  219. return std::make_pair(false, std::string("unknown err_page_reason:").append(errType._to_string()));
  220. }
  221. CSimpleStringA errPageUrl(true);
  222. if (!errPageParams.IsNullOrEmpty()) {
  223. errPageUrl = CSimpleStringA("file:///") + errPagePathBase + "?" + errPageParams;
  224. } else {
  225. errPageUrl = CSimpleStringA("file:///") + errPagePathBase;
  226. }
  227. #if defined(RVC_OS_WIN)
  228. std::string in = errPageUrl.GetData();
  229. errPageUrl = UrlEncoder::Encode(in).c_str();
  230. errPageUrl.Append(" ");
  231. CSimpleStringA cachePath;
  232. this->m_pEntity->GetFunction()->GetPath("Temp", cachePath);
  233. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("cefCache_").
  234. Append(ConfigManager::getInstance().m_strCacheHead.length() > 0 ? ConfigManager::getInstance().m_strCacheHead.c_str() : "").Append(errType._to_string());
  235. errPageUrl.Append(" --cache-path=").Append(cachePath);
  236. if (ConfigManager::getInstance().m_extension_withTerminal && ConfigManager::getInstance().m_extension_headerStr.length() > 0)
  237. errPageUrl.Append(" --modify-header=").Append(ConfigManager::getInstance().m_extension_headerStr.c_str());
  238. if (ConfigManager::getInstance().m_extension_debugOpen)
  239. errPageUrl.Append(" --right-menu");
  240. if (ConfigManager::getInstance().isUseMagic())
  241. errPageUrl.Append(" --magic-str=").Append(m_magicStr.c_str());
  242. if (ConfigManager::getInstance().m_withMedia)
  243. errPageUrl.Append(" --enable-media-stream=1 --enable-speech-input=1");
  244. if (ConfigManager::getInstance().m_withConsole)
  245. errPageUrl.Append(" --with-console");
  246. if(ConfigManager::getInstance().getSogouForce() != 0)
  247. errPageUrl.Append(" --adapt-sogou=").Append(std::to_string(ConfigManager::getInstance().getSogouForce()).c_str());
  248. errPageUrl.Append(" --no-sandbox").Append(" --hide-controls --hide-overlays --hide-frame --winhttp-proxy-resolver").Append(" --always-on-top");
  249. switch (errType) {
  250. case ERR_PAGE_REASON::TerminalManagerKickOut:
  251. case ERR_PAGE_REASON::TerminalManagerOff:
  252. errPageUrl.Append(" --logextend=").Append((+PAGE_TYPE::TerminalManager)._to_string());
  253. break;
  254. case ERR_PAGE_REASON::CardStoreIsBusy:
  255. errPageUrl.Append(" --logextend=").Append((+PAGE_TYPE::CardStoreIsBusy)._to_string());
  256. break;
  257. case ERR_PAGE_REASON::CameraConfig:
  258. errPageUrl.Append(" --logextend=").Append((+PAGE_TYPE::CameraConfig)._to_string());
  259. break;
  260. case ERR_PAGE_REASON::disabled:
  261. case ERR_PAGE_REASON::jobuncomplete:
  262. errPageUrl.Append(" --logextend=").Append((+PAGE_TYPE::TradeManager)._to_string());
  263. break;
  264. case ERR_PAGE_REASON::warnPrompt:
  265. case ERR_PAGE_REASON::breakdown:
  266. case ERR_PAGE_REASON::audioErr:
  267. errPageUrl.Append(" --logextend=").Append((+PAGE_TYPE::breakdown)._to_string());
  268. break;
  269. default:
  270. errPageUrl.Append(" --logextend=").Append((+PAGE_TYPE::errPage)._to_string());
  271. break;
  272. }
  273. CSimpleStringA strChromiumPath = GetCefHead(this->m_pEntity);
  274. CSimpleString dstCmd = strChromiumPath + " --url=" + errPageUrl;
  275. #else
  276. CSimpleString dstCmd = generateBrowserCMDForEverything(errPageUrl.GetData(), 2, false).c_str();
  277. #endif //RVC_OS_WIN
  278. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GenerateErrPage dstPage=%s", dstCmd.GetData());
  279. return std::make_pair(true, (const char*)dstCmd.GetData());
  280. }
  281. std::string formatTime(SYSTEMTIME time)
  282. {
  283. char tBuf[1024] = "";
  284. //sprintf(tBuf, "%04u-%02u-%02u %02u:%02u:%02u:%03u", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);
  285. sprintf(tBuf, "%04u-%02u-%02u %02u:%02u:%02u", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond);
  286. return tBuf;
  287. }
  288. std::string CModTools::GetSpShellNextRebootTime()
  289. {
  290. static std::string shellBootTime;
  291. if (0 == shellBootTime.length())
  292. {
  293. CAutoArray<CSimpleStringA> t_names;
  294. CAutoArray<int> t_Idx;
  295. CAutoArray<CEntityStartInfo> t_Infos;
  296. this->m_pEntity->GetFunction()->GetAllEntityStartInfo(t_names, t_Idx, t_Infos);
  297. for (int i = 0; i < t_Infos.GetCount(); i++) {
  298. //if (0 == t_Idx[i])//shell
  299. if (t_names[i].Compare("HealthManager") == 0) {
  300. SYSTEMTIME startTime = t_Infos[i].startTime;
  301. CSmallDateTime dateTime;
  302. dateTime.FromSystemTime(startTime);
  303. /** 十分钟后重启 [Gifur@2022324]*/
  304. CSmallDateTime dateNewTime((DWORD)dateTime + 10 * 60);
  305. shellBootTime = dateNewTime.ToTimeString().GetData();
  306. #if defined(RVC_OS_LINUX)
  307. SP::Utility::replaceInPlace(shellBootTime, " ", "-");
  308. #endif //RVC_OS_LINUX
  309. break;
  310. }
  311. }
  312. }
  313. return shellBootTime;
  314. }
  315. void CModTools::setLimitReason(const char* reason) {
  316. CSimpleStringA m_strLimitReason = reason;
  317. if (m_strLimitReason.Compare("breakdown") != 0 && !m_strLimitReason.IsNullOrEmpty())
  318. {
  319. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("reason:%d, setState:%s", reason, "L");
  320. this->m_pEntity->GetFunction()->SetSysVar("TradeManageState", "L", true);
  321. }
  322. if (m_strLimitReason.Compare("breakdown") == 0) {
  323. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("reason:%d, no setState", reason);
  324. }
  325. if (m_strLimitReason.IsNullOrEmpty())
  326. {
  327. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("reason:%d, setState:%s", reason, "T");
  328. this->m_pEntity->GetFunction()->SetSysVar("TradeManageState", "T", true);
  329. }
  330. }
  331. void CModTools::stopCommonPage(std::string name)
  332. {
  333. #if defined(_MSC_VER)
  334. CSimpleStringA strChromiumPath = GetCefHead(this->m_pEntity), strCmdLine = "";
  335. strCmdLine.Append(strChromiumPath).Append(" --kill");
  336. strCmdLine.Append(" --logextend=").Append(name.c_str());
  337. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("stopCommonPage cmdline:%s", strCmdLine.GetData());
  338. SYSTEM_ON(strCmdLine.GetData());
  339. #endif //_MSC_VER
  340. }
  341. /** 这个接口将移除,信创不再使用CEFClient [Gifur@2023626]*/
  342. void CModTools::StopChromiumBrowser(ERR_PAGE_REASON reason, bool stopAll)
  343. {
  344. #if defined(_MSC_VER)
  345. CSimpleStringA strChromiumPath = GetCefHead(this->m_pEntity), strCmdLine = "";
  346. strCmdLine.Append(strChromiumPath).Append(" --kill");
  347. if (!stopAll)
  348. strCmdLine.Append(" --logextend=").Append(reason._to_string());
  349. if (reason._to_integral() == ERR_PAGE_REASON::main)
  350. m_isGuardMainBrowser = false;
  351. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("StopChromiumBrowser cmdline:%s, current guardian main browser : %d", strCmdLine.GetData(), m_isGuardMainBrowser);
  352. SYSTEM_ON(strCmdLine.GetData());
  353. if (reason._to_integral() == ERR_PAGE_REASON::breakdown || reason._to_integral() == ERR_PAGE_REASON::warnPrompt || reason._to_integral() == ERR_PAGE_REASON::audioErr)
  354. breakdownExist = false;
  355. if (reason._to_integral() == ERR_PAGE_REASON::ErrNotify)
  356. notifyExist = false;
  357. if (stopAll)
  358. breakdownExist = notifyExist = m_isAdOpen = false;
  359. #else
  360. if (stopAll)
  361. killAllChromium();
  362. else
  363. LogWarn(Severity_Low, Error_NotSupport, LOG_WARN_CHROMIUM_NOT_SUPPORT_TOKILL_SPECIFIED_BROWSER,
  364. CSimpleStringA::Format("StopChromiumBrowser: %s", reason._to_string()));
  365. #endif //_MSC_VER
  366. }
  367. void CModTools::StopChromiumBrowser_security(ERR_PAGE_REASON reason, int times)
  368. {
  369. for (int i = 0; i < times; )
  370. {
  371. StopChromiumBrowser(reason, false);
  372. i++;
  373. if(i < times)
  374. std::this_thread::sleep_for(std::chrono::seconds(2));
  375. }
  376. }
  377. /** 这个接口将移除,信创不再使用CEFClient [Gifur@2023626]*/
  378. void CModTools::StopSingleChromiumBrowserByName(std::string cefName)
  379. {
  380. #if defined(_MSC_VER)
  381. CSimpleStringA strChromiumPath = GetCefHead(this->m_pEntity), strCmdLine = "";
  382. strCmdLine.Append(strChromiumPath).Append(" --kill");
  383. strCmdLine.Append(" --logextend=").Append(cefName.c_str());
  384. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("StopChromiumBrowser cmdline:%s, current guardian main browser : %d", strCmdLine.GetData(), m_isGuardMainBrowser);
  385. SYSTEM_ON(strCmdLine.GetData());
  386. #else
  387. LogWarn(Severity_Low, Error_NotSupport, LOG_WARN_CHROMIUM_NOT_SUPPORT_TOKILL_SPECIFIED_BROWSER,
  388. CSimpleStringA::Format("StopSingleChromiumBrowserByName: %s", cefName.c_str()));
  389. #endif //_MSC_VER
  390. }
  391. std::pair<bool, std::string> CModTools::getMainUrl()
  392. {
  393. CSimpleStringA strUrl;
  394. bool isOpenExtend = false;
  395. auto mainUrl = ConfigManager::getInstance().m_UserMgrUrlFultureStr;
  396. //strUrl = detectUrls(mainUrl.GetData()).c_str();
  397. strUrl = mainUrl;
  398. isOpenExtend = false;
  399. return std::make_pair(isOpenExtend, strUrl.GetData());
  400. }
  401. std::pair<bool, std::string> CModTools::getExtendUrl()
  402. {
  403. CSimpleStringA strUrl;
  404. if (ConfigManager::getInstance().m_UserMgrUrlNoSidebarStr.Compare(""))
  405. {
  406. auto mainUrl = ConfigManager::getInstance().m_UserMgrUrlNoSidebarStr;
  407. auto exUrl = ConfigManager::getInstance().m_UserMgrUrlNoSidebarMutiStr;
  408. auto t_Arr = exUrl.Split('|');
  409. std::vector<std::string> urlArr;
  410. urlArr.emplace_back(mainUrl.GetData());
  411. for (int i = 0; i < t_Arr.GetCount(); i++)
  412. urlArr.emplace_back(t_Arr[i].GetData());
  413. auto checkRet = DetectActiveHttp(urlArr);
  414. if (checkRet.first)
  415. strUrl = checkRet.second.c_str();
  416. else
  417. strUrl = mainUrl;
  418. }
  419. else
  420. strUrl = ConfigManager::getInstance().m_UserMgrUrlStr;
  421. return std::make_pair(strUrl.GetLength() > 0, strUrl.GetData());
  422. }
  423. std::string CModTools::generateAdCmd(std::string AdUrl) {
  424. #if defined(RVC_OS_LINUX)
  425. return generateBrowserCMDForEverything(AdUrl, 1, ConfigManager::getInstance().m_withUnsafeAd);
  426. #else
  427. CSimpleStringA strChromiumPath = GetCefHead(this->m_pEntity), strCmdLine = "";
  428. strCmdLine.Append(strChromiumPath).Append(" --url=").Append(AdUrl.c_str());
  429. if (ConfigManager::getInstance().m_extension_debugOpen)
  430. strCmdLine.Append(" --right-menu");
  431. if (ConfigManager::getInstance().m_extension_withTerminal && ConfigManager::getInstance().m_extension_headerStr.length() > 0)
  432. strCmdLine.Append(" --modify-header=").Append(ConfigManager::getInstance().m_extension_headerStr.c_str());
  433. CSimpleStringA cachePath;
  434. this->m_pEntity->GetFunction()->GetPath("Temp", cachePath);
  435. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("cefCache_").
  436. Append(ConfigManager::getInstance().m_strCacheHead.length() > 0 ? ConfigManager::getInstance().m_strCacheHead.c_str() : "").Append((+ERR_PAGE_REASON::Ad)._to_string());
  437. strCmdLine.Append(" --cache-path=").Append(cachePath);
  438. if (ConfigManager::getInstance().isUseMagic())
  439. strCmdLine.Append(" --magic-str=").Append(m_magicStr.c_str());
  440. if (ConfigManager::getInstance().m_withMedia)
  441. strCmdLine.Append(" --enable-media-stream=1 --enable-speech-input=1");
  442. if (ConfigManager::getInstance().m_withConsole)
  443. strCmdLine.Append(" --with-console");
  444. if (ConfigManager::getInstance().getSogouForce() != 0)
  445. strCmdLine.Append(" --adapt-sogou=").Append(std::to_string(ConfigManager::getInstance().getSogouForce()).c_str());
  446. strCmdLine.Append(" --no-sandbox").Append(" --hide-controls --hide-overlays --hide-frame --winhttp-proxy-resolver");
  447. strCmdLine.Append(" --logextend=").Append((+PAGE_TYPE::Ad)._to_string());
  448. strCmdLine.Append(" --top=-1");
  449. strCmdLine.Append(" --run-extend");
  450. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cmdline : %s", strCmdLine.GetData());
  451. return strCmdLine.GetData();
  452. #endif //RVC_OS_LINUX
  453. }
  454. #if defined(RVC_OS_WIN)
  455. std::string CModTools::generateCefclientCmd(bool isExtend, std::string mainUrl)
  456. {
  457. CSimpleStringA strChromiumPath = GetCefHead(this->m_pEntity), strCmdLine = "";
  458. strCmdLine.Append(strChromiumPath).Append(" --url=").Append(mainUrl.c_str());
  459. if (ConfigManager::getInstance().m_withUnsafeMain)
  460. strCmdLine.Append(" --allow-running-insecure-content");
  461. if (ConfigManager::getInstance().m_extension_debugOpen)
  462. strCmdLine.Append(" --right-menu");
  463. if (ConfigManager::getInstance().m_extension_withTerminal && ConfigManager::getInstance().m_extension_headerStr.length() > 0)
  464. strCmdLine.Append(" --modify-header=").Append(ConfigManager::getInstance().m_extension_headerStr.c_str());
  465. if (isExtend) {
  466. //not support
  467. }
  468. CSimpleStringA cachePath;
  469. this->m_pEntity->GetFunction()->GetPath("Temp", cachePath);
  470. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("cefCache_").
  471. Append(ConfigManager::getInstance().m_strCacheHead.length() > 0 ? ConfigManager::getInstance().m_strCacheHead.c_str() : "")
  472. .Append(isExtend ? (+ERR_PAGE_REASON::extend)._to_string() : (+ERR_PAGE_REASON::main)._to_string());
  473. //strCmdLine.Append(" --hide-controls=true")
  474. strCmdLine.Append(" --cache-path=").Append(cachePath);
  475. strCmdLine.Append(" --no-sandbox").Append(" --hide-controls --hide-overlays --hide-frame --winhttp-proxy-resolver --hide-tabs");
  476. if (ConfigManager::getInstance().isUseMagic())
  477. strCmdLine.Append(" --magic-str=").Append(m_magicStr.c_str());
  478. if (ConfigManager::getInstance().m_withMedia)
  479. strCmdLine.Append(" --enable-media-stream=1 --enable-speech-input=1");
  480. if (ConfigManager::getInstance().m_withConsole)
  481. strCmdLine.Append(" --with-console");
  482. if (ConfigManager::getInstance().getSogouForce() != 0)
  483. strCmdLine.Append(" --adapt-sogou=").Append(std::to_string(ConfigManager::getInstance().getSogouForce()).c_str());
  484. if (ConfigManager::getInstance().m_withDebugMode)
  485. strCmdLine.Append(" --remote-debugging-port=9222");
  486. if (isExtend) {
  487. strCmdLine.Append(" --logextend=").Append((+PAGE_TYPE::extend)._to_string());
  488. } else
  489. strCmdLine.Append(" --logextend=").Append((+PAGE_TYPE::slv)._to_string());
  490. strCmdLine.Append(" --errurl=file:\/\/\/").Append(getErrUrl(ERR_PAGE_REASON::main).second.c_str());
  491. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cmdline : %s", strCmdLine.GetData());
  492. return strCmdLine.GetData();
  493. }
  494. #endif //RVC_OS_WIN
  495. std::string CModTools::generateMainCmd(std::string mainUrl)
  496. {
  497. #if defined(_MSC_VER)
  498. return generateCefclientCmd(false, mainUrl);
  499. #else
  500. return generateBrowserCMDForEverything(mainUrl, 0, ConfigManager::getInstance().m_withUnsafeMain);
  501. #endif //_MSC_VER
  502. }
  503. std::string CModTools::generateInstallCmd(std::string installUrl)
  504. {
  505. #if defined(RVC_OS_LINUX)
  506. return generateBrowserCMDForEverything(installUrl, 0, false);
  507. #else
  508. CSimpleStringA strChromiumPath = GetCefHead(this->m_pEntity), strCmdLine = "";
  509. strCmdLine.Append(strChromiumPath).Append(" --url=").Append(installUrl.c_str());
  510. if (ConfigManager::getInstance().m_extension_debugOpen)
  511. strCmdLine.Append(" --right-menu");
  512. if (ConfigManager::getInstance().m_extension_withTerminal && ConfigManager::getInstance().m_extension_headerStr.length() > 0)
  513. strCmdLine.Append(" --modify-header=").Append(ConfigManager::getInstance().m_extension_headerStr.c_str());
  514. CSimpleStringA cachePath;
  515. this->m_pEntity->GetFunction()->GetPath("Temp", cachePath);
  516. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("cefCache_").
  517. Append(ConfigManager::getInstance().m_strCacheHead.length() > 0 ? ConfigManager::getInstance().m_strCacheHead.c_str() : "").
  518. Append((+ERR_PAGE_REASON::Ad)._to_string());
  519. strCmdLine.Append(" --cache-path=").Append(cachePath);
  520. strCmdLine.Append(" --no-sandbox").Append(" --hide-controls --hide-overlays --hide-frame --winhttp-proxy-resolver --hide-tabs");
  521. strCmdLine.Append(" --lang=zh-CN");//install page need language chinese
  522. strCmdLine.Append(" --logextend=").Append((+PAGE_TYPE::Install)._to_string());
  523. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cmdline : %s", strCmdLine.GetData());
  524. return strCmdLine.GetData();
  525. #endif //RVC_OS_LINUX
  526. }
  527. bool startsWithHttpCaseInsensitive(std::string url) {
  528. std::transform(url.begin(), url.end(), url.begin(), ::tolower);
  529. return url.find("http") == 0;
  530. }
  531. std::string CModTools::generateCommonPage(std::string url, std::string name, int width, int height, int point_x, int point_y, int top)
  532. {
  533. #if defined(RVC_OS_LINUX)
  534. std::string dstUrl = url;
  535. CSimpleStringA strBasePath(true);
  536. this->m_pEntity->GetFunction()->GetPath("BaseDir", strBasePath);
  537. std::string strPath(strBasePath.GetData());
  538. const std::string execute_newbrowser_path = strPath + SPLIT_SLASH_STR + "res" + SPLIT_SLASH_STR + "RunScript" + SPLIT_SLASH_STR + "startBrower.sh";
  539. const std::string execute_oldbrowser_path = strPath + SPLIT_SLASH_STR + "res" + SPLIT_SLASH_STR + "RunScript" + SPLIT_SLASH_STR + "startUOSBrower.sh";
  540. CSimpleStringA strParams(true);
  541. if (width > 0 && height > 0)
  542. strParams.Append(" --window-size=").Append(std::to_string(width).c_str()).Append(",").Append(std::to_string(height).c_str());
  543. do
  544. {
  545. if (point_x != -1 && point_y != -1)
  546. {
  547. strParams.Append(" --window-position=").Append(std::to_string(point_x).c_str()).Append(",").Append(std::to_string(point_y).c_str());
  548. break;
  549. }
  550. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  551. break;
  552. }
  553. SDL_DisplayMode mode;
  554. if (SDL_GetDesktopDisplayMode(0, &mode) != 0) {
  555. // 错误处理
  556. SDL_Quit();
  557. break;
  558. }
  559. // 获取屏幕的宽度和高度
  560. int screen_width = mode.w;
  561. int screen_height = mode.h;
  562. SDL_Quit();
  563. point_x = (screen_width - width) / 2;
  564. point_y = (screen_height - height) / 2;
  565. strParams.Append(" --window-position=").Append(std::to_string(point_x).c_str()).Append(",").Append(std::to_string(point_y).c_str());
  566. } while (false);
  567. //旧版浏览器(系统自带)
  568. if (m_UseUOSBrowser == 1) {
  569. CSimpleStringA strUOSBrowserPath(execute_oldbrowser_path.c_str());
  570. strParams.Append(" --new-window --no-first-run --disable-popup-blocking --disable-notifications --disable-desktop-notifications ");
  571. if (ConfigManager::getInstance().m_extension_withTerminal)
  572. {
  573. strParams.Append(" --load-extension=").Append(ConfigManager::getInstance().m_extensionPath.c_str());
  574. if (startsWithHttpCaseInsensitive(url))
  575. {
  576. auto redirctUrlRet = getErrUrl(ERR_PAGE_REASON::redirect);
  577. dstUrl = "\"file:///" + redirctUrlRet.second + "?redirect_open=" + url + "&redirect_type=" + name + "\"";
  578. }
  579. }
  580. strParams.Append(" --allow-running-insecure-content --disable-infobars --disable-suggestions-service --disable-save-password-bubble --disable-component-update");
  581. CSimpleStringA tempPath;
  582. this->m_pEntity->GetFunction()->GetPath("Temp", tempPath);
  583. CSimpleStringA cachePath(tempPath);
  584. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("UOSBrowser_").Append(name.c_str());
  585. strParams.Append(" --disk-cache-dir=").Append(cachePath);
  586. strParams.Append(" --disk-cache-size=").Append("2147483647"); //2G
  587. if (ConfigManager::getInstance().m_withMedia)
  588. strParams.Append(" --use-fake-ui-for-media-stream");
  589. CSimpleStringA usrDataPath(tempPath);
  590. usrDataPath.Append(SPLIT_SLASH_STR).Append("UOSBrowserConfig_").Append(name.c_str());
  591. strParams.Append(" --user-data-dir=").Append(usrDataPath);
  592. CSimpleStringA debug_log_path = usrDataPath + "/chrome_debug.log";
  593. if (ConfigManager::getInstance().m_withDebugMode)
  594. strParams.Append(" --enable-logging --vmodule=*/webrtc/*=2,*/media/*=2 --log-file=").Append(debug_log_path);
  595. strParams.Append(" --app=").Append(dstUrl.c_str());
  596. strUOSBrowserPath.Append(strParams);
  597. return strUOSBrowserPath.GetData();
  598. }
  599. //新版浏览器
  600. if (m_UseUOSBrowser == 2) {
  601. CSimpleStringA strUOSBrowserPath(execute_newbrowser_path.c_str());
  602. strParams.Append(" --allow-running-insecure-content --new-window");
  603. if (ConfigManager::getInstance().m_extension_withTerminal)
  604. {
  605. strParams.Append(" --load-extension=").Append(ConfigManager::getInstance().m_extensionPath.c_str());
  606. if (startsWithHttpCaseInsensitive(url))
  607. {
  608. auto redirctUrlRet = getErrUrl(ERR_PAGE_REASON::redirect);
  609. dstUrl = "\"file:///" + redirctUrlRet.second + "?redirect_open=" + url + "&redirect_type=" + name + "\"";
  610. }
  611. }
  612. CSimpleStringA tempPath;
  613. this->m_pEntity->GetFunction()->GetPath("Temp", tempPath);
  614. CSimpleStringA cachePath(tempPath);
  615. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("Browser_").Append(name.c_str());
  616. strParams.Append(" --disk-cache-dir=").Append(cachePath);
  617. strParams.Append(" --disk-cache-size=").Append("2147483647"); //2G
  618. if (ConfigManager::getInstance().m_withMedia)
  619. strParams.Append(" --use-fake-ui-for-media-stream");
  620. CSimpleStringA usrDataPath(tempPath);
  621. usrDataPath.Append(SPLIT_SLASH_STR).Append("BrowserConfig_").Append(name.c_str());
  622. strParams.Append(" --user-data-dir=").Append(usrDataPath);
  623. CSimpleStringA debug_log_path = usrDataPath + "/chrome_debug.log";
  624. if (ConfigManager::getInstance().m_withDebugMode)
  625. strParams.Append(" --enable-logging --vmodule=*/webrtc/*=2,*/media/*=2 --log-file=").Append(debug_log_path);
  626. strParams.Append(" --app=").Append(dstUrl.c_str());
  627. strUOSBrowserPath.Append(strParams);
  628. return strUOSBrowserPath.GetData();
  629. }
  630. return std::string("");
  631. #else
  632. CSimpleStringA strChromiumPath = GetCefHead(this->m_pEntity), strCmdLine = "";
  633. strCmdLine.Append(strChromiumPath).Append(" --url=").Append(url.c_str());
  634. if (ConfigManager::getInstance().m_extension_debugOpen)
  635. strCmdLine.Append(" --right-menu");
  636. if (ConfigManager::getInstance().m_extension_withTerminal && ConfigManager::getInstance().m_extension_headerStr.length() > 0)
  637. strCmdLine.Append(" --modify-header=").Append(ConfigManager::getInstance().m_extension_headerStr.c_str());
  638. if (width > 0 && height > 0)
  639. strCmdLine.Append(" --center-size=").Append(std::to_string(width).c_str()).Append("*").Append(std::to_string(height).c_str());
  640. if (point_x != -1 && point_y != -1)
  641. {
  642. strCmdLine.Append(" --src-pos=").Append(std::to_string(point_x).c_str()).Append("*").Append(std::to_string(point_y).c_str());
  643. }
  644. CSimpleStringA cachePath;
  645. this->m_pEntity->GetFunction()->GetPath("Temp", cachePath);
  646. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("cefCache_").
  647. Append(ConfigManager::getInstance().m_strCacheHead.length() > 0 ? ConfigManager::getInstance().m_strCacheHead.c_str() : "").Append(name.c_str());
  648. strCmdLine.Append(" --cache-path=").Append(cachePath);
  649. strCmdLine.Append(" --no-sandbox").Append(" --hide-controls --hide-overlays --hide-frame --winhttp-proxy-resolver --hide-tabs");
  650. strCmdLine.Append(" --lang=zh-CN");//install page need language chinese
  651. strCmdLine.Append(" --logextend=").Append(name.c_str());
  652. strCmdLine.Append(" --top=").Append(std::to_string(top).c_str());
  653. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("%s cmdline : %s", name.c_str(), strCmdLine.GetData());
  654. return strCmdLine.GetData();
  655. #endif
  656. }
  657. std::string CModTools::generateSpecialPageFromOtherEntityCmd(std::string mainUrl)
  658. {
  659. #if defined(RVC_OS_WIN)
  660. CSimpleStringA cachePath;
  661. CSimpleStringA strChromiumPath = GetCefHead(this->m_pEntity), strCmdLine = "";
  662. strCmdLine.Append(strChromiumPath).Append(" --url=").Append(mainUrl.c_str());
  663. if (ConfigManager::getInstance().m_extension_debugOpen)
  664. strCmdLine.Append(" --right-menu");
  665. if (ConfigManager::getInstance().m_extension_withTerminal && ConfigManager::getInstance().m_extension_headerStr.length() > 0)
  666. strCmdLine.Append(" --modify-header=").Append(ConfigManager::getInstance().m_extension_headerStr.c_str());
  667. this->m_pEntity->GetFunction()->GetPath("Temp", cachePath);
  668. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("cefCache_").
  669. Append(ConfigManager::getInstance().m_strCacheHead.length() > 0 ? ConfigManager::getInstance().m_strCacheHead.c_str() : "").
  670. Append(m_specialPage_temp_name.c_str());
  671. //strCmdLine.Append(" --hide-controls=true")
  672. strCmdLine.Append(" --cache-path=").Append(cachePath);
  673. strCmdLine.Append(" --no-sandbox").Append(" --hide-controls --hide-overlays --hide-frame --winhttp-proxy-resolver --hide-tabs");;//don't use single-process, it will affect the cef log
  674. if (ConfigManager::getInstance().isUseMagic())
  675. strCmdLine.Append(" --magic-str=").Append(m_magicStr.c_str());
  676. if (ConfigManager::getInstance().m_withMedia)
  677. strCmdLine.Append(" --enable-media-stream=1 --enable-speech-input=1");
  678. if (ConfigManager::getInstance().m_withConsole)
  679. strCmdLine.Append(" --with-console");
  680. if (ConfigManager::getInstance().getSogouForce() != 0)
  681. strCmdLine.Append(" --adapt-sogou=").Append(std::to_string(ConfigManager::getInstance().getSogouForce()).c_str());
  682. strCmdLine.Append(" --logextend=").Append(m_specialPage_temp_name.c_str());
  683. if (m_specialPage_temp_size.length() > 0)
  684. strCmdLine.Append(" --center-size=").Append(m_specialPage_temp_size.c_str());
  685. if (m_specialPage_temp_point.length() > 0)
  686. strCmdLine.Append(" --src-pos=").Append(m_specialPage_temp_point.c_str());
  687. strCmdLine.Append(" --top=").Append(std::to_string((unsigned long long)m_specialPage_temp_top).c_str());
  688. strCmdLine.Append(" --errurl=file:\/\/\/").Append(getErrUrl(ERR_PAGE_REASON::main).second.c_str());
  689. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cmdline : %s", strCmdLine.GetData());
  690. return strCmdLine.GetData();
  691. #else
  692. return generateBrowserCMDForEverything(mainUrl, 3, false);
  693. #endif //RVC_OS_WIN
  694. }
  695. #if defined(RVC_OS_LINUX)
  696. std::string CModTools::generateBrowserCMDForEverything(const std::string& url, int pageType, bool ignoreSecurity)
  697. {
  698. std::string dstUrl = url;
  699. CSimpleStringA strBasePath(true);
  700. this->m_pEntity->GetFunction()->GetPath("BaseDir", strBasePath);
  701. std::string strPath(strBasePath.GetData());
  702. const std::string execute_newbrowser_path = strPath + SPLIT_SLASH_STR + "res" + SPLIT_SLASH_STR + "RunScript" + SPLIT_SLASH_STR + "startBrower.sh";
  703. const std::string execute_oldbrowser_path = strPath + SPLIT_SLASH_STR + "res" + SPLIT_SLASH_STR + "RunScript" + SPLIT_SLASH_STR + "startUOSBrower.sh";
  704. std::string name = "";
  705. if (pageType == 0 || pageType == 2 || pageType == 3) {
  706. name = (+ERR_PAGE_REASON::main)._to_string();
  707. }
  708. else if (pageType == 1) {
  709. name = (+ERR_PAGE_REASON::Ad)._to_string();
  710. }
  711. else if (pageType == 2) {
  712. name = (+ERR_PAGE_REASON::breakdown)._to_string();
  713. }
  714. else if (pageType == 3) {
  715. name = (+ERR_PAGE_REASON::OutsideRequest)._to_string();
  716. }
  717. auto getUnsafeDomain = [](const std::string& url)
  718. {
  719. // 移除协议部分
  720. std::string::size_type pos = url.find("://");
  721. if (pos != std::string::npos) {
  722. pos += 3;
  723. }
  724. else {
  725. pos = 0;
  726. }
  727. // 查找域名结束位置
  728. std::string::size_type endPos = url.find('/', pos);
  729. if (endPos == std::string::npos) {
  730. endPos = url.length();
  731. }
  732. // 提取域名
  733. std::string domain = url.substr(pos, endPos - pos);
  734. domain = "http://" + domain;
  735. return domain;
  736. };
  737. //旧版浏览器(系统自带)
  738. if (m_UseUOSBrowser == 1) {
  739. CSimpleStringA strUOSBrowserPath(execute_oldbrowser_path.c_str());
  740. CSimpleStringA strParams(" --new-window --no-first-run --disable-popup-blocking --disable-notifications --disable-desktop-notifications --allow-running-insecure-content --disable-infobars --disable-suggestions-service --disable-save-password-bubble --disable-component-update");
  741. if (!ConfigManager::getInstance().m_extension_debugOpen)
  742. strParams.Append(" --kiosk");
  743. if (ConfigManager::getInstance().m_extension_withTerminal)
  744. {
  745. strParams.Append(" --load-extension=").Append(ConfigManager::getInstance().m_extensionPath.c_str());
  746. if (startsWithHttpCaseInsensitive(url))
  747. {
  748. auto redirctUrlRet = getErrUrl(ERR_PAGE_REASON::redirect);
  749. dstUrl = "\"file:///" + redirctUrlRet.second + "?redirect_open=" + url + "&redirect_type=" + name + "\"";
  750. }
  751. }
  752. if (pageType == 0 || pageType == 2 || pageType == 3) {
  753. } else if (pageType == 1) {
  754. strParams.Append(" --window-position=1280,1024");
  755. }
  756. CSimpleStringA tempPath;
  757. this->m_pEntity->GetFunction()->GetPath("Temp", tempPath);
  758. CSimpleStringA cachePath(tempPath);
  759. if (pageType == 0 || pageType == 2 || pageType == 3) {
  760. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("UOSBrowserMain_").Append((+ERR_PAGE_REASON::main)._to_string());
  761. } else if (pageType == 1) {
  762. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("UOSBrowserVice_").Append((+ERR_PAGE_REASON::Ad)._to_string());
  763. } else if (pageType == 2) {
  764. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("UOSBrowserMain_").Append((+ERR_PAGE_REASON::breakdown)._to_string());
  765. } else if (pageType == 3) {
  766. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("UOSBrowserMain_").Append((+ERR_PAGE_REASON::OutsideRequest)._to_string());
  767. }
  768. strParams.Append(" --disk-cache-dir=").Append(cachePath);
  769. strParams.Append(" --disk-cache-size=").Append("2147483647"); //2G
  770. if (ConfigManager::getInstance().m_withMedia)
  771. {
  772. strParams.Append(" --use-fake-ui-for-media-stream");
  773. }
  774. if (ignoreSecurity)
  775. {
  776. auto unsafeDomain = getUnsafeDomain(url);
  777. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("srcurl:%s, unsafeDomain:%s", url.c_str(), unsafeDomain.c_str());
  778. strParams.Append(" --unsafely-treat-insecure-origin-as-secure=").Append(unsafeDomain.c_str());
  779. }
  780. CSimpleStringA usrDataPath(tempPath);
  781. if (pageType == 0) {
  782. usrDataPath.Append(SPLIT_SLASH_STR).Append("UOSBrowserConfigMain_").Append((+ERR_PAGE_REASON::main)._to_string());
  783. } else if (pageType == 1) {
  784. usrDataPath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("UOSBrowserConfigVice_").Append((+ERR_PAGE_REASON::Ad)._to_string());
  785. } else if (pageType == 2) {
  786. usrDataPath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("UOSBrowserConfigMain_").Append((+ERR_PAGE_REASON::breakdown)._to_string());
  787. } else if (pageType == 3) {
  788. usrDataPath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("UOSBrowserConfigMain_").Append((+ERR_PAGE_REASON::OutsideRequest)._to_string());
  789. }
  790. strParams.Append(" --user-data-dir=").Append(usrDataPath);
  791. CSimpleStringA debug_log_path = usrDataPath + "/chrome_debug.log";
  792. if (ConfigManager::getInstance().m_withDebugMode)
  793. strParams.Append(" --enable-logging --vmodule=*/webrtc/*=2,*/media/*=2 --log-file=").Append(debug_log_path);
  794. strParams.Append(" ");
  795. strParams.Append(dstUrl.c_str());
  796. strUOSBrowserPath.Append(strParams);
  797. if (pageType == 0) {
  798. LogWarn(Severity_Low, Error_Unexpect, LOG_EVT_CHROMIUM_OPEN_MAIN_WITH_UOS, strUOSBrowserPath);
  799. } else if (pageType == 1) {
  800. LogWarn(Severity_Low, Error_Unexpect, LOG_EVT_CHROMIUM_OPEN_AD_WITH_UOS, strUOSBrowserPath);
  801. }
  802. return strUOSBrowserPath.GetData();
  803. }
  804. //新版浏览器
  805. if (m_UseUOSBrowser == 2) {
  806. CSimpleStringA strUOSBrowserPath(execute_newbrowser_path.c_str());
  807. CSimpleStringA strParams(" --allow-running-insecure-content --new-window");
  808. if (!ConfigManager::getInstance().m_extension_debugOpen)
  809. strParams.Append(" --kiosk");
  810. if (ConfigManager::getInstance().m_extension_withTerminal)
  811. {
  812. strParams.Append(" --load-extension=").Append(ConfigManager::getInstance().m_extensionPath.c_str());
  813. if (startsWithHttpCaseInsensitive(url))
  814. {
  815. auto redirctUrlRet = getErrUrl(ERR_PAGE_REASON::redirect);
  816. dstUrl = "\"file:///" + redirctUrlRet.second + "?redirect_open=" + url + "&redirect_type=" + name + "\"";
  817. }
  818. }
  819. if (pageType == 0 || pageType == 2 || pageType == 3) {
  820. } else if (pageType == 1) {
  821. strParams.Append(" --window-position=1280,1024");
  822. }
  823. CSimpleStringA tempPath;
  824. this->m_pEntity->GetFunction()->GetPath("Temp", tempPath);
  825. CSimpleStringA cachePath(tempPath);
  826. if (pageType == 0) {
  827. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("BrowserMain_").Append((+ERR_PAGE_REASON::main)._to_string());
  828. } else if (pageType == 1) {
  829. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("BrowserVice_").Append((+ERR_PAGE_REASON::Ad)._to_string());
  830. } else if (pageType == 2) {
  831. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("BrowserMain_").Append((+ERR_PAGE_REASON::breakdown)._to_string());
  832. } else if (pageType == 3) {
  833. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("BrowserMain_").Append((+ERR_PAGE_REASON::OutsideRequest)._to_string());
  834. }
  835. strParams.Append(" --disk-cache-dir=").Append(cachePath);
  836. strParams.Append(" --disk-cache-size=").Append("2147483647"); //2G
  837. if (ConfigManager::getInstance().m_withMedia)
  838. {
  839. strParams.Append(" --use-fake-ui-for-media-stream");
  840. }
  841. if (ignoreSecurity)
  842. {
  843. auto unsafeDomain = getUnsafeDomain(url);
  844. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("srcurl:%s, unsafeDomain:%s", url.c_str(), unsafeDomain.c_str());
  845. strParams.Append(" --unsafely-treat-insecure-origin-as-secure=").Append(unsafeDomain.c_str());
  846. }
  847. CSimpleStringA usrDataPath(tempPath);
  848. if (pageType == 0) {
  849. usrDataPath.Append(SPLIT_SLASH_STR).Append("BrowserConfigMain_").Append((+ERR_PAGE_REASON::main)._to_string());
  850. } else if (pageType == 1) {
  851. usrDataPath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("BrowserConfigVice_").Append((+ERR_PAGE_REASON::Ad)._to_string());
  852. } else if (pageType == 2) {
  853. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("BrowserConfigMain_").Append((+ERR_PAGE_REASON::breakdown)._to_string());
  854. } else if (pageType == 3) {
  855. cachePath.Append(CSimpleStringA(SPLIT_SLASH_STR)).Append("BrowserConfigMain_").Append((+ERR_PAGE_REASON::OutsideRequest)._to_string());
  856. }
  857. strParams.Append(" --user-data-dir=").Append(usrDataPath);
  858. CSimpleStringA debug_log_path = usrDataPath + "/chrome_debug.log";
  859. if (ConfigManager::getInstance().m_withDebugMode)
  860. strParams.Append(" --enable-logging --vmodule=*/webrtc/*=2,*/media/*=2 --log-file=").Append(debug_log_path);
  861. strParams.Append(" ");
  862. strParams.Append(dstUrl.c_str());
  863. strUOSBrowserPath.Append(strParams);
  864. if (pageType == 0) {
  865. LogWarn(Severity_Low, Error_Unexpect, LOG_EVT_CHROMIUM_OPEN_MAIN_WITH_UOSV2, strUOSBrowserPath);
  866. } else if (pageType == 1) {
  867. LogWarn(Severity_Low, Error_Unexpect, LOG_EVT_CHROMIUM_OPEN_AD_WITH_UOSV2, strUOSBrowserPath);
  868. }
  869. return strUOSBrowserPath.GetData();
  870. }
  871. return std::string("");
  872. }
  873. #endif //RVC_OS_LINUX
  874. std::pair<bool, std::string> CModTools::openAdPage()
  875. {
  876. if (m_isAdOpen)//only open ad in first time
  877. return std::make_pair(false, "open AdPage failed! Already exist.");
  878. auto AdUrlRet = getAdUrl();
  879. if (AdUrlRet.first)
  880. {
  881. auto strAdCmd = generateAdCmd(AdUrlRet.second);
  882. auto openAdRet = openCef(strAdCmd, (m_UseUOSBrowser == 0));
  883. if (Error_Succeed != openAdRet.first)
  884. return std::make_pair(false, "open Ad err!");
  885. else
  886. {
  887. m_isAdOpen = true;
  888. return std::make_pair(true, "");
  889. }
  890. }
  891. else
  892. return std::make_pair(false, "StartChromiumBrowser Ad url err!");
  893. }
  894. std::pair<ErrorCodeEnum, int> CModTools::StartChromiumBrowser(
  895. ERR_PAGE_REASON reason, std::tuple < std::string, std::string> normalParam
  896. , std::tuple<std::string, std::string, std::string, DWORD, DWORD> exParam) {
  897. g_mutexFreerdp.lock();
  898. std::shared_ptr<void> delHandleFun((void*)0, [&](void*) {
  899. g_mutexFreerdp.unlock();
  900. });
  901. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("start browser %d, %s", reason._to_integral(), reason._to_string());
  902. if (reason._to_integral() == ERR_PAGE_REASON::main)
  903. {
  904. auto mainUrlRet = getMainUrl();
  905. if (0 == mainUrlRet.second.length())
  906. {
  907. LogManager::getInstance().logEntityOpenPage(reason._to_string(), 0, 2, "none", Error_Unexpect, "StartChromiumBrowser get url failed!");
  908. LogWarn(Severity_High, Error_Unexpect, LOG_EVT_CHROMIUM_DETECT_MAIN_URL_EMPTY,
  909. CSimpleStringA::Format("StartChromiumBrowser get %s url failed!", reason._to_string()));
  910. return std::make_pair(Error_Unexpect, 0);
  911. }
  912. auto strCmdLine = generateMainCmd(mainUrlRet.second);
  913. auto openCefRet = openCef(strCmdLine, (m_UseUOSBrowser == 0));//with guard
  914. if (Error_Succeed == openCefRet.first) {
  915. LogWarn(Severity_Low, Error_Debug, LOG_EVT_CHROMIUM_OPEN_MAIN_URL,
  916. CSimpleStringA::Format("StartChromiumBrowser %s", mainUrlRet.second.c_str()));
  917. LogManager::getInstance().logEntityOpenPage(reason._to_string(), openCefRet.second, 0, "open");
  918. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)(CSimpleStringA::Format("StartChromiumBrowser %s", mainUrlRet.second.c_str()).GetData());
  919. /*
  920. #ifndef DEVOPS_ON_PRD
  921. LogEvent(Severity_High, LOG_EVT_CHROMIUM_OPEN_MAIN_URL, CSimpleStringA::Format("当前打开的业务链接:%s", mainUrlRet.second.c_str()));
  922. #endif
  923. */
  924. return std::make_pair(Error_Succeed, openCefRet.second);
  925. }
  926. }
  927. else if (reason._to_integral() == ERR_PAGE_REASON::extend)
  928. {
  929. LogManager::getInstance().logEntityOpenPage(reason._to_string(), 0, 2, "open", Error_Unexpect, "do not support freerdp");
  930. }
  931. else if (reason._to_integral() == ERR_PAGE_REASON::Ad)
  932. {
  933. auto AdRet = openAdPage();
  934. if(AdRet.first)
  935. LogManager::getInstance().logEntityOpenPage(reason._to_string(), 0, 0, "open");
  936. else
  937. LogManager::getInstance().logEntityOpenPage(reason._to_string(), 0, 2, "none", Error_Unexpect, AdRet.second);
  938. return std::make_pair(AdRet.first ? Error_Succeed : Error_Unexpect, 0);
  939. }
  940. else if (reason._to_integral() == ERR_PAGE_REASON::OutsideRequest) {
  941. #if defined(RVC_OS_WIN)
  942. auto strCmdLine = generateCefclientCmd(false, std::get<0>(normalParam));
  943. #else
  944. auto strCmdLine = generateBrowserCMDForEverything(std::get<0>(normalParam), 3, false);
  945. #endif //RVC_OS_WIN
  946. auto openCefRet = openCef(strCmdLine, false);
  947. if (Error_Succeed == openCefRet.first)
  948. {
  949. LogManager::getInstance().logEntityOpenPage(reason._to_string(), openCefRet.second, 0, "open");
  950. return std::make_pair(Error_Succeed, openCefRet.second);
  951. }
  952. else
  953. {
  954. LogManager::getInstance().logEntityOpenPage(reason._to_string(), openCefRet.second, 0, "none", openCefRet.first, "");
  955. }
  956. }
  957. else if (reason._to_integral() == ERR_PAGE_REASON::SpecialPageFromOtherEntity)
  958. {
  959. auto strCmdLine = generateSpecialPageFromOtherEntityCmd(std::get<0>(normalParam));
  960. auto openCefRet = openCef(strCmdLine, false);
  961. if (Error_Succeed == openCefRet.first)
  962. {
  963. LogManager::getInstance().logEntityOpenPage(reason._to_string(), openCefRet.second, 0, "open");
  964. return std::make_pair(Error_Succeed, openCefRet.second);
  965. }
  966. else
  967. LogManager::getInstance().logEntityOpenPage(reason._to_string(), openCefRet.second, 0, "none", openCefRet.first, "");
  968. }
  969. else if (reason._to_integral() == ERR_PAGE_REASON::startup)
  970. {
  971. auto url = std::string(R"(file:///)") + getErrUrl(reason).second;
  972. #if defined(RVC_OS_WIN)
  973. auto strCmdline = generateCommonPage(url, (+ERR_PAGE_REASON::startup)._to_string(), 900, 600, -1, -1, (int)HWND_NOTOPMOST);
  974. #else
  975. auto strCmdline = generateCommonPage(url, (+ERR_PAGE_REASON::startup)._to_string(), 900, 650, -1, -1, (int)HWND_NOTOPMOST);
  976. #endif
  977. auto openCefRet = openCef(strCmdline, false);
  978. if (Error_Succeed == openCefRet.first)
  979. {
  980. LogManager::getInstance().logEntityOpenPage(reason._to_string(), openCefRet.second, 0, "open");
  981. return std::make_pair(Error_Succeed, openCefRet.second);
  982. }
  983. else
  984. LogManager::getInstance().logEntityOpenPage(reason._to_string(), openCefRet.second, 0, "none", openCefRet.first, "");
  985. }
  986. else if (reason._to_integral() == ERR_PAGE_REASON::performance_monitor)
  987. {
  988. auto url = std::string(R"(file:///)") + getErrUrl(reason).second;
  989. auto strCmdline = generateCommonPage(url, (+ERR_PAGE_REASON::performance_monitor)._to_string(), 900, 600, -1, -1, (int)HWND_NOTOPMOST);
  990. auto openCefRet = openCef(strCmdline, false);
  991. if (Error_Succeed == openCefRet.first)
  992. {
  993. LogManager::getInstance().logEntityOpenPage(reason._to_string(), openCefRet.second, 0, "open");
  994. return std::make_pair(Error_Succeed, openCefRet.second);
  995. }
  996. else
  997. LogManager::getInstance().logEntityOpenPage(reason._to_string(), openCefRet.second, 0, "none", openCefRet.first, "");
  998. }
  999. else if (reason._to_integral() == ERR_PAGE_REASON::Install)
  1000. {
  1001. std::vector<std::string> installUrls;
  1002. #ifdef DEVOPS_ON_ST /*DevOps流水线编译,ST环境*/
  1003. installUrls.push_back("https://deviceinstallweb.paasst.cmbchina.cn/installRequire");
  1004. #elif defined(DEVOPS_ON_UAT)/*DevOps流水线编译,UAT环境*/
  1005. installUrls.push_back("https://deviceinstallweb.paasuat.cmbchina.cn/installRequire");
  1006. #elif defined(DEVOPS_ON_PRD)/*DevOps流水线编译,PRD环境*/
  1007. installUrls.push_back("https://deviceinstallweb.paas.cmbchina.cn/installRequire");
  1008. #else/*本地编译等非DevOps环境编译的版本*/
  1009. installUrls.push_back("https://deviceinstallweb.paasst.cmbchina.cn/installRequire");
  1010. #endif
  1011. auto checkRet = DetectActiveHttp(installUrls);
  1012. if (!checkRet.first)
  1013. {
  1014. LogWarn(Severity_High, Error_NetBroken, LOG_WARN_CHROMIUM_INSTALL_URLS_CHECK,
  1015. CSimpleString::Format("install page check err, can not connect to %s or %s", installUrls[0].c_str(), installUrls[1].c_str()).GetData());
  1016. }
  1017. std::string dstInstallUrl = checkRet.first ? checkRet.second : installUrls[0];
  1018. CSimpleString runningVer = "";
  1019. m_pEntity->GetFunction()->GetRunningVersion(runningVer);
  1020. dstInstallUrl.append("?terminalVersion=").append(runningVer.GetData());
  1021. #if defined(RVC_OS_LINUX)
  1022. auto replaceAll = [](std::string& str, const std::string& search, const std::string& replace) {
  1023. size_t pos = 0;
  1024. while ((pos = str.find(search, pos)) != std::string::npos) {
  1025. str.replace(pos, search.length(), replace);
  1026. pos += replace.length();
  1027. }
  1028. };
  1029. replaceAll(dstInstallUrl, "&", "\\&");
  1030. #endif // RVC_OS_WIN
  1031. auto strCmdLine = generateInstallCmd(dstInstallUrl);
  1032. auto openCefRet = openCef(strCmdLine, false);
  1033. if (Error_Succeed == openCefRet.first)
  1034. {
  1035. LogManager::getInstance().logEntityOpenPage(reason._to_string(), openCefRet.second, 0, "open");
  1036. return std::make_pair(Error_Succeed, openCefRet.second);
  1037. }
  1038. else
  1039. LogManager::getInstance().logEntityOpenPage(reason._to_string(), openCefRet.second, 0, "none", openCefRet.first, "");
  1040. }
  1041. else
  1042. {
  1043. //open err page
  1044. if (notifyExist)
  1045. return std::make_pair(Error_Duplication, 0);
  1046. if ((ERR_PAGE_REASON::breakdown == reason._to_integral()
  1047. || reason._to_integral() == ERR_PAGE_REASON::warnPrompt
  1048. || reason._to_integral() == ERR_PAGE_REASON::audioErr) && breakdownExist)
  1049. return std::make_pair(Error_Duplication, 0);//只打开一个breakdown页面,后续通过cefclient改动,避免打开同种页面
  1050. auto errPageUrl = GenerateErrPage(reason, normalParam, exParam);
  1051. if (!errPageUrl.first)
  1052. {
  1053. LogManager::getInstance().logEntityOpenPage(reason._to_string(), 2, 0, "none", Error_Unexpect, "errPage get url failed!");
  1054. return std::make_pair(Error_Unexpect, 0);
  1055. }
  1056. auto errPageRet = openCef(errPageUrl.second);
  1057. if (Error_Succeed == errPageRet.first)
  1058. {
  1059. if (ERR_PAGE_REASON::ErrNotify == reason._to_integral())
  1060. notifyExist = true;
  1061. else if (ERR_PAGE_REASON::breakdown == reason._to_integral()
  1062. || reason._to_integral() == ERR_PAGE_REASON::warnPrompt
  1063. || reason._to_integral() == ERR_PAGE_REASON::audioErr)
  1064. breakdownExist = true;
  1065. LogManager::getInstance().logEntityOpenPage(reason._to_string(), errPageRet.second, 0, "open");
  1066. }
  1067. else
  1068. LogManager::getInstance().logEntityOpenPage(reason._to_string(), errPageRet.second, 0, "none", errPageRet.first, "");
  1069. return std::make_pair(errPageRet.first, 0);
  1070. }
  1071. return std::make_pair(Error_Unexpect, 0);
  1072. }
  1073. std::pair<ErrorCodeEnum, int> CModTools::startCommonPage(std::string name, std::string url, int width, int height, int point_x, int point_y, std::string top)
  1074. {
  1075. CSimpleString topStr = top.c_str();
  1076. int top_param = (int)HWND_NOTOPMOST;
  1077. if (topStr.Compare("top", true) == 0)
  1078. top_param = (int)HWND_TOP;
  1079. else if (topStr.Compare("topmost", true) == 0)
  1080. top_param = (int)HWND_TOPMOST;
  1081. else if (topStr.Compare("notopmost", true) == 0)
  1082. top_param = (int)HWND_NOTOPMOST;
  1083. else if (topStr.Compare("bottom", true) == 0)
  1084. top_param = (int)HWND_BOTTOM;
  1085. auto strCmdline = generateCommonPage(url, name, width, height, point_x, point_y, top_param);
  1086. auto openCefRet = openCef(strCmdline, false);
  1087. if (Error_Succeed == openCefRet.first)
  1088. return std::make_pair(Error_Succeed, openCefRet.second);
  1089. else
  1090. {
  1091. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("open cef %s size:%d*%d at %d*%d in layer %s failed: %d"
  1092. , name.c_str(), width, height, height, point_x, point_y, top.c_str(), openCefRet.first);
  1093. return std::make_pair(openCefRet.first, openCefRet.first);
  1094. }
  1095. }
  1096. void CModTools::cefClientGuardian(std::string cmdline)
  1097. {
  1098. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create thread:%s", __FUNCTION__);
  1099. m_isGuardMainBrowser = true;
  1100. while (m_isGuardMainBrowser)
  1101. {
  1102. try
  1103. {
  1104. #if (defined _WIN32 || defined _WIN64)
  1105. auto startRet = startProcessInJob(cmdline, "");
  1106. if (!std::get<0>(startRet))
  1107. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cefClientGuardian startProcessInJob failed!");
  1108. else
  1109. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cefClientGuardian open cefclient pid:%d, cmd:%s", std::get<1>(startRet), cmdline.c_str());
  1110. WaitForSingleObject(std::get<3>(startRet), INFINITE);
  1111. boost::mutex::scoped_lock(m_guardInterruptLock);//必须保证unlock被执行,不然就会有问题
  1112. #else
  1113. boost::process::child child_process(cmdline);
  1114. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("open cefclient pid:%d, cmd:%s", child_process.id(), cmdline.c_str());
  1115. child_process.wait();
  1116. #endif
  1117. }
  1118. catch (const std::exception& e)
  1119. {
  1120. std::cout << e.what() << std::endl;
  1121. return;
  1122. }
  1123. catch (...)
  1124. {
  1125. return;
  1126. }
  1127. boost::this_thread::sleep_for(boost::chrono::seconds(3));
  1128. }
  1129. return;
  1130. }
  1131. // 输入为北京时间 输出为北京时间戳
  1132. int CModTools::StandardToStamp(const char* str_time, bool dateOnly)
  1133. {
  1134. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("StandardToStamp str = %s", str_time);
  1135. struct tm stm;
  1136. int iY, iM, iD, iH, iMin, iS;
  1137. // 检查有效性
  1138. if ('-' != str_time[4] || '-' != str_time[7])
  1139. {
  1140. return 0;
  1141. }
  1142. memset(&stm, 0, sizeof(stm));
  1143. iY = atoi(str_time);
  1144. iM = atoi(str_time + 5);
  1145. iD = atoi(str_time + 8);
  1146. if (dateOnly)
  1147. {
  1148. iH = 0;
  1149. iMin = 0;
  1150. iS = 0;
  1151. }
  1152. else {
  1153. iH = atoi(str_time + 11);
  1154. iMin = atoi(str_time + 14);
  1155. iS = atoi(str_time + 17);
  1156. }
  1157. stm.tm_year = iY - 1900;
  1158. stm.tm_mon = iM - 1;
  1159. stm.tm_mday = iD;
  1160. stm.tm_hour = iH;
  1161. stm.tm_min = iMin;
  1162. stm.tm_sec = iS;
  1163. /*printf("%d-%0d-%0d %0d:%0d:%0d\n", iY, iM, iD, iH, iMin, iS);*/ //标准时间格式例如:2016:08:02 12:12:30
  1164. return (int)mktime(&stm);
  1165. }
  1166. bool CModTools::findRestartCode(DWORD userCode) {
  1167. DWORD authArr_noretry_norestart[] = ERRARR_ACCESSAUTH_NORETRY_NORESTART;
  1168. DWORD authArr_retry_norestart[] = ERRARR_ACCESSAUTH_RETRY_NORESTART;
  1169. std::vector<DWORD> norestartArr;
  1170. #if (defined _WIN32 || defined _WIN64)
  1171. for each (auto i in authArr_noretry_norestart)
  1172. #else
  1173. for (auto i : authArr_noretry_norestart)
  1174. #endif
  1175. norestartArr.emplace_back(i);
  1176. #if (defined _WIN32 || defined _WIN64)
  1177. for each (auto i in authArr_retry_norestart)
  1178. #else
  1179. for (auto i : authArr_retry_norestart)
  1180. #endif
  1181. norestartArr.emplace_back(i);
  1182. #if (defined _WIN32 || defined _WIN64)
  1183. for each (auto i in norestartArr)
  1184. #else
  1185. for (auto i : norestartArr)
  1186. #endif
  1187. {
  1188. if (userCode == i) {
  1189. return false;
  1190. }
  1191. }
  1192. return true;
  1193. }
  1194. DWORD CModTools::spiltErrMsg(std::string errMsg)
  1195. {
  1196. DWORD usercode = 0;
  1197. CSimpleString m_sAuthErrMsg = errMsg.c_str();
  1198. auto authArr = m_sAuthErrMsg.Split('|');
  1199. if (authArr.GetCount() > 1)
  1200. {
  1201. auto ErrcodeStr = authArr[0];
  1202. if (ErrcodeStr[0] == '0' && ErrcodeStr[1] == 'x')
  1203. usercode = std::stoi(ErrcodeStr.GetData(), 0, 0);
  1204. }
  1205. return usercode;
  1206. }
  1207. bool CModTools::CheckTradeRecord() {
  1208. std::list<int> timeList;
  1209. using namespace Upload;
  1210. UploadService_ClientBase* pClient = new UploadService_ClientBase(m_pEntity);
  1211. auto rc = pClient->Connect();
  1212. if (rc != Error_Succeed)
  1213. {
  1214. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("connect to Upload entity fail: %d", rc);
  1215. pClient->SafeDelete();
  1216. pClient = NULL;
  1217. }
  1218. else
  1219. {
  1220. UploadService_UploadDateList_Req req = {};
  1221. UploadService_UploadDateList_Ans ans = {};
  1222. rc = (*pClient)(EntityResource::getLink().upgradeLink())->UploadDateList(req, ans, 10000);
  1223. if (rc != Error_Succeed)
  1224. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get CreateProcessRunAs fail from FreeRDPWrap: %d", rc);
  1225. else
  1226. {
  1227. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UploadDateList succeed from Upload");
  1228. for (int i = 0; i < ans.uploadDateStr.GetCount(); ++i)
  1229. {
  1230. CSimpleStringA str = (CSimpleStringA)ans.uploadDateStr[i];
  1231. int t = StandardToStamp(ans.uploadDateStr[i], true);
  1232. timeList.emplace_back(t);
  1233. }
  1234. }
  1235. pClient->GetFunction()->CloseSession();
  1236. pClient = NULL;
  1237. }
  1238. // 比较时间戳 登记时间 <= 列表时间 < 今天 返回true
  1239. time_t now = time(0);
  1240. localtime(&now);
  1241. now -= now % (3600 * 24);
  1242. now -= 3600 * 8; //处理东八区问题
  1243. CSmartPointer<IConfigInfo> spConfig;
  1244. m_pEntity->GetFunction()->OpenConfig(Config_Run, spConfig);
  1245. CSimpleStringA strLastRecordTime = "";
  1246. ErrorCodeEnum errorRead = Error_Succeed;
  1247. errorRead = spConfig->ReadConfigValue("Record", "LastRecordTime", strLastRecordTime);
  1248. if (errorRead != Error_Succeed)
  1249. {
  1250. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Read Record LastRecordTime -> Failed! Maybe LastRecordTime not exist!");
  1251. strLastRecordTime = "";
  1252. }
  1253. int timestamp_lastRecordTime = now;
  1254. if (!strLastRecordTime.IsNullOrEmpty())
  1255. {
  1256. timestamp_lastRecordTime = StandardToStamp(strLastRecordTime, false);
  1257. }
  1258. for (std::list<int>::iterator it = timeList.begin(); it != timeList.end(); ++it)
  1259. {
  1260. if (timestamp_lastRecordTime <= *it && *it < now)
  1261. {
  1262. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CheckTradeRecord lastRecordTime=%d, time_list=%d, now=%d", timestamp_lastRecordTime, *it, now);
  1263. return true;
  1264. }
  1265. }
  1266. return false;
  1267. }
  1268. void CModTools::InitBrowserUseMode()
  1269. {
  1270. #if (defined _WIN32 || defined _WIN64)
  1271. #else
  1272. if (m_pEntity != nullptr) {
  1273. CSmartPointer<IConfigInfo> spConfig;
  1274. m_pEntity->GetFunction()->OpenConfig(Config_Cache, spConfig);
  1275. CSimpleStringA value(true);
  1276. spConfig->ReadConfigValue("BusinessBrowserType", "FultureEx", value);
  1277. if (!(value.IsNullOrEmpty() || (value.Compare("1") != 0))) {
  1278. m_UseUOSBrowser = 0;
  1279. } else {
  1280. m_UseUOSBrowser = ToGetUOSBrowserType();
  1281. }
  1282. } else {
  1283. m_UseUOSBrowser = ToGetUOSBrowserType();
  1284. }
  1285. #endif
  1286. }
  1287. int CModTools::ToGetUOSBrowserType()
  1288. {
  1289. #if (defined _WIN32 || defined _WIN64)
  1290. return 0;
  1291. #else
  1292. const std::string execute_newbrowser_path_check = "/usr/share/browser/browser";
  1293. const std::string execute_oldbrowser_path_check = "/usr/share/uosbrowser/uosbrowser";
  1294. CSimpleStringA strBasePath(true);
  1295. this->m_pEntity->GetFunction()->GetPath("BaseDir", strBasePath);
  1296. std::string strPath(strBasePath.GetData());
  1297. const std::string execute_newbrowser_path = strPath + SPLIT_SLASH_STR + "res" + SPLIT_SLASH_STR + "RunScript" + SPLIT_SLASH_STR + "startBrower.sh";
  1298. const std::string execute_oldbrowser_path = strPath + SPLIT_SLASH_STR + "res" + SPLIT_SLASH_STR + "RunScript" + SPLIT_SLASH_STR + "startUOSBrower.sh";
  1299. int result(0);
  1300. std::string additional("");
  1301. std::string succStr, errStr;
  1302. std::string runStr("dpkg -l | grep org.deepin.browser | awk '{print $3}'");
  1303. if (SP::Module::Util::ShellExecute(runStr, succStr, errStr)) {
  1304. if (succStr.empty()) {
  1305. if (ExistsFileA(execute_oldbrowser_path_check.c_str())) {
  1306. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("%s exists!", execute_oldbrowser_path_check.c_str());
  1307. result = 1;
  1308. } else {
  1309. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("%s not exists!", execute_oldbrowser_path_check.c_str());
  1310. }
  1311. LogWarn(Severity_Low, Error_Debug, LOG_EVT_CHROMIUM_BROWSER_LACK_OF_NEWONE, "have not installered new version UOS browser");
  1312. } else {
  1313. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("new browser version: %s", succStr.c_str());
  1314. additional = succStr;
  1315. if (!ExistsFileA(execute_newbrowser_path.c_str())) {
  1316. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("%s not exists!", execute_newbrowser_path.c_str());
  1317. }
  1318. result = 2;
  1319. }
  1320. } else {
  1321. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("execute '%s' failed!", runStr.c_str());
  1322. }
  1323. UploadBrowserInfo(result, additional.c_str());
  1324. return result;
  1325. #endif
  1326. }
  1327. void CModTools::UploadBrowserInfo(int usingType, LPCTSTR info)
  1328. {
  1329. CSmartPointer<IConfigInfo> spConfig;
  1330. this->m_pEntity->GetFunction()->OpenConfig(Config_Run, spConfig);
  1331. int oldType(0);
  1332. CSimpleStringA strVersion;
  1333. spConfig->ReadConfigValueInt("BrowserUse", "Type", oldType);
  1334. spConfig->ReadConfigValue("BrowserUse", "Version", strVersion);
  1335. if (oldType != usingType || strVersion.Compare(info) != 0) {
  1336. LogWarn(Severity_Low, Error_Unexpect, LOG_EVT_CHROMIUM_BROWSER_USING_TYPE_CHANGE,
  1337. CSimpleStringA::Format("{ \"type\":%d, \"version\":\"%s\"}", usingType, info));
  1338. spConfig->WriteConfigValueInt("BrowserUse", "Type", usingType);
  1339. spConfig->WriteConfigValue("BrowserUse", "Version", info);
  1340. }
  1341. }
  1342. std::pair<ErrorCodeEnum, int> CModTools::openCef(std::string cmdline, bool isGuard, bool usingSystemCmd)
  1343. {
  1344. if (isGuard) {
  1345. //mod_chromium相当于cefclient的守护进程
  1346. boost::thread cefGuardian(boost::bind(&CModTools::cefClientGuardian, this, cmdline));
  1347. cefGuardian.detach();
  1348. }
  1349. else
  1350. {
  1351. #if (defined _WIN32 || defined _WIN64)
  1352. auto startRet = startProcessInJob(cmdline, "");
  1353. if (!std::get<0>(startRet))
  1354. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("startProcessInJob failed!");
  1355. return std::make_pair(std::get<0>(startRet) ? Error_Succeed : Error_Unexpect, std::get<1>(startRet));
  1356. #else
  1357. auto systemStartChromium = [](std::string str, bool systemCmd) {
  1358. if (systemCmd) {
  1359. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("system(%s)", str.c_str());
  1360. system(str.c_str());
  1361. } else {
  1362. SystemRunTest(str);
  1363. }
  1364. };
  1365. boost::thread(systemStartChromium, cmdline, usingSystemCmd).detach();
  1366. #endif
  1367. }
  1368. return std::make_pair(Error_Succeed, 0);
  1369. }
  1370. bool CModTools::RestartProxyServer() {
  1371. // start tcp bridge service
  1372. ErrorCodeEnum Error;
  1373. CSmartPointer<IEntityFunction> spEntityFunction = this->m_pEntity->GetFunction();
  1374. Error = spEntityFunction->StopTcpBridgeServer();
  1375. if (Error != Error_Succeed)
  1376. {
  1377. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("stop tcp bridge server failed! error = %d", Error);
  1378. return false;
  1379. }
  1380. Error = spEntityFunction->StartTcpBridgeServer(4504);
  1381. if (Error != Error_Succeed)
  1382. {
  1383. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("start tcp bridge server failed! error = %d", Error);
  1384. return false;
  1385. }
  1386. return true;
  1387. }
  1388. std::tuple<CSimpleStringA, CSimpleStringA, CSimpleStringA, CSimpleStringA, CSimpleStringA> CModTools::ReadCenterConfigStr(CSimpleStringA entityName) {
  1389. CSimpleStringA str = "", strNew = "", strEx = "", strFulture = "", strAd = "", strCacheHead;
  1390. bool forceCleanCache = false;
  1391. CSmartPointer<IConfigInfo> spCerConfig;
  1392. ErrorCodeEnum err = this->m_pEntity->GetFunction()->OpenConfig(Config_CenterSetting, spCerConfig);
  1393. if (entityName == "")
  1394. entityName = this->m_pEntity->GetEntityName();
  1395. SpIniMappingTable table;
  1396. // clean cache every time
  1397. table.AddEntryString(entityName, "UserMgrUrlFulture", strFulture, "");
  1398. table.AddEntryString(entityName, "UserMgrAd", strAd, "");
  1399. table.AddEntryString(entityName, "CacheHead", strCacheHead, "");
  1400. if (Error_Succeed == table.Load(spCerConfig))
  1401. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)(CSimpleString::Format("read centersetting config : UserMgrUrlFulture-%s, UserMgrAd-%s", strFulture.GetData(), strAd.GetData()));
  1402. CChromiumEntity* pChromiumEntity = reinterpret_cast<CChromiumEntity*>(m_pEntity);
  1403. if (pChromiumEntity != nullptr && pChromiumEntity->HasCustomMainUrl()) {
  1404. strFulture = pChromiumEntity->GetCustomMainUrl();
  1405. CSimpleStringA strTmp = CSimpleStringA::Format("更新业务中台链接为:[%s]", strFulture.GetData());
  1406. LogWarn(Severity_Low, Error_Debug, LOG_EVT_CHROMIUM_USE_CUSTOM_FULTURE_URL, strTmp);
  1407. }
  1408. ConfigManager::getInstance().m_strCacheHead = strCacheHead.GetData();
  1409. return std::make_tuple(str, strNew, strEx, strFulture, strAd);
  1410. }
  1411. }