优化多线程与分发

Signed-off-by: fong <wuwenfengmi@outlook.com>
This commit is contained in:
fong
2024-01-30 17:14:24 +08:00
parent ce64cc2f5b
commit 958500acd7
5 changed files with 593 additions and 32 deletions
+110 -8
View File
@@ -26,11 +26,25 @@ quint32 ipv4str_to_int(const QString& ipstr)
} }
} }
trytry::trytry()
{
}
void trytry::run() void trytry::run()
{ {
qDebug()<<this->ipstr<<":"<<this->ipint;
sleep(1);
} }
dispatch::dispatch() dispatch::dispatch()
{ {
@@ -45,8 +59,11 @@ void dispatch::run()
QStringList str_list_more; QStringList str_list_more;
quint32 ipa,ipb; quint32 ipa,ipb;
QStringList str_port_list = port_list.split("\n"); QStringList str_port_list = port_list.split("\n");
QStringList str_ports_list; QStringList str_ports_temp;
QStringList str_porttoport_temp;
quint32 porta,portb;
QList <quint32> ports_list;
quint64 ports_num=0;
quint64 ips_num=0; quint64 ips_num=0;
QRegExp ex_ipv4 ("^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$"); QRegExp ex_ipv4 ("^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$");
QRegExp ex_ipv4_more("^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)-((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$"); QRegExp ex_ipv4_more("^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)-((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$");
@@ -121,16 +138,101 @@ void dispatch::run()
} }
qDebug() << str_ips_list.size();
for(int ii=0;ii<str_ips_list.size();ii++)
{
qDebug() << str_ips_list.at(ii); QRegExp ex_port ("^(0|[1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$");
QRegExp ex_porttoport("^(0|[1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])-(0|[1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$");
//QRegExp ex_portmore ("^(0|[1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]);$");
for (int i = 0; i < str_port_list.size(); ++i)
{
str_ports_temp=str_port_list.at(i).split(";");
for (int ii = 0; ii < str_ports_temp.size(); ++ii)
{
if(ex_port.exactMatch(str_ports_temp.at(ii)))
{
qDebug()<<str_ports_temp.at(ii)<<" norlmore port";
ports_list.append(str_ports_temp.at(ii).toInt());
ports_num+=1;
}else if(ex_porttoport.exactMatch(str_ports_temp.at(ii)))
{
qDebug()<<str_ports_temp.at(ii)<<" port to port";
str_porttoport_temp=str_ports_temp.at(ii).split("-");
porta=str_porttoport_temp.at(0).toInt();
portb=str_porttoport_temp.at(1).toInt();
if(porta>portb)
{
for(;portb<=porta;portb++)
{
ports_list.append(portb);
ports_num+=1;
}
}else if(portb>porta)
{
for(;porta<=portb;porta++)
{
ports_list.append(porta);
ports_num+=1;
}
}else//=
{
ports_list.append(porta);
ports_num+=1;
}
}
}
} }
quint64 scantimes=ips_num*ports_num,now_scan=0;
qDebug() << "IP: "<<ips_num;
qDebug() << "port :"<<ports_num;
qDebug() << "scan times :"<<scantimes;
quint16 jindu=0,jindu_old=0;
for(quint64 ii=0;ii<ips_num;ii++)
{
for(quint64 iii=0;iii<ports_num;iii++)
{
//qDebug() <<str_ips_list.at(ii)<<":"<< ports_list.at(iii);
qDebug()<<"now:"<<this->now_thread_num<<" - set:"<<this->set_thread_num;
while(this->now_thread_num>this->set_thread_num);
connecttry=new trytry;
connecttry->timeout=this->timeout;
connecttry->ipstr=str_ips_list.at(ii);
connecttry->ipint=ports_list.at(iii);
this->now_thread_num+=1;
//connect(connecttry,&trytry::started,[=]{this->now_thread_num+=1;});
connect(connecttry,&trytry::finished,[=]{this->now_thread_num-=1;});
connecttry->start();
jindu=(quint16)(((qfloat16)(now_scan)/(qfloat16)(scantimes))*100);
if(jindu!=jindu_old)
{
jindu_old=jindu;
//qDebug() <<jindu;
emit return_jindu(jindu);
}
now_scan+=1;
}
}
emit dispatch_finish(); emit dispatch_finish();
} }
+5 -10
View File
@@ -10,23 +10,17 @@
#include <QLabel> #include <QLabel>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <qmath.h> #include <qmath.h>
#include <QFloat16>
class trytry : public QThread class trytry : public QThread
{ {
Q_OBJECT
public: public:
trytry();
quint16 *outputbusy;
quint16 *barbusy;
quint16 timeout; quint16 timeout;
QString ipstr; QString ipstr;
quint32 ipint; quint32 ipint;
int *now_thread_num;
QTextEdit* output_list;
quint64 *t_bar;
quint64 *nt_bar;
void run(); void run();
@@ -47,7 +41,7 @@ public:
QString ip_list; QString ip_list;
QString port_list; QString port_list;
trytry *connecttry;
void run(); void run();
@@ -55,6 +49,7 @@ public:
signals: signals:
void dispatch_finish(); void dispatch_finish();
void dispatch_one(); void dispatch_one();
void return_jindu(quint16);
}; };
+157 -6
View File
@@ -8,9 +8,10 @@
void Widget::auto_edit() void Widget::jindu_chuli(quint16 temp)
{ {
//qDebug("%d",rand()); //qDebug()<<temp;
ui->progressBar->setValue(temp);
} }
@@ -39,8 +40,11 @@ Widget::Widget(QWidget *parent)
connect(dispatch_thread,&dispatch::dispatch_finish,[=]{ connect(dispatch_thread,&dispatch::dispatch_finish,[=]{
emit stop_scan(); emit stop_scan();
}); });
dispatch_thread->start(); connect(dispatch_thread,&dispatch::return_jindu,this,&Widget::jindu_chuli);
ui->progressBar->setValue(0);
dispatch_thread->start();
ui->stard_scan->setDisabled(false);
}); });
connect(this,&Widget::stop_scan,[=]{ connect(this,&Widget::stop_scan,[=]{
@@ -50,20 +54,25 @@ Widget::Widget(QWidget *parent)
ui->timeout->setReadOnly(false); ui->timeout->setReadOnly(false);
ui->threads->setReadOnly(false); ui->threads->setReadOnly(false);
ui->stard_scan->setText("start scan"); ui->stard_scan->setText("start scan");
qDebug() << "tray_exit";
dispatch_thread->disconnect(); dispatch_thread->disconnect();
dispatch_thread->terminate(); dispatch_thread->quit();
delete dispatch_thread; dispatch_thread->wait();
//delete dispatch_thread;
ui->stard_scan->setDisabled(false);
qDebug() << "tray_exit";
}); });
connect(ui->stard_scan, &QPushButton::pressed, [=]() connect(ui->stard_scan, &QPushButton::pressed, [=]()
{ {
ui->stard_scan->setDisabled(true);
if(scan_flag==0) if(scan_flag==0)
{ {
emit start_scan(); emit start_scan();
}else }else
{ {
dispatch_thread->terminate();
emit stop_scan(); emit stop_scan();
} }
@@ -166,6 +175,148 @@ Widget::Widget(QWidget *parent)
ui->IP_list->clear(); ui->IP_list->clear();
}); });
connect(ui->pushButton_22, &QPushButton::pressed, [=]()
{
ui->port_list->appendPlainText(ui->pushButton_22->text());
});
connect(ui->pushButton_23, &QPushButton::pressed, [=]()
{
ui->port_list->appendPlainText(ui->pushButton_23->text());
});
connect(ui->pushButton_24, &QPushButton::pressed, [=]()
{
ui->port_list->appendPlainText(ui->pushButton_24->text());
});
connect(ui->pushButton_25, &QPushButton::pressed, [=]()
{
ui->port_list->appendPlainText(ui->pushButton_25->text());
});
connect(ui->pushButton_26, &QPushButton::pressed, [=]()
{
ui->port_list->appendPlainText(ui->pushButton_26->text());
});
connect(ui->pushButton_27, &QPushButton::pressed, [=]()
{
ui->port_list->appendPlainText(ui->pushButton_27->text());
});
connect(ui->pushButton_28, &QPushButton::pressed, [=]()
{
ui->port_list->appendPlainText(ui->pushButton_28->text());
});
connect(ui->pushButton_29, &QPushButton::pressed, [=]()
{
ui->port_list->appendPlainText(ui->pushButton_29->text());
});
connect(ui->pushButton_30, &QPushButton::pressed, [=]()
{
ui->port_list->appendPlainText(ui->pushButton_30->text());
});
connect(ui->pushButton_31, &QPushButton::pressed, [=]()
{
ui->port_list->insertPlainText(ui->pushButton_31->text());
});
connect(ui->pushButton_32, &QPushButton::pressed, [=]()
{
ui->port_list->insertPlainText(ui->pushButton_32->text());
});
connect(ui->pushButton_33, &QPushButton::pressed, [=]()
{
ui->port_list->insertPlainText(ui->pushButton_33->text());
});
connect(ui->pushButton_34, &QPushButton::pressed, [=]()
{
ui->port_list->insertPlainText(ui->pushButton_34->text());
});
connect(ui->pushButton_35, &QPushButton::pressed, [=]()
{
ui->port_list->insertPlainText(ui->pushButton_35->text());
});
connect(ui->pushButton_36, &QPushButton::pressed, [=]()
{
ui->port_list->insertPlainText(ui->pushButton_36->text());
});
connect(ui->pushButton_37, &QPushButton::pressed, [=]()
{
ui->port_list->insertPlainText(ui->pushButton_37->text());
});
connect(ui->pushButton_38, &QPushButton::pressed, [=]()
{
ui->port_list->insertPlainText(ui->pushButton_38->text());
});
connect(ui->pushButton_39, &QPushButton::pressed, [=]()
{
ui->port_list->insertPlainText(ui->pushButton_39->text());
});
connect(ui->pushButton_40, &QPushButton::pressed, [=]()
{
ui->port_list->insertPlainText("\n");
});
connect(ui->pushButton_41, &QPushButton::pressed, [=]()
{
ui->port_list->insertPlainText(ui->pushButton_41->text());
});
connect(ui->pushButton_42, &QPushButton::pressed, [=]()
{
ui->port_list->insertPlainText(ui->pushButton_42->text());
});
connect(ui->pushButton_43, &QPushButton::pressed, [=]()
{
ui->port_list->clear();
});
connect(ui->pushButton_44, &QPushButton::pressed, [=]()
{
ui->port_list->insertPlainText(ui->pushButton_44->text());
});
} }
Widget::~Widget() Widget::~Widget()
+1
View File
@@ -33,6 +33,7 @@ public:
quint64 t_bar=0; quint64 t_bar=0;
quint64 nt_bar=0; quint64 nt_bar=0;
void jindu_chuli(quint16 temp);
signals: signals:
void stop_scan(); void stop_scan();
void start_scan(); void start_scan();
+320 -8
View File
@@ -6,19 +6,19 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>600</width> <width>800</width>
<height>500</height> <height>500</height>
</rect> </rect>
</property> </property>
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>600</width> <width>800</width>
<height>500</height> <height>500</height>
</size> </size>
</property> </property>
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>600</width> <width>800</width>
<height>500</height> <height>500</height>
</size> </size>
</property> </property>
@@ -44,9 +44,9 @@
<widget class="QPushButton" name="stard_scan"> <widget class="QPushButton" name="stard_scan">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>480</x> <x>690</x>
<y>220</y> <y>220</y>
<width>111</width> <width>91</width>
<height>41</height> <height>41</height>
</rect> </rect>
</property> </property>
@@ -81,7 +81,7 @@
<number>999</number> <number>999</number>
</property> </property>
<property name="value"> <property name="value">
<number>10</number> <number>100</number>
</property> </property>
</widget> </widget>
<widget class="QLabel" name="label_4"> <widget class="QLabel" name="label_4">
@@ -144,7 +144,7 @@ domain.com</string>
<rect> <rect>
<x>10</x> <x>10</x>
<y>270</y> <y>270</y>
<width>581</width> <width>771</width>
<height>221</height> <height>221</height>
</rect> </rect>
</property> </property>
@@ -456,7 +456,7 @@ domain.com</string>
<rect> <rect>
<x>320</x> <x>320</x>
<y>220</y> <y>220</y>
<width>51</width> <width>61</width>
<height>41</height> <height>41</height>
</rect> </rect>
</property> </property>
@@ -464,6 +464,318 @@ domain.com</string>
<string>clear</string> <string>clear</string>
</property> </property>
</widget> </widget>
<widget class="QPushButton" name="pushButton_22">
<property name="geometry">
<rect>
<x>600</x>
<y>100</y>
<width>61</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>1-65535</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_23">
<property name="geometry">
<rect>
<x>600</x>
<y>130</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>80;443</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_24">
<property name="geometry">
<rect>
<x>620</x>
<y>160</y>
<width>21</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>22</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_25">
<property name="geometry">
<rect>
<x>640</x>
<y>160</y>
<width>21</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>23</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_26">
<property name="geometry">
<rect>
<x>600</x>
<y>160</y>
<width>21</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>21</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_27">
<property name="geometry">
<rect>
<x>640</x>
<y>130</y>
<width>51</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>3389</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_28">
<property name="geometry">
<rect>
<x>660</x>
<y>160</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>445</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_29">
<property name="geometry">
<rect>
<x>600</x>
<y>190</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>25565</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_30">
<property name="geometry">
<rect>
<x>640</x>
<y>190</y>
<width>51</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>25;587</string>
</property>
</widget>
<widget class="QProgressBar" name="progressBar">
<property name="geometry">
<rect>
<x>487</x>
<y>230</y>
<width>111</width>
<height>23</height>
</rect>
</property>
<property name="value">
<number>0</number>
</property>
</widget>
<widget class="QPushButton" name="pushButton_31">
<property name="geometry">
<rect>
<x>690</x>
<y>130</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>4</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_32">
<property name="geometry">
<rect>
<x>750</x>
<y>130</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>6</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_33">
<property name="geometry">
<rect>
<x>690</x>
<y>100</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>1</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_34">
<property name="geometry">
<rect>
<x>720</x>
<y>100</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>2</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_35">
<property name="geometry">
<rect>
<x>720</x>
<y>130</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>5</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_36">
<property name="geometry">
<rect>
<x>720</x>
<y>160</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>8</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_37">
<property name="geometry">
<rect>
<x>750</x>
<y>160</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>9</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_38">
<property name="geometry">
<rect>
<x>690</x>
<y>160</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>7</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_39">
<property name="geometry">
<rect>
<x>750</x>
<y>100</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>3</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_41">
<property name="geometry">
<rect>
<x>720</x>
<y>190</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>0</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_42">
<property name="geometry">
<rect>
<x>750</x>
<y>190</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>-</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_40">
<property name="geometry">
<rect>
<x>660</x>
<y>100</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>ENT</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_43">
<property name="geometry">
<rect>
<x>600</x>
<y>220</y>
<width>91</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>clear</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_44">
<property name="geometry">
<rect>
<x>690</x>
<y>190</y>
<width>31</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>;</string>
</property>
</widget>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>