Skip to content

Commit 7ecfaa6

Browse files
committed
added:http basic and digest verification support
delete:register authorize
1 parent 4ac2044 commit 7ecfaa6

File tree

8 files changed

+124
-31
lines changed

8 files changed

+124
-31
lines changed

XEngine_Release/XEngine_Config/XEngine_Config.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242
"nTimeLast":3600,
4343
"nTimeCount":0
4444
},
45-
"XAuthorize":{
46-
"bHTTPAuth":true
45+
"XVerification":{
46+
"bEnable": false,
47+
"nVType": 1,
48+
"tszAuthPass": "http://192.168.1.7:5200/Api/Pass/UPFile"
4749
},
4850
"XReport":{
4951
"bEnable":true,

XEngine_Source/MQCore_ConfigModule/Config_Define.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ typedef struct
6666
}st_XMemory;
6767
struct
6868
{
69-
bool bHTTPAuth;
70-
}st_XAuthorize;
69+
bool bEnable;
70+
int nVType;
71+
XCHAR tszAuthPass[XPATH_MID];
72+
}st_XVerification;
7173
struct
7274
{
7375
bool bEnable;

XEngine_Source/MQCore_ConfigModule/Config_Json/Config_Json.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,16 @@ bool CConfig_Json::Config_Json_File(LPCXSTR lpszConfigFile,XENGINE_SERVERCONFIG
128128
pSt_ServerConfig->st_XMemory.nTimeLast = st_JsonXMemory["nTimeLast"].asInt();
129129
pSt_ServerConfig->st_XMemory.nTimeCount = st_JsonXMemory["nTimeCount"].asInt();
130130

131-
if (st_JsonRoot["XAuthorize"].empty() || (1 != st_JsonRoot["XAuthorize"].size()))
131+
if (st_JsonRoot["XVerification"].empty() || (3 != st_JsonRoot["XVerification"].size()))
132132
{
133133
Config_IsErrorOccur = true;
134134
Config_dwErrorCode = ERROR_MQ_MODULE_CONFIG_JSON_XAUTHORIZE;
135135
return false;
136136
}
137-
Json::Value st_JsonXAuthorize = st_JsonRoot["XAuthorize"];
138-
pSt_ServerConfig->st_XAuthorize.bHTTPAuth = st_JsonXAuthorize["bHTTPAuth"].asBool();
137+
Json::Value st_JsonXVerification = st_JsonRoot["XVerification"];
138+
pSt_ServerConfig->st_XVerification.bEnable = st_JsonXVerification["bEnable"].asBool();
139+
pSt_ServerConfig->st_XVerification.nVType = st_JsonXVerification["nVType"].asInt();
140+
_tcsxcpy(pSt_ServerConfig->st_XVerification.tszAuthPass, st_JsonXVerification["tszAuthPass"].asCString());
139141

140142
if (st_JsonRoot["XReport"].empty() || (3 != st_JsonRoot["XReport"].size()))
141143
{

XEngine_Source/Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ FLAGS=
66
BASE_THIRDPART_JSONCPP = ./XEngine_DependLibrary/XEngine_Module/jsoncpp
77
BASE_THIRDPART_REPORT = ./XEngine_DependLibrary/XEngine_Module/XEngine_InfoReport
88
BASE_THIRDPART_TOKEN = ./XEngine_DependLibrary/XEngine_Module/XEngine_Token
9+
BASE_THIRDPART_VERIFICATION = ./XEngine_DependLibrary/XEngine_Module/XEngine_Verification
910

1011
BASE_CONFIG_PATH = ./MQCore_ConfigModule
1112
BASE_PROTOCOL_PATH = ./MQCore_ProtocolModule
@@ -22,7 +23,7 @@ else ifeq ($(PLATFORM),mac)
2223
FILEEXT = dylib
2324
endif
2425

25-
XENGINE_MODULES = libjsoncpp.so libXEngine_InfoReport.so libXEngine_Token.so \
26+
XENGINE_MODULES = libjsoncpp.so libXEngine_InfoReport.so libXEngine_Token.so libXEngine_Verification.so \
2627
libMQCore_ConfigModule.so libMQCore_ProtocolModule.so libMQCore_SessionModule.so libMQCore_MemoryCache.so libMQCore_DBModule.so libMQCore_HelpModule.so \
2728
XEngine_MQServiceApp.exe
2829

@@ -48,6 +49,12 @@ ifeq ($(FLAGS), InstallAll)
4849
else
4950
make -C $(BASE_THIRDPART_TOKEN) PLATFORM=$(PLATFORM) $(FLAGS)
5051
endif
52+
libXEngine_Verification.so:
53+
ifeq ($(FLAGS), InstallAll)
54+
cp $(BASE_THIRDPART_VERIFICATION)/libXEngine_Verification.$(FILEEXT) ../XEngine_Release/
55+
else
56+
make -C $(BASE_THIRDPART_VERIFICATION) PLATFORM=$(PLATFORM) $(FLAGS)
57+
endif
5158

5259
libMQCore_ConfigModule.so:
5360
make -C $(BASE_CONFIG_PATH) PLATFORM=$(PLATFORM) UNICODE=$(UNICODE) RELEASE=$(RELEASE) $(FLAGS)

XEngine_Source/XEngine_MQServiceApp/MQService_HTTPTask/MQService_HTTPPost.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,6 @@ bool MessageQueue_HttpTask_Post(LPCXSTR lpszClientAddr, LPCXSTR lpszFuncName, LP
2525
LPCXSTR lpszAPICreateTopic = _X("createtopic");
2626
LPCXSTR lpszAPIDelTopic = _X("deletetopic");
2727
LPCXSTR lpszAPIDelUser = _X("deleteuser");
28-
29-
//判断是否需要验证,不是注册协议
30-
if (st_ServiceCfg.st_XAuthorize.bHTTPAuth && (0 != _tcsxnicmp(lpszAPIRegister, lpszFuncName, _tcsxlen(lpszAPIRegister))))
31-
{
32-
if (ProtocolModule_Parse_Token(lpszMsgBuffer, nMsgLen, &xhToken))
33-
{
34-
if (!Session_Token_Get(xhToken))
35-
{
36-
ProtocolModule_Packet_Http(tszSDBuffer, &nSDLen, ERROR_XENGINE_MESSAGE_HTTP_AUTHORIZE, "not authorize");
37-
XEngine_MQXService_Send(lpszClientAddr, tszSDBuffer, nSDLen, XENGINE_MQAPP_NETTYPE_HTTP);
38-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _X("HTTP客户端:%s,请求的API:%s 失败,因为没有经过验证"), lpszClientAddr, lpszFuncName);
39-
return false;
40-
}
41-
}
42-
}
4328
//判断请求
4429
if (0 == _tcsxnicmp(lpszAPIRegister, lpszFuncName, _tcsxlen(lpszAPIRegister)))
4530
{

XEngine_Source/XEngine_MQServiceApp/MQService_Hdr.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,27 +114,30 @@ extern MESSAGEQUEUE_DBCONFIG st_DBConfig;
114114
#pragma comment(lib,"../x64/Debug/MQCore_SessionModule.lib")
115115
#pragma comment(lib,"../x64/Debug/MQCore_DBModule.lib")
116116
#pragma comment(lib,"../x64/Debug/MQCore_HelpModule.lib")
117+
#pragma comment(lib,"../x64/Debug/MQCore_MemoryCache.lib")
117118
#pragma comment(lib,"../x64/Debug/XEngine_InfoReport.lib")
118119
#pragma comment(lib,"../x64/Debug/XEngine_Token.lib")
119-
#pragma comment(lib,"../x64/Debug/MQCore_MemoryCache.lib")
120+
#pragma comment(lib,"../x64/Debug/XEngine_Verification.lib")
120121
#elif _M_ARM64
121122
#pragma comment(lib,"../ARM64/Debug/MQCore_ConfigModule.lib")
122123
#pragma comment(lib,"../ARM64/Debug/MQCore_ProtocolModule.lib")
123124
#pragma comment(lib,"../ARM64/Debug/MQCore_SessionModule.lib")
124125
#pragma comment(lib,"../ARM64/Debug/MQCore_DBModule.lib")
125126
#pragma comment(lib,"../ARM64/Debug/MQCore_HelpModule.lib")
127+
#pragma comment(lib,"../ARM64/Debug/MQCore_MemoryCache.lib")
126128
#pragma comment(lib,"../ARM64/Debug/XEngine_InfoReport.lib")
127129
#pragma comment(lib,"../ARM64/Debug/XEngine_Token.lib")
128-
#pragma comment(lib,"../ARM64/Debug/MQCore_MemoryCache.lib")
130+
#pragma comment(lib,"../ARM64/Debug/XEngine_Verification.lib")
129131
#elif _M_IX86
130132
#pragma comment(lib,"../Debug/MQCore_ConfigModule.lib")
131133
#pragma comment(lib,"../Debug/MQCore_ProtocolModule.lib")
132134
#pragma comment(lib,"../Debug/MQCore_SessionModule.lib")
133135
#pragma comment(lib,"../Debug/MQCore_DBModule.lib")
134136
#pragma comment(lib,"../Debug/MQCore_HelpModule.lib")
137+
#pragma comment(lib,"../Debug/MQCore_MemoryCache.lib")
135138
#pragma comment(lib,"../Debug/XEngine_InfoReport.lib")
136139
#pragma comment(lib,"../Debug/XEngine_Token.lib")
137-
#pragma comment(lib,"../Debug/MQCore_MemoryCache.lib")
140+
#pragma comment(lib,"../Debug/XEngine_Verification.lib")
138141
#endif
139142
#else
140143
#ifdef _M_X64
@@ -143,27 +146,30 @@ extern MESSAGEQUEUE_DBCONFIG st_DBConfig;
143146
#pragma comment(lib,"../x64/Release/MQCore_SessionModule.lib")
144147
#pragma comment(lib,"../x64/Release/MQCore_DBModule.lib")
145148
#pragma comment(lib,"../x64/Release/MQCore_HelpModule.lib")
149+
#pragma comment(lib,"../x64/Release/MQCore_MemoryCache.lib")
146150
#pragma comment(lib,"../x64/Release/XEngine_InfoReport.lib")
147151
#pragma comment(lib,"../x64/Release/XEngine_Token.lib")
148-
#pragma comment(lib,"../x64/Release/MQCore_MemoryCache.lib")
152+
#pragma comment(lib,"../x64/Release/XEngine_Verification.lib")
149153
#elif _M_ARM64
150154
#pragma comment(lib,"../ARM64/Release/MQCore_ConfigModule.lib")
151155
#pragma comment(lib,"../ARM64/Release/MQCore_ProtocolModule.lib")
152156
#pragma comment(lib,"../ARM64/Release/MQCore_SessionModule.lib")
153157
#pragma comment(lib,"../ARM64/Release/MQCore_DBModule.lib")
154158
#pragma comment(lib,"../ARM64/Release/MQCore_HelpModule.lib")
159+
#pragma comment(lib,"../ARM64/Release/MQCore_MemoryCache.lib")
155160
#pragma comment(lib,"../ARM64/Release/XEngine_InfoReport.lib")
156161
#pragma comment(lib,"../ARM64/Release/XEngine_Token.lib")
157-
#pragma comment(lib,"../ARM64/Release/MQCore_MemoryCache.lib")
162+
#pragma comment(lib,"../ARM64/Release/XEngine_Verification.lib")
158163
#elif _M_IX86
159164
#pragma comment(lib,"../Release/MQCore_ConfigModule.lib")
160165
#pragma comment(lib,"../Release/MQCore_ProtocolModule.lib")
161166
#pragma comment(lib,"../Release/MQCore_SessionModule.lib")
162167
#pragma comment(lib,"../Release/MQCore_DBModule.lib")
163168
#pragma comment(lib,"../Release/MQCore_HelpModule.lib")
169+
#pragma comment(lib,"../Release/MQCore_MemoryCache.lib")
164170
#pragma comment(lib,"../Release/XEngine_InfoReport.lib")
165171
#pragma comment(lib,"../Release/XEngine_Token.lib")
166-
#pragma comment(lib,"../Release/MQCore_MemoryCache.lib")
172+
#pragma comment(lib,"../Release/XEngine_Verification.lib")
167173
#endif
168174
#endif
169175
#endif

XEngine_Source/XEngine_MQServiceApp/MQService_HttpTask.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,95 @@ bool MessageQueue_Http_Handle(RFCCOMPONENTS_HTTP_REQPARAM* pSt_HTTPParam, LPCXST
4848
LPCXSTR lpszPostMethod = _X("POST");
4949
LPCXSTR lpszGetMethod = _X("GET");
5050

51+
if (st_ServiceCfg.st_XVerification.bEnable)
52+
{
53+
int nVType = 0;
54+
RFCCOMPONENTS_HTTP_HDRPARAM st_HDRParam = {};
55+
56+
st_HDRParam.nHttpCode = 401;
57+
st_HDRParam.bIsClose = true;
58+
st_HDRParam.bAuth = true;
59+
//打包验证信息
60+
int nHDRLen = 0;
61+
XCHAR tszHDRBuffer[XPATH_MAX] = {};
62+
if (1 == st_ServiceCfg.st_XVerification.nVType)
63+
{
64+
Verification_HTTP_BasicServerPacket(tszHDRBuffer, &nHDRLen);
65+
}
66+
else
67+
{
68+
XCHAR tszNonceStr[64] = {};
69+
XCHAR tszOpaqueStr[64] = {};
70+
Verification_HTTP_DigestServerPacket(tszHDRBuffer, &nHDRLen, tszNonceStr, tszOpaqueStr);
71+
}
72+
//后去验证方法
73+
if (!Verification_HTTP_GetType(pptszListHdr, nHdrCount, &nVType))
74+
{
75+
HttpProtocol_Server_SendMsgEx(xhHTTPPacket, tszPKTBuffer, &nPKTLen, &st_HDRParam, NULL, 0, tszHDRBuffer);
76+
NetCore_TCPXCore_SendEx(xhHTTPSocket, lpszClientAddr, tszPKTBuffer, nPKTLen);
77+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _X("HTTP客户端:%s,用户验证失败,验证方式:%d,错误:%lX"), lpszClientAddr, st_ServiceCfg.st_XVerification.nVType, Verification_GetLastError());
78+
return false;
79+
}
80+
//验证方式是否一致
81+
if (st_ServiceCfg.st_XVerification.nVType != nVType)
82+
{
83+
HttpProtocol_Server_SendMsgEx(xhHTTPPacket, tszPKTBuffer, &nPKTLen, &st_HDRParam, NULL, 0, tszHDRBuffer);
84+
NetCore_TCPXCore_SendEx(xhHTTPSocket, lpszClientAddr, tszPKTBuffer, nPKTLen);
85+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _X("HTTP客户端:%s,用户验证失败,验证方式错误,请求:%d,需求:%d"), lpszClientAddr, nVType, st_ServiceCfg.st_XVerification.nVType);
86+
return false;
87+
}
88+
bool bRet = false;
89+
int nHTTPCode = 0;
90+
int nMSGLen = 0;
91+
XCLIENT_APIHTTP st_APIHttp = {};
92+
93+
XCHAR* ptszMSGBuffer = NULL;
94+
if (!APIClient_Http_Request(_X("GET"), st_ServiceCfg.st_XVerification.tszAuthPass, NULL, &nHTTPCode, &ptszMSGBuffer, &nMSGLen, NULL, NULL, &st_APIHttp))
95+
{
96+
st_HDRParam.nHttpCode = 500;
97+
HttpProtocol_Server_SendMsgEx(xhHTTPPacket, tszPKTBuffer, &nPKTLen, &st_HDRParam, NULL, 0, tszHDRBuffer);
98+
NetCore_TCPXCore_SendEx(xhHTTPSocket, lpszClientAddr, tszPKTBuffer, nPKTLen);
99+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _X("HTTP客户端:%s,用户验证失败,GET请求验证服务:%s 失败,错误码:%lX"), lpszClientAddr, st_ServiceCfg.st_XVerification.tszAuthPass, APIClient_GetLastError());
100+
return false;
101+
}
102+
if (200 != nHTTPCode)
103+
{
104+
st_HDRParam.nHttpCode = 500;
105+
HttpProtocol_Server_SendMsgEx(xhHTTPPacket, tszPKTBuffer, &nPKTLen, &st_HDRParam, NULL, 0, tszHDRBuffer);
106+
NetCore_TCPXCore_SendEx(xhHTTPSocket, lpszClientAddr, tszPKTBuffer, nPKTLen);
107+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _X("HTTP客户端:%s,用户验证失败,GET请求验证服务:%s 失败,错误:%d"), lpszClientAddr, st_ServiceCfg.st_XVerification.tszAuthPass, nHTTPCode);
108+
return false;
109+
}
110+
XENGINE_PROTOCOL_USERINFO st_UserAuth = {};
111+
if (!ProtocolModule_Parse_Register(ptszMSGBuffer, nMsgLen, &st_UserAuth))
112+
{
113+
st_HDRParam.nHttpCode = 500;
114+
HttpProtocol_Server_SendMsgEx(xhHTTPPacket, tszPKTBuffer, &nPKTLen, &st_HDRParam, NULL, 0, tszHDRBuffer);
115+
NetCore_TCPXCore_SendEx(xhHTTPSocket, lpszClientAddr, tszPKTBuffer, nPKTLen);
116+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _X("HTTP客户端:%s,用户验证失败,返回内容:%s 错误,无法继续"), lpszClientAddr, ptszMSGBuffer);
117+
BaseLib_Memory_FreeCStyle((XPPMEM)&ptszMSGBuffer);
118+
return false;
119+
}
120+
BaseLib_Memory_FreeCStyle((XPPMEM)&ptszMSGBuffer);
121+
122+
if (1 == nVType)
123+
{
124+
bRet = Verification_HTTP_Basic(st_UserAuth.tszUserName, st_UserAuth.tszUserPass, pptszListHdr, nHdrCount);
125+
}
126+
else if (2 == nVType)
127+
{
128+
bRet = Verification_HTTP_Digest(st_UserAuth.tszUserName, st_UserAuth.tszUserPass, pSt_HTTPParam->tszHttpMethod, pptszListHdr, nHdrCount);
129+
}
130+
131+
if (!bRet)
132+
{
133+
HttpProtocol_Server_SendMsgEx(xhHTTPPacket, tszPKTBuffer, &nPKTLen, &st_HDRParam, NULL, 0, tszHDRBuffer);
134+
NetCore_TCPXCore_SendEx(xhHTTPSocket, lpszClientAddr, tszPKTBuffer, nPKTLen);
135+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _X("HTTP客户端:%s,用户验证失败,验证处理错误,可能用户密码登信息不匹配,类型:%d"), lpszClientAddr, nVType);
136+
return false;
137+
}
138+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _X("HTTP客户端:%s,HTTP验证类型:%d 通过"), lpszClientAddr, nVType);
139+
}
51140
int nUrlCount = 0;
52141
XCHAR** ppSt_ListUrl;
53142
HttpProtocol_ServerHelp_GetParament(pSt_HTTPParam->tszHttpUri, &ppSt_ListUrl, &nUrlCount);

XEngine_Source/XEngine_MQServiceApp/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ CC = g++ -Wall -std=c++20
22
PLATVER =
33
LOADHDR = -I ./
44
LOADSO = -L ../MQCore_ConfigModule -L ../MQCore_ProtocolModule -L ../MQCore_SessionModule -L ../MQCore_MemoryCache -L ../MQCore_DBModule -L ../MQCore_HelpModule \
5-
-L ../XEngine_DependLibrary/XEngine_Module/jsoncpp -L ../XEngine_DependLibrary/XEngine_Module/XEngine_InfoReport -L ../XEngine_DependLibrary/XEngine_Module/XEngine_Token
5+
-L ../XEngine_DependLibrary/XEngine_Module/jsoncpp -L ../XEngine_DependLibrary/XEngine_Module/XEngine_InfoReport -L ../XEngine_DependLibrary/XEngine_Module/XEngine_Token -L ../XEngine_DependLibrary/XEngine_Module/XEngine_Verification
66
LIB = -lXEngine_BaseLib -lXEngine_Algorithm -lXEngine_Core -lXEngine_ManagePool -lXClient_APIHelp -lXEngine_SystemApi -lHelpComponents_XLog -lHelpComponents_Packets -lRfcComponents_WSProtocol -lRfcComponents_HttpProtocol -lRfcComponents_MQTTProtocol \
77
-lMQCore_ConfigModule -lMQCore_ProtocolModule -lMQCore_SessionModule -lMQCore_MemoryCache -lMQCore_DBModule -lMQCore_HelpModule \
8-
-ljsoncpp -lXEngine_InfoReport -lXEngine_Token
8+
-ljsoncpp -lXEngine_InfoReport -lXEngine_Token -lXEngine_Verification
99
LIBEX =
1010
OBJECTS = MQService_Net.o MQService_Config.o MQService_TCPTask.o MQService_HttpTask.o MQService_WSTask.o MQService_Task.o MQService_MQTTTask.o MQService_HTTPPost.o MQService_HTTPGet.o XEngine_MQServiceApp.o
1111

0 commit comments

Comments
 (0)