Browse Source

Z991239-4421 #comment [AccessAuth] 时间同步告警添加

80374374 2 years ago
parent
commit
0a37705a72

+ 21 - 0
CMakePresets.json

@@ -22,6 +22,27 @@
         "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": { "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}" }
       }
     },
+    {
+      "name": "linux-release",
+      "displayName": "Linux Release",
+      "description": "面向适用于 Linux 的 Windows 子系统(WSL)或远程 Linux 系统。",
+      "generator": "Unix Makefiles",
+      "binaryDir": "${sourceDir}/out/build/${presetName}",
+      "cacheVariables": {
+        "CMAKE_BUILD_TYPE": "Release",
+        "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
+        "SIMULATE_ON": {
+          "value": "False",
+          "type": "BOOL"
+        },
+        "BUILD_TESTING": "OFF",
+        "BUILD_DEVADAPTER": "ON"
+      },
+      "vendor": {
+        "microsoft.com/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Linux" ] },
+        "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": { "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}" }
+      }
+    },
     {
       "name": "windows-default",
       "displayName": "Windows x64 Debug",

+ 23 - 30
Module/mod_accessauth/AccessAuthConn.cpp

@@ -9,7 +9,7 @@
 #include <windows.h>
 #include <Wincrypt.h>
 #endif // RVC_OS_WIN
-
+#include "Event.h"
 #include <fstream>
 using namespace std;
 
@@ -1336,43 +1336,36 @@ DWORD CAccessAuthConn::HandleUpdateMD5Ret(const CSmartPointer<IPackage> &pRecvPk
 	ErrorCodeEnum rc = Error_Succeed;
 	DWORD dwSysCode, dwUserCode;
 	string strErrMsg;
-
-	/*if (pRecvPkg->GetErrMsg(dwSysCode, dwUserCode, strErrMsg))
-	{
-		rc = (ErrorCodeEnum)dwSysCode;
-		m_pFSM->doWarnMsg(dwUserCode, strErrMsg);
-		CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();
-	}
-
-	m_pFSM->PostEventFIFO(new FSMEvent(rc==Error_Succeed ? CAccessAuthFSM::Event_CheckMD5Succ:CAccessAuthFSM::Event_CheckMD5Fail));*/
-
 	return rc;
 }
 DWORD CAccessAuthConn::HandleTimeSyn(int nTimeDiff,BYTE nAuthVersion,BYTE* nSessionKey) {
-	// 比较终端和服务器时间, 时差小于3分钟不纠正	
-	DWORD dwTimeDiff = nTimeDiff;
-	if (dwTimeDiff > 180)
-	{
-		Dbg("time diff is too large (%ds), sync time now", dwTimeDiff);
-
-		CSmallDateTime dtServerTime(CSmallDateTime::GetNow() + dwTimeDiff);
+	// 比较终端和服务器时间, 时差小于3分钟(默认,可通过集中配置配置)不纠正	
+	const long dwTimeDiff = nTimeDiff > 0 ? nTimeDiff : 0 - nTimeDiff;
+	const long torelateTime = m_torelateDiffSyncTimeSecs > 0 ? m_torelateDiffSyncTimeSecs : 0 - m_torelateDiffSyncTimeSecs;
+	if (torelateTime < dwTimeDiff) {
+		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleTimeSyn")
+			("time diff is too large (%ds), sync time now", nTimeDiff);
+
+		CSmallDateTime dtServerTime((DWORD)(CSmallDateTime::GetNow()) + nTimeDiff);
 		SYSTEMTIME stServerTime = dtServerTime.ToSystemTime();
 #ifdef RVC_OS_WIN
-		if (SetLocalTime(&stServerTime))
+		if (SetLocalTime(&stServerTime)) {
 #else
-		if (set_system_time_by_sec(dwTimeDiff))
+		if (set_system_time_by_sec(dwTimeDiff)) {
 #endif // RVC_OS_WIN
-			Dbg("sync time with server succeed, server time: [%s]", (const char*)dtServerTime.ToTimeString());
-		else
-		{
-			//LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_SET_LOCALE_TIME,
-			//	GetOutPutStr("%s%s", "stServerTime", dtServerTime.ToTimeString()).c_str());
-			return ERR_ACCESSAUTH_SET_LOCALE_TIME;
+			DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleTimeSyn")
+				("sync time with server succeed, server time: [%s]", (const char*)dtServerTime.ToTimeString());
+			LogWarn(Severity_Low, Error_Debug, AccessAuthorization_UserErrorCode_Sync_Time_Succ,
+				CSimpleStringA::Format("sync time succ:  server time: [%s],diff[%ld],threshold:[%d]", (const char*)dtServerTime.ToTimeString(), nTimeDiff, m_torelateDiffSyncTimeSecs));
+		} else {
+			LogWarn(Severity_Middle, Error_Unexpect, AccessAuthorization_UserErrorCode_Sync_Time_Failed,
+				CSimpleStringA::Format("sync time failed:  server time: [%s],diff[%ld],threshold:[%d](GLE=%u)",
+					(const char*)dtServerTime.ToTimeString(), nTimeDiff, m_torelateDiffSyncTimeSecs, GetLastError()));
+			return Error_Unexpect;
 		}
-	}
-	else
-	{
-		Dbg("time diff is acceptable (%ds)", dwTimeDiff);
+	} else {
+		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("HandleTimeSyn")
+			("time diff is acceptable (%lds), threshold(%d),", nTimeDiff, m_torelateDiffSyncTimeSecs);
 	}
 
 	// 检查准入请求版本 //会话密钥缓存

+ 3 - 0
Module/mod_accessauth/AccessAuthConn.h

@@ -307,6 +307,9 @@ public:
 		CSimpleStringA& strModel, CSimpleStringA& strVendor, CSimpleStringA& strVersion);
 	DWORD GetAllDevices(CEntityBase* pEntity, CAutoArray<CSimpleStringA>& devs);
 	SpReqAnsContext<AccessAuthService_InitDev_Req, AccessAuthService_InitDev_Ans>::Pointer m_ctxInitDev;
+public:
+	/** 两边差异太大了,后续再合并吧 Gifur@2023424]*/
+	int m_torelateDiffSyncTimeSecs;
 private:
 	CAccessAuthFSM *m_pFSM;
 };

+ 17 - 2
Module/mod_accessauth/AccessAuthFSM.cpp

@@ -21,7 +21,7 @@
 #endif
 
 CAccessAuthFSM::CAccessAuthFSM()
-	:m_pConnection(NULL),m_bAccessACS(false), m_fNetworkChecking(false)
+	:m_pConnection(NULL),m_bAccessACS(false), m_fNetworkChecking(false), m_torelateDiffSyncTimeSecs(180)
 {
 	m_nAccessFailedCount = 0;
 }
@@ -50,8 +50,10 @@ ErrorCodeEnum CAccessAuthFSM::OnInit()
 	if (Error != Error_Succeed) 
 	{
 		LOG_TRACE("load CenterSetting.ini failed!");
-	}	
+	}
+    GetDiffSyncTimeFromCenterSettings();
 	m_pConnection = new CAccessAuthConn(m_pEntity, this);
+    m_pConnection->m_torelateDiffSyncTimeSecs = m_torelateDiffSyncTimeSecs;
 	return Error_Succeed;
 }
 
@@ -1347,6 +1349,7 @@ void CAccessAuthFSM::s3_on_entry()
 
 void CAccessAuthFSM::s3_on_exit()
 {
+    LOG_FUNCTION();
 }
 
 unsigned int CAccessAuthFSM::s3_on_event(FSMEvent* event)
@@ -1413,6 +1416,18 @@ DWORD CAccessAuthFSM::InitDevice(SpReqAnsContext<AccessAuthService_InitDev_Req,
 	return Error_Succeed;
 }
 
+void CAccessAuthFSM::GetDiffSyncTimeFromCenterSettings()
+{
+	CSmartPointer<IConfigInfo> spConfig;
+	GetEntityBase()->GetFunction()->OpenConfig(Config_CenterSetting, spConfig);
+	int nValue(0);
+	spConfig->ReadConfigValueInt(GetEntityBase()->GetEntityName(), "SyncTimeThreshold", nValue);
+	if (nValue != 0) {
+		m_torelateDiffSyncTimeSecs = nValue;
+		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_BUSINESS_SYSTEM)("Fetch SyncTimeThreshold from CS returns: %d", m_torelateDiffSyncTimeSecs);
+	}
+}
+
 DWORD CAccessAuthFSM::SyncTime()
 {
 	auto rc = SecureClientConnect();

+ 12 - 93
Module/mod_accessauth/AccessAuthFSM.h

@@ -621,20 +621,7 @@ typedef struct CInitlizerMKRet : CHTTPRet {
 class MyMutex;
 class CAccessAuthFSM : public FSMImpl<CAccessAuthFSM>, public IFSMStateHooker
 {
-public:	
-	/*struct ReportStateEvent : public FSMEvent
-	{
-		ReportStateEvent(char cNewStage, DWORD dwNewStageTime, char cOldStage, DWORD dwOldStageTime):
-			FSMEvent(Event_ReportStage), cNewStage(cNewStage), dwNewStageTime(dwNewStageTime),
-			cOldStage(cOldStage), dwOldStageTime(dwOldStageTime)
-		{}
-
-		char cNewStage;
-		char cOldStage;
-		DWORD dwNewStageTime;
-		DWORD dwOldStageTime;
-	};*/
-
+public:
 	CAccessAuthFSM();
 	virtual ~CAccessAuthFSM();
 
@@ -642,67 +629,11 @@ public:
 	virtual ErrorCodeEnum OnInit();
 	virtual ErrorCodeEnum OnExit();
 
-	//enum{s1, s2, s3, s4, s5, s6, s7};
-
-	//enum
-	//{
-	//	Event_StartRegist = EVT_USER+1,
-	//	Event_StartReregist,	
-	//	Event_ConnectionOK,
-	//	Event_EndSyncTime,
-	//	Event_UpdateWKSucc,
-	//	Event_IgnoreUpdateWK,
-	//	Event_UpdateWKFail,
-	//	Event_ReqTokenSucc,
-	//	Event_ReqTokenFail,
-	//	Event_ReqTokenCancel,
-	//	Event_StartUnregist,
-	//	Event_LostTrust,
-	//	Event_StateTimeout,		// 临时状态超时
-	//	Event_ReportStage,
-	//	Event_CheckMD5Succ,
-	//	Event_CheckMD5Fail,
-	//	Event_InitFinishOK,
-	//	Event_GetHsotFailed,
-	//	Event_NetworkIllegal,
-	//	Event_SyncTimeFailed  //同步时间失败
-	//};
-
-	//BEGIN_FSM_STATE(CAccessAuthFSM)
-	//	FSM_STATE_ENTRY(s1, "Isolate",s1_on_entry,s1_on_exit,s1_on_event)
-	//	FSM_STATE_ENTRY(s2, "Checking",s2_on_entry,s2_on_exit,s2_on_event) //开始准入
-	//	FSM_STATE_ENTRY(s3, "Failure",s3_on_entry,s3_on_exit,s3_on_event) //准入失败
-	//	FSM_STATE_ENTRY(s4, "Cancel", s4_on_entry, s4_on_exit, s4_on_event) //准入超时
-	//	FSM_STATE_ENTRY(s5, "Login", s5_on_entry, s5_on_exit, s5_on_event) //准入成功
-	//	FSM_STATE_ENTRY(s6, "Leaving", s6_on_entry, s6_on_exit, s6_on_event)
-	//	FSM_STATE_ENTRY(s7, "LostTrust", s7_on_entry, s7_on_exit, s7_on_event)
-	//END_FSM_STATE()
-
-	//BEGIN_FSM_RULE(CAccessAuthFSM,s1)
-	//	FSM_RULE_ENTRY_ANY(s1, s2, Event_StartRegist)
-	//	FSM_RULE_ENTRY_ANY(s1, s3, Event_GetHsotFailed)
-	//	//FSM_RULE_ENTRY_ANY(s2, s3, Event_UpdateWKFail)
-	//	FSM_RULE_ENTRY_ANY(s2, s3, Event_ReqTokenFail)
-	//	FSM_RULE_ENTRY_ANY(s2, s3, Event_CheckMD5Fail)
-	//	FSM_RULE_ENTRY_ANY(s2, s3, Event_NetworkIllegal)
-	//	FSM_RULE_ENTRY_ANY(s2, s4, Event_ReqTokenCancel)
-	//	FSM_RULE_ENTRY_ANY(s2, s5, Event_ReqTokenSucc)
-	//	FSM_RULE_ENTRY_ANY(s2, s3, Event_SyncTimeFailed)
-	//	FSM_RULE_ENTRY_ANY(s3, s2, Event_StartRegist)
-	//	FSM_RULE_ENTRY_ANY(s4, s1, Event_StateTimeout)
-	//	FSM_RULE_ENTRY_ANY(s5, s6, Event_StartUnregist)			
-	//	FSM_RULE_ENTRY_ANY(s5, s2, Event_StartReregist)
-	//	FSM_RULE_ENTRY_ANY(s5, s2, Event_StartRegist)
-	//	FSM_RULE_ENTRY_ANY(s5, s7, Event_LostTrust)
-	//	FSM_RULE_ENTRY_ANY(s6, s1, Event_StateTimeout)
-	//	FSM_RULE_ENTRY_ANY(s7, s1, Event_StateTimeout)		
-	//END_FSM_RULE()
-
-	enum { s1, s2, s3 };
+	enum{s1, s2, s3};
 
 	enum
 	{
-		Event_StartRegist = EVT_USER + 1,
+		Event_StartRegist = EVT_USER+1,
 		Event_ConnectionOK,
 		Event_EndSyncTime,
 		Event_ReqTokenFail,
@@ -714,17 +645,17 @@ public:
 	};
 
 	BEGIN_FSM_STATE(CAccessAuthFSM)
-		FSM_STATE_ENTRY(s1, "Isolate", s1_on_entry, s1_on_exit, s1_on_event)
-		FSM_STATE_ENTRY(s2, "Checking", s2_on_entry, s2_on_exit, s2_on_event)
+		FSM_STATE_ENTRY(s1, "Isolate",s1_on_entry,s1_on_exit,s1_on_event)
+		FSM_STATE_ENTRY(s2, "Checking",s2_on_entry,s2_on_exit,s2_on_event)
 		FSM_STATE_ENTRY(s3, "Authorized", s3_on_entry, s3_on_exit, s3_on_event)
-		END_FSM_STATE()
+	END_FSM_STATE()
 
-		BEGIN_FSM_RULE(CAccessAuthFSM, s1)
+	BEGIN_FSM_RULE(CAccessAuthFSM,s1)
 		FSM_RULE_ENTRY_ANY(s1, s2, Event_StartRegist)
 		FSM_RULE_ENTRY_ANY(s1, s3, Event_AccessAuthSucc)
 		FSM_RULE_ENTRY_ANY(s2, s1, Event_ReqTokenCancel)
 		FSM_RULE_ENTRY_ANY(s2, s3, Event_AccessAuthSucc)
-		END_FSM_RULE()
+	END_FSM_RULE()
 
 	void s1_on_entry();
 	void s1_on_exit();
@@ -735,24 +666,10 @@ public:
 	unsigned int s2_on_event(FSMEvent* event);
 
 	void s3_on_entry();
-	void s3_on_exit();
-	unsigned int s3_on_event(FSMEvent* event);
-
-	/*void s4_on_entry();
-	void s4_on_exit();
-	unsigned int s4_on_event(FSMEvent* event);
-
-	void s5_on_entry();
-	void s5_on_exit();
-	unsigned int s5_on_event(FSMEvent* event);
 
-	void s6_on_entry();
-	void s6_on_exit();
-	unsigned int s6_on_event(FSMEvent* event);
+	void s3_on_exit();
 
-	void s7_on_entry();
-	void s7_on_exit();
-	unsigned int s7_on_event(FSMEvent* event);*/
+	unsigned int s3_on_event(FSMEvent* event);
 
 public:
 	void UpdateWK();
@@ -795,6 +712,7 @@ private:
 	ErrorCodeEnum LoadCenterConfig();
 
 	bool IsNetworkChecking() const { return m_fNetworkChecking; }
+	void GetDiffSyncTimeFromCenterSettings();
 
 	int m_nExitReason;
 	int m_nExitWay;
@@ -812,6 +730,7 @@ private:
 	int m_nAccessFailedCount;
 
 	bool m_fNetworkChecking;
+	int m_torelateDiffSyncTimeSecs;
 };
 class MyMutex {
 public:

+ 7 - 0
Module/mod_accessauth/AccessAuthorization_def_g.h

@@ -26,6 +26,13 @@ namespace AccessAuthorization {
 #define AccessAuthService_MethodSignature_InitializeNew 1851143282
 #define AccessAuthService_MethodSignature_SyncTime 1195907872
 
+#define AccessAuthService_LogCode_Regist "QLR040250200"
+#define AccessAuthService_LogCode_Unregist "QLR040250201"
+#define AccessAuthService_LogCode_InitDev "QLR040250202"
+#define AccessAuthService_LogCode_UpdateWK "QLR040250203"
+#define AccessAuthService_LogCode_InitializeNew "QLR040250204"
+#define AccessAuthService_LogCode_SyncTime "QLR040250205"
+
 struct AccessAuthService_Regist_Info
 {