增加加密
This commit is contained in:
+89
-10
@@ -11,6 +11,7 @@
|
||||
#include "Serial.h"
|
||||
#include "Delay.h"
|
||||
#include "Debug.h"
|
||||
#include "Crypto.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h> // ← 添加atoi函数声明
|
||||
@@ -98,6 +99,42 @@ static void EpochToDateTimeUTC8(uint32_t epoch_seconds, ESP8266_DateTime* dateti
|
||||
datetime->day = (uint8_t)(days + 1U);
|
||||
}
|
||||
|
||||
static uint8_t ESP8266_CopyDecryptedBody(char* response,
|
||||
char* body_start,
|
||||
uint16_t body_len,
|
||||
char* response_buffer,
|
||||
uint16_t buffer_size)
|
||||
{
|
||||
uint8_t encrypted = 0;
|
||||
char saved_char;
|
||||
uint32_t plain_len;
|
||||
|
||||
if (strstr(response, "X-Encrypted: AES128-CBC") != NULL) {
|
||||
encrypted = 1;
|
||||
}
|
||||
|
||||
if (!encrypted) {
|
||||
if (body_len >= buffer_size) {
|
||||
body_len = buffer_size - 1;
|
||||
}
|
||||
strncpy(response_buffer, body_start, body_len);
|
||||
response_buffer[body_len] = '\0';
|
||||
return ESP8266_SUCCESS;
|
||||
}
|
||||
|
||||
saved_char = body_start[body_len];
|
||||
body_start[body_len] = '\0';
|
||||
plain_len = Crypto_DecryptJSON(body_start, response_buffer, buffer_size);
|
||||
body_start[body_len] = saved_char;
|
||||
|
||||
if (plain_len == 0) {
|
||||
DEBUG_ERROR("[ESP8266] Failed to decrypt encrypted HTTP response");
|
||||
return ESP8266_FAIL;
|
||||
}
|
||||
|
||||
return ESP8266_SUCCESS;
|
||||
}
|
||||
|
||||
/* --- 公共接口函数实现 --- */
|
||||
|
||||
uint8_t ESP8266_Setup(void)
|
||||
@@ -182,6 +219,7 @@ uint8_t ESP8266_SendEncryptedData(const char* encrypted_data)
|
||||
{
|
||||
char http_request[1024];
|
||||
char cmd_buffer[32];
|
||||
char response_body[192];
|
||||
uint32_t content_length = strlen(encrypted_data);
|
||||
char* response;
|
||||
uint8_t send_ok = 0;
|
||||
@@ -202,6 +240,7 @@ uint8_t ESP8266_SendEncryptedData(const char* encrypted_data)
|
||||
"Content-Type: application/octet-stream\r\n"
|
||||
"X-Api-Token: %s\r\n"
|
||||
"X-Encrypted: AES128-CBC\r\n"
|
||||
"X-Encrypted-Response: true\r\n"
|
||||
"Content-Length: %u\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n"
|
||||
@@ -254,10 +293,49 @@ uint8_t ESP8266_SendEncryptedData(const char* encrypted_data)
|
||||
send_ok = 1;
|
||||
}
|
||||
|
||||
if (send_ok && strstr(response, "HTTP/1.1 200") != NULL &&
|
||||
strstr(response, "\"success\":true") != NULL) {
|
||||
DEBUG_OK("[ESP8266] POST accepted by server");
|
||||
return ESP8266_SUCCESS;
|
||||
if (send_ok && strstr(response, "HTTP/1.1 200") != NULL) {
|
||||
char* ipd_marker = strstr(response, "+IPD,");
|
||||
if (ipd_marker != NULL) {
|
||||
char* data_start = strchr(ipd_marker, ':');
|
||||
if (data_start != NULL) {
|
||||
response = data_start + 1;
|
||||
}
|
||||
}
|
||||
|
||||
char* body_start = strstr(response, "\r\n\r\n");
|
||||
if (body_start != NULL) {
|
||||
uint16_t body_len;
|
||||
body_start += 4;
|
||||
body_len = strlen(body_start);
|
||||
char* cl_str = strstr(response, "Content-Length:");
|
||||
if (cl_str != NULL) {
|
||||
uint32_t post_content_length;
|
||||
cl_str += 15;
|
||||
while (*cl_str == ' ') cl_str++;
|
||||
post_content_length = (uint32_t)atoi(cl_str);
|
||||
if (post_content_length > 0 && body_len > post_content_length) {
|
||||
body_len = (uint16_t)post_content_length;
|
||||
}
|
||||
}
|
||||
|
||||
char* closed_marker = strstr(body_start, "\r\nCLOSED");
|
||||
if (closed_marker != NULL) {
|
||||
*closed_marker = '\0';
|
||||
body_len = strlen(body_start);
|
||||
}
|
||||
closed_marker = strstr(body_start, "CLOSED");
|
||||
if (closed_marker != NULL) {
|
||||
*closed_marker = '\0';
|
||||
body_len = strlen(body_start);
|
||||
}
|
||||
|
||||
if (ESP8266_CopyDecryptedBody(response, body_start, body_len,
|
||||
response_body, sizeof(response_body)) == ESP8266_SUCCESS
|
||||
&& strstr(response_body, "\"success\":true") != NULL) {
|
||||
DEBUG_OK("[ESP8266] POST accepted by server");
|
||||
return ESP8266_SUCCESS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (strstr(response, "HTTP/1.1 401") != NULL ||
|
||||
@@ -366,7 +444,7 @@ uint8_t ESP8266_CheckConnection(void)
|
||||
*/
|
||||
uint8_t ESP8266_HttpGet(const char* path, char* response_buffer, uint16_t buffer_size)
|
||||
{
|
||||
char http_request[256];
|
||||
char http_request[320];
|
||||
char cmd_buffer[32];
|
||||
uint8_t request_sent = 0;
|
||||
|
||||
@@ -390,6 +468,7 @@ uint8_t ESP8266_HttpGet(const char* path, char* response_buffer, uint16_t buffer
|
||||
"GET %s HTTP/1.1\r\n"
|
||||
"Host: %s:%s\r\n"
|
||||
"X-Api-Token: %s\r\n"
|
||||
"X-Encrypted-Response: true\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n",
|
||||
path,
|
||||
@@ -486,12 +565,12 @@ uint8_t ESP8266_HttpGet(const char* path, char* response_buffer, uint16_t buffer
|
||||
// 5. 如果服务端给了Content-Length,必须收完整;否则才使用超时兜底
|
||||
if ((found_header && body_len >= content_length) ||
|
||||
(!found_header && Get_Time_ms() - start_time > 3000 && body_len > 0)) {
|
||||
if (body_len >= buffer_size) {
|
||||
body_len = buffer_size - 1;
|
||||
if (found_header && body_len > content_length) {
|
||||
body_len = (uint16_t)content_length;
|
||||
}
|
||||
if (ESP8266_CopyDecryptedBody(response, body_start, body_len, response_buffer, buffer_size) != ESP8266_SUCCESS) {
|
||||
return ESP8266_FAIL;
|
||||
}
|
||||
|
||||
strncpy(response_buffer, body_start, body_len);
|
||||
response_buffer[body_len] = '\0';
|
||||
|
||||
DEBUG_PRINT("[ESP8266] Received response (%d bytes)\r\n", strlen(response_buffer));
|
||||
DEBUG_PRINT("[ESP8266] Response: %s\r\n", response_buffer);
|
||||
|
||||
Reference in New Issue
Block a user