123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- #pragma once
- #include "SpBase.h"
- #include<vector>
- #include<string>
- #include<map>
- #include<deque>
- #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<std::string> 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<unsigned long, std::string> splitStrToUserCodeAndErrMsg(std::string srcMsg);
- void InitUserCodeToMsgTip(CAutoArray<CSimpleStringA>& strErrorCodeArr, CAutoArray<CSimpleStringA>& strDescriptionArr, CAutoArray<CSimpleStringA>& 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<unsigned long, ErrMsgStruct> 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<int> getUosBrowserPIDs(int UseUOSBrowser);
- #endif
- class json_deal {
- public:
- static std::pair<bool, CSimpleStringA> getStringFromCjsonObj(void* object, const char* name);
- static std::pair<bool, int> getIntergerFromCjsonObj(void* object, const char* name);
- static std::pair<bool, double> 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<bool, std::string> checkSM2Enable();
- std::pair<std::string, std::string> EncryptMsg(std::string msg);//return signature, encryptMsg
- std::pair<bool, std::string> DecryptMsg(std::string sign, std::string msg);
- std::pair<bool, std::string> 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<std::string, void*> 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<std::string, void*>& 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<SYS_EVENT_PARAM> m_eventArr;
- bool m_noStartupPage;
- std::map<std::string, std::vector<int>> 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<unsigned int, websocketpp::connection_hdl> m_connection_hdls;
- std::map<unsigned int, websocket_info> m_ws_infoArr;
- std::map<unsigned int, websocketpp::connection_hdl> m_connection_ws_sm2_hdls;
- std::map<unsigned int, websocketpp::connection_hdl> m_connection_wss_hdls;
- std::map<unsigned int, std::shared_ptr<SM2_Encrypt_Manager>> m_ws_sm2_hdls_manager;
- std::map<std::string, std::vector<std::pair<websocketpp::connection_hdl, std::string>> > m_msg_pool;
- std::map<std::string, std::string> m_entityAndClass;//不知道为什么不能用map<string,map<>>,猜测应该是websocketpp::connection_hdl生成问题
- std::map<unsigned, unsigned> 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<std::pair<int, int>>& 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<int, std::map<std::string, LogInfo>> m_logTotal;
- void writeLog(LOG_LEVEL_E level, const Json::Value& log);
- std::string getCurrentTimestamp();
- };
|