baseEx.h 15 KB

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