OCT 1. 重构接口协议解析,支持协议解密功能
This commit is contained in:
parent
a0a59a6756
commit
b9abec4f16
|
@ -151,7 +151,7 @@ void hw_http_response_send_error(hw_http_response *response, const char *error,
|
|||
hw_string keep_alive_value;
|
||||
|
||||
SETSTRING(content_type_name, "Content-Type");
|
||||
SETSTRING(content_type_value, "text/html");
|
||||
SETSTRING(content_type_value, "application/json");
|
||||
hw_set_response_header(response, &content_type_name, &content_type_value);
|
||||
|
||||
SETSTRING(status_code, error);
|
||||
|
|
|
@ -10,7 +10,6 @@ extern "C" {
|
|||
#endif
|
||||
#include <cjson/cJSON.h>
|
||||
|
||||
|
||||
typedef enum {
|
||||
CRYPTO_NONE = 0,
|
||||
CRYPTO_BASE64 = 1,
|
||||
|
@ -19,8 +18,8 @@ typedef enum {
|
|||
CRYPTO_AES256 = 4,
|
||||
} PROTO_CRYPTO_TYPE;
|
||||
|
||||
const char *proto_create_new(cJSON *pMsgCtx, int rspCode);
|
||||
|
||||
const char *proto_create_new(cJSON *pMsgCtx, int httpCode);
|
||||
const char *proto_decode_context(const char *pString, unsigned int *pVer, unsigned long long *pTm);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -50,6 +50,7 @@ extern "C" {
|
|||
ERR_CODE(ERR_MQ_CONN_SERVER, "消息队列连接服务器失败") \
|
||||
ERR_CODE(ERR_MQ_SEND_MSG, "消息队列发送消息失败") \
|
||||
ERR_CODE(ERR_JSON_CREAT_OBJ, "创建JSON对象失败") \
|
||||
ERR_CODE(ERR_JSON_PRASE_OBJ, "解析JSON对象失败") \
|
||||
ERR_CODE(ERR_CREATE_NETIF, "创建网络接口失败") \
|
||||
ERR_CODE(ERR_CREATE_PPPOE_NETIF, "创建PPPoE网络接口失败") \
|
||||
ERR_CODE(ERR_CREATE_PPP_SESSION, "创建PPP连接失败") \
|
||||
|
@ -57,7 +58,8 @@ extern "C" {
|
|||
ERR_CODE(ERR_MISC_GET_NETMASK, "获取网卡子网掩码失败") \
|
||||
ERR_CODE(ERR_MISC_GET_GATEWAY, "获取网卡网关地址失败") \
|
||||
ERR_CODE(ERR_MISC_GET_MACADDR, "获取网卡MAC地址失败") \
|
||||
ERR_CODE(ERR_MENU_EXIT, "菜单执行完后自动退出")
|
||||
ERR_CODE(ERR_MENU_EXIT, "菜单执行完后自动退出") \
|
||||
ERR_CODE(ERR_HTTP_UNSUP_METHOD, "不支持的 HTTP 请求方法")
|
||||
|
||||
#define GENERATE_ENUM(ENUM, x) ENUM,
|
||||
|
||||
|
|
|
@ -21,13 +21,105 @@ typedef struct {
|
|||
cJSON *msgContend;
|
||||
} PROTOCOL_WARP, *PPROTOCOL_WARP;
|
||||
|
||||
const char *proto_create_new(cJSON *pMsgCtx, int rspCode) {
|
||||
const char *proto_decode_context(const char *pString, unsigned int *pVer, unsigned long long *pTm) {
|
||||
cJSON *pMsgCtx;
|
||||
unsigned char *pBase64;
|
||||
int decodeSize, ret;
|
||||
unsigned int outSize = 0;
|
||||
char *pMsgContent = NULL;
|
||||
cJSON *pRoot = cJSON_Parse(pString);
|
||||
|
||||
if (!pRoot) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cJSON *pCrypto = cJSON_GetObjectItem(pRoot, "cryptoType");
|
||||
|
||||
if (!pCrypto) {
|
||||
cJSON_free(pRoot);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (pVer) {
|
||||
cJSON *pObj = cJSON_GetObjectItem(pRoot, "ver");
|
||||
|
||||
if (pObj) {
|
||||
*pVer = pObj->valueint;
|
||||
}
|
||||
}
|
||||
|
||||
if (pTm) {
|
||||
cJSON *pObj = cJSON_GetObjectItem(pRoot, "timeStamp");
|
||||
|
||||
if (pObj) {
|
||||
*pTm = (unsigned long long)pObj->valuedouble;
|
||||
}
|
||||
}
|
||||
|
||||
pMsgCtx = cJSON_GetObjectItem(pRoot, "msgContent");
|
||||
|
||||
if (!pMsgCtx) {
|
||||
cJSON_free(pRoot);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
switch (pCrypto->valueint) {
|
||||
case CRYPTO_NONE:
|
||||
pMsgContent = strdup(cJSON_Print(pMsgCtx));
|
||||
break;
|
||||
case CRYPTO_BASE64:
|
||||
pMsgContent = (char *)base64_decode(pMsgCtx->valuestring, (unsigned int *)&outSize);
|
||||
break;
|
||||
case CRYPTO_AES128:
|
||||
case CRYPTO_AES256:
|
||||
case CRYPTO_3DES: {
|
||||
int cryptoType;
|
||||
const char *pKey = config_get_proto_crypto_key();
|
||||
|
||||
if (pCrypto->valueint == CRYPTO_AES128) {
|
||||
cryptoType = AES128_ECB_PKCS7PADDING;
|
||||
} else if (pCrypto->valueint == CRYPTO_AES256) {
|
||||
cryptoType = AES256_ECB_PKCS7PADDING;
|
||||
} else if (pCrypto->valueint == CRYPTO_3DES) {
|
||||
cryptoType = DES3_ECB_PKCS7PADDING;
|
||||
} else {
|
||||
cJSON_free(pRoot);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pBase64 = (unsigned char *)base64_decode(pMsgCtx->valuestring,
|
||||
(unsigned int *)&outSize);
|
||||
|
||||
if (symmetric_decrypto(cryptoType,
|
||||
pBase64,
|
||||
outSize,
|
||||
(unsigned char **)(&pMsgContent),
|
||||
&decodeSize,
|
||||
pKey) != ERR_SUCCESS) {
|
||||
free((void *)pBase64);
|
||||
if (pMsgContent) {
|
||||
free(pMsgContent);
|
||||
}
|
||||
cJSON_free(pRoot);
|
||||
return NULL;
|
||||
} else {
|
||||
pMsgContent[decodeSize] = 0;
|
||||
}
|
||||
free((void *)pBase64);
|
||||
} break;
|
||||
}
|
||||
|
||||
cJSON_free(pRoot);
|
||||
return pMsgContent;
|
||||
}
|
||||
|
||||
const char *proto_create_new(cJSON *pMsgCtx, int httpCode) {
|
||||
const char *pStrProto;
|
||||
cJSON *pRoot;
|
||||
PROTOCOL_WARP pro = {.ver = CURRENT_PROTOCOL_VERSION,
|
||||
.cryptoType = config_get_proto_crypto_type(),
|
||||
.timeStamp = get_current_time_ms(),
|
||||
.code = rspCode};
|
||||
.code = httpCode};
|
||||
|
||||
pRoot = cJSON_CreateObject();
|
||||
|
||||
|
@ -74,7 +166,9 @@ const char *proto_create_new(cJSON *pMsgCtx, int rspCode) {
|
|||
}
|
||||
|
||||
if (pKey == NULL || strlen(pKey) == 0) {
|
||||
dzlog_error("Cryptography key empty of algorithm %d, Used default algorithm BASE64\n", cryptoType);
|
||||
dzlog_error(
|
||||
"Cryptography key empty of algorithm %d, Used default algorithm BASE64\n",
|
||||
cryptoType);
|
||||
base64 = base64_encode((unsigned char *)pStrMsg, strlen(pStrMsg));
|
||||
pro.cryptoType = CRYPTO_BASE64;
|
||||
} else {
|
||||
|
@ -82,10 +176,16 @@ const char *proto_create_new(cJSON *pMsgCtx, int rspCode) {
|
|||
unsigned char *buf;
|
||||
int outSize = 0;
|
||||
|
||||
ret = symmetric_encrypto(cryptoType, (unsigned char *)pStrMsg, strlen(pStrMsg), &buf, &outSize, pKey);
|
||||
ret = symmetric_encrypto(cryptoType,
|
||||
(unsigned char *)pStrMsg,
|
||||
strlen(pStrMsg),
|
||||
&buf,
|
||||
&outSize,
|
||||
pKey);
|
||||
|
||||
if (ret != ERR_SUCCESS) {
|
||||
dzlog_error("Unsupported protocol crypto : %d, Used default algorithm BASE64\n", cryptoType);
|
||||
dzlog_error("Unsupported protocol crypto : %d, Used default algorithm BASE64\n",
|
||||
cryptoType);
|
||||
base64 = base64_encode((unsigned char *)pStrMsg, strlen(pStrMsg));
|
||||
pro.cryptoType = CRYPTO_BASE64;
|
||||
} else {
|
||||
|
@ -98,7 +198,9 @@ const char *proto_create_new(cJSON *pMsgCtx, int rspCode) {
|
|||
cJSON_free(pro.msgContend);
|
||||
} break;
|
||||
default:
|
||||
dzlog_error("Unsupported protocol crypto algorithms: %d, Used default algorithm BASE64\n", pro.cryptoType);
|
||||
dzlog_error(
|
||||
"Unsupported protocol crypto algorithms: %d, Used default algorithm BASE64\n",
|
||||
pro.cryptoType);
|
||||
cJSON_free(pro.msgContend);
|
||||
cJSON_Delete(pRoot);
|
||||
return NULL;
|
||||
|
|
|
@ -22,6 +22,7 @@ using namespace std;
|
|||
#include <zlog.h>
|
||||
#include "config.h"
|
||||
#include "proto.h"
|
||||
#include "user_errno.h"
|
||||
|
||||
extern data2 cfig;
|
||||
extern bool kRunning;
|
||||
|
@ -30,6 +31,7 @@ extern time_t t;
|
|||
|
||||
static void sendUserList(data19 *req, const char *pRequest) {
|
||||
char logBuff[512];
|
||||
const char *pStrContent;
|
||||
dhcpMap::iterator p;
|
||||
|
||||
dzlog_debug("Input: %s\n", pRequest);
|
||||
|
@ -40,20 +42,22 @@ static void sendUserList(data19 *req, const char *pRequest) {
|
|||
return;
|
||||
}
|
||||
|
||||
cJSON *pRoot = cJSON_Parse(pRequest);
|
||||
pStrContent = proto_decode_context(pRequest, nullptr, nullptr);
|
||||
|
||||
if (pStrContent == nullptr) {
|
||||
sprintf(logBuff, "Requeset Json error %s", pRequest);
|
||||
logDHCPMess(logBuff, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *pRoot = cJSON_Parse(pStrContent);
|
||||
free((void *)pStrContent);
|
||||
|
||||
if (!pRoot) {
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *pMsgContent = cJSON_GetObjectItem(pRoot, "msgContent");
|
||||
|
||||
if (!pMsgContent) {
|
||||
cJSON_Delete(pRoot);
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *pUserMac = cJSON_GetObjectItem(pMsgContent, "userMac");
|
||||
cJSON *pUserMac = cJSON_GetObjectItem(pRoot, "userMac");
|
||||
|
||||
if (!pUserMac) {
|
||||
cJSON_Delete(pRoot);
|
||||
|
@ -107,6 +111,9 @@ static void sendUserList(data19 *req, const char *pRequest) {
|
|||
cJSON_AddItemToArray(pMsgArray, pRspItem);
|
||||
}
|
||||
|
||||
cJSON_AddNumberToObject(pRspMsg, "status", ERR_SUCCESS);
|
||||
cJSON_AddStringToObject(pRspMsg, "message", getErrorEnumDesc(ERR_SUCCESS));
|
||||
|
||||
const char *pStrPro = proto_create_new(pRspMsg, 200);
|
||||
|
||||
//cJSON_AddItemToObject(pRspRoot, "msgContent", pRspMsg);
|
||||
|
@ -154,6 +161,8 @@ static void sendAllLists(data19 *req) {
|
|||
}
|
||||
|
||||
//cJSON_AddItemToObject(pRspRoot, "msgContent", pRspMsg);
|
||||
cJSON_AddNumberToObject(pRspMsg, "status", ERR_SUCCESS);
|
||||
cJSON_AddStringToObject(pRspMsg, "message", getErrorEnumDesc(ERR_SUCCESS));
|
||||
|
||||
const char *pStrPro = proto_create_new(pRspMsg, 200);
|
||||
|
||||
|
@ -430,7 +439,24 @@ static void response_complete(void *user_data) {
|
|||
}
|
||||
}
|
||||
|
||||
static void opendhcp_http_info(http_request *request, hw_http_response *response, void *UNUSED(user_data)) {
|
||||
static void proto_response_error(hw_http_response *response,
|
||||
int httpCode,
|
||||
const char *httpCodeStr,
|
||||
int errCode) {
|
||||
cJSON *pRspMsg = cJSON_CreateObject();
|
||||
|
||||
cJSON_AddNumberToObject(pRspMsg, "status", errCode);
|
||||
cJSON_AddStringToObject(pRspMsg, "message", getErrorEnumDesc(errCode));
|
||||
|
||||
const char *pStrPro = proto_create_new(pRspMsg, httpCode);
|
||||
|
||||
hw_http_response_send_error(response, httpCodeStr, pStrPro);
|
||||
free((void *)pStrPro);
|
||||
}
|
||||
|
||||
static void opendhcp_http_info(http_request *request,
|
||||
hw_http_response *response,
|
||||
void *UNUSED(user_data)) {
|
||||
hw_string status_code;
|
||||
hw_string content_type_name;
|
||||
hw_string content_type_value;
|
||||
|
@ -441,12 +467,12 @@ static void opendhcp_http_info(http_request *request, hw_http_response *response
|
|||
auto *req = (data19 *)malloc(sizeof(struct data19));
|
||||
|
||||
if (req == nullptr) {
|
||||
hw_http_response_send_error(response, HTTP_STATUS_500, "memory error");
|
||||
proto_response_error(response, 500, HTTP_STATUS_500, ERR_MALLOC_MEMORY);
|
||||
return;
|
||||
}
|
||||
|
||||
if (request->method != HW_HTTP_GET) {
|
||||
hw_http_response_send_error(response, HTTP_STATUS_405, HTTP_STATUS_405);
|
||||
proto_response_error(response, 405, HTTP_STATUS_405, ERR_HTTP_UNSUP_METHOD);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -473,7 +499,9 @@ static void opendhcp_http_info(http_request *request, hw_http_response *response
|
|||
hw_http_response_send(response, req, response_complete);
|
||||
}
|
||||
|
||||
static void opendhcp_http_get_userinfo(http_request *request, hw_http_response *response, void *UNUSED(user_data)) {
|
||||
static void opendhcp_http_get_userinfo(http_request *request,
|
||||
hw_http_response *response,
|
||||
void *UNUSED(user_data)) {
|
||||
hw_string status_code;
|
||||
hw_string content_type_name;
|
||||
hw_string content_type_value;
|
||||
|
@ -484,12 +512,12 @@ static void opendhcp_http_get_userinfo(http_request *request, hw_http_response *
|
|||
auto *req = (data19 *)malloc(sizeof(struct data19));
|
||||
|
||||
if (req == nullptr) {
|
||||
hw_http_response_send_error(response, HTTP_STATUS_500, "memory error");
|
||||
proto_response_error(response, 500, HTTP_STATUS_500, ERR_MALLOC_MEMORY);
|
||||
return;
|
||||
}
|
||||
|
||||
if (request->method != HW_HTTP_POST) {
|
||||
hw_http_response_send_error(response, HTTP_STATUS_405, HTTP_STATUS_405);
|
||||
proto_response_error(response, 405, HTTP_STATUS_405, ERR_HTTP_UNSUP_METHOD);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -501,6 +529,11 @@ static void opendhcp_http_get_userinfo(http_request *request, hw_http_response *
|
|||
|
||||
SETSTRING(status_code, HTTP_STATUS_200);
|
||||
sendUserList(req, request->body->value);
|
||||
|
||||
if (req->dp == nullptr) {
|
||||
proto_response_error(response, 500, HTTP_STATUS_500, ERR_JSON_PRASE_OBJ);
|
||||
return;
|
||||
}
|
||||
SETSTRING(body, req->dp);
|
||||
hw_set_body(response, &body);
|
||||
hw_set_response_status_code(response, &status_code);
|
||||
|
@ -516,7 +549,9 @@ static void opendhcp_http_get_userinfo(http_request *request, hw_http_response *
|
|||
hw_http_response_send(response, req, response_complete);
|
||||
}
|
||||
|
||||
static void opendhcp_http_get_alluser(http_request *request, hw_http_response *response, void *UNUSED(user_data)) {
|
||||
static void opendhcp_http_get_alluser(http_request *request,
|
||||
hw_http_response *response,
|
||||
void *UNUSED(user_data)) {
|
||||
hw_string status_code;
|
||||
hw_string content_type_name;
|
||||
hw_string content_type_value;
|
||||
|
@ -527,19 +562,19 @@ static void opendhcp_http_get_alluser(http_request *request, hw_http_response *r
|
|||
auto *req = (data19 *)malloc(sizeof(struct data19));
|
||||
|
||||
if (req == nullptr) {
|
||||
hw_http_response_send_error(response, HTTP_STATUS_500, "memory error");
|
||||
proto_response_error(response, 500, HTTP_STATUS_500, ERR_MALLOC_MEMORY);
|
||||
return;
|
||||
}
|
||||
|
||||
if (request->method != HW_HTTP_GET) {
|
||||
hw_http_response_send_error(response, HTTP_STATUS_405, HTTP_STATUS_405);
|
||||
proto_response_error(response, 405, HTTP_STATUS_405, ERR_HTTP_UNSUP_METHOD);
|
||||
return;
|
||||
}
|
||||
|
||||
memset(req, 0, sizeof(struct data19));
|
||||
|
||||
SETSTRING(content_type_name, "Content-Type");
|
||||
SETSTRING(content_type_value, "text/html");
|
||||
SETSTRING(content_type_value, "application/json");
|
||||
hw_set_response_header(response, &content_type_name, &content_type_value);
|
||||
|
||||
SETSTRING(status_code, HTTP_STATUS_200);
|
||||
|
@ -559,7 +594,9 @@ static void opendhcp_http_get_alluser(http_request *request, hw_http_response *r
|
|||
hw_http_response_send(response, req, response_complete);
|
||||
}
|
||||
|
||||
static void opendhcp_http_expand_rangeset(http_request *request, hw_http_response *response, void *UNUSED(user_data)) {
|
||||
static void opendhcp_http_expand_rangeset(http_request *request,
|
||||
hw_http_response *response,
|
||||
void *UNUSED(user_data)) {
|
||||
hw_string status_code;
|
||||
hw_string content_type_name;
|
||||
hw_string content_type_value;
|
||||
|
|
Loading…
Reference in New Issue