baseEx.h 16 KB

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