baseEx.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. #pragma once
  2. #include "SpBase.h"
  3. #include<vector>
  4. #include<string>
  5. #include<map>
  6. #include<deque>
  7. #include "websocketpp/config/asio.hpp"
  8. #include "websocketpp/server.hpp"
  9. #include "json/json.h"
  10. #define DEFAULT_KEY_LEN 256
  11. #define DEFAULT_KEY_LEN 256
  12. #define HEADER_TERMINALNO_NAME "vtm-terminalno"
  13. #define SUC_RETURN_STR "SUC0000"
  14. #define RTA_UNKNOW_ERR "RTA42F2"
  15. std::vector<std::string> find_files(const std::string srcPath, const std::string fileName, bool isDir = false);
  16. std::string generateTimeStr(bool isSimple = false);
  17. DWORD SaveCefclientLog(std::string magicStr);
  18. bool SYSTEM_ON(std::string cmdLine, bool isWait = true);
  19. bool InitFileLogger(std::string dbgPath);
  20. void DbgToFileLogger(std::string msg);
  21. bool deleteDir_byFileSystem(const std::string srcPath);
  22. void InitTranslateFile(std::string srcFile);
  23. std::pair<unsigned long, std::string> splitStrToUserCodeAndErrMsg(std::string srcMsg);
  24. void InitUserCodeToMsgTip(CAutoArray<CSimpleStringA>& strErrorCodeArr, CAutoArray<CSimpleStringA>& strDescriptionArr, CAutoArray<CSimpleStringA>& strRemarkArr);
  25. bool isMsgTipExist();
  26. struct ErrMsgStruct {
  27. std::string VTMCode;
  28. std::string errMsg;
  29. std::string userCode;
  30. // 带参数的构造函数,参数有默认值
  31. ErrMsgStruct(const std::string& tmp_VTMCode = "", const std::string& tmp_errMsg = "", const std::string& tmp_userCode = "")
  32. : VTMCode(tmp_VTMCode), errMsg(tmp_errMsg), userCode(tmp_userCode) {}
  33. // 拷贝构造函数
  34. ErrMsgStruct(const ErrMsgStruct& other)
  35. : VTMCode(other.VTMCode), errMsg(other.errMsg), userCode(other.userCode) {}
  36. // 移动构造函数
  37. ErrMsgStruct(ErrMsgStruct&& other) noexcept
  38. : VTMCode(std::move(other.VTMCode)), errMsg(std::move(other.errMsg)), userCode(std::move(other.userCode)) {}
  39. };
  40. std::pair<unsigned long, ErrMsgStruct> getErrMsgByRemark(std::string srcMsg, bool isSafe);
  41. std::string UtfToGbk(const char* utf8);
  42. std::string ConvertGBKToUtf8(std::string& strGBK);
  43. void hexdump(const char* buf, const int num);
  44. std::string hexdumpToString(const char* buf, const int num);
  45. bool modifyBySpecialStr(std::wstring& src);
  46. void receivehexdump(const char* buf, const int num);
  47. void doWithDebugModeData(const char* strMethod, const char* buf);
  48. #ifdef RVC_OS_LINUX
  49. std::vector<int> getUosBrowserPIDs(int UseUOSBrowser);
  50. #endif
  51. class json_deal {
  52. public:
  53. static std::pair<bool, CSimpleStringA> getStringFromCjsonObj(void* object, const char* name);
  54. static std::pair<bool, int> getIntergerFromCjsonObj(void* object, const char* name);
  55. static std::pair<bool, double> getDoubleFromCjsonObj(void* object, const char* name);
  56. };
  57. #if (defined _WIN32 || defined _WIN64)
  58. long WINAPI printSEG(struct _EXCEPTION_POINTERS* ExceptionInfo);
  59. #else
  60. long printSEG();
  61. void set_traceback_path(std::string path);
  62. void seg_signal_handler(int signum);
  63. void normal_signal_handle(int signum);
  64. #endif
  65. typedef enum{
  66. WAIT_KEY_EXCHANGE,
  67. DATA_EXCHANGE
  68. }SM2ProtocolState;
  69. class SM2_Encrypt_Manager {
  70. public:
  71. SM2_Encrypt_Manager();
  72. ~SM2_Encrypt_Manager();
  73. bool GenerateSM2_key(std::string& pub, std::string& pri);
  74. std::string binToHex(const unsigned char* data, int len);
  75. void hexToBin(const std::string& hexStr, unsigned char* binData, int& binLen);
  76. void setOpposite(const std::string pub);
  77. std::pair<bool, std::string> checkSM2Enable();
  78. std::pair<std::string, std::string> EncryptMsg(std::string msg);//return signature, encryptMsg
  79. std::pair<bool, std::string> DecryptMsg(std::string sign, std::string msg);
  80. std::pair<bool, std::string> GenerateEncPubKey();
  81. bool verifySM2Manager();
  82. SM2ProtocolState getState()
  83. {
  84. return m_state;
  85. };
  86. void setState(SM2ProtocolState tmpState)
  87. {
  88. m_state = tmpState;
  89. };
  90. private:
  91. unsigned char m_public_key_buf[DEFAULT_KEY_LEN], m_private_key_buf[DEFAULT_KEY_LEN], m_opposite_public_key_buf[DEFAULT_KEY_LEN];
  92. int m_public_key_buf_len, m_private_key_buf_len, m_opposite_public_key_buf_len;
  93. std::string m_public_key, m_private_key, m_opposite_public_key;
  94. SM2ProtocolState m_state;
  95. };
  96. struct SYS_EVENT_PARAM
  97. {
  98. std::string key;
  99. std::string value;
  100. std::string oldValue;
  101. std::string entityName;
  102. // 默认构造函数
  103. SYS_EVENT_PARAM() = default;
  104. // 带参数的构造函数
  105. SYS_EVENT_PARAM(const std::string& k, const std::string& v, const std::string& ov, const std::string& en)
  106. : key(k), value(v), oldValue(ov), entityName(en) {}
  107. // 拷贝构造函数
  108. SYS_EVENT_PARAM(const SYS_EVENT_PARAM& other)
  109. : key(other.key), value(other.value), oldValue(other.oldValue), entityName(other.entityName) {}
  110. // 拷贝赋值运算符
  111. SYS_EVENT_PARAM& operator=(const SYS_EVENT_PARAM& other) {
  112. if (this != &other) {
  113. key = other.key;
  114. value = other.value;
  115. oldValue = other.oldValue;
  116. entityName = other.entityName;
  117. }
  118. return *this;
  119. }
  120. // 禁用移动构造函数
  121. SYS_EVENT_PARAM(SYS_EVENT_PARAM&&) = delete;
  122. // 禁用移动赋值运算符
  123. SYS_EVENT_PARAM& operator=(SYS_EVENT_PARAM&&) = delete;
  124. };
  125. struct websocket_info
  126. {
  127. std::string name;
  128. bool isSafe;
  129. // 默认构造函数:初始化为默认值
  130. websocket_info() : name(""), isSafe(false) {}
  131. // 带参数的构造函数:根据传入的参数初始化
  132. websocket_info(const std::string& n, bool s) : name(n), isSafe(s) {}
  133. };
  134. class ConfigManager {
  135. public:
  136. static ConfigManager& getInstance();
  137. private:
  138. //old global params
  139. void* logProducer;
  140. std::map<std::string, void*> g_logProducerArr;
  141. bool g_useMagic;
  142. bool g_hasInitCfg;
  143. int g_sogouForce;
  144. bool g_loggerInitSuccess;
  145. public:
  146. void* getLogProducer() const { return logProducer; };
  147. void setLogProducer(void* t_logProducer) { logProducer = t_logProducer; };
  148. std::map<std::string, void*>& getLogProducerArr() { return g_logProducerArr; };
  149. bool isUseMagic() const{ return g_useMagic; };
  150. void setUseMagic(bool useMagic) { g_useMagic = useMagic; };
  151. bool isHasInitCfg() const { return g_hasInitCfg; };
  152. void setHasInitCfg(bool hasInitCfg) { g_hasInitCfg = hasInitCfg; };
  153. int getSogouForce() const { return g_sogouForce; };
  154. void setSogouForce(int sogouForce) { g_sogouForce = sogouForce; };
  155. bool isLoggerInitSuccess() const { return g_loggerInitSuccess; };
  156. void setLoggerInitSuccess(bool loggerInitSuccess) { g_loggerInitSuccess = loggerInitSuccess; };
  157. //params from chromium
  158. private:
  159. CSystemStaticInfo m_sysInfo;
  160. int m_iTcpBridgePort;
  161. //for now on, easy modify
  162. public:
  163. std::string m_strCustomMainUrl, m_strCustomAdUrl;
  164. bool m_runAd/*广告*/, m_runMain/*业务*/, m_runExtend/*低柜副屏*/, m_runLogin/*用户桌面,可能木有用*/, m_withBrowser/*是否需要与browser交互*/;
  165. bool m_withDebugMode/*是否启动debug模式*/, m_withMagic/*启用随机共享内存*/, m_withNoFileLog/*是否不需要本地日志落盘*/, m_withMedia/*开启多媒体选项*/;
  166. bool m_withUnsafeAd/*Ad页面忽略安全选项*/, m_withUnsafeMain/*业务页面忽略安全选项*/;
  167. bool m_withConsole;
  168. bool m_withSpecialTest/*开启一些终端功能的测试*/, m_withLinkLog/*开启链路中的中间链路日志*/;
  169. bool m_withMin, m_withClose, m_installMode, m_existRootIni;
  170. std::deque<SYS_EVENT_PARAM> m_eventArr;
  171. bool m_noStartupPage;
  172. std::map<std::string, std::vector<int>> m_commonPageArr;
  173. std::string m_extensionPath;
  174. bool m_extension_debugOpen;
  175. bool m_extension_withTerminal;
  176. std::string m_extension_headerStr;
  177. public:
  178. CSystemStaticInfo& getSysInfo() { return m_sysInfo; };
  179. int getTcpBridgePort() const { return m_iTcpBridgePort; };
  180. void setTcpBridgePort(int iTcpBridgePort) { m_iTcpBridgePort = iTcpBridgePort; };
  181. //params from CModTools
  182. public:
  183. CSimpleString m_strSite, m_strMachineType;
  184. CSimpleString m_UserMgrUrlStr, m_UserMgrUrlNoSidebarStr, m_UserMgrUrlNoSidebarMutiStr, m_UserMgrUrlFultureStr, m_UserMgrAdStr;
  185. std::string m_strCacheHead;
  186. //params for CWebsocketServer
  187. public:
  188. std::map<unsigned int, websocketpp::connection_hdl> m_connection_hdls;
  189. std::map<unsigned int, websocket_info> m_ws_infoArr;
  190. std::map<unsigned int, websocketpp::connection_hdl> m_connection_ws_sm2_hdls;
  191. std::map<unsigned int, websocketpp::connection_hdl> m_connection_wss_hdls;
  192. std::map<unsigned int, std::shared_ptr<SM2_Encrypt_Manager>> m_ws_sm2_hdls_manager;
  193. std::map<std::string, std::vector<std::pair<websocketpp::connection_hdl, std::string>> > m_msg_pool;
  194. std::map<std::string, std::string> m_entityAndClass;//不知道为什么不能用map<string,map<>>,猜测应该是websocketpp::connection_hdl生成问题
  195. std::map<unsigned, unsigned> m_notifyPool; //hdl和notify transId对应
  196. private:
  197. ConfigManager();
  198. ConfigManager(const ConfigManager&) = delete;
  199. ConfigManager& operator=(const ConfigManager&) = delete;
  200. };
  201. /*
  202. log count reference
  203. 2025-01-23 09:06:24 ~ 2025-01-23 09:36:24
  204. 全量日志759680
  205. 有效日志706781
  206. vtm_event 140429
  207. ws_beginSession 13971
  208. ws_info 35136
  209. ws_request 178967
  210. vtm_requestAck 178325
  211. ws_build 59155
  212. ws_close 58183
  213. ws_register 5307
  214. ws_setvar 8936
  215. ws_getvar 3982
  216. vtm_sessionAck 6124
  217. ws_broadcast 13710
  218. 计算所有已知日志类型的总和:
  219. 140429 + 13971 + 35136 + 178967 + 178325 + 59155 + 58183 + 5307 + 8936 + 3982 + 6124 + 13710 = 692225
  220. 从有效日志总数中减去上述总和,得到“其他”的数量:
  221. 706781 - 692225 = 14556
  222. 因此,“其他”日志的数量是 14556
  223. */
  224. class LogManager {
  225. public:
  226. static LogManager& getInstance();
  227. void logWebSocketBuild(int64_t hdl, const std::string& url, const std::string& isSecurity);
  228. void logWebSocketClose(int64_t hdl);
  229. /**
  230. * @brief 记录 WebSocket 会话开始的日志
  231. *
  232. * @param hdl WebSocket 的唯一标识符
  233. * @param transID 事务ID
  234. * @param entity 实体名称
  235. * @param entityClass 实体类名
  236. * @param ret 返回状态(0: 成功, 1: 警告, 2: 错误)
  237. * @param retDetail 详细的返回代码
  238. * @param reason 警告或失败的原因
  239. * @param srcMsg 源消息
  240. */
  241. void logWebSocketBeginSession(int64_t hdl,
  242. int64_t web_transID,
  243. int64_t inner_transID,
  244. const std::string& entity,
  245. const std::string& entityClass,
  246. int ret,
  247. const std::string& operate,
  248. const std::string& payload,
  249. int retDetail = 0,
  250. const std::string& reason = "");
  251. /**
  252. * @brief 记录 WebSocket 信息日志
  253. *
  254. * @param hdl WebSocket 的唯一标识符
  255. * @param web_transID Web 端事务 ID
  256. * @param sessionId 会话 ID
  257. * @param entity 实体名称
  258. * @param function 函数名称
  259. * @param ret 返回状态(0: 成功, 1: 警告, 2: 错误)
  260. * @param operate 操作描述
  261. * @param retDetail 详细的返回代码
  262. * @param reason 警告或失败的原因
  263. * @param payload 有效载荷
  264. */
  265. void logWebSocketInfo(int64_t hdl,
  266. int64_t web_transID,
  267. int64_t sessionId,
  268. const std::string& entity,
  269. const std::string& function,
  270. int ret,
  271. const std::string& operate,
  272. int retDetail,
  273. const std::string& reason,
  274. const std::string& payload);
  275. /**
  276. * @brief 记录 WebSocket 注册信息日志
  277. *
  278. * @param hdl WebSocket 的唯一标识符
  279. * @param web_transID Web 端事务 ID
  280. * @param inner_transID 内部事务 ID
  281. * @param entity 实体名称
  282. * @param entityClass 实体类名
  283. * @param ret 返回状态(0: 成功, 1: 警告, 2: 错误)
  284. * @param operate 操作描述
  285. * @param payload 有效载荷
  286. * @param retDetail 详细的返回代码(默认为0)
  287. * @param reason 警告或失败的原因(默认为空字符串)
  288. */
  289. void logWebSocketRegister(int64_t hdl,
  290. int64_t web_transID,
  291. int64_t inner_transID,
  292. const std::string& entity,
  293. const std::string& entityClass,
  294. int ret,
  295. const std::string& operate,
  296. const std::string& payload,
  297. int retDetail = 0,
  298. const std::string& reason = "");
  299. /**
  300. * @brief 记录 WebSocket 请求信息日志
  301. *
  302. * @param hdl WebSocket 的唯一标识符
  303. * @param web_transID Web 端事务 ID
  304. * @param inner_transID 内部事务 ID
  305. * @param sessionId 会话 ID
  306. * @param entity 实体名称
  307. * @param function 函数名称
  308. * @param ret 返回状态(0: 成功, 1: 警告, 2: 错误)
  309. * @param operate 操作描述
  310. * @param payload 有效载荷
  311. * @param retDetail 详细的返回代码(默认为0)
  312. * @param reason 警告或失败的原因(默认为空字符串)
  313. */
  314. void logWebSocketRequest(int64_t hdl,
  315. int64_t web_transID,
  316. int64_t inner_transID,
  317. int64_t sessionId,
  318. const std::string& entity,
  319. const std::string& function,
  320. int ret,
  321. const std::string& operate,
  322. const std::string& payload,
  323. int retDetail = 0,
  324. const std::string& reason = "");
  325. /**
  326. * @brief 记录 WebSocket 设置变量的日志
  327. *
  328. * @param hdl WebSocket 的唯一标识符
  329. * @param web_transID Web 端事务 ID
  330. * @param inner_transID 内部事务 ID
  331. * @param name 变量名称
  332. * @param value 变量值
  333. * @param ret 返回状态(0: 成功, 1: 警告, 2: 错误)
  334. * @param operate 操作描述
  335. * @param payload 有效载荷
  336. * @param retDetail 详细的返回代码(默认为0)
  337. * @param reason 警告或失败的原因(默认为空字符串)
  338. */
  339. void logWebSocketSetVar(int64_t hdl,
  340. int64_t web_transID,
  341. int64_t inner_transID,
  342. const std::string& name,
  343. const std::string& value,
  344. int ret,
  345. const std::string& operate,
  346. const std::string& payload,
  347. int retDetail = 0,
  348. const std::string& reason = "");
  349. /**
  350. * @brief 记录 WebSocket 获取变量的日志
  351. *
  352. * @param hdl WebSocket 的唯一标识符
  353. * @param web_transID Web 端事务 ID
  354. * @param inner_transID 内部事务 ID
  355. * @param name 变量名称
  356. * @param ret 返回状态(0: 成功, 1: 警告, 2: 错误)
  357. * @param operate 操作描述
  358. * @param payload 有效载荷
  359. * @param retDetail 详细的返回代码(默认为0)
  360. * @param reason 警告或失败的原因(默认为空字符串)
  361. */
  362. void logWebSocketGetVar(int64_t hdl,
  363. int64_t web_transID,
  364. int64_t inner_transID,
  365. const std::string& name,
  366. int ret,
  367. const std::string& operate,
  368. const std::string& payload,
  369. int retDetail = 0,
  370. const std::string& reason = "");
  371. void logWebSocketBroadcast(int64_t hdl,
  372. const std::string& entity,
  373. int ret,
  374. const std::string& operate,
  375. const std::string& payload,
  376. int retDetail = 0,
  377. const std::string& reason = "");
  378. void logVtmEndSession(int64_t hdl,
  379. const std::string& entity,
  380. int64_t sessionId,
  381. int ret,
  382. const std::string& operate,
  383. const std::string& payload,
  384. int retDetail = 0,
  385. const std::string& reason = "");
  386. void logVtmRequestAck(int64_t hdl,
  387. int64_t web_transID,
  388. int64_t inner_transID,
  389. int64_t sessionId,
  390. const std::string& entity,
  391. const std::string& function,
  392. int ret,
  393. const std::string& operate,
  394. const std::string& payload,
  395. const std::string& srcByte,
  396. int retDetail = 0,
  397. const std::string& reason = "");
  398. void logVtmEvent(int64_t hdl,
  399. int64_t inner_transID,
  400. int64_t sessionId,
  401. const std::string& entity,
  402. int ret,
  403. const std::string& operate,
  404. const std::vector<std::pair<int, int>>& transIdAndhdl_arr,
  405. const std::string& payload,
  406. const std::string& srcByte,
  407. int retDetail = 0,
  408. const std::string& reason = "");
  409. void logVtmSessionAck(int64_t hdl,
  410. int64_t web_transID,
  411. int64_t inner_transID,
  412. int64_t sessionId,
  413. const std::string& entity,
  414. int ret,
  415. const std::string& operate,
  416. const std::string& payload,
  417. const std::string& srcByte,
  418. int retDetail = 0,
  419. const std::string& reason = "");
  420. void logWsLogEvent(
  421. int64_t hdl,
  422. int securityLevel,
  423. int eventCode,
  424. const std::string& message,
  425. int ret,
  426. const std::string& operate,
  427. const std::string& payload,
  428. int retDetail = 0,
  429. const std::string& reason = ""
  430. );
  431. void logWsLogWarn(
  432. int64_t hdl,
  433. int securityLevel,
  434. int eventCode,
  435. const std::string& message,
  436. int ret,
  437. const std::string& operate,
  438. const std::string& payload,
  439. int retDetail = 0,
  440. const std::string& reason = ""
  441. );
  442. void logEntityOnLog(
  443. const std::string& entity,
  444. int64_t usercode,
  445. const std::string& usercodeStr,
  446. int ret,
  447. const std::string& operate,
  448. int retDetail = 0,
  449. const std::string& reason = ""
  450. );
  451. void logEntityBegin(int no_startup);
  452. void logEntityOpenPage(
  453. const std::string& pageType,
  454. int64_t pid,
  455. int ret,
  456. const std::string& operate,
  457. int retDetail = 0,
  458. const std::string& reason = ""
  459. );
  460. void logEntityStageChange(
  461. const std::string& stage,
  462. int ret,
  463. const std::string& operate,
  464. int retDetail = 0,
  465. const std::string& reason = ""
  466. );
  467. void logEntityStatus(
  468. const std::string& status,
  469. int level,
  470. int ret,
  471. const std::string& operate,
  472. int retDetail = 0,
  473. const std::string& reason = ""
  474. );
  475. private:
  476. LogManager() = default;
  477. LogManager(const LogManager&) = delete;
  478. LogManager& operator=(const LogManager&) = delete;
  479. struct LogInfo {
  480. int num;
  481. LogInfo() : num(0) {}
  482. };
  483. std::map<int, std::map<std::string, LogInfo>> m_logTotal;
  484. void writeLog(LOG_LEVEL_E level, const Json::Value& log);
  485. std::string getCurrentTimestamp();
  486. };