我目前正在尝试实现一个保存方法,以便有人通过异步ESP服务器上的网页将ESP-32的模式从Accesspoint模式更改为Station模式。数据被放入JSON字符串并发送到ESP-32,在ESP-32中写入JSON文件,稍后再次读出并用于连接到网络。
else {
IP = "NULL";
Netmask = "NULL";
Gateway = "NULL";
var data = {ssid:ssid, password:password, IP:IP, Netmask:Netmask, Gateway:Gateway};
console.log(data);
var xhr = new XMLHttpRequest();
var url ="/settings";
xhr.onreadystatechange = function() {
if(this.readyState == 4 && this.status ==200) {
console.log(xhr.response);
}
};
xhr.open("POST", url, true);
xhr.send(JSON.stringify(data));
}这种方法的问题是,刺探网络的人可以直接获取JSON字符串,并访问ESP-32试图连接到的网络。这就是为什么我想实现一种轻量级的方式,在发送之前加密这个字符串,并在网页上使用Javascript进行本地加密。问题是,当我执行这个操作的时候,我不在线,所以从外部抓取脚本是不可能的,比如CryptoJS。我被困在这里好几个星期了,完全不知道该怎么做。
有谁有主意吗?
发布于 2021-04-01 04:13:04
防止窃听的实际方法是实现HTTPS。
多亏了wolfssl,ESP32具有内置的TLSv1.3支持。
下面是一个来自官方ESP-IDF examples的HTTPS服务器示例。
您需要一个服务器证书才能运行它。您可以使用生成带有openssl的自签名证书,或者如果您有一个有效的DNS名称,则可以从LetsEncrypt获取一个CA签名的证书。即使是带有TLS 1.2+的自签名证书也可以防止嗅探。
#include <esp_wifi.h>
#include <esp_event.h>
#include <esp_log.h>
#include <esp_system.h>
#include <nvs_flash.h>
#include <sys/param.h>
#include "esp_netif.h"
#include "esp_eth.h"
#include "protocol_examples_common.h"
#include <esp_https_server.h>
/* A simple example that demonstrates how to create GET and POST
* handlers and start an HTTPS server.
*/
static const char *TAG = "example";
/* An HTTP GET handler */
static esp_err_t root_get_handler(httpd_req_t *req)
{
httpd_resp_set_type(req, "text/html");
httpd_resp_send(req, "<h1>Hello Secure World!</h1>", HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}
static const httpd_uri_t root = {
.uri = "/",
.method = HTTP_GET,
.handler = root_get_handler
};
static httpd_handle_t start_webserver(void)
{
httpd_handle_t server = NULL;
// Start the httpd server
ESP_LOGI(TAG, "Starting server");
httpd_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT();
extern const unsigned char cacert_pem_start[] asm("_binary_cacert_pem_start");
extern const unsigned char cacert_pem_end[] asm("_binary_cacert_pem_end");
conf.cacert_pem = cacert_pem_start;
conf.cacert_len = cacert_pem_end - cacert_pem_start;
extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
extern const unsigned char prvtkey_pem_end[] asm("_binary_prvtkey_pem_end");
conf.prvtkey_pem = prvtkey_pem_start;
conf.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start;
esp_err_t ret = httpd_ssl_start(&server, &conf);
if (ESP_OK != ret) {
ESP_LOGI(TAG, "Error starting server!");
return NULL;
}
// Set URI handlers
ESP_LOGI(TAG, "Registering URI handlers");
httpd_register_uri_handler(server, &root);
return server;
}
static void stop_webserver(httpd_handle_t server)
{
// Stop the httpd server
httpd_ssl_stop(server);
}
static void disconnect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
httpd_handle_t* server = (httpd_handle_t*) arg;
if (*server) {
stop_webserver(*server);
*server = NULL;
}
}
static void connect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
httpd_handle_t* server = (httpd_handle_t*) arg;
if (*server == NULL) {
*server = start_webserver();
}
}
void app_main(void)
{
static httpd_handle_t server = NULL;
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
/* Register event handlers to start server when Wi-Fi or Ethernet is connected,
* and stop server when disconnection happens.
*/
#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
* Read "Establishing Wi-Fi or Ethernet Connection" section in
* examples/protocols/README.md for more information about this function.
*/
ESP_ERROR_CHECK(example_connect());
}ESP32甚至可以在SSL上做Websocket --参见wss_server示例。
https://stackoverflow.com/questions/66887995
复制相似问题