2015-11-23 17:43:21 +01:00
|
|
|
#include "VersionChecker.h"
|
|
|
|
#include <sstream>
|
2015-11-24 08:08:11 +01:00
|
|
|
#include <messmer/cpp-utils/network/CurlHttpClient.h>
|
2015-11-23 17:43:21 +01:00
|
|
|
#include <boost/property_tree/json_parser.hpp>
|
|
|
|
#include <messmer/cpp-utils/logging/logging.h>
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
using boost::optional;
|
|
|
|
using boost::none;
|
|
|
|
using std::string;
|
2015-11-24 08:08:11 +01:00
|
|
|
using std::shared_ptr;
|
|
|
|
using std::make_shared;
|
|
|
|
using cpputils::HttpClient;
|
|
|
|
using cpputils::CurlHttpClient;
|
2015-11-23 17:43:21 +01:00
|
|
|
using boost::property_tree::ptree;
|
|
|
|
using boost::property_tree::json_parser_error;
|
|
|
|
using namespace cpputils::logging;
|
|
|
|
|
|
|
|
namespace cryfs {
|
|
|
|
|
2015-11-24 08:08:11 +01:00
|
|
|
VersionChecker::VersionChecker()
|
|
|
|
:VersionChecker(make_shared<CurlHttpClient>()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
VersionChecker::VersionChecker(shared_ptr<HttpClient> httpClient)
|
|
|
|
: _versionInfo(_getVersionInfo(std::move(httpClient))) {
|
2015-11-23 17:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
optional<string> VersionChecker::newestVersion() const {
|
|
|
|
if (_versionInfo == none) {
|
|
|
|
return none;
|
|
|
|
}
|
|
|
|
string version = _versionInfo->get("version_info.current", "");
|
|
|
|
if (version == "") {
|
|
|
|
return none;
|
|
|
|
}
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<string> VersionChecker::securityWarningFor(const string &version) const {
|
|
|
|
if (_versionInfo == none) {
|
|
|
|
return none;
|
|
|
|
}
|
2015-11-24 08:08:11 +01:00
|
|
|
auto warnings = _versionInfo->get_child_optional("warnings");
|
|
|
|
if (warnings == none) {
|
|
|
|
return none;
|
|
|
|
}
|
|
|
|
BOOST_FOREACH(const ptree::value_type &v, *warnings) {
|
2015-11-23 17:43:21 +01:00
|
|
|
if(v.first == version) {
|
|
|
|
return v.second.get_value<std::string>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return none;
|
|
|
|
}
|
|
|
|
|
2015-11-24 08:08:11 +01:00
|
|
|
optional<ptree> VersionChecker::_getVersionInfo(shared_ptr<HttpClient> httpClient) {
|
2015-11-24 08:24:37 +01:00
|
|
|
long timeoutMsec = 2000;
|
|
|
|
optional<string> response = httpClient->get("http://www.cryfs.org/version_info.json", timeoutMsec);
|
2015-11-23 17:43:21 +01:00
|
|
|
if (response == none) {
|
|
|
|
return none;
|
|
|
|
}
|
|
|
|
return _parseJson(*response);
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<ptree> VersionChecker::_parseJson(const string &json) {
|
|
|
|
try {
|
|
|
|
ptree pt;
|
|
|
|
std::istringstream input(json);
|
|
|
|
read_json(input, pt);
|
|
|
|
return pt;
|
|
|
|
} catch (const json_parser_error &e) {
|
|
|
|
LOG(WARN) << "Error parsing version information json object";
|
|
|
|
return none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|