mirror of https://github.com/layters/testshop
parent
278bc127f8
commit
58d5b44d68
@ -1,106 +0,0 @@
|
||||
#include "downloader.hpp"
|
||||
|
||||
#if defined(NEROSHOP_USE_QT)
|
||||
#include <QUrl>
|
||||
#include <QFile>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QString>
|
||||
#else
|
||||
#include <curl/curl.h>
|
||||
/* For older cURL versions you will also need
|
||||
#include <curl/types.h>
|
||||
#include <curl/easy.h>
|
||||
*/
|
||||
#endif
|
||||
|
||||
#include "../../neroshop_config.hpp" // for config path
|
||||
#include "extractor.hpp" // ../tools/tools.hpp included here
|
||||
#include "../tools/tools.hpp"
|
||||
|
||||
namespace {
|
||||
const std::string tor_archive_url { "https://archive.torproject.org/tor-package-archive/torbrowser" };
|
||||
const std::string tor_browser_version { "12.0.4" }; // tor 0.4.7.13
|
||||
const std::string tor_expert_bundle { "tor-expert-bundle-" + tor_browser_version + "-" +
|
||||
neroshop::device::get_os() + "-" +
|
||||
neroshop::device::get_architecture() + ".tar.gz" };
|
||||
|
||||
}
|
||||
|
||||
#if !defined(NEROSHOP_USE_QT)
|
||||
size_t neroshop::tools::downloader::write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
|
||||
size_t written = fwrite(ptr, size, nmemb, stream);
|
||||
return written;
|
||||
}
|
||||
#endif
|
||||
|
||||
void neroshop::tools::downloader::download_tor() {
|
||||
std::string config_path = NEROSHOP_DEFAULT_CONFIGURATION_PATH;
|
||||
// check if tor already exists, if so then exit function
|
||||
if(neroshop::filesystem::is_file(config_path + "/" + "tor/tor")) {
|
||||
std::cout << "Tor has already been installed at " << (config_path + "/" + "tor/tor") << "\n"; return;
|
||||
}
|
||||
|
||||
std::string tor { tor_archive_url + "/" + tor_browser_version + "/" + tor_expert_bundle };
|
||||
|
||||
std::string save_as { config_path + "/" + tor_expert_bundle };//"tor.tar.gz" };
|
||||
std::cout << "downloading " << tor << " to " << config_path << "\n\n";
|
||||
#if defined(NEROSHOP_USE_QT)
|
||||
QNetworkAccessManager * manager = new QNetworkAccessManager(nullptr);
|
||||
QNetworkReply * reply = manager->get(QNetworkRequest(QUrl(QString::fromStdString(tor))));
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::downloadProgress, [=](qint64 bytesReceived, qint64 bytesTotal) {
|
||||
qInfo() << "Download progress:" << bytesReceived << "/" << bytesTotal;
|
||||
});
|
||||
|
||||
QFile* file = new QFile(QString::fromStdString(save_as));
|
||||
if (!file->open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
qDebug() << "Failed to open file for writing:" << file->errorString();
|
||||
delete file;
|
||||
delete manager;
|
||||
return;
|
||||
}
|
||||
// Connect to the finished signal to write the file
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
file->write(reply->readAll());
|
||||
qInfo() << "Download succeeded";
|
||||
} else {
|
||||
qWarning() << "Download failed:" << reply->errorString();
|
||||
}
|
||||
file->flush();
|
||||
file->close();
|
||||
delete file;
|
||||
reply->deleteLater();
|
||||
manager->deleteLater();// or delete manager;
|
||||
// Extract the file
|
||||
if(!neroshop::filesystem::is_file(save_as)) { std::cout << "Error downloading tor\n"; return; }
|
||||
neroshop::tools::extractor::extract_tar(save_as);
|
||||
});
|
||||
#else
|
||||
CURL *curl;
|
||||
FILE *fp;
|
||||
CURLcode res;
|
||||
const char * outfilename = save_as.c_str();
|
||||
assert(save_as.length() <= FILENAME_MAX); // FILENAME_MAX=4096
|
||||
curl = curl_easy_init();
|
||||
if (curl) {
|
||||
fp = fopen(outfilename,"wb");
|
||||
curl_easy_setopt(curl, CURLOPT_URL, tor.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
|
||||
res = curl_easy_perform(curl);
|
||||
/* always cleanup */
|
||||
curl_easy_cleanup(curl);
|
||||
fclose(fp);
|
||||
}
|
||||
// Extract the file
|
||||
if(!neroshop::filesystem::is_file(save_as)) { std::cout << "Error downloading tor\n"; return; }
|
||||
neroshop::tools::extractor::extract_tar(save_as);
|
||||
#endif
|
||||
}
|
||||
|
||||
void neroshop::tools::downloader::download_i2pd() {
|
||||
// TODO: use libi2pd instead
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef DOWNLOADER_HPP_NEROSHOP
|
||||
#define DOWNLOADER_HPP_NEROSHOP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace neroshop {
|
||||
|
||||
namespace tools {
|
||||
|
||||
struct downloader {
|
||||
static void download_tor();
|
||||
static void download_i2pd();
|
||||
private:
|
||||
#if !defined(NEROSHOP_USE_QT)
|
||||
static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream);
|
||||
#endif
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
@ -1,54 +0,0 @@
|
||||
#include "extractor.hpp"
|
||||
|
||||
#include "../tools/tools.hpp" // neroshop::filesystem::
|
||||
|
||||
#if defined(NEROSHOP_USE_QT)
|
||||
#include <QDebug>
|
||||
#include <QProcess>
|
||||
#include <QString>
|
||||
#else
|
||||
#include <cstdlib> // std::system
|
||||
#include <cstdio> // std::remove, FILE
|
||||
#endif
|
||||
|
||||
#include <cassert> // assert
|
||||
|
||||
void neroshop::tools::extractor::extract_tar(const std::string& filename) {
|
||||
std::cout << "extracting " << "tor/ from " << filename << "\n";
|
||||
|
||||
std::string folder = filename.substr(0, filename.find_last_of("\\/")); // get path from filename
|
||||
assert(neroshop::filesystem::is_directory(folder));
|
||||
|
||||
#if defined(NEROSHOP_USE_QT)
|
||||
QString fileName = QString::fromStdString(filename);
|
||||
QString extractDir = QString::fromStdString(folder);
|
||||
QString wildcards = "tor/*";
|
||||
|
||||
QProcess process;
|
||||
process.setProgram("tar");
|
||||
|
||||
QStringList arguments;
|
||||
arguments << "-xzf" << fileName << "-C" << extractDir << "--wildcards" << wildcards;
|
||||
process.setArguments(arguments);
|
||||
|
||||
process.start();
|
||||
process.waitForFinished();
|
||||
|
||||
if(process.exitCode() != 0) {
|
||||
qDebug() << process.readAllStandardError();
|
||||
}
|
||||
#elif defined(__gnu_linux__) && !defined(NEROSHOP_USE_QT)
|
||||
std::system(std::string("tar -xzf " + filename + " -C " + folder + " --wildcards tor/*").c_str());
|
||||
#endif
|
||||
// Check if tor/tor file was extracted or not
|
||||
std::string out_file { folder + "/" + "tor/tor" };
|
||||
|
||||
if(!neroshop::filesystem::is_file(out_file)) {
|
||||
std::cout << "Error extracting tar\n"; return;// or try a different method?
|
||||
}
|
||||
std::cout << "Extraction completed!\n";
|
||||
std::remove(filename.c_str()); // Delete the tar.gz file after we're done extracting
|
||||
}
|
||||
|
||||
void neroshop::tools::extractor::extract_zip(const std::string& filename) {
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace neroshop {
|
||||
|
||||
namespace tools {
|
||||
|
||||
namespace extractor {
|
||||
void extract_tar(const std::string& filename);
|
||||
void extract_zip(const std::string& filename);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue