add clen function & delete threadVector & remove remove in desteructor

This commit is contained in:
Markus 2019-08-16 20:12:25 +02:00
parent 031a0b2bc9
commit 4194eafb9c
6 changed files with 73 additions and 30 deletions

View File

@ -8,8 +8,8 @@ PageManager::PageManager(std::string sock5Proxy, std::string cookieFilePath)


PageManager::~PageManager() PageManager::~PageManager()
{ {
remove(cookieFilePath.c_str());
curl_global_cleanup(); curl_global_cleanup();
std::cout << "des" << std::endl;
} }


void PageManager::setProxy(std::string ip, int port) void PageManager::setProxy(std::string ip, int port)
@ -42,7 +42,7 @@ Reply PageManager::getServerRequest(std::string Url, bool useCookies, std::strin
char *url; char *url;
std::string returnUrl; std::string returnUrl;


std::cout << "\33[2K\r" << "Lade: '" << Url << "'..." << std::flush; std::cout << ( "\33[2K\rLade: '" + Url + "'..." ) << std::flush;


curl = curl_easy_init(); curl = curl_easy_init();
if(!curl) { if(!curl) {
@ -68,14 +68,13 @@ Reply PageManager::getServerRequest(std::string Url, bool useCookies, std::strin
for (int timeout = 1; timeout <= maxTimeout; ++timeout) { for (int timeout = 1; timeout <= maxTimeout; ++timeout) {
/* Perform the request, res will get the return code */ /* Perform the request, res will get the return code */
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
std::cout << "\33[2K\r" << std::flush;


if(res != CURLE_OK) { if(res != CURLE_OK) {
if(timeout == maxTimeout) { if(timeout == maxTimeout) {
perror((std::string(" => Error: curl_easy_perform() failed: ") + curl_easy_strerror(res)).c_str()); perror((std::string("\33[2K\r => Error: curl_easy_perform() failed: ") + curl_easy_strerror(res)).c_str());
return Reply("-1"); return Reply("-1");
} else { } else {
std::cout << "\33[2K\r" << " => Warning: Versuch " << timeout << " von " << maxTimeout << ": curl_easy_perform() failed: " << curl_easy_strerror(res) << std::flush; std::cout << std::string( "\33[2K\r => Warning: Versuch " + std::to_string(timeout) + " von " + std::to_string(maxTimeout) + ": curl_easy_perform() failed: " + curl_easy_strerror(res) )<< std::flush;
sleep(1); sleep(1);
} }
} else { } else {
@ -87,7 +86,7 @@ Reply PageManager::getServerRequest(std::string Url, bool useCookies, std::strin
//Get Url //Get Url
res = curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url); res = curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
if( res != CURLE_OK || !url ) { if( res != CURLE_OK || !url ) {
perror((std::string(" => Error: curl_easy_getinfo failed: ") + curl_easy_strerror(res)).c_str()); perror((std::string("\33[2K\r => Error: curl_easy_getinfo failed: ") + curl_easy_strerror(res)).c_str());
return Reply("-1"); return Reply("-1");
} else } else
returnUrl=url; returnUrl=url;

View File

@ -1,16 +1,14 @@
#ifndef MANAGEPAGE_H #ifndef MANAGEPAGE_H
#define MANAGEPAGE_H #define MANAGEPAGE_H


#include <iostream> #include "accountManager.h"
// if complied for windows; everything is in */projectfolder/curl/
#include <curl/curl.h> #include <curl/curl.h>
#include <string.h> #include <string.h>
#include <sstream> #include <sstream>
#include <unistd.h> #include <unistd.h>

#include <algorithm> #include <algorithm>


#include "accountManager.h"


struct Reply { struct Reply {
Reply() {} Reply() {}

View File

@ -32,7 +32,7 @@ int manageParameter(Settings &settings, int argc, char **argv)
return 1; return 1;
} }


int res = compare("--help\ndefault\nurl\n--version\nsearch\ninfo", argv[1]); int res = compare("--help\ndefault\nurl\n--version\nsearch\ninfo\nclean", argv[1]);
if(res != 1) { if(res != 1) {
std::cout << " => Error: " << ( (res == 0) ? std::string("Unbekannte Unteroption: '") + argv[1] + "'" : std::cout << " => Error: " << ( (res == 0) ? std::string("Unbekannte Unteroption: '") + argv[1] + "'" :
std::string("Mehrere Optionen für '") + argv[1] + std::string("' gefunden.") ) << std::endl; std::string("Mehrere Optionen für '") + argv[1] + std::string("' gefunden.") ) << std::endl;
@ -65,6 +65,11 @@ int manageParameter(Settings &settings, int argc, char **argv)
argv[1][0] = '\0'; argv[1][0] = '\0';
return unterOption_info(&settings, argc, argv); return unterOption_info(&settings, argc, argv);


} else if (strncmp(argv[1], "clean", strlen(argv[1])) == 0) {
argv[1][0] = '\0';
unterOption_clean(&settings, argc, argv);
return -1;

} else { } else {
std::cout << "Error: Invalid option " << argv[1] << ", but not detected in compare-Function" << std::endl; std::cout << "Error: Invalid option " << argv[1] << ", but not detected in compare-Function" << std::endl;
return 3; return 3;
@ -80,7 +85,8 @@ int unterOption_help()
<< "\t„url“\t\tModus um eigene Redirect-Links umzuwandeln." << std::endl << "\t„url“\t\tModus um eigene Redirect-Links umzuwandeln." << std::endl
<< "\t„default“\tModus um Links von Serien zu bekommen." << std::endl << "\t„default“\tModus um Links von Serien zu bekommen." << std::endl
<< "\t„search“\tModus um Serien zu suchen." << std::endl << "\t„search“\tModus um Serien zu suchen." << std::endl
<< "\t„info“\tModus um Infos einer Serien zu bekommen." << std::endl; << "\t„info“\tModus um Infos einer Serien zu bekommen." << std::endl
<< "\t„clean“\tModus um Cookie-Files zu löschen." << std::endl;


return -1; return -1;
} }
@ -717,3 +723,44 @@ void setPathSymbol(Settings &settings)
settings.pathSymbol = '\\'; settings.pathSymbol = '\\';
#endif #endif
} }

bool fileExists (const std::string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0 && S_ISREG(buffer.st_mode));
}

void unterOption_clean(Settings * settings, int argc, char **argv)
{
if(argc > 1)
if(strcmp(argv[1], "--help") == 0) {
std::cout << "Aufruf: " << getProgramName() << " clean" << std::endl << std::endl;
std::cout << "Mit dieser Function werden die Cookie-Files gelöscht." << std::endl;
return;
}

unsigned count = 0;
if(fileExists(settings->cookieFilePath)) {
if(remove(settings->cookieFilePath.c_str()) != 0) {
std::cout << "Das löschen von " << settings->cookieFilePath << " ist fehlgeschlagen: " << errno << std::endl;
return;
} else {
count++;
}
}

for (unsigned i = 0; i < UINT_MAX && fileExists(settings->cookieFilePath + std::to_string(i)); i++) {
if(remove( (settings->cookieFilePath + std::to_string(i)).c_str() ) != 0) {
std::cout << "Das löschen von " << settings->cookieFilePath + std::to_string(i) << " ist fehlgeschlagen: " << errno << std::endl;
return;
} else {
count++;
}
}

if(count == 0)
std::cout << " => Nichts zu tun: Keine Cookies vorhanden." << std::endl;
else
std::cout << " => " << count << " Cookie-Files gelöscht." << std::endl;

return;
}

View File

@ -81,5 +81,7 @@ void unterOption_search_help();
int unterOption_info(Settings * settings, int argc, char **argv); int unterOption_info(Settings * settings, int argc, char **argv);
void unterOption_info_help(); void unterOption_info_help();


void unterOption_clean(Settings * settings, int argc, char **argv);



#endif // PARAMETERMANAGER_H #endif // PARAMETERMANAGER_H

View File

@ -9,7 +9,11 @@ ProgramManager::ProgramManager()


ProgramManager::~ProgramManager() ProgramManager::~ProgramManager()
{ {

for(auto e : threadList) {
delete e->pageManager;
delete e;
e = nullptr;
}
} }


int ProgramManager::start(Settings *settings) int ProgramManager::start(Settings *settings)
@ -162,18 +166,13 @@ int ProgramManager::defaultModus(Settings *settings)
return 29; return 29;
} else { } else {
for (unsigned i = 0; i < settings->maxThreads; ++i) { for (unsigned i = 0; i < settings->maxThreads; ++i) {
ka * newKa = new ka(i); PageManager * pm = new PageManager("socks5://" + settings->proxy_ip + ":" + std::to_string(settings->proxy_port), settings->cookieFilePath + std::to_string( i ));
PageManager * pm = new PageManager;
pm->setProxy(settings->proxy_ip, settings->proxy_port);
pm->setCookieFilePath(settings->cookieFilePath + std::to_string( i ));
pm->setDebugMode(settings->debugMode); pm->setDebugMode(settings->debugMode);
newKa->pageManager = pm;


newKa->settings = settings; //pm->setProxy(settings->proxy_ip, settings->proxy_port);
newKa->accountManager = &accountManager; //pm->setCookieFilePath(settings->cookieFilePath + std::to_string( i ));
newKa->nameInUrl = nameInUrl;
newKa->thread = 0; // Sonst error wenn join & nicht gesetzt ka * newKa = new ka(i, 0, nameInUrl, -1, settings, &accountManager, pm);
newKa->exitState = -1;
threadList.push_back(newKa); threadList.push_back(newKa);
} }
} }
@ -286,11 +285,11 @@ int ProgramManager::waitForThreads()
for( auto &e : threadList) { for( auto &e : threadList) {
if( e->exitState != -1 && pthread_join(e->thread, nullptr) == 0) { if( e->exitState != -1 && pthread_join(e->thread, nullptr) == 0) {
if(e->exitState != 0) { if(e->exitState != 0) {
std::cout << "Error: Thread gab error zurück." << std::endl; std::cout << "\33[2K\r => Error: Thread gab error zurück." << std::endl;
return e->exitState; return e->exitState;
} else } else
if(e->returnValue != "") if(e->returnValue != "")
std::cout << "(T" << e->id << ")" << e->returnValue << std::endl; std::cout << ("\33[2K\r(T" + std::to_string(e->id) + ")" + e->returnValue ) << std::endl;
} }
e->exitState = -1; e->exitState = -1;
e->returnValue = ""; e->returnValue = "";

View File

@ -4,18 +4,16 @@
#include "pageManager.h" #include "pageManager.h"
#include "parameterManager.h" #include "parameterManager.h"
#include "accountManager.h" #include "accountManager.h"
#include <unistd.h>

#include <sys/types.h> #include <sys/types.h>
#include <dirent.h>


#include <dirent.h>
#include <pthread.h> #include <pthread.h>




struct ka struct ka
{ {
ka(unsigned id) : id(id) {} ka(unsigned id, pthread_t init, std::string nameinUrl,int exitStateInit, Settings * settings, AccountManager * accountMg, PageManager * pM)
~ka() { std::cout << "Deskrtuktor" << std::endl; } : id(id), thread(init), nameInUrl(nameinUrl), exitState(exitStateInit), settings(settings), accountManager(accountMg), pageManager(pM) {}


unsigned id; unsigned id;
pthread_t thread; pthread_t thread;