fix updater

This commit is contained in:
unknown 2020-03-09 19:45:49 +01:00
parent b16766c65e
commit f4ee4d5a7c
5 changed files with 56 additions and 10 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ _nemazat/css/main.css
_nemazat/css/font-awesome.min.css _nemazat/css/font-awesome.min.css
app/logs/*.log app/logs/*.log
.vscode/sftp.json .vscode/sftp.json
app/updater/*.bin

View File

@ -54,6 +54,8 @@ void setup() {
attachInterrupt(digitalPinToInterrupt(SWITCH), handleInterrupt, CHANGE); attachInterrupt(digitalPinToInterrupt(SWITCH), handleInterrupt, CHANGE);
//wifi //wifi
if (ssid != "") { if (ssid != "") {
WiFi.disconnect();
WiFi.softAPdisconnect(true);
WiFi.persistent(false); WiFi.persistent(false);
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pasw); WiFi.begin(ssid, pasw);
@ -81,7 +83,7 @@ void setup() {
); );
auto ret = ESPhttpUpdate.update(client, host2, url2); auto ret = ESPhttpUpdate.update(client, host2, 80, url2);
delay(500); delay(500);
switch(ret) { switch(ret) {
case HTTP_UPDATE_FAILED: case HTTP_UPDATE_FAILED:
@ -105,11 +107,11 @@ void setup() {
break; break;
} }
delay(500); delay(500);
Serial.println("Update proběhl!");
Serial.println(WiFi.localIP());
jsonContent = {}; jsonContent = {};
jsonContent["token"] = apiToken; jsonContent["token"] = apiToken;
jsonContent["values"]["on/off"]["value"] = (String)state; jsonContent["values"]["on/off"]["value"] = (String)state;
jsonContent["settings"]["network"]["ip"] = WiFi.localIP().toString();
jsonContent["settings"]["network"]["mac"] = WiFi.macAddress();
sendDataToWeb(); sendDataToWeb();
return; return;
} }

View File

@ -38,7 +38,9 @@ Db::connect (DBHOST, DBUSER, DBPASS, DBNAME);
//Read API data //Read API data
$json = file_get_contents('php://input'); $json = file_get_contents('php://input');
$obj = json_decode($json, true); $obj = json_decode($json, true);
if (defined(DEBUGMOD) && DEBUGMOD == 1) {
$logManager->write("[API] Rest API request body -> decodet to json \n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO); $logManager->write("[API] Rest API request body -> decodet to json \n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
}
//zabespecit proti Ddosu //zabespecit proti Ddosu
if (isset($obj['user']) && $obj['user'] != ''){ if (isset($obj['user']) && $obj['user'] != ''){
@ -48,7 +50,7 @@ if (isset($obj['user']) && $obj['user'] != ''){
$userId = $user['user_id']; $userId = $user['user_id'];
$atHome = $obj['atHome']; $atHome = $obj['atHome'];
UserManager::atHome($userId, $atHome); UserManager::atHome($userId, $atHome);
$logManager->write("[Record] user " . $userId . "changet his home state to " . $atHome . RECORDTIMOUT , LogRecordType::WARNING); $logManager->write("[Record] user " . $userId . " changet his home state to " . $atHome . " " . RECORDTIMOUT , LogRecordType::INFO);
echo 'Saved: ' . $atHome; echo 'Saved: ' . $atHome;
header("HTTP/1.1 200 OK"); header("HTTP/1.1 200 OK");
die(); die();

View File

@ -1,6 +1,7 @@
<pre> <pre>
<?php <?php
var_dump($_POST); var_dump($_POST);
var_dump(file_exists("./app/updater/" . "3C71BF22FDCF" . ".bin"));
?> ?>
</pre> </pre>

View File

@ -1,7 +1,31 @@
<?PHP <?PHP
/** Includes **/
include_once('./config.php');
//Autoloader
$files = scandir('./app/class/');
$files = array_diff($files, array(
'.',
'..',
'app',
'ChartJS.php',
'ChartJS_Line.php',
'ChartManager.php',
'DashboardManager.php',
'Partial.php',
'Form.php',
'Route.php',
'Template.php',
'Ajax.php',
));
foreach($files as $file) {
include './app/class/'. $file;
}
$logManager = new LogManager();
header('Content-type: text/plain; charset=utf8', true); header('Content-type: text/plain; charset=utf8', true);
/*
function check_header($name, $value = false) function check_header($name, $value = false)
{ {
if (!isset($_SERVER[$name])) { if (!isset($_SERVER[$name])) {
@ -11,7 +35,7 @@ function check_header($name, $value = false)
return false; return false;
} }
return true; return true;
} }*/
function sendFile($path) function sendFile($path)
{ {
@ -23,7 +47,8 @@ function sendFile($path)
readfile($path); readfile($path);
} }
if (!check_header('HTTP_USER_AGENT', 'ESP8266-http-Update')) {
/*if (!check_header('HTTP_USER_AGENT', 'ESP8266-http-Update')) {
header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden', true, 403); header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden', true, 403);
echo "only for ESP8266 updater!\n"; echo "only for ESP8266 updater!\n";
exit(); exit();
@ -41,7 +66,22 @@ if (
header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden', true, 403); header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden', true, 403);
echo "only for ESP8266 updater! (header)\n"; echo "only for ESP8266 updater! (header)\n";
exit(); exit();
} }*/
$localBinary = "./app/updater/" . $_SERVER['HTTP_X_ESP8266_STA_MAC'] . ".bin";
$localBinary = "./app/updater/" . str_replace(':', '', $_SERVER['HTTP_X_ESP8266_STA_MAC']) . ".bin";
$logManager->write("[Update] url: " . $localBinary, LogRecordType::INFO);
$logManager->write("[Update] version: " . $_SERVER['HTTP_X_ESP8266_SKETCH_MD5'], LogRecordType::INFO);
if (file_exists($localBinary)) {
$logManager->write("[Update] version PHP: " . md5_file($localBinary), LogRecordType::INFO);
if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) {
sendFile($localBinary); sendFile($localBinary);
} else {
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
}
} else {
header("HTTP/1.1 404 Not Found");
}
die();