Skip to content

Commit edba2d2

Browse files
JeroenVogelpoeligrr
authored andcommitted
Exposed sketch MD5 through HTTP headers (#2236)
* Exposed sketch MD5 through HTTP headers * Updated spacing, docs, example
1 parent 0d996ab commit edba2d2

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

doc/ota_updates/readme.md

+17-9
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ Example header data:
371371
[HTTP_X_ESP8266_AP_MAC] => 1A:FE:AA:AA:AA:AA
372372
[HTTP_X_ESP8266_FREE_SPACE] => 671744
373373
[HTTP_X_ESP8266_SKETCH_SIZE] => 373940
374+
[HTTP_X_ESP8266_SKETCH_MD5] => a56f8ef78a0bebd812f62067daf1408a
374375
[HTTP_X_ESP8266_CHIP_SIZE] => 4194304
375376
[HTTP_X_ESP8266_SDK_VERSION] => 1.3.0
376377
[HTTP_X_ESP8266_VERSION] => DOOR-7-g14f53a19
@@ -415,9 +416,9 @@ if(
415416
!check_header('HTTP_X_ESP8266_AP_MAC') ||
416417
!check_header('HTTP_X_ESP8266_FREE_SPACE') ||
417418
!check_header('HTTP_X_ESP8266_SKETCH_SIZE') ||
419+
!check_header('HTTP_X_ESP8266_SKETCH_MD5') ||
418420
!check_header('HTTP_X_ESP8266_CHIP_SIZE') ||
419-
!check_header('HTTP_X_ESP8266_SDK_VERSION') ||
420-
!check_header('HTTP_X_ESP8266_VERSION')
421+
!check_header('HTTP_X_ESP8266_SDK_VERSION')
421422
) {
422423
header($_SERVER["SERVER_PROTOCOL"].' 403 Forbidden', true, 403);
423424
echo "only for ESP8266 updater! (header)\n";
@@ -429,13 +430,20 @@ $db = array(
429430
"18:FE:AA:AA:AA:BB" => "TEMP-1.0.0"
430431
);
431432

432-
if(isset($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']])) {
433-
if($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']] != $_SERVER['HTTP_X_ESP8266_VERSION']) {
434-
sendFile("./bin/".$db[$_SERVER['HTTP_X_ESP8266_STA_MAC']]."bin");
435-
} else {
436-
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
437-
}
438-
exit();
433+
if(!isset($db[$_SERVER['HTTP_X_ESP8266_STA_MAC']])) {
434+
header($_SERVER["SERVER_PROTOCOL"].' 500 ESP MAC not configured for updates', true, 500);
435+
}
436+
437+
$localBinary = "./bin/".$db[$_SERVER['HTTP_X_ESP8266_STA_MAC']].".bin";
438+
439+
// Check if version has been set and does not match, if not, check if
440+
// MD5 hash between local binary and ESP8266 binary do not match if not.
441+
// then no update has been found.
442+
if((!check_header('HTTP_X_ESP8266_SDK_VERSION') && $db[$_SERVER['HTTP_X_ESP8266_STA_MAC']] != $_SERVER['HTTP_X_ESP8266_VERSION'])
443+
|| $_SERVER["HTTP_X_ESP8266_SKETCH_MD5"] != md5_file($localBinary)) {
444+
sendFile($localBinary);
445+
} else {
446+
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
439447
}
440448

441449
header($_SERVER["SERVER_PROTOCOL"].' 500 no version for ESP MAC', true, 500);

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String&
175175
http.addHeader(F("x-ESP8266-AP-MAC"), WiFi.softAPmacAddress());
176176
http.addHeader(F("x-ESP8266-free-space"), String(ESP.getFreeSketchSpace()));
177177
http.addHeader(F("x-ESP8266-sketch-size"), String(ESP.getSketchSize()));
178+
http.addHeader(F("x-ESP8266-sketch-md5"), String(ESP.getSketchMD5()));
178179
http.addHeader(F("x-ESP8266-chip-size"), String(ESP.getFlashChipRealSize()));
179180
http.addHeader(F("x-ESP8266-sdk-version"), ESP.getSdkVersion());
180181

0 commit comments

Comments
 (0)