thread pooling 2

This commit is contained in:
cora48 2015-03-19 17:34:35 +03:00
parent 9b920a6e39
commit b2b246010e
6 changed files with 115 additions and 272 deletions

View File

@ -252,7 +252,6 @@ int Connector::nConnect(const char *ip, const int port, std::string *buffer,
buffer->clear();
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 0L);
int res = 0;
if (curl)
{
@ -277,6 +276,8 @@ int Connector::nConnect(const char *ip, const int port, std::string *buffer,
if(strlen(gProxyIP) != 0 && (proxyPort > 0 && proxyPort < 65535)) {
curl_easy_setopt(curl, CURLOPT_PROXY, gProxyIP);
curl_easy_setopt(curl, CURLOPT_PROXYPORT, proxyPort);
} else {
curl_easy_setopt(curl, CURLOPT_PROXY, "");
}
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, gTimeOut);
@ -303,10 +304,15 @@ int Connector::nConnect(const char *ip, const int port, std::string *buffer,
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
//curl_easy_setopt(curl, CURLOPT_FTPLISTONLY, TRUE);
curl_easy_setopt(curl, CURLOPT_USERPWD, lpString->c_str());
};//
};
if(curl_easy_perform(curl) != CURLE_OK) return -1;
if(curl_easy_perform(curl) == CURLE_OK) {
curl_easy_cleanup(curl);
} else {
curl_easy_cleanup(curl);
return -1;
}
} else {
stt->doEmitionRedFoundData("Curl error.");
return -1;
@ -334,14 +340,14 @@ int Connector::_ConnectToPort(char *ip, int port, char *hl)
if(port == 22) size = _SSHLobby(ip, port, &buffer);
else size = nConnect(ip, port, &buffer);
if(size > 0)
{
++Alive;
++found;
stt->doEmitionChangeParsed(QString::number(saved) + "/" + QString::number(found));
Lexems lx;
lx._filler(port, buffer.c_str(), ip, size, &lx, hl);
};
// if(size > 0)
// {
// ++Alive;
// ++found;
// stt->doEmitionChangeParsed(QString::number(saved) + "/" + QString::number(found));
// Lexems lx;
// lx._filler(port, buffer.c_str(), ip, size, &lx, hl);
// };
return 0;
}

View File

@ -102,13 +102,13 @@ void MakePolygonLine(int gWidth)
};
}
if(xtx > 234 && xtx < 269)
if(xtx > 234 && xtx < 278)
{
qp = QPointF(xtx, state ? qrand() % 3 + ME2YPOS - Alive * 2 - fact7 : ME2YPOS);
if(Alive > 0)
{
if(xtx < 252 ) fact7+=1;
if(xtx < 261 ) fact7+=1;
else fact7-=1;
};
};

View File

@ -1,39 +1,13 @@
#include <Threader.h>
std::vector<char*> Threader::threadPool;
int Threader::threadPoolSize;
bool Threader::mayRun;
void *Threader::lFunc;
void Threader::recreateThreadPool(int poolSize) {
if(mayRun) {
createThreadPool(poolSize, (void* (*)(int))lFunc);
}
}
void Threader::createThreadPool(int poolSize, void *func(int)) {
cleanUp();
lFunc = (void*)func;
threadPoolSize = poolSize;
for(int i = 0; i < threadPoolSize; ++i) {
char *res = NULL;
threadPool.push_back(res);
std::thread thr12(func, i);
thr12.detach();
//#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
// _beginthread((void(*)(void*))_connect, 0, (void *)i);
//#else
// pthread_t thrc;
// pthread_create(&thrc, NULL, (void *(*)(void*))func, (void *)i);
//#endif
Sleep(1);
}
mayRun = true;
}
std::vector<std::condition_variable*> cvPool;
std::vector<bool> Threader::readyPool;
int Threader::threadId = 0;
int Threader::getFreeDataSlotId() {
for(int i = 0; i != threadPoolSize; ++i) {
int tps = threadPool.size();
for(int i = 0; i != (gThreads > tps ? tps : gThreads); ++i) {
if(threadPool[i] == NULL) return i;
}
return -1;
@ -41,19 +15,29 @@ int Threader::getFreeDataSlotId() {
int Threader::getFreeThreadId() {
int res;
while((res = getFreeDataSlotId()) < 0) Sleep(10);
while((res = getFreeDataSlotId()) < 0) Sleep(50);
return res;
}
void Threader::cleanUp() {
mayRun = false;
//for(int i = 0; i != threadPoolSize; ++i) {
//if(threadPool[i] != NULL) delete threadPool[i];
//}
threadPool.clear();
void Threader::fireThread(char *res, void *func(int,std::condition_variable*)) {
if(threadId < gThreads) {
threadPool.push_back(res);
std::condition_variable cv;
cvPool.push_back(&cv);
std::thread workerThread(func, threadId++, &cv);
workerThread.detach();
readyPool.push_back(true);
cv.notify_one();
} else {
int id = getFreeThreadId();
threadPool[id] = res;
readyPool[id] = true;
cvPool[id]->notify_one();
}
}
void Threader::fireThread(char *res) {
//while(!mayRun) sleep(10);
threadPool[getFreeThreadId()] = res;
void Threader::cleanUp() {
threadId = 0;
threadPool.clear();
cvPool.clear();
}

View File

@ -2,29 +2,28 @@
#define THREADER_H
#include <mainResources.h>
#include <externData.h>
#include <thread>
//typedef struct {
// char argv[MAX_ADDR_LEN] = {0};
//} ST;
#include <chrono>
#include <future>
#include <mutex>
#include <condition_variable>
class Threader {
private:
static int threadPoolSize;
static void* lFunc;
public:
static std::vector<char *> threadPool;
static bool mayRun;
static int threadId;
static std::condition_variable cv;
static std::vector<bool> readyPool;
private:
static int getFree();
static int getFreeDataSlotId();
static int getFreeThreadId();
public:
static void recreateThreadPool(int poolSize);
static void createThreadPool(int poolSize, void *func(int));
static void fireThread(char *st);
static void fireThread(char *st,
void* func(int,std::condition_variable*));
static void cleanUp();
};

View File

@ -2531,6 +2531,7 @@ void nesca_3::IPScanSeq()
{
if(ui->portLine->text() != "")
{
saveOptions();
ui->labelParsed_Value->setText("0/0");
stopFirst = false;
ui->tabMainWidget->setTabEnabled(1, false);
@ -2614,6 +2615,7 @@ void nesca_3::DNSScanSeq()
{
if(ui->lineEditPort->text() != "")
{
saveOptions();
ui->labelParsed_Value->setText("0/0");
if(ui->lineEditStartIPDNS->text().indexOf(".") > 0)
{
@ -2670,6 +2672,7 @@ void nesca_3::ImportScanSeq()
else fileName = importFileName;
if(fileName != "")
{
saveOptions();
ui->tabMainWidget->setTabEnabled(0, false);
ui->tabMainWidget->setTabEnabled(1, false);
@ -3572,7 +3575,6 @@ void nesca_3::ChangeLabelTO_ValueChanged(QString str)
void nesca_3::ChangeLabelThreads_ValueChanged(QString str)
{
gThreads = str.toInt();
//Threader::recreateThreadPool(gThreads);
}
void nesca_3::PingTO_ChangeValue(QString str)

View File

@ -6,8 +6,6 @@
#include "Threader.h"
#include <thread>
//ST *st = NULL;
QJsonArray *jsonArr = new QJsonArray();
bool gShuffle = true;
@ -810,184 +808,82 @@ unsigned long int numOfIps(int ipsstart[], int ipsend[])
return gTargets;
}
//#include <sys/types.h>
//#include <stdio.h>
//#include <signal.h>
//#include <adns.h>
//char *GetHost(char *ip)
//{
// char res[256] = {0};
// adns_state ads = NULL;
// adns_init(&ads, adns_if_nosigpipe, 0);
//
// adns_query aquery = 0;
// adns_answer* answer = NULL;
// sockaddr_in sockAddr;
// sockAddr.sin_family = AF_INET;
// sockAddr.sin_addr.S_un.S_addr = inet_addr(ip);
//
// adns_submit_reverse(ads,
// (struct sockaddr *)&sockAddr,
// (adns_rrtype)adns_r_ptr,
// (adns_queryflags)adns_qf_quoteok_anshost,
// NULL,
// &aquery);
// adns_wait(ads, &aquery, &answer, NULL);
//
// if(answer->status == adns_s_ok)
// {
// strncpy(res, (const char*)answer->rrs.bytes, 256);
// };
//
// adns_finish(ads);
// //struct hostent *result;
// //unsigned long addr = inet_addr(ip);
// //result = gethostbyaddr((char*)&addr, sizeof(addr), AF_INET);
// return res;
//
//#pragma region Reverse-ip
// //struct hostent *result;
// //unsigned long addr = inet_addr(ip);
// //result = gethostbyaddr((char*)&addr, sizeof(addr), AF_INET);
// //char res[256] = {0};
//
// //if( result == NULL )
// //{
// // strcpy(res, "");
// //}
// //else
// //{
// // if( result->h_name == NULL )
// // {
// // strcpy(res, "");
// // }
// // else
// // {
// // strcpy(res, "Hostname: ");
// // //if(result->h_length != NULL) strcat(res, std::to_string(result->h_length).c_str());
// // //strcat(res, " ");
// // //if(result->h_name != NULL) strcat(res, (result->h_name));
// // //strcat(res, ":");
// // if(result->h_addr_list[0] != NULL)
// // {
// // int sz = strlen((result->h_addr_list[0] + 4));
// // if(sz > 200) stt->doEmitionYellowFoundData("[LOL] Very long host detected [<a href=\"http://" + QString(ip) + "/\">" + QString(ip) + "</a>]");
// // strncpy(res, (result->h_addr_list[0] + 4), sz < 256 ? sz : 256);
// // };
// // };
// //};
//
//
// return res;
//}
//#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
//void _connect(int ss)
//#else
//void _connect(int ss)
//void _connect(char *ip)
//#endif
//{
// while(globalScanFlag) {
// while(Threader::threadPool[ss] == NULL) {
// if(!globalScanFlag) return;
// Sleep(10);
// }
// ++ipCounter;
// //char ip[MAX_ADDR_LEN] = {0};
// //strcpy(ip, Threader::threadPool[ss]);
// //char hostLog[256] = {0};
// //strcpy(hostLog, GetHost(ip));
// //delete Threader::threadPool[ss];
// ConInc();
// for(int i = 0; i <= overallPorts; ++i)
// {
// if(globalScanFlag == false) break;
// //if(Connector::_ConnectToPort( ip, portArr[i], "" ) == -2) break;
// //if(Connector::_ConnectToPort( Threader::threadPool[ss], portArr[i], "" ) == -2) break;
// if(Connector::_ConnectToPort( ip, portArr[i], "" ) == -2) break;
// };
// ConDec();
// Threader::threadPool[ss] = NULL;
// }
//}
std::mutex m;
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
void _connect(char* ip)
void _connect(int ss)
#else
void _connect(char* ip)
void _connect(int ss, std::condition_variable *cv)
#endif
{
//std::chrono::duration<int, std::milli> duration(200);
while(globalScanFlag) {
// std::future<int> f2 = Threader::threadPool[ss];
// f2.wait();
std::unique_lock<std::mutex> lk(m);
cv->wait(lk, [ss]{return Threader::readyPool[ss];});
// while(Threader::threadPool[ss] == NULL) {
// if(!globalScanFlag) return;
// std::this_thread::sleep_for(duration);
// }
// if(Threader::threadPool[ss] == (char*)-1) {
// --Threader::threadId;
// return;
// }
++ipCounter;
ConInc();
for(int i = 0; i <= overallPorts; ++i)
{
if(globalScanFlag == false) break;
if(Connector::_ConnectToPort( ip, portArr[i], "" ) == -2) break;
if(Connector::_ConnectToPort( Threader::threadPool[ss], portArr[i], "" ) == -2) break;
};
ConDec();
Threader::readyPool[ss] = false;
Threader::threadPool[ss] = NULL;
lk.unlock();
}
}
void targetAndIPWriter(long long unsigned int target, const char *buff)
void targetAndIPWriter(long long unsigned int target, const char *ip)
{
char curIPBuff[256] = {0}, targetNPers[32] = {0};
char targetNPers[32] = {0};
float percent = (gTargetsOverall != 0 ? (100 - target/(double)gTargetsOverall * 100) : 0);
stt->doEmitionIPRANGE(QString(ip));
strcpy(metaIPDNS, ip);
strcpy(metaIPDNS, buff);
sprintf(targetNPers, "%Lu (%.1f%%)",
target, (gTargetsOverall != 0 ? (100 - target/(double)gTargetsOverall * 100) : 0));
target,
percent);
stt->doEmitionTargetsLeft(QString(targetNPers));
sprintf(metaTargets, "%Lu", target);
sprintf(metaPercent, "%.1f",
(gTargetsOverall != 0 ? (100 - target/(double)gTargetsOverall * 100) : 0));
sprintf(curIPBuff, "--->%s", buff);
stt->doEmitionIPRANGE(QString(curIPBuff));
stt->doEmitionTargetsLeft(QString(targetNPers));
percent);
}
void _passLoginLoader()
@ -2301,8 +2197,6 @@ int _GetDNSFromMask(char *mask, char *saveMask, char *saveMaskEnder)
{
strcpy(endIP2, saveMask);
//st = new ST();
//ZeroMemory(st->argv, sizeof(st->argv));
ZeroMemory(iip, sizeof(iip));
while(cons >= gThreads && globalScanFlag) Sleep(300);
@ -2319,21 +2213,8 @@ int _GetDNSFromMask(char *mask, char *saveMask, char *saveMaskEnder)
targetAndIPWriter(--gTargets, res);
// std::thread thr(_connect, res);
// thr.detach();
//Threader::fireThread(st);
//#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
// if(globalScanFlag) _beginthread( (void(*)(void*))_connect, 0, st );
//#else
// if(globalScanFlag)
// {
// pthread_t thrc;
// pthread_create(&thrc, NULL, (void *(*)(void*))&_connect, st );
// };
//#endif
Threader::fireThread(res, (void* (*)(int,std::condition_variable*))_connect);
Sleep(gThreadDelay);
};
}
@ -2344,8 +2225,6 @@ int startScan(char* args)
OpenSSL_add_all_algorithms(); /* Load cryptos, et.al. */
SSL_load_error_strings(); /* Bring in and register error messages */
// Threader::createThreadPool(gThreads, (void* (*)(int))_connect);
horLineFlag = false;
flCounter = 0;
PieAnomC1 = 0, PieWF = 0, PieBA = 0, PieSusp = 0, PieLowl = 0, PieSSH = 0;
@ -2358,6 +2237,7 @@ int startScan(char* args)
overallPorts = 0;
found = 0;
indexIP = 0;
ZeroMemory(octet, sizeof(octet));
ZeroMemory(ipsstart, sizeof(ipsstart));
ZeroMemory(ipsend, sizeof(ipsend));
@ -2392,8 +2272,8 @@ int startScan(char* args)
return -1;
};
stt->doEmitionIPRANGE(QString(saveEndIP));
stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
stt->doEmitionIPRANGE(QString(saveEndIP));
stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
_passLoginLoader();
_NegativeLoader();
@ -2409,7 +2289,6 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
#endif
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
if (trackerOK) _beginthread((void(*)(void*))_tracker, 0, NULL);
Sleep(50);
_beginthread((void(*)(void*))_timer, 0, NULL);
#else
pthread_t thrt;
@ -2448,19 +2327,15 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
while (cons >= gThreads && globalScanFlag) Sleep(500);
if (globalScanFlag == false) goto haters_gonna_hate_IPM;
//st = new ST();
//ZeroMemory(st->argv, MAX_ADDR_LEN);
ZeroMemory(res, sizeof(res));
++indexIP;
//strcpy(st->argv, ipVec[0].c_str());
strcpy(res, ipVec[0].c_str());
strcpy(saveStartIP, res);
ipVec.erase(ipVec.begin());
targetAndIPWriter(gTargets--, res);
std::thread thr(_connect, res);
thr.detach();
//Threader::fireThread(res);
//std::thread workerThread(_connect, res);
//workerThread.detach();
Threader::fireThread(res, (void* (*)(int,std::condition_variable*))_connect);
Sleep(gThreadDelay);
}
}
@ -2475,8 +2350,6 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
while (cons >= gThreads && globalScanFlag) Sleep(500);
if (globalScanFlag == false) break;
//st = new ST();
//ZeroMemory(st->argv, sizeof(st->argv));
ZeroMemory(res, sizeof(res));
++indexIP;
@ -2490,16 +2363,9 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
tAddr.s_addr = ntohl(tAddr.s_addr);
strcpy(res, inet_ntoa(tAddr));
#endif
//strcpy(st->argv, res);
strcpy(saveStartIP, res);
targetAndIPWriter(gTargets--, res);
// std::thread thr(_connect, res);
// thr.detach();
//Threader::fireThread(st);
Threader::fireThread(res, (void* (*)(int,std::condition_variable*))_connect);
Sleep(gThreadDelay);
}
break;
@ -2727,19 +2593,14 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
while (cons >= gThreads && globalScanFlag) Sleep(500);
if (globalScanFlag == false) goto haters_gonna_hate_IM;
//st = new ST();
//ZeroMemory(st->argv, MAX_ADDR_LEN);
ZeroMemory(res, sizeof(res));
++indexIP;
strcpy(res, ipVec[0].c_str());
strcpy(saveStartIP, res);
ipVec.erase(ipVec.begin());
targetAndIPWriter(gTargets--, res);
// std::thread thr(_connect, res);
// thr.detach();
//Threader::fireThread(st);
Threader::fireThread(res, (void* (*)(int,std::condition_variable*))_connect);
Sleep(gThreadDelay);
}
}
@ -2753,8 +2614,6 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
while (cons >= gThreads && globalScanFlag) Sleep(500);
if (globalScanFlag == false) break;
//st = new ST();
//ZeroMemory(st->argv, sizeof(st->argv));
ZeroMemory(res, sizeof(res));
++indexIP;
@ -2765,16 +2624,9 @@ stt->doEmitionThreads(QString::number(0) + "/" + QString::number(gThreads));
tAddr.s_addr = ntohl(tAddr.s_addr);
strcpy(res, inet_ntoa(tAddr));
#endif
//strcpy(st->argv, res);
strcpy(saveStartIP, res);
targetAndIPWriter(gTargets--, res);
// std::thread thr(_connect, res);
// thr.detach();
//Threader::fireThread(st);
Threader::fireThread(res, (void* (*)(int,std::condition_variable*))_connect);
Sleep(gThreadDelay);
}
break;