libcryfs/src/cryfs-cli/VersionChecker.cpp

74 lines
2.1 KiB
C++
Raw Normal View History

#include "VersionChecker.h"
#include <sstream>
#include <boost/property_tree/json_parser.hpp>
2016-02-11 16:39:42 +01:00
#include <cpp-utils/logging/logging.h>
#include <boost/foreach.hpp>
using boost::optional;
using boost::none;
using std::string;
using cpputils::HttpClient;
using boost::property_tree::ptree;
using boost::property_tree::json_parser_error;
using namespace cpputils::logging;
namespace cryfs_cli {
2017-09-30 23:24:33 +02:00
VersionChecker::VersionChecker(HttpClient* httpClient)
2018-09-17 08:37:12 +02:00
: _versionInfo(_getVersionInfo(httpClient)) {}
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;
}
auto warnings = _versionInfo->get_child_optional("warnings");
if (warnings == none) {
return none;
}
// NOLINTNEXTLINE(bugprone-branch-clone)
BOOST_FOREACH(const ptree::value_type &v, *warnings) {
if(v.first == version) {
return v.second.get_value<std::string>();
}
}
return none;
}
2017-09-30 23:24:33 +02:00
optional<ptree> VersionChecker::_getVersionInfo(HttpClient* httpClient) {
long timeoutMsec = 2000;
2018-09-17 08:37:12 +02:00
string response;
try {
response = httpClient->get("https://www.cryfs.org/version_info.json", timeoutMsec);
}
catch (const std::exception& e) {
LOG(WARN, "HTTP Error: {}", e.what());
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) {
2017-02-04 20:03:20 +01:00
LOG(WARN, "Error parsing version information json object");
return none;
}
}
}