-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
executable file
·488 lines (353 loc) · 13.6 KB
/
Copy pathMainWindow.cpp
File metadata and controls
executable file
·488 lines (353 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//
// Created by gaochong on 17-11-24.
//
#include "MainWindow.h"
#include <QLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QDebug>
#include <QMessageBox>
#include <QNetworkInterface>
#include <QNetworkAddressEntry>
#include <QAbstractSocket>
#include <QVariant>
//#include <QOverload>
#include <QStatusBar>
#include <QProgressBar>
#include <QtCore/QFile>
#include "GCArp.h"
#include "GCIP.h"
#include "QCTools.h"
#include "GCSQLite.h"
#include "WorkerThread.h"
#include "ListThread.h"
#include "PortListView.h"
#include "PortTestThread.h"
#include "PortTableDlg.h"
#define DBNAME "macport.db"
MainWindow::MainWindow() {
QWidget *mainWidget = new QWidget;
this->setCentralWidget( mainWidget );
QVBoxLayout *mainlayout = new QVBoxLayout;
mainWidget->setLayout( mainlayout );
QWidget *topWidget = new QWidget;
QWidget *centerWidget = new QWidget;
QWidget *bottomWidget = new QWidget;
mainlayout->addWidget( topWidget );
mainlayout->addWidget( centerWidget );
mainlayout->addWidget( bottomWidget );
QHBoxLayout *centerLayout = new QHBoxLayout;
centerWidget->setLayout( centerLayout );
QVBoxLayout *leftLayout = new QVBoxLayout;
QStringList tableHand;
tableHand << "IP" << "MAC" << "HOSTNAME" << "Organization";
lanIPTable = new LanIPTable( tableHand );
leftLayout->addWidget( lanIPTable );
QVBoxLayout *rightLayout = new QVBoxLayout;
QStringList tableHand2;
tableHand2 << "Port" << "OPEN";
portListView = new PortListView( tableHand2 );
portListView->setFixedWidth( 250 );
portListView->setColumnWidth( 0,80 );
portTestLabel = new QLabel("IP:");
testPortBtn = new QPushButton("端口探测");
QPushButton *portSetupBtn = new QPushButton("端口探测配置");
rightLayout->addWidget( portListView );
rightLayout->addWidget( portTestLabel );
rightLayout->addWidget( testPortBtn );
rightLayout->addWidget( portSetupBtn );
centerLayout->addLayout( leftLayout );
centerLayout->addLayout( rightLayout );
testButton2 = new QPushButton("列队测试");
testButton2->setFixedWidth( 150 );
//testButton1 = new QPushButton("极速测试");
nicList = new QComboBox;
QLabel *SIPLabel = new QLabel( "启始IP:" );
sIPEdit = new QLineEdit;
sIPEdit->setFixedWidth( 120 );
QLabel *EIPLabel = new QLabel( "结止IP:" );
dIPEdit = new QLineEdit;
dIPEdit->setFixedWidth( 120 );
QLabel *srnLabel = new QLabel( "重试:" );
srCount = new QComboBox;
QLabel *otLabel = new QLabel( "超时:" );
outTime = new QComboBox;
QLabel *fPortlabel = new QLabel( "探测端口:" );
fPort = new QComboBox;
initValue();
QHBoxLayout *topLayout = new QHBoxLayout;
topWidget->setLayout( topLayout );
topLayout->addWidget( nicList );
topLayout->addWidget( SIPLabel );
topLayout->addWidget( sIPEdit );
topLayout->addWidget( EIPLabel );
topLayout->addWidget( dIPEdit );
topLayout->addWidget( srnLabel );
topLayout->addWidget( srCount );
topLayout->addWidget( otLabel );
topLayout->addWidget( outTime );
topLayout->addWidget( fPortlabel );
topLayout->addWidget( fPort );
topLayout->addWidget( testButton2 );
topLayout->addStretch();
QHBoxLayout *bottomLayout = new QHBoxLayout;
bottomWidget->setLayout( bottomLayout );
label1 = new QLabel;
label2 = new QLabel;
label3 = new QLabel;
bottomLayout->addWidget( label1 );
bottomLayout->addWidget( label2 );
bottomLayout->addWidget( label3 );
bottomLayout->addStretch();
label1->setText( "探测IP: " );
progressbar = new QProgressBar;
statusBar()->addWidget( progressbar,100 );
QString dbfilename = DBNAME;
if( QFile::exists( dbfilename ) ){
oui = new GCSQLite( dbfilename );
}else{
findON = false;
QMessageBox::warning( this,"提醒", "数据库文件丢失,请确认当目录下存在"+ dbfilename +" 文件,否则无法查询出设备厂商." );
}
setWindowTitle( "以太网活动机器探测" );
connect( testButton2,&QPushButton::clicked,this,&MainWindow::TestLan2 );
connect( testPortBtn,&QPushButton::clicked,this,&MainWindow::TestPort );
connect( portSetupBtn, &QPushButton::clicked,this,&MainWindow::PortSetup );
connect( lanIPTable,
SIGNAL(doubleClicked(QModelIndex)),
this,
SLOT( LanIPTableItemDBClick(QModelIndex)));
connect( portListView,
SIGNAL(doubleClicked(QModelIndex)),
this,
SLOT( PortListViewItemDBClick(QModelIndex)));
}
MainWindow::~MainWindow() {
}
QSize MainWindow::sizeHint() const {
return QSize( 1024,768 ); //QWidget::sizeHint();
}
void MainWindow::initValue(){
/**
* 读取网卡列表 ;MAC
===================================================================*/
QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
QString newMAC = "";
QString Address = "";
QString Netmask = "";
QString Broadcast = "";
foreach(QNetworkInterface interface,list){
if ( "lo" == interface.name() ){
continue;
}
/**
qDebug() << "Device:"<<interface.name();
qDebug() << "mac:"<<interface.hardwareAddress();
*/
newMAC = interface.hardwareAddress();
//取 IP 地址
QList<QNetworkAddressEntry> entryList = interface.addressEntries();
foreach( QNetworkAddressEntry entry,entryList ){
QHostAddress address2 = entry.ip();
if( address2.protocol() == QAbstractSocket::IPv4Protocol ) {
/**
qDebug()<< "IPV4 Address:" <<entry.ip().toString();
qDebug()<< "Netmask: " <<entry.netmask().toString();
qDebug()<< "Broadcast:" <<entry.broadcast().toString();
*/
Address = entry.ip().toString();
Netmask = entry.netmask().toString();
Broadcast = entry.broadcast().toString();
nicList->addItem( interface.name() , Address );
break; //取出第一个IP即可,忽略多IP情况
}
}
//break; //取出第一个网卡即可,忽略多网卡情况
}
sIPEdit = new QLineEdit;
dIPEdit = new QLineEdit;
connect(nicList, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),[this](int index){
qDebug()<< index << nicList->currentData().toString();
QString tip = nicList->currentData().toString();
sIPEdit->setText( tip.mid(0 , tip.lastIndexOf(".") +1 ) + "1" );
dIPEdit->setText( tip.mid(0 , tip.lastIndexOf(".") +1 ) + "254" );
});
/** 超时 */
outTime->addItem( "50","50" );
outTime->addItem( "200","200" );
outTime->addItem( "500","500" );
outTime->addItem( "1000","1000" );
outTime->addItem( "2000","2000" );
outTime->setCurrentIndex(1);
outTime->currentIndexChanged( 1 );
/** 重试 */
srCount->addItem( "1" ,"1" );
srCount->addItem( "2" ,"2" );
srCount->addItem( "3" ,"2" );
srCount->addItem( "4" ,"3" );
srCount->setCurrentIndex(0);
srCount->currentIndexChanged( 0 );
/**
135/TCP
135/UDP
137/UDP
138/UDP
139/TCP
445/TCP
445/UDP
**/
fPort->addItem( "不进行" ,"-9999" );
fPort->addItem( "135:tcp" ,"135:tcp" );
fPort->addItem( "137:udp" ,"137:udp" );
fPort->addItem( "138:udp" ,"138:udp" );
fPort->addItem( "139:udp" ,"139:udp" );
fPort->addItem( "445:tcp" ,"445:tcp" );
fPort->setCurrentIndex(0);
fPort->currentIndexChanged( 0 );
nicList->setCurrentIndex(0);
nicList->currentIndexChanged(0);
/**
connect( outTime, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),[this](int index){
qDebug()<< index << outTime->currentData().toString();
});
connect( srCount, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),[this](int index){
qDebug()<< index << srCount->currentData().toString();
});
connect( fPort, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),[this](int index){
qDebug()<< index << fPort->currentData().toString();
});
*/
}
void MainWindow::TestLan2(){
if ( ! isInputIP( sIPEdit->text() ) || ! isInputIP( dIPEdit->text() ) ) {
QMessageBox::information( this, "提醒" ,"输入的IP地址有误,请填写正确的IP");
return;
}
label2->setText("");
label3->setText("");
this->ktCount = 0;
this->okCount = 0;
this->lanIPTable->ClearAllItem();
progressbar->setValue( 0 );
GCIP *testIP = new GCIP;
/** 启地址 */
char sip_pchar[20]; memset( sip_pchar ,0 ,20);
strcpy( sip_pchar, QCTools::QStringTOpchar( sIPEdit->text() ));
/** 止地址 */
char dip_pchar[20]; memset( dip_pchar,0 ,20 );
strcpy( dip_pchar, QCTools::QStringTOpchar( dIPEdit->text() ));
/** 地址段总数 */
int count = testIP->IP2Count( sip_pchar ,dip_pchar );
/** 重试次数 超时时间*/
int srCount_i = srCount->currentData().toInt();
int outTime_i = outTime->currentData().toInt();
/** 同步嗅探端口 协议*/
QString fPortStr = fPort->currentData().toString();
QStringList mfPort = fPortStr.split(":");
int fPort_i = -9999;
int fPortType = 0;
if ( mfPort.size() == 2 ){
fPort_i = mfPort.at(0).toInt(NULL,10);
QString protType = ( QString )mfPort.at(1);
if ( protType == "tcp" ){
fPortType = 1;
}else{
fPortType = 2;
}
}
/** 准备参数 */
struct TLanNetList *tlanNetList = new TLanNetList;
memset( tlanNetList,0 , sizeof( TLanNetList ) );
strcpy( tlanNetList->Local , QCTools::QStringTOpchar( nicList->currentText() ) );
strcpy( tlanNetList->LocalIP , QCTools::QStringTOpchar( nicList->currentData().toString() ) );
strcpy( tlanNetList->SIP ,sip_pchar );
strcpy( tlanNetList->DIP ,dip_pchar );
/** 创建一个新的线程 */
ListThread *listThread = new ListThread( tlanNetList , srCount_i , outTime_i ,fPort_i ,fPortType );
listThread->start();
/** 监听线程发送 notify 消息,由主线程更新主界面*/
connect( listThread ,SIGNAL( notify(LanTableRecord *) ),this,SLOT( OnNotify(LanTableRecord *) ) );
connect( listThread, &ListThread::notifyNextIP ,this,[=](QString nip){
this->ktCount++;
qDebug() << "Thread Over:" << this->ktCount;
progressbar->setValue( ktCount * 100 / count );
label2->setText( nip );
if( count == ktCount-1 ){
label2->setText( "结束" );
label3->setText( "共探测到 "+ QString::number( okCount ) +" 台设备" );
}
});
}
void MainWindow::AppendItem( LanTableRecord* ltRecord ){
int nRow = this->lanIPTable->getDataModel()->rowCount();
this->lanIPTable->getDataModel()->insertRow( nRow );
this->lanIPTable->getDataModel()->setItem( nRow ,0 , new QStandardItem( ltRecord->IP ) );
this->lanIPTable->getDataModel()->setItem( nRow ,1 , new QStandardItem( ltRecord->MAC ) );
this->lanIPTable->getDataModel()->setItem( nRow ,2 , new QStandardItem( ltRecord->HOSTNAME ) );
this->lanIPTable->getDataModel()->setItem( nRow ,3 , new QStandardItem( ltRecord->Organization ) );
}
void MainWindow::OnNotifyPortTest( int port , int type ) {
int nRow = this->portListView->getDataModel()->rowCount();
this->portListView->getDataModel()->insertRow( nRow );
this->portListView->getDataModel()->setItem( nRow ,0 , new QStandardItem( QString::number( port ) ) );
if( type == 0 ){
this->portListView->getDataModel()->setItem( nRow ,1 , new QStandardItem( "开启" ) );
}else{
this->portListView->getDataModel()->setItem( nRow ,1 , new QStandardItem( "未启用" ) );
}
}
void MainWindow::OnNotify(LanTableRecord *ltRecord) {
++okCount;
if ( findON && oui != NULL ){
ltRecord->Organization = oui->FindMADI( ltRecord->MAC.left(8).replace( ":","-" ) );
}
AppendItem( ltRecord );
}
bool MainWindow::isInputIP( QString IPStr ){
QRegExp regExp("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
if( !regExp.exactMatch( IPStr ) ) {
return false;
}
return true;
}
//
void MainWindow::TestPort(){
if ( this->getSSPort().size() == 0 ){
QMessageBox::warning( this,"提醒","未选择需要探测的端口列表,请先行配置.");
return;
}
this->portListView->ClearAllItem();
if( this->MMIP.length() != 0 ){
qDebug()<< this->MMIP;
char tIP[20];
memset( tIP,0,sizeof( tIP )*sizeof( char ) );
strcpy( tIP, QCTools::QStringTOpchar( MMIP ));
PortTestThread *connectPort = new PortTestThread( tIP , this->getSSPort() );
connectPort->start();
connect( connectPort ,SIGNAL( OnNotifyPortTest(int,int) ),this,SLOT( OnNotifyPortTest( int,int ) ) );
}
}
void MainWindow::PortSetup(){
PortTableDlg *psDlg = new PortTableDlg( this );
psDlg->show();
}
void MainWindow::LanIPTableItemDBClick( QModelIndex modelIndex){
this->MMIP = modelIndex.model()->index( modelIndex.row(),0 ).data().toString().trimmed();
portTestLabel->setText( "IP:" + this->MMIP );
}
void MainWindow::PortListViewItemDBClick( QModelIndex modelIndex ){
//qDebug()<< modelIndex;
}
void MainWindow::setSSPort(QList<PortData> SSPort) {
this->SSPort = SSPort;
if( this->SSPort.size() >0 ){
fPort->clear();
fPort->addItem( "不进行" ,"-9999" );
foreach( PortData t , this->SSPort ){
fPort->addItem( t.port +":"+ t.Protocol ,t.port +":"+ t.Protocol);
}
}
}
QList<PortData> MainWindow::getSSPort() {
return QList<PortData>( this->SSPort );
}