OTA: strenghten checks and introduce HTTPS support

This commit is contained in:
robert committed 2026-08-07 17:20:26 +03:00
1 parent 9f16806a22
commit 2696c1ff94
4 files changed
+86 -12

No files matched your search

+28
View File
@@ -3,6 +3,7 @@
#include "json.h"
#include "log.h"
#include "printf.h"
#include "tls.h"
#include "url.h"
#include "util.h"
@@ -26,6 +27,17 @@ static struct mg_ota_state {
static void s_firmware_fn(struct mg_connection *c, int ev, void *ev_data);
static bool s_tls_init(struct mg_connection *c, const char *url) {
struct mg_tls_opts opts = {
.ca = mg_str(MG_OTA_TLS_CA), .name = mg_url_host(url)};
if (opts.ca.len == 0) {
mg_error(c, "MG_OTA_TLS_CA is not configured");
} else {
mg_tls_init(c, &opts);
}
return c->is_tls_hs != 0 && !c->is_closing;
}
#if MG_ENABLE_CUSTOM_DEVICE_ID
#else
void mg_ota_device_id(char *buf, size_t len) {
@@ -58,6 +70,11 @@ void mg_ota_device_id(char *buf, size_t len) {
static void s_version_fn(struct mg_connection *c, int ev, void *ev_data) {
uint64_t expiration = *(uint64_t *) c->data;
if (s_ota == NULL) {
c->fn = NULL;
c->is_closing = 1;
return;
}
if (ev == MG_EV_POLL) {
if (mg_millis() > expiration) mg_error(c, "Metadata timeout");
} else if (ev == MG_EV_CONNECT) {
@@ -65,6 +82,9 @@ static void s_version_fn(struct mg_connection *c, int ev, void *ev_data) {
struct mg_str host = mg_url_host(s_ota->json_url);
const char *uri = mg_url_uri(s_ota->json_url);
const char *sep = strchr(uri, '?') == NULL ? "?" : "&";
if (mg_url_is_ssl(s_ota->json_url)) {
if (!s_tls_init(c, s_ota->json_url)) return;
}
mg_ota_device_id(id, sizeof(id));
id[sizeof(id) - 1] = '\0';
mg_printf(
@@ -130,10 +150,18 @@ static void status_fn_2(struct mg_connection *c, const char *errmsg) {
static void s_firmware_fn(struct mg_connection *c, int ev, void *ev_data) {
uint64_t expiration = *(uint64_t *) c->data;
if (s_ota == NULL) {
c->fn = NULL;
c->is_closing = 1;
return;
}
if (ev == MG_EV_POLL) {
if (mg_millis() > expiration) mg_error(c, "OTA timeout");
} else if (ev == MG_EV_CONNECT) {
struct mg_str host = mg_url_host(s_ota->url);
if (mg_url_is_ssl(s_ota->url)) {
if (!s_tls_init(c, s_ota->url)) return;
}
mg_printf(c,
"GET %s HTTP/1.1\r\n"
"Host: %.*s\r\n"