#pragma once #include "SpBase.h" #include #include #include #include #include "websocketpp/config/asio.hpp" #include "websocketpp/server.hpp" #include "json/json.h" #define DEFAULT_KEY_LEN 256 #define DEFAULT_KEY_LEN 256 #define HEADER_TERMINALNO_NAME "vtm-terminalno" #define SUC_RETURN_STR "SUC0000" #define RTA_UNKNOW_ERR "RTA42F2" std::vector find_files(const std::string srcPath, const std::string fileName, bool isDir = false); std::string generateTimeStr(bool isSimple = false); DWORD SaveCefclientLog(std::string magicStr); bool SYSTEM_ON(std::string cmdLine, bool isWait = true); bool InitFileLogger(std::string dbgPath); void DbgToFileLogger(std::string msg); bool deleteDir_byFileSystem(const std::string srcPath); void InitTranslateFile(std::string srcFile); std::pair splitStrToUserCodeAndErrMsg(std::string srcMsg); void InitUserCodeToMsgTip(CAutoArray& strErrorCodeArr, CAutoArray& strDescriptionArr, CAutoArray& strRemarkArr); bool isMsgTipExist(); struct ErrMsgStruct { std::string VTMCode; std::string errMsg; std::string userCode; // 带参数的构造函数,参数有默认值 ErrMsgStruct(const std::string& tmp_VTMCode = "", const std::string& tmp_errMsg = "", const std::string& tmp_userCode = "") : VTMCode(tmp_VTMCode), errMsg(tmp_errMsg), userCode(tmp_userCode) {} // 拷贝构造函数 ErrMsgStruct(const ErrMsgStruct& other) : VTMCode(other.VTMCode), errMsg(other.errMsg), userCode(other.userCode) {} // 移动构造函数 ErrMsgStruct(ErrMsgStruct&& other) noexcept : VTMCode(std::move(other.VTMCode)), errMsg(std::move(other.errMsg)), userCode(std::move(other.userCode)) {} }; std::pair getErrMsgByRemark(std::string srcMsg, bool isSafe); std::string UtfToGbk(const char* utf8); void hexdump(const char* buf, const int num); std::string hexdumpToString(const char* buf, const int num); bool modifyBySpecialStr(std::wstring& src); void receivehexdump(const char* buf, const int num); void doWithDebugModeData(const char* strMethod, const char* buf); #ifdef RVC_OS_LINUX std::vector getUosBrowserPIDs(int UseUOSBrowser); #endif class json_deal { public: static std::pair getStringFromCjsonObj(void* object, const char* name); static std::pair getIntergerFromCjsonObj(void* object, const char* name); static std::pair getDoubleFromCjsonObj(void* object, const char* name); }; #if (defined _WIN32 || defined _WIN64) long WINAPI printSEG(struct _EXCEPTION_POINTERS* ExceptionInfo); #else long printSEG(); void set_traceback_path(std::string path); void seg_signal_handler(int signum); void normal_signal_handle(int signum); #endif typedef enum{ WAIT_KEY_EXCHANGE, DATA_EXCHANGE }SM2ProtocolState; class SM2_Encrypt_Manager { public: SM2_Encrypt_Manager(); ~SM2_Encrypt_Manager(); bool GenerateSM2_key(std::string& pub, std::string& pri); std::string binToHex(const unsigned char* data, int len); void hexToBin(const std::string& hexStr, unsigned char* binData, int& binLen); void setOpposite(const std::string pub); std::pair checkSM2Enable(); std::pair EncryptMsg(std::string msg);//return signature, encryptMsg std::pair DecryptMsg(std::string sign, std::string msg); std::pair GenerateEncPubKey(); bool verifySM2Manager(); SM2ProtocolState getState() { return m_state; }; void setState(SM2ProtocolState tmpState) { m_state = tmpState; }; private: unsigned char m_public_key_buf[DEFAULT_KEY_LEN], m_private_key_buf[DEFAULT_KEY_LEN], m_opposite_public_key_buf[DEFAULT_KEY_LEN]; int m_public_key_buf_len, m_private_key_buf_len, m_opposite_public_key_buf_len; std::string m_public_key, m_private_key, m_opposite_public_key; SM2ProtocolState m_state; }; struct SYS_EVENT_PARAM { std::string key; std::string value; std::string oldValue; std::string entityName; // 默认构造函数 SYS_EVENT_PARAM() = default; // 带参数的构造函数 SYS_EVENT_PARAM(const std::string& k, const std::string& v, const std::string& ov, const std::string& en) : key(k), value(v), oldValue(ov), entityName(en) {} // 拷贝构造函数 SYS_EVENT_PARAM(const SYS_EVENT_PARAM& other) : key(other.key), value(other.value), oldValue(other.oldValue), entityName(other.entityName) {} // 拷贝赋值运算符 SYS_EVENT_PARAM& operator=(const SYS_EVENT_PARAM& other) { if (this != &other) { key = other.key; value = other.value; oldValue = other.oldValue; entityName = other.entityName; } return *this; } // 禁用移动构造函数 SYS_EVENT_PARAM(SYS_EVENT_PARAM&&) = delete; // 禁用移动赋值运算符 SYS_EVENT_PARAM& operator=(SYS_EVENT_PARAM&&) = delete; }; struct websocket_info { std::string name; bool isSafe; // 默认构造函数:初始化为默认值 websocket_info() : name(""), isSafe(false) {} // 带参数的构造函数:根据传入的参数初始化 websocket_info(const std::string& n, bool s) : name(n), isSafe(s) {} }; class ConfigManager { public: static ConfigManager& getInstance(); private: //old global params void* logProducer; std::map g_logProducerArr; bool g_useMagic; bool g_hasInitCfg; int g_sogouForce; bool g_loggerInitSuccess; public: void* getLogProducer() const { return logProducer; }; void setLogProducer(void* t_logProducer) { logProducer = t_logProducer; }; std::map& getLogProducerArr() { return g_logProducerArr; }; bool isUseMagic() const{ return g_useMagic; }; void setUseMagic(bool useMagic) { g_useMagic = useMagic; }; bool isHasInitCfg() const { return g_hasInitCfg; }; void setHasInitCfg(bool hasInitCfg) { g_hasInitCfg = hasInitCfg; }; int getSogouForce() const { return g_sogouForce; }; void setSogouForce(int sogouForce) { g_sogouForce = sogouForce; }; bool isLoggerInitSuccess() const { return g_loggerInitSuccess; }; void setLoggerInitSuccess(bool loggerInitSuccess) { g_loggerInitSuccess = loggerInitSuccess; }; //params from chromium private: CSystemStaticInfo m_sysInfo; int m_iTcpBridgePort; //for now on, easy modify public: std::string m_strCustomMainUrl, m_strCustomAdUrl; bool m_runAd/*广告*/, m_runMain/*业务*/, m_runExtend/*低柜副屏*/, m_runLogin/*用户桌面,可能木有用*/, m_withBrowser/*是否需要与browser交互*/; bool m_withDebugMode/*是否启动debug模式*/, m_withMagic/*启用随机共享内存*/, m_withNoFileLog/*是否不需要本地日志落盘*/, m_withMedia/*开启多媒体选项*/; bool m_withUnsafeAd/*Ad页面忽略安全选项*/, m_withUnsafeMain/*业务页面忽略安全选项*/; bool m_withConsole; bool m_withSpecialTest/*开启一些终端功能的测试*/, m_withLinkLog/*开启链路中的中间链路日志*/; bool m_withMin, m_withClose, m_installMode, m_existRootIni; std::deque m_eventArr; bool m_noStartupPage; std::map> m_commonPageArr; std::string m_extensionPath; bool m_extension_debugOpen; bool m_extension_withTerminal; std::string m_extension_headerStr; public: CSystemStaticInfo& getSysInfo() { return m_sysInfo; }; int getTcpBridgePort() const { return m_iTcpBridgePort; }; void setTcpBridgePort(int iTcpBridgePort) { m_iTcpBridgePort = iTcpBridgePort; }; //params from CModTools public: CSimpleString m_strSite, m_strMachineType; CSimpleString m_UserMgrUrlStr, m_UserMgrUrlNoSidebarStr, m_UserMgrUrlNoSidebarMutiStr, m_UserMgrUrlFultureStr, m_UserMgrAdStr; std::string m_strCacheHead; //params for CWebsocketServer public: std::map m_connection_hdls; std::map m_ws_infoArr; std::map m_connection_ws_sm2_hdls; std::map m_connection_wss_hdls; std::map> m_ws_sm2_hdls_manager; std::map> > m_msg_pool; std::map m_entityAndClass;//不知道为什么不能用map>,猜测应该是websocketpp::connection_hdl生成问题 std::map m_notifyPool; //hdl和notify transId对应 private: ConfigManager(); ConfigManager(const ConfigManager&) = delete; ConfigManager& operator=(const ConfigManager&) = delete; }; /* log count reference 2025-01-23 09:06:24 ~ 2025-01-23 09:36:24 全量日志759680 有效日志706781 vtm_event 140429 ws_beginSession 13971 ws_info 35136 ws_request 178967 vtm_requestAck 178325 ws_build 59155 ws_close 58183 ws_register 5307 ws_setvar 8936 ws_getvar 3982 vtm_sessionAck 6124 ws_broadcast 13710 计算所有已知日志类型的总和: 140429 + 13971 + 35136 + 178967 + 178325 + 59155 + 58183 + 5307 + 8936 + 3982 + 6124 + 13710 = 692225 从有效日志总数中减去上述总和,得到“其他”的数量: 706781 - 692225 = 14556 因此,“其他”日志的数量是 14556 */ class LogManager { public: static LogManager& getInstance(); void logWebSocketBuild(int64_t hdl, const std::string& url, const std::string& isSecurity); void logWebSocketClose(int64_t hdl); /** * @brief 记录 WebSocket 会话开始的日志 * * @param hdl WebSocket 的唯一标识符 * @param transID 事务ID * @param entity 实体名称 * @param entityClass 实体类名 * @param ret 返回状态(0: 成功, 1: 警告, 2: 错误) * @param retDetail 详细的返回代码 * @param reason 警告或失败的原因 * @param srcMsg 源消息 */ void logWebSocketBeginSession(int64_t hdl, int64_t web_transID, int64_t inner_transID, const std::string& entity, const std::string& entityClass, int ret, const std::string& operate, const std::string& payload, int retDetail = 0, const std::string& reason = ""); /** * @brief 记录 WebSocket 信息日志 * * @param hdl WebSocket 的唯一标识符 * @param web_transID Web 端事务 ID * @param sessionId 会话 ID * @param entity 实体名称 * @param function 函数名称 * @param ret 返回状态(0: 成功, 1: 警告, 2: 错误) * @param operate 操作描述 * @param retDetail 详细的返回代码 * @param reason 警告或失败的原因 * @param payload 有效载荷 */ void logWebSocketInfo(int64_t hdl, int64_t web_transID, int64_t sessionId, const std::string& entity, const std::string& function, int ret, const std::string& operate, int retDetail, const std::string& reason, const std::string& payload); /** * @brief 记录 WebSocket 注册信息日志 * * @param hdl WebSocket 的唯一标识符 * @param web_transID Web 端事务 ID * @param inner_transID 内部事务 ID * @param entity 实体名称 * @param entityClass 实体类名 * @param ret 返回状态(0: 成功, 1: 警告, 2: 错误) * @param operate 操作描述 * @param payload 有效载荷 * @param retDetail 详细的返回代码(默认为0) * @param reason 警告或失败的原因(默认为空字符串) */ void logWebSocketRegister(int64_t hdl, int64_t web_transID, int64_t inner_transID, const std::string& entity, const std::string& entityClass, int ret, const std::string& operate, const std::string& payload, int retDetail = 0, const std::string& reason = ""); /** * @brief 记录 WebSocket 请求信息日志 * * @param hdl WebSocket 的唯一标识符 * @param web_transID Web 端事务 ID * @param inner_transID 内部事务 ID * @param sessionId 会话 ID * @param entity 实体名称 * @param function 函数名称 * @param ret 返回状态(0: 成功, 1: 警告, 2: 错误) * @param operate 操作描述 * @param payload 有效载荷 * @param retDetail 详细的返回代码(默认为0) * @param reason 警告或失败的原因(默认为空字符串) */ void logWebSocketRequest(int64_t hdl, int64_t web_transID, int64_t inner_transID, int64_t sessionId, const std::string& entity, const std::string& function, int ret, const std::string& operate, const std::string& payload, int retDetail = 0, const std::string& reason = ""); /** * @brief 记录 WebSocket 设置变量的日志 * * @param hdl WebSocket 的唯一标识符 * @param web_transID Web 端事务 ID * @param inner_transID 内部事务 ID * @param name 变量名称 * @param value 变量值 * @param ret 返回状态(0: 成功, 1: 警告, 2: 错误) * @param operate 操作描述 * @param payload 有效载荷 * @param retDetail 详细的返回代码(默认为0) * @param reason 警告或失败的原因(默认为空字符串) */ void logWebSocketSetVar(int64_t hdl, int64_t web_transID, int64_t inner_transID, const std::string& name, const std::string& value, int ret, const std::string& operate, const std::string& payload, int retDetail = 0, const std::string& reason = ""); /** * @brief 记录 WebSocket 获取变量的日志 * * @param hdl WebSocket 的唯一标识符 * @param web_transID Web 端事务 ID * @param inner_transID 内部事务 ID * @param name 变量名称 * @param ret 返回状态(0: 成功, 1: 警告, 2: 错误) * @param operate 操作描述 * @param payload 有效载荷 * @param retDetail 详细的返回代码(默认为0) * @param reason 警告或失败的原因(默认为空字符串) */ void logWebSocketGetVar(int64_t hdl, int64_t web_transID, int64_t inner_transID, const std::string& name, int ret, const std::string& operate, const std::string& payload, int retDetail = 0, const std::string& reason = ""); void logWebSocketBroadcast(int64_t hdl, const std::string& entity, int ret, const std::string& operate, const std::string& payload, int retDetail = 0, const std::string& reason = ""); void logVtmEndSession(int64_t hdl, const std::string& entity, int64_t sessionId, int ret, const std::string& operate, const std::string& payload, int retDetail = 0, const std::string& reason = ""); void logVtmRequestAck(int64_t hdl, int64_t web_transID, int64_t inner_transID, int64_t sessionId, const std::string& entity, const std::string& function, int ret, const std::string& operate, const std::string& payload, const std::string& srcByte, int retDetail = 0, const std::string& reason = ""); void logVtmEvent(int64_t hdl, int64_t inner_transID, int64_t sessionId, const std::string& entity, int ret, const std::string& operate, const std::vector>& transIdAndhdl_arr, const std::string& payload, const std::string& srcByte, int retDetail = 0, const std::string& reason = ""); void logVtmSessionAck(int64_t hdl, int64_t web_transID, int64_t inner_transID, int64_t sessionId, const std::string& entity, int ret, const std::string& operate, const std::string& payload, const std::string& srcByte, int retDetail = 0, const std::string& reason = ""); void logWsLogEvent( int64_t hdl, int securityLevel, int eventCode, const std::string& message, int ret, const std::string& operate, const std::string& payload, int retDetail = 0, const std::string& reason = "" ); void logWsLogWarn( int64_t hdl, int securityLevel, int eventCode, const std::string& message, int ret, const std::string& operate, const std::string& payload, int retDetail = 0, const std::string& reason = "" ); void logEntityOnLog( const std::string& entity, int64_t usercode, const std::string& usercodeStr, int ret, const std::string& operate, int retDetail = 0, const std::string& reason = "" ); void logEntityBegin(int no_startup); void logEntityOpenPage( const std::string& pageType, int64_t pid, int ret, const std::string& operate, int retDetail = 0, const std::string& reason = "" ); void logEntityStageChange( const std::string& stage, int ret, const std::string& operate, int retDetail = 0, const std::string& reason = "" ); void logEntityStatus( const std::string& status, int level, int ret, const std::string& operate, int retDetail = 0, const std::string& reason = "" ); private: LogManager() = default; LogManager(const LogManager&) = delete; LogManager& operator=(const LogManager&) = delete; struct LogInfo { int num; LogInfo() : num(0) {} }; std::map> m_logTotal; void writeLog(LOG_LEVEL_E level, const Json::Value& log); std::string getCurrentTimestamp(); };