From 63b3d97bafca284f5cb655f1c6fd0111b8faedab Mon Sep 17 00:00:00 2001 From: Valentino Orlandi Date: Thu, 18 Aug 2022 20:55:22 +0200 Subject: [PATCH] Improvements Removed repo-link from external file for better security, added built-in repository links. Code improvements. --- logdoctor/tools/crapup/crapup.cpp | 137 ++++++++++++++++-------------- logdoctor/tools/crapup/crapup.h | 2 +- 2 files changed, 73 insertions(+), 66 deletions(-) diff --git a/logdoctor/tools/crapup/crapup.cpp b/logdoctor/tools/crapup/crapup.cpp index c4c04aaf..66005d9f 100644 --- a/logdoctor/tools/crapup/crapup.cpp +++ b/logdoctor/tools/crapup/crapup.cpp @@ -1,10 +1,13 @@ #include "crapup.h" +#include "modules/dialogs.h" + #include "utilities/io.h" #include "utilities/strings.h" #include +#include Crapup::Crapup() @@ -20,7 +23,7 @@ size_t Crapup::WriteMemoryCallback(void *contents, size_t size, size_t nmemb, vo } -const std::tuple Crapup::versionCheck( const std::string& link ) +void Crapup::versionCheck( const float& v, const int& dialog_level ) { bool successful = false; float version = -1; @@ -30,90 +33,94 @@ const std::tuple Crapup::versionCheck( const std::string& link ) std::string content; std::string URL; - if ( link.size() > 0 ) { - URL = link; - } else { - //URL = "https://github.com/elB4RTO/LogDoctor/version.txt"; - URL = "https://github.com/elB4RTO/craplog-GUI/blob/main/version_check"; - } - /*#ifdef SKIP_PEER_VERIFICATION - /* - * If you want to connect to a site who is not using a certificate that is - * signed by one of the certs in the CA bundle you have, you can skip the - * verification of the server's certificate. This makes the connection - * A LOT LESS SECURE. - * - * If you have a CA cert for the server stored someplace else than in the - * default bundle, then the CURLOPT_CAPATH option might come handy for - * you. - * / - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); - #endif - - #ifdef SKIP_HOSTNAME_VERIFICATION - /* - * If the site you are connecting to uses a different host name that what - * they have mentioned in their server certificate's commonName (or - * subjectAltName) fields, libcurl will refuse to connect. You can skip - * this check, but this will make the connection less secure. - * / - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); - #endif*/ + const std::string links[3] = {"https://raw.githubusercontent.com/elB4RTO/LogDoctor/main/version.txt", + "https://git.disroot.org/elB4RTO/LogDoctor/raw/branch//main/version.txt", + "https://gitlab.com/elB4RTO/LogDoctor/-/raw/main/version.txt"}; // init the curl session curl_global_init( CURL_GLOBAL_ALL ); curl = curl_easy_init(); - // set the URL - curl_easy_setopt( curl, CURLOPT_URL, URL.c_str() ); // fetched data will be sent to this function curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback ); // fetched data will be stored in this variable curl_easy_setopt( curl, CURLOPT_WRITEDATA, &content ); // set the user agent - curl_easy_setopt( curl, CURLOPT_USERAGENT, "LogDoctor-libcurl/1.0 (version check)" ); - // make the request - response = curl_easy_perform( curl ); + curl_easy_setopt( curl, CURLOPT_USERAGENT, "LogDoctor/"+std::to_string(v)+" (version check)" ); - // check for errors - if ( response != CURLE_OK ) { - // failed - // !!! PUT A DIALOG MESSAGE HERE !!! - fprintf(stderr, "curl_easy_perform() failed: %s\n", - curl_easy_strerror(response)); - } - else { - // successful - const std::string version_mark = ".:!¦version¦!:."; - int start = content.find( version_mark ); - if ( start != std::string::npos ) { - // first found - start += version_mark.size(); - const int stop = content.find( version_mark, start ); - if ( stop != std::string::npos ) { - // second found too - try { - // get the version - version = std::stof( content.substr( start, stop-start ) ); + int err = 0; - } catch (std::invalid_argument& err) { - // failed to convert string to float - // !!! PUT A DIALOG MESSAGE HERE !!! + for ( const std::string& URL : links ) { - } - } else { - // second version mark not found - // !!! PUT A DIALOG MESSAGE HERE !!! + // reset the content + content.clear(); + + // set the URL + curl_easy_setopt( curl, CURLOPT_URL, URL.c_str() ); + // make the request + response = curl_easy_perform( curl ); + + // check for errors + if ( response != CURLE_OK ) { + // failed + err = 1; + if ( dialog_level == 2 ) { + DialogSec::warnConnectionFailed( nullptr, + QString::fromStdString( URL ), + QString::fromStdString( curl_easy_strerror(response)) ); } } else { - // first version mark not found - // !!! PUT A DIALOG MESSAGE HERE !!! + // successful + const std::string version_mark = ".:!¦version¦!:."; + int start = content.find( version_mark ); + if ( start != std::string::npos ) { + // first found + start += version_mark.size(); + const int stop = content.find( version_mark, start ); + if ( stop != std::string::npos ) { + // second found too + try { + // get the version + version = std::stof( content.substr( start, stop-start ) ); + successful = true; + break; + + } catch (std::invalid_argument& e) { + // failed to convert string to float + err = 11; + } + } else { + // second version mark not found + err = 10; + } + } else { + // first version mark not found + err = 10; + } } } + // cleanup curl stuff curl_easy_cleanup( curl ); curl_global_cleanup(); - return std::make_tuple( successful, version ); + if ( successful == true ) { + // check the versions + if ( version > v ) { + // new version available + DialogSec::msgNewVersion( nullptr ); + } else if ( version == v ) { + // same version + DialogSec::msgSameVersion( nullptr ); + } else if ( version > 0 ) { + // this version is beyond the current upstream version + DialogSec::warnFutureVersion( nullptr ); + } else { + // something went wrong, can't be successful if version is less than 0 + successful = false; + } + } else { + DialogSec::errVersionCheckFailed( nullptr, err ); + } } diff --git a/logdoctor/tools/crapup/crapup.h b/logdoctor/tools/crapup/crapup.h index 8dee4b70..55b14b00 100644 --- a/logdoctor/tools/crapup/crapup.h +++ b/logdoctor/tools/crapup/crapup.h @@ -11,7 +11,7 @@ public: Crapup(); // perform a GET request for a version-check - const std::tuple versionCheck( const std::string& link ); + void versionCheck( const float& current_version, const int& dialog_level ); private: