mirror of https://github.com/layters/testshop
commit
93c680a614
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,105 +0,0 @@
|
||||
#include "regex.hpp"
|
||||
|
||||
#include "../tools/logger.hpp" // neroshop::print
|
||||
|
||||
bool neroshop::string_tools::is_email(const std::string& email) {
|
||||
const std::regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
|
||||
return std::regex_match(email, pattern);
|
||||
}
|
||||
|
||||
bool neroshop::string_tools::is_strong_password(const std::string& password) {
|
||||
// Minimum eight characters, at least one uppercase letter, one lowercase letter, one number and one special character:
|
||||
const std::regex pattern("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$"); // source: https://stackoverflow.com/questions/19605150/regex-for-password-must-contain-at-least-eight-characters-at-least-one-number-a
|
||||
return std::regex_match(password, pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
The username must begin with a letter (uppercase or lowercase), represented by [a-zA-Z].
|
||||
The username can contain any combination of letters (uppercase or lowercase), numbers, period . , underscore _ or hyphen -, represented by [a-zA-Z0-9._-].
|
||||
The username must be between 2 and 30 characters long, which is enforced by {0,28} in the regex. The total length includes the starting and ending letter, hence 0 to 28 characters are allowed between them.
|
||||
The username must end with a letter or number, which is enforced by [a-zA-Z0-9]$.
|
||||
**/
|
||||
bool neroshop::string_tools::is_valid_username(const std::string& username) {
|
||||
const std::regex pattern("^[a-zA-Z][a-zA-Z0-9._-]{0,28}[a-zA-Z0-9]$");
|
||||
return std::regex_match(username, pattern);
|
||||
}
|
||||
|
||||
// untested
|
||||
bool is_product_code(const std::string& code) {
|
||||
// Define regular expressions for each product code type
|
||||
std::regex upc("^\\d{12}$");
|
||||
std::regex ean("^\\d{13}$");
|
||||
std::regex jan("^49\\d{10}$");
|
||||
std::regex isbn("^(\\d{9}(\\d|X)|978\\d{10}|979\\d{10})$"); // validates both ISBN-10 and ISBN-13 codes
|
||||
std::regex issn("^\\d{8}$");
|
||||
std::regex gtin("^\\d{8,14}$");
|
||||
std::regex sku("^\\w+$"); // Assumes SKU can contain alphanumeric characters
|
||||
std::regex mpn("^\\w+$"); // Assumes MPN can contain alphanumeric characters
|
||||
std::regex ndc("^\\d{10}$");
|
||||
|
||||
// Check if the code matches any of the regular expressions
|
||||
return std::regex_match(code, upc) || std::regex_match(code, ean) || std::regex_match(code, isbn) ||
|
||||
std::regex_match(code, issn) || std::regex_match(code, gtin) || std::regex_match(code, sku) ||
|
||||
std::regex_match(code, mpn) || std::regex_match(code, ndc);
|
||||
}
|
||||
|
||||
/*int main() {
|
||||
std::string email = "mail@neroshop.org";
|
||||
if(!neroshop::string_tools::is_email(email)) {
|
||||
neroshop::print("Email address is not valid", 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string password = "supersecretpassword123";
|
||||
if(!neroshop::string_tools::is_strong_password(password))
|
||||
{
|
||||
// Figure out the specific reason why password failed to follow the regex rules //if(password.empty()) { std::cout << "Please enter a valid password" << std::endl; return false; }
|
||||
if(!std::regex_search(password.c_str(), std::regex("(?=.*?[A-Z])"))) {
|
||||
neroshop::print("Password must have at least one upper case letter", 1);
|
||||
}
|
||||
if(!std::regex_search(password.c_str(), std::regex("(?=.*?[a-z])"))) {
|
||||
neroshop::print("Password must have at least one lower case letter", 1);
|
||||
}
|
||||
if(!std::regex_search(password.c_str(), std::regex("(?=.*?[0-9])"))) {
|
||||
neroshop::print("Password must have at least one digit", 1);
|
||||
}
|
||||
if(!std::regex_search(password.c_str(), std::regex("(?=.*?[#?!@$%^&*-])"))) {
|
||||
neroshop::print("Password must have at least one special character", 1);
|
||||
}
|
||||
if(password.length() < 8) {
|
||||
neroshop::print("Password must be at least 8 characters long", 1);
|
||||
}
|
||||
////neroshop::print("Please enter a stronger password (at least 1 upper case letter, 1 lower case letter, 1 digit, and 1 special character)", 1);
|
||||
////return 1;
|
||||
}
|
||||
|
||||
std::string username = "Jack_ass-wipe.dude";//"k_u_n";
|
||||
if(!neroshop::string_tools::is_valid_username(username)) {
|
||||
neroshop::print("Invalid username: " + username, 1);
|
||||
if (username.length() < 2) {
|
||||
std::cout << "must be at least 2 characters in length\n";
|
||||
}
|
||||
if (username.length() > 30) {
|
||||
std::cout << "cannot exceed 30 characters in length\n";
|
||||
}
|
||||
if (std::regex_search(username, std::regex("\\s"))) {
|
||||
std::cout << "cannot contain spaces\n";
|
||||
}
|
||||
if (!std::regex_search(username, std::regex("^[a-zA-Z]"))) {
|
||||
std::cout << "must begin with a letter (cannot start with a symbol or number)\n";
|
||||
}
|
||||
if (!std::regex_search(username, std::regex("[a-zA-Z0-9]$"))) {
|
||||
std::cout << "must end with a letter or number (cannot end with a symbol)\n";
|
||||
}
|
||||
if (std::regex_search(username, std::regex("[^a-zA-Z0-9._-]"))) {
|
||||
std::cout << "contains invalid symbol(s) (only '.', '_', and '-' are allowed in between the username)\n";
|
||||
}
|
||||
if (username == "Guest") {
|
||||
std::cout << "name \"Guest\" is reserved for guests only and cannot be used by any other user\n";
|
||||
}
|
||||
////return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
} // g++ regex.cpp logger.cpp -std=c++17 -I.
|
||||
*/
|
@ -1,18 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
|
||||
namespace neroshop {
|
||||
|
||||
namespace string_tools {
|
||||
|
||||
bool is_email(const std::string& email);
|
||||
bool is_strong_password(const std::string& password);
|
||||
bool is_valid_username(const std::string& username);
|
||||
|
||||
bool is_product_code(const std::string& code);
|
||||
}
|
||||
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef TOOLS_HPP_NEROSHOP
|
||||
#define TOOLS_HPP_NEROSHOP
|
||||
|
||||
#include "device.hpp"
|
||||
#include "filesystem.hpp"
|
||||
#include "string.hpp"
|
||||
#include "uuid.hpp"
|
||||
|
||||
#endif
|
Loading…
Reference in new issue