Updater now uses HTTP downloader class

This commit is contained in:
Thulinma 2017-10-31 16:58:20 +01:00
parent 528727329b
commit 11ea8b0ef2
2 changed files with 20 additions and 70 deletions

View file

@ -9,6 +9,7 @@
#include <mist/auth.h> #include <mist/auth.h>
#include <mist/config.h> #include <mist/config.h>
#include <mist/defines.h> #include <mist/defines.h>
#include <mist/downloader.h>
#include <mist/http_parser.h> #include <mist/http_parser.h>
#include <mist/timing.h> #include <mist/timing.h>
#include <signal.h> //for raise #include <signal.h> //for raise
@ -48,6 +49,7 @@ static bool writeFile(std::string filename, std::string &contents){
tthread::mutex updaterMutex; tthread::mutex updaterMutex;
uint8_t updatePerc = 0; uint8_t updatePerc = 0;
JSON::Value updates; JSON::Value updates;
HTTP::Downloader DL;
namespace Controller{ namespace Controller{
@ -65,21 +67,14 @@ namespace Controller{
updates = result; updates = result;
} }
if (!result["uptodate"] && updatePerc){ if (!result["uptodate"] && updatePerc){
Socket::Connection updrConn("releases.mistserver.org", 80, true); // loop through the available components, update them
if (!updrConn){ unsigned int needCount = result["needs_update"].size();
FAIL_MSG("Could not connect to releases.mistserver.org for update"); if (needCount){
}else{ jsonForEach(result["needs_update"], it){
// loop through the available components, update them if (!Controller::conf.is_active){break;}
unsigned int needCount = result["needs_update"].size(); updatePerc = ((it.num() * 99) / needCount) + 1;
if (needCount){ updateComponent(it->asStringRef(), result[it->asStringRef()].asStringRef());
jsonForEach(result["needs_update"], it){
if (!Controller::conf.is_active){break;}
updatePerc = ((it.num() * 99) / needCount) + 1;
updateComponent(it->asStringRef(), result[it->asStringRef()].asStringRef(),
updrConn);
}
} }
updrConn.close();
} }
updatePerc = 0; updatePerc = 0;
} }
@ -109,29 +104,14 @@ namespace Controller{
} }
HTTP::Parser http; HTTP::Parser http;
JSON::Value updrInfo; JSON::Value updrInfo;
// retrieve update information if (DL.get("http://releases.mistserver.org/getsums.php?verinfo=1&rel=" RELEASE "&pass=" SHARED_SECRET "&iid=" + instanceId) && DL.isOk()){
Socket::Connection updrConn("releases.mistserver.org", 80, true); updrInfo = JSON::fromString(DL.data());
if (!updrConn){ }else{
Log("UPDR", "Could not connect to releases.mistserver.org to get update information."); Log("UPDR", "Error getting update info: "+DL.getStatusText());
ret["error"] = "Could not connect to releases.mistserver.org to get update information."; ret["error"] = "Error getting update info: "+DL.getStatusText();
ret["uptodate"] = 1;
return ret; return ret;
} }
http.url = "/getsums.php?verinfo=1&rel=" RELEASE "&pass=" SHARED_SECRET "&iid=" + instanceId;
http.method = "GET";
http.SetHeader("Host", "releases.mistserver.org");
http.SetHeader("X-Version", PACKAGE_VERSION);
updrConn.SendNow(http.BuildRequest());
http.Clean();
unsigned int startTime = Util::epoch();
while ((Util::epoch() - startTime < 10) && (updrConn || updrConn.Received().size())){
if (updrConn.spool() && http.Read(updrConn)){
updrInfo = JSON::fromString(http.body);
break; // break out of while loop
}
Util::sleep(250);
}
updrConn.close();
if (updrInfo){ if (updrInfo){
if (updrInfo.isMember("error")){ if (updrInfo.isMember("error")){
Log("UPDR", updrInfo["error"].asStringRef()); Log("UPDR", updrInfo["error"].asStringRef());
@ -173,46 +153,17 @@ namespace Controller{
/// \param md5sum The MD5 sum of the latest version of this file. /// \param md5sum The MD5 sum of the latest version of this file.
/// \param updrConn An connection to releases.mistserver.org to (re)use. Will be (re)opened if /// \param updrConn An connection to releases.mistserver.org to (re)use. Will be (re)opened if
/// closed. /// closed.
void updateComponent(const std::string &component, const std::string &md5sum, void updateComponent(const std::string &component, const std::string &md5sum){
Socket::Connection &updrConn){
Log("UPDR", "Updating " + component); Log("UPDR", "Updating " + component);
std::string new_file; if (!DL.get("http://releases.mistserver.org/getfile.php?rel=" RELEASE "&pass=" SHARED_SECRET "&file=" + component) || !DL.isOk() || !DL.data().size()){
HTTP::Parser http;
http.url = "/getfile.php?rel=" RELEASE "&pass=" SHARED_SECRET "&file=" + component;
http.method = "GET";
http.SetHeader("Host", "releases.mistserver.org");
http.SetHeader("X-Version", PACKAGE_VERSION);
if (!updrConn){
updrConn = Socket::Connection("releases.mistserver.org", 80, true);
if (!updrConn){
FAIL_MSG("Could not connect to releases.mistserver.org for file download.");
return;
}
}
http.SendRequest(updrConn);
http.Clean();
uint64_t startTime = Util::bootSecs();
while ((Util::bootSecs() < startTime + 10) && updrConn && Controller::conf.is_active){
if (!updrConn.spool()){
Util::sleep(250);
continue;
}
if (http.Read(updrConn)){
new_file = http.body;
break; // break out of while loop
}
startTime = Util::bootSecs();
}
http.Clean();
if (new_file == ""){
FAIL_MSG("Could not retrieve new version of %s, continuing without", component.c_str()); FAIL_MSG("Could not retrieve new version of %s, continuing without", component.c_str());
return; return;
} }
if (Secure::md5(new_file) != md5sum){ if (Secure::md5(DL.data()) != md5sum){
FAIL_MSG("Checksum of %s incorrect, continuing without", component.c_str()); FAIL_MSG("Checksum of %s incorrect, continuing without", component.c_str());
return; return;
} }
if (!writeFile(Util::getMyPath() + component, new_file)){ if (!writeFile(Util::getMyPath() + component, DL.data())){
FAIL_MSG("Could not write updated version of %s, continuing without", component.c_str()); FAIL_MSG("Could not write updated version of %s, continuing without", component.c_str());
return; return;
} }

View file

@ -10,7 +10,6 @@ namespace Controller{
JSON::Value checkUpdateInfo(); JSON::Value checkUpdateInfo();
void checkUpdates(); void checkUpdates();
void insertUpdateInfo(JSON::Value &ret); void insertUpdateInfo(JSON::Value &ret);
void updateComponent(const std::string &component, const std::string &md5sum, void updateComponent(const std::string &component, const std::string &md5sum);
Socket::Connection &updrConn);
} }