Skip to content

Commit

Permalink
Add https server to prevent mixed protocol errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis960 committed Sep 5, 2024
1 parent 5d10b15 commit 5f12eda
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 42 deletions.
4 changes: 3 additions & 1 deletion Firmware/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
idf_component_register(SRCS "index_html.c" "plantstore.c" "plantfi.c" "configuration_mode_server.c" "peripherals/sensors.c" "peripherals/adc.c" "main.c"
INCLUDE_DIRS ".")
INCLUDE_DIRS "."
EMBED_TXTFILES "certs/servercert.pem"
"certs/prvtkey.pem")
1 change: 1 addition & 0 deletions Firmware/main/certs/generate-certs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
openssl req -newkey rsa:2048 -nodes -keyout prvtkey.pem -x509 -days 3650 -out servercert.pem -subj "/CN=Blumy HTTPS Server"
28 changes: 28 additions & 0 deletions Firmware/main/certs/prvtkey.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDHsxSujHZ3DT6/
T6uiGVG7fnYQwK2fzvYaPSdOy7g1iO4GfZbOBfQNTQ71XRqvCCPNBijE7Qwur76p
YcQpR36ddlQ1rSl8mOwT/klDUNKXqDqHPseJ8F0PYw8Jgbcv5VbIuZTyDmYysDuW
5Lm6UPWmxZh+DxcORL1YEDs7c0UbUIFpoQoOplGSYSE9MrcOECLXLqVw208WZ4Bm
2J0xwO9u+gbQ9JPU4Zruja3uyHmpw7GYukf9E0Y7gFs6sBLAINqdBLoIscYb0DpL
6AnVXVd+SmmN+JrmyEpjYQzSGBhGIhz/vdyMd9zbqBlxVVv7ju9jAUPlDxcsm624
mceUjUUvAgMBAAECggEAHkbZlGCJwiFZHufv2trhiD6InPVG0vYDBwBi5MXrOaah
2+G6Svsea3kq6EAY6xTzWe1x90VZ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jMXByDh6xCEvZszAcj17Txmg9kz+LlgNFPUcfjaeqcclbwdajphetzwqpdDTVP
ANp5dThZ5BQUJTgf/8qBQg==
-----END PRIVATE KEY-----
19 changes: 19 additions & 0 deletions Firmware/main/certs/servercert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDGzCCAgOgAwIBAgIUQ9eSTZ4LT3UdjZwf+8CQahcRgUMwDQYJKoZIhvcNAQEL
BQAwHTEbMBkGA1UEAwwSQmx1bXkgSFRUUFMgU2VydmVyMB4XDTI0MDkwNTA5MDA1
MFoXDTM0MDkwMzA5MDA1MFowHTEbMBkGA1UEAwwSQmx1bXkgSFRUUFMgU2VydmVy
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAx7MUrox2dw0+v0+rohlR
u352EMCtn872Gj0nTsu4NYjuBn2WzgX0DU0O9V0arwgjzQYoxO0MLq++qWHEKUd+
nXZUNa0pfJjsE/5JQ1DSl6g6hz7HifBdD2MPCYG3L+VWyLmU8g5mMrA7luS5ulD1
psWYfg8XDkS9WBA7O3NFG1CBaaEKDqZRkmEhPTK3DhAi1y6lcNtPFmeAZtidMcDv
bvoG0PST1OGa7o2t7sh5qcOxmLpH/RNGO4BbOrASwCDanQS6CLHGG9A6S+gJ1V1X
fkppjfia5shKY2EM0hgYRiIc/73cjHfc26gZcVVb+47vYwFD5Q8XLJutuJnHlI1F
LwIDAQABo1MwUTAdBgNVHQ4EFgQU46/kQsNdMIUViADkWp1Zd0kL0rYwHwYDVR0j
BBgwFoAU46/kQsNdMIUViADkWp1Zd0kL0rYwDwYDVR0TAQH/BAUwAwEB/zANBgkq
hkiG9w0BAQsFAAOCAQEAhz5OC7yEN5Rel9E7AGf5NHKX19Bxg8sKJwFo93QPTgWy
tMIPR2O4HeGxaF0wOBH+CAWYF+/3r/OXSVIs7iz42i0lPZQ7ucagv32vQllBtaiZ
Tlen4dRYRZWIh3guh9UeGHaWjn3x04NgLyPBxoQLH8VO1rGGQbNb1iygzr7uDXEL
4rnqscXyHThCLLp6InKliRIF6GTwFeU5dt4SiKZj+1BxFsRy9pe4r/MC6q/tHdPz
ZI/A/vawdEkQ1VL9d0M1/pYYxTlm+3QQWfRdEFddhECycHqSeC8c28RUWeE0OXX0
RJiWDPo59gbGZwwQPF43N4SrHcv4lzq6gPSEuXtgJw==
-----END CERTIFICATE-----
114 changes: 78 additions & 36 deletions Firmware/main/configuration_mode_server.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "configuration_mode_server.h"

#include "esp_log.h"
#include "esp_http_server.h"
#include "esp_https_server.h"
#include "cJSON.h"

#include "plantfi.h"
Expand Down Expand Up @@ -792,54 +792,96 @@ httpd_uri_t get = {
.handler = get_handler,
.user_ctx = NULL};

void register_uri_handlers(httpd_handle_t server)
{
httpd_register_uri_handler(server, &post_api_connect);
httpd_register_uri_handler(server, &post_api_reset);
httpd_register_uri_handler(server, &get_api_networks);
httpd_register_uri_handler(server, &get_api_isConnected);
httpd_register_uri_handler(server, &post_api_cloudSetup_mqtt);
httpd_register_uri_handler(server, &post_api_cloudSetup_http);
httpd_register_uri_handler(server, &post_api_cloudSetup_blumy);
httpd_register_uri_handler(server, &get_api_cloudSetup_mqtt);
httpd_register_uri_handler(server, &get_api_cloudSetup_http);
httpd_register_uri_handler(server, &get_api_cloudSetup_blumy);
httpd_register_uri_handler(server, &post_api_cloudTest_mqtt);
httpd_register_uri_handler(server, &post_api_cloudTest_http);
httpd_register_uri_handler(server, &post_api_cloudTest_blumy);
httpd_register_uri_handler(server, &post_api_timeouts_sleep);
httpd_register_uri_handler(server, &get_api_timeouts_sleep);
httpd_register_uri_handler(server, &get_api_timeouts_configurationMode);
httpd_register_uri_handler(server, &post_api_timeouts_configurationMode);
httpd_register_uri_handler(server, &get_api_timeouts_wdt);
httpd_register_uri_handler(server, &post_api_timeouts_wdt);
httpd_register_uri_handler(server, &get_api_update_percentage);
httpd_register_uri_handler(server, &get_api_connectedNetwork);
httpd_register_uri_handler(server, &get_api_sensorData);
httpd_register_uri_handler(server, &post_api_factoryReset);
httpd_register_uri_handler(server, &post_api_update_firmware);
httpd_register_uri_handler(server, &get_api_update_firmware);
httpd_register_uri_handler(server, &post_api_update_check);

// Every other get request returns index.html
httpd_register_uri_handler(server, &get);
}

/* Function for starting the webserver */
httpd_handle_t start_https_webserver(void)
{
ESP_LOGI("HTTP", "Starting HTTPS server");
/* Empty handle to esp_http_server */
httpd_handle_t server = NULL;

/* Generate default configuration */
httpd_ssl_config_t config = HTTPD_SSL_CONFIG_DEFAULT();
config.httpd.uri_match_fn = httpd_uri_match_wildcard;
config.httpd.max_uri_handlers = 50;

extern const unsigned char servercert_start[] asm("_binary_servercert_pem_start");
extern const unsigned char servercert_end[] asm("_binary_servercert_pem_end");
config.servercert = servercert_start;
config.servercert_len = servercert_end - servercert_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");
config.prvtkey_pem = prvtkey_pem_start;
config.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start;

/* Start the httpd server */
ESP_ERROR_CHECK(httpd_ssl_start(&server, &config));
register_uri_handlers(server);
/* If server failed to start, handle will be NULL */
return server;
}

httpd_handle_t start_webserver(void)
{
ESP_LOGI("HTTP", "Starting HTTP server");
/* Empty handle to esp_http_server */
httpd_handle_t server = NULL;

/* Generate default configuration */
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.uri_match_fn = httpd_uri_match_wildcard;
config.max_uri_handlers = 50;

/* Empty handle to esp_http_server */
httpd_handle_t server = NULL;

/* Start the httpd server */
if (httpd_start(&server, &config) == ESP_OK)
{
httpd_register_uri_handler(server, &post_api_connect);
httpd_register_uri_handler(server, &post_api_reset);
httpd_register_uri_handler(server, &get_api_networks);
httpd_register_uri_handler(server, &get_api_isConnected);
httpd_register_uri_handler(server, &post_api_cloudSetup_mqtt);
httpd_register_uri_handler(server, &post_api_cloudSetup_http);
httpd_register_uri_handler(server, &post_api_cloudSetup_blumy);
httpd_register_uri_handler(server, &get_api_cloudSetup_mqtt);
httpd_register_uri_handler(server, &get_api_cloudSetup_http);
httpd_register_uri_handler(server, &get_api_cloudSetup_blumy);
httpd_register_uri_handler(server, &post_api_cloudTest_mqtt);
httpd_register_uri_handler(server, &post_api_cloudTest_http);
httpd_register_uri_handler(server, &post_api_cloudTest_blumy);
httpd_register_uri_handler(server, &post_api_timeouts_sleep);
httpd_register_uri_handler(server, &get_api_timeouts_sleep);
httpd_register_uri_handler(server, &get_api_timeouts_configurationMode);
httpd_register_uri_handler(server, &post_api_timeouts_configurationMode);
httpd_register_uri_handler(server, &get_api_timeouts_wdt);
httpd_register_uri_handler(server, &post_api_timeouts_wdt);
httpd_register_uri_handler(server, &get_api_update_percentage);
httpd_register_uri_handler(server, &get_api_connectedNetwork);
httpd_register_uri_handler(server, &get_api_sensorData);
httpd_register_uri_handler(server, &post_api_factoryReset);
httpd_register_uri_handler(server, &post_api_update_firmware);
httpd_register_uri_handler(server, &get_api_update_firmware);
httpd_register_uri_handler(server, &post_api_update_check);

// Every other get request returns index.html
httpd_register_uri_handler(server, &get);
}
ESP_ERROR_CHECK(httpd_start(&server, &config));
register_uri_handlers(server);
/* If server failed to start, handle will be NULL */
return server;
}

/* Function for stopping the webserver */
void stop_https_webserver(httpd_handle_t server)
{
if (server)
{
/* Stop the httpd server */
httpd_ssl_stop(server);
}
}

/* Function for stopping the webserver */
void stop_webserver(httpd_handle_t server)
{
Expand Down
4 changes: 3 additions & 1 deletion Firmware/main/configuration_mode_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

/* Function for starting the webserver */
httpd_handle_t start_webserver(void);
httpd_handle_t start_https_webserver(void);
/* Function for stopping the webserver */
void stop_webserver(httpd_handle_t server);
void stop_webserver(httpd_handle_t server);
void stop_https_webserver(httpd_handle_t server);
4 changes: 3 additions & 1 deletion Firmware/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ void configuration_mode(bool isConfigured)
plantfi_configureAp("Blumy", "", 4, &userConnectedToAp);

ESP_LOGI("MODE", "Starting webserver");
httpd_handle_t webserver = webserver = start_webserver();
httpd_handle_t webserver = start_webserver();
httpd_handle_t https_webserver = start_https_webserver();
plantfi_configureStaFromPlantstore();

bool wasBootButtonPressed = false;
Expand All @@ -63,6 +64,7 @@ void configuration_mode(bool isConfigured)
vTaskDelay(10 / portTICK_PERIOD_MS); // Reset watchdog
}
stop_webserver(webserver);
stop_https_webserver(https_webserver);
sensors_detach_boot_button_interrupt();
sensors_playShutdownSound();
start_deep_sleep();
Expand Down
4 changes: 2 additions & 2 deletions Firmware/sdkconfig
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS=y
#
# HTTP Server
#
CONFIG_HTTPD_MAX_REQ_HDR_LEN=512
CONFIG_HTTPD_MAX_REQ_HDR_LEN=1024
CONFIG_HTTPD_MAX_URI_LEN=512
CONFIG_HTTPD_ERR_RESP_NO_DELAY=y
CONFIG_HTTPD_PURGE_BUF_LEN=32
Expand All @@ -868,7 +868,7 @@ CONFIG_ESP_HTTPS_OTA_ALLOW_HTTP=y
#
# ESP HTTPS server
#
# CONFIG_ESP_HTTPS_SERVER_ENABLE is not set
CONFIG_ESP_HTTPS_SERVER_ENABLE=y
# end of ESP HTTPS server

#
Expand Down
2 changes: 1 addition & 1 deletion Server/src/routes/selector/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
async function sensorClick(sensor: SensorDTO) {
// TODO extract this constant value to an environment variable
await fetch('http://192.168.4.1/api/cloudSetup/blumy', {
await fetch('https://192.168.4.1/api/cloudSetup/blumy', {
method: 'POST',
body: `token=${sensor.writeToken}\nurl=${window.location.origin}/api/v2/data\n`
});
Expand Down

0 comments on commit 5f12eda

Please sign in to comment.