From 22b5c97c93dcd481a21835b9b3e29d7dac438697 Mon Sep 17 00:00:00 2001 From: Valentino Orlandi Date: Wed, 17 Aug 2022 23:36:29 +0200 Subject: [PATCH] Added new tool 'Crapup' Implemented method: 'versionCheck'. --- logdoctor/tools/crapup/crapup.cpp | 119 ++++++++++++++++++++++++++++++ logdoctor/tools/crapup/crapup.h | 22 ++++++ 2 files changed, 141 insertions(+) create mode 100644 logdoctor/tools/crapup/crapup.cpp create mode 100644 logdoctor/tools/crapup/crapup.h diff --git a/logdoctor/tools/crapup/crapup.cpp b/logdoctor/tools/crapup/crapup.cpp new file mode 100644 index 00000000..c4c04aaf --- /dev/null +++ b/logdoctor/tools/crapup/crapup.cpp @@ -0,0 +1,119 @@ + +#include "crapup.h" + +#include "utilities/io.h" +#include "utilities/strings.h" + +#include + + +Crapup::Crapup() +{ + +} + + +size_t Crapup::WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) +{ + ((std::string*)userp)->append((char*)contents, size * nmemb); + return size * nmemb; +} + + +const std::tuple Crapup::versionCheck( const std::string& link ) +{ + bool successful = false; + float version = -1; + + CURL *curl; + CURLcode response; + + 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*/ + + // 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 ); + + // 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 ) ); + + } catch (std::invalid_argument& err) { + // failed to convert string to float + // !!! PUT A DIALOG MESSAGE HERE !!! + + } + } else { + // second version mark not found + // !!! PUT A DIALOG MESSAGE HERE !!! + } + } else { + // first version mark not found + // !!! PUT A DIALOG MESSAGE HERE !!! + } + } + + // cleanup curl stuff + curl_easy_cleanup( curl ); + curl_global_cleanup(); + + return std::make_tuple( successful, version ); +} diff --git a/logdoctor/tools/crapup/crapup.h b/logdoctor/tools/crapup/crapup.h new file mode 100644 index 00000000..8dee4b70 --- /dev/null +++ b/logdoctor/tools/crapup/crapup.h @@ -0,0 +1,22 @@ +#ifndef CRAPUP_H +#define CRAPUP_H + +#include +#include + + +class Crapup +{ +public: + Crapup(); + + // perform a GET request for a version-check + const std::tuple versionCheck( const std::string& link ); + + +private: + // write fetched data + static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp); +}; + +#endif // CRAPUP_H