seperated thread for each connection
This commit is contained in:
parent
af4ad93292
commit
4135fe740b
61
main.cpp
61
main.cpp
|
@ -23,9 +23,7 @@ const static uint16_t PORT = 8956;
|
||||||
|
|
||||||
static bool run = true;
|
static bool run = true;
|
||||||
|
|
||||||
std::mutex lock;
|
std::list<std::thread*> clients;
|
||||||
std::condition_variable lock_cv; // not empty or eol
|
|
||||||
std::list<int> clients;
|
|
||||||
|
|
||||||
void sig_handler(int sig_num) {
|
void sig_handler(int sig_num) {
|
||||||
std::cerr << "signalHandler triggered" << std::endl;
|
std::cerr << "signalHandler triggered" << std::endl;
|
||||||
|
@ -138,6 +136,11 @@ void clientShell( int client ) {
|
||||||
int sockVal = 1;
|
int sockVal = 1;
|
||||||
::setsockopt( client, SOL_TCP, TCP_NODELAY, &sockVal, 4);
|
::setsockopt( client, SOL_TCP, TCP_NODELAY, &sockVal, 4);
|
||||||
|
|
||||||
|
sendInstant(
|
||||||
|
"\033[90m╔═════════════╗\n"
|
||||||
|
"\033[90m║ \033[94mHacker 9000 \033[90m║\n"
|
||||||
|
"\033[90m╚═════════════╝\033[0m\n", client);
|
||||||
|
|
||||||
sendTyped("Bonjour Eliot!\n\nWhat is your target?\n", client);
|
sendTyped("Bonjour Eliot!\n\nWhat is your target?\n", client);
|
||||||
|
|
||||||
const std::string target = read(client);
|
const std::string target = read(client);
|
||||||
|
@ -197,26 +200,6 @@ void clientShell( int client ) {
|
||||||
::close( client );
|
::close( client );
|
||||||
}
|
}
|
||||||
|
|
||||||
void backgroundTask() {
|
|
||||||
while ( run ) {
|
|
||||||
int nextClient = -1;
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> guard(lock);
|
|
||||||
if( clients.empty() ) {
|
|
||||||
lock_cv.wait(guard);
|
|
||||||
}
|
|
||||||
if ( clients.empty() ) {
|
|
||||||
// no more clients
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
nextClient = clients.front();
|
|
||||||
clients.pop_front();
|
|
||||||
}
|
|
||||||
|
|
||||||
clientShell( nextClient );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main () {
|
int main () {
|
||||||
// init rand seed
|
// init rand seed
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
@ -244,10 +227,9 @@ int main () {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::thread backgroundThread( backgroundTask );
|
|
||||||
|
|
||||||
::signal(SIGINT, sig_handler);
|
::signal(SIGINT, sig_handler);
|
||||||
::signal(SIGTERM, sig_handler);
|
::signal(SIGTERM, sig_handler);
|
||||||
|
::signal(SIGPIPE, SIG_IGN);
|
||||||
|
|
||||||
while ( run ) {
|
while ( run ) {
|
||||||
int status = pollSocket( sock );
|
int status = pollSocket( sock );
|
||||||
|
@ -260,24 +242,27 @@ int main () {
|
||||||
inet_ntop(addr.sin6_family, &addr.sin6_addr, buffer, INET6_ADDRSTRLEN);
|
inet_ntop(addr.sin6_family, &addr.sin6_addr, buffer, INET6_ADDRSTRLEN);
|
||||||
std::cout << "accepted Client: " << buffer << std::endl;
|
std::cout << "accepted Client: " << buffer << std::endl;
|
||||||
|
|
||||||
{
|
clients.push_back( new std::thread(clientShell, client) );
|
||||||
std::lock_guard<std::mutex> guard(lock);
|
}
|
||||||
clients.push_back( client );
|
|
||||||
|
if ( clients.size() > 1000 ) {
|
||||||
|
for ( uint32_t i = 0; i < 500; ++i ) {
|
||||||
|
std::thread* th = clients.front();
|
||||||
|
clients.pop_front();
|
||||||
|
if( th->joinable() ) {
|
||||||
|
th->join();
|
||||||
|
}
|
||||||
|
delete th;
|
||||||
}
|
}
|
||||||
lock_cv.notify_one();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// notify thread
|
for ( std::thread* th : clients ) {
|
||||||
{
|
if( th->joinable() ) {
|
||||||
std::lock_guard<std::mutex> guard(lock);
|
th->join();
|
||||||
clients.clear();
|
}
|
||||||
}
|
delete th;
|
||||||
lock_cv.notify_all();
|
|
||||||
|
|
||||||
if( backgroundThread.joinable() ) {
|
|
||||||
backgroundThread.join();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
::close(sock);
|
::close(sock);
|
||||||
|
|
Loading…
Reference in New Issue