-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmainwindow.cpp
1061 lines (874 loc) · 29.4 KB
/
mainwindow.cpp
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
senderHexEdit = new QHexEdit(this);
receiverHexEdit = new QHexEdit(this);
senderHexEdit->setAddressWidth(2);
receiverHexEdit->setAddressWidth(2);
ui->gridLayout_7->addWidget(senderHexEdit);
ui->gridLayout_8->addWidget(receiverHexEdit);
passAllSender = false;
passAllReceiver = false;
senderFollow = true;
receiverFollow = true;
senderLexer = NULL;
receiverLexer = NULL;
ui->senderMessageList->setSelectionMode(QAbstractItemView::SingleSelection);
ui->receiverMessageList->setSelectionMode(QAbstractItemView::SingleSelection);
ui->senderScintilla->setWrapMode(QsciScintilla::WrapWord);
ui->receiverScintilla->setWrapMode(QsciScintilla::WrapWord);
ui->senderScintilla->setMarginLineNumbers(1, true);
ui->receiverScintilla->setMarginLineNumbers(1, true);
// status labels
statusConnected = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\np, li { white-space: pre-wrap; }\n</style></head><body style=\" font-family:'Samyak Oriya'; font-size:11pt; font-weight:400; font-style:normal;\">\n<p align=\"right\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Status: <span style=\" font-weight:600; color:#00aa00;\">connected</span></p></body></html>";
statusNotConnected = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\np, li { white-space: pre-wrap; }\n</style></head><body style=\" font-family:'Samyak Oriya'; font-size:11pt; font-weight:400; font-style:normal;\">\n<p align=\"right\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Status: <span style=\" font-weight:600; color:#ff0000;\">not connected</span></p></body></html>";
statusBlocked = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\np, li { white-space: pre-wrap; }\n</style></head><body style=\" font-family:'Samyak Oriya'; font-size:11pt; font-weight:400; font-style:normal;\">\n<p align=\"right\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Status: <span style=\" font-weight:600; color:#00aa00;\">connected (<span style=\" font-weight:600; color:#ff0000;\">message blocked</span>)</span></p></body></html>";
// errors
connect(&server, SIGNAL(connectionError(QString)), this, SLOT(displayErrorMessage(QString)));
// read
connect(&server, SIGNAL(stringRead(QByteArray)), this, SLOT(senderStringRead(QByteArray)));
connect(&receiver, SIGNAL(stringRead(QByteArray)), this, SLOT(receiverStringRead(QByteArray)));
// write
connect(this, SIGNAL(senderStringWrote(QByteArray)), &server, SLOT(clientWriteString(QByteArray)));
connect(this, SIGNAL(receiverStringWrote(QByteArray)), &receiver, SLOT(clientWriteString(QByteArray)));
// connections
connect(&server, SIGNAL(connectionReady()), this, SLOT(senderConnectionReady()));
connect(&server, SIGNAL(connectionNotReady()), this, SLOT(senderConnectionNotReady()));
connect(&receiver, SIGNAL(connectionReady()), this, SLOT(receiverConnectionReady()));
connect(&receiver, SIGNAL(connectionNotReady()), this, SLOT(receiverConnectionNotReady()));
// timer
//timer.setSingleShot(true);
//connect(&timer, SIGNAL(timeout()), this, SLOT(updateMessageSelection()));
//timer.start(10000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
/**
* Display network errors via a MessageBox
*/
void MainWindow::displayErrorMessage(QString error)
{
QMessageBox::critical(this, "Connection error", error);
}
/**
* Check if the sender is the GUI
*/
bool MainWindow::senderIsGUI()
{
if (ui->senderHostCombo->currentText().compare("GUI") == 0)
return true;
else
return false;
}
/**
* Check if the receiver is the GUI
*/
bool MainWindow::receiverIsGUI()
{
if (ui->receiverHostCombo->currentText().compare("GUI") == 0)
return true;
else
return false;
}
/**
* Check if the sender tab is hexEditor
*/
bool MainWindow::senderIsHexEditor()
{
if (ui->senderTabWidget->currentIndex() == 1)
return true;
else
return false;
}
/**
* Check if the receiver tab is hexEditor
*/
bool MainWindow::receiverIsHexEditor()
{
if (ui->receiverTabWidget->currentIndex() == 1)
return true;
else
return false;
}
/**
* Start or stop the connections
*/
void MainWindow::on_connectButton_toggled(bool checked)
{
// start connections
if(checked) {
// start connections
if (receiverIsGUI()) {
receiverConnectionReady();
} else {
receiver.setHost(ui->receiverHostCombo->currentText());
receiver.setPort(ui->receiverPortSpin->value());
}
if (senderIsGUI()) {
senderConnectionReady();
} else {
server.setPort(ui->senderPortSpin->value());
server.start();
}
// disable connection groups
ui->senderConnectionOptionGroup->setEnabled(false);
ui->receiverConnectionOptionGroup->setEnabled(false);
}
// stop connections
else {
// stop connections
if (senderIsGUI()) {
senderConnectionNotReady();
} else {
server.stop();
}
if (receiverIsGUI()) {
receiverConnectionNotReady();
} else {
receiver.stop();
}
// enable connection groups
ui->senderConnectionOptionGroup->setEnabled(true);
ui->receiverConnectionOptionGroup->setEnabled(true);
}
}
/**
* Return a key for the sender message
* Format: "host(:port):message_number
*/
QString MainWindow::getSenderKey() {
QString key;
if (senderIsGUI()) {
key.append("GUI");
} else {
key.append(server.getRemoteIp() + ":" + QString::number(server.getRemotePort()));
}
key.append(":" + QString::number(ui->senderMessageList->count()+1));
return key;
}
/**
* Return a key for the receiver message
* Format: "host(:port):message_number
*/
QString MainWindow::getReceiverKey() {
QString key;
key.append(ui->receiverHostCombo->currentText());
if (receiverIsGUI() == false) {
key.append(":" + QString::number(ui->receiverPortSpin->value()));
}
key.append(":" + QString::number(ui->receiverMessageList->count()+1));
return key;
}
/**
* Save sender just read buffer
*/
void MainWindow::senderStringRead(QByteArray buffer)
{
QString key = getSenderKey();
// replace the message
for (int i=0; i < ui->senderRegexpReplaceList->count(); i++) {
QRegExp reg = QRegExp(ui->senderRegexpReplaceList->item(i)->text().split(" -- ").at(0));
if(QString(buffer).contains(reg)) {
buffer = QString(buffer).replace(reg, ui->senderRegexpReplaceList->item(i)->text().split(" -- ").at(1)).toUtf8();
}
}
// save
senderMessages.insert(key, buffer);
ui->senderMessageList->addItem(key);
// if follow select last message in the GUI
if (senderFollow && ui->senderMessageList->count() > 0) {
ui->senderMessageList->blockSignals( true );
ui->senderMessageList->setCurrentRow(ui->senderMessageList->count()-1);
ui->senderScintilla->setText(QString(buffer));
senderHexEdit->setData(buffer);
ui->senderMessageList->blockSignals( false );
}
// if pass all sender... pass sender message to the receiver :)
if (passAllSender) {
bool block = false;
// check if we must block the stream
for (int i=0; i < ui->senderRegexpBlockList->count(); i++) {
QRegExp reg = QRegExp(ui->senderRegexpBlockList->item(i)->text());
if(QString(buffer).contains(reg)) {
block = true;
ui->senderStatusLabel->setText(statusBlocked);
ui->senderPassRequestButton->setEnabled(true);
}
}
// pass the buffer
if (block == false) {
receiverStringWrite(buffer);
}
}
}
/**
* Save receiver just read buffer
*/
void MainWindow::receiverStringRead(QByteArray buffer)
{
QString key = getReceiverKey();
// replace the message
for (int i=0; i < ui->receiverRegexpReplaceList->count(); i++) {
QRegExp reg = QRegExp(ui->receiverRegexpReplaceList->item(i)->text().split(" -- ").at(0));
if(QString(buffer).contains(reg)) {
buffer = QString(buffer).replace(reg, ui->receiverRegexpReplaceList->item(i)->text().split(" -- ").at(1)).toUtf8();
}
}
// save
receiverMessages.insert(key, buffer);
ui->receiverMessageList->addItem(key);
// if follow select last message in the GUI
if (receiverFollow && ui->receiverMessageList->count() > 0) {
ui->receiverMessageList->blockSignals( true );
ui->receiverMessageList->setCurrentRow(ui->receiverMessageList->count()-1);
ui->receiverScintilla->setText(QString(buffer));
receiverHexEdit->setData(buffer);
ui->receiverMessageList->blockSignals( false );
}
// if pass all receiver... pass receiver message to the sender :)
if (passAllReceiver) {
bool block = false;
// check if we must block the stream
for (int i=0; i < ui->receiverRegexpBlockList->count(); i++) {
QRegExp reg = QRegExp(ui->receiverRegexpBlockList->item(i)->text());
if(QString(buffer).contains(reg)) {
block = true;
ui->receiverStatusLabel->setText(statusBlocked);
ui->receiverPassResponseButton->setEnabled(true);
}
}
// pass the buffer
if (block == false) {
senderStringWrite(buffer);
}
}
}
/**
* Send string to the sender
*/
void MainWindow::senderStringWrite(QByteArray buffer)
{
emit senderStringWrote(buffer);
}
/**
* Send string to the receiver
*/
void MainWindow::receiverStringWrite(QByteArray buffer)
{
emit receiverStringWrote(buffer);
}
/**
* Pass a single sender request
*/
void MainWindow::on_senderPassRequestButton_clicked()
{
QByteArray buffer;
if (senderIsHexEditor()) {
buffer = senderHexEdit->data();
} else {
buffer = ui->senderScintilla->text().toUtf8();
}
QString key;
QByteArray original;
if (ui->senderMessageList->currentItem()) {
key = ui->senderMessageList->currentItem()->text();
original = senderMessages[key];
}
// request modified
if(QString(buffer).compare(QString(original)) != 0) {
senderStringRead(buffer);
receiverStringWrite(buffer);
}
// request not modified
else {
receiverStringWrite(original);
}
// if only this request was blocked
if (passAllSender) {
ui->senderStatusLabel->setText(statusConnected);
ui->senderPassRequestButton->setEnabled(false);
}
}
/**
* Pass a single receiver request
*/
void MainWindow::on_receiverPassResponseButton_clicked()
{
QByteArray buffer;
if (receiverIsHexEditor()) {
buffer = receiverHexEdit->data();
} else {
buffer = ui->receiverScintilla->text().toUtf8();
}
QString key;
QByteArray original;
if (ui->receiverMessageList->currentItem()) {
key = ui->receiverMessageList->currentItem()->text();
original = receiverMessages[key];
}
// response modified
if(QString(buffer).compare(QString(original)) != 0) {
receiverStringRead(buffer);
senderStringWrite(buffer);
}
// response not modified
else {
senderStringWrite(original);
}
// if only this response was blocked
if (passAllReceiver) {
ui->receiverStatusLabel->setText(statusConnected);
ui->receiverPassResponseButton->setEnabled(false);
}
}
/**
* Display the selected sender message
*/
void MainWindow::on_senderMessageList_itemSelectionChanged()
{
QString key = ui->senderMessageList->currentItem()->text();
QByteArray buffer = senderMessages[key];
ui->senderScintilla->setText(QString(buffer));
senderHexEdit->setData(buffer);
}
/**
* Display the selected receiver message
*/
void MainWindow::on_receiverMessageList_itemSelectionChanged()
{
QString key = ui->receiverMessageList->currentItem()->text();
QByteArray buffer = receiverMessages[key];
ui->receiverScintilla->setText(QString(buffer));
receiverHexEdit->setData(buffer);
}
/**
* Enable disable senderConnectionGroup, i.e. the sender is the GUI or not
*/
void MainWindow::on_senderConnectionOptionGroup_toggled(bool checked)
{
static int prev_index = 0;
if(checked == false) {
prev_index = ui->senderHostCombo->currentIndex();
ui->senderHostCombo->setCurrentIndex(1);
} else {
ui->senderHostCombo->setCurrentIndex(prev_index);
}
}
/**
* Enable disable receiverConnectionGroup, i.e. the receiver is the GUI or not
*/
void MainWindow::on_receiverConnectionOptionGroup_toggled(bool checked)
{
static int prev_index = 0;
if(checked == false) {
prev_index = ui->receiverHostCombo->currentIndex();
ui->receiverHostCombo->setCurrentIndex(1);
} else {
ui->receiverHostCombo->setCurrentIndex(prev_index);
}
}
/**
* Set sender syntax highlight
*/
void MainWindow::on_senderSyntaxHighlightCombo_currentIndexChanged(QString type)
{
if(senderLexer)
senderLexer->deleteLater();
if (type == "HTML")
senderLexer = new QsciLexerHTML();
else if (type == "bash")
senderLexer = new QsciLexerBash();
else if (type == "batch")
senderLexer = new QsciLexerBatch();
else if (type == "cmake")
senderLexer = new QsciLexerCMake();
else if (type == "c++")
senderLexer = new QsciLexerCPP();
else if (type == "CSS")
senderLexer = new QsciLexerCSS();
else if (type == "d")
senderLexer = new QsciLexerD();
else if (type == "diff")
senderLexer = new QsciLexerDiff();
else if (type == "fortran")
senderLexer = new QsciLexerFortran();
else if (type == "lua")
senderLexer = new QsciLexerLua();
else if (type == "makefile")
senderLexer = new QsciLexerMakefile();
else if (type == "pascal")
senderLexer = new QsciLexerPascal();
else if (type == "perl")
senderLexer = new QsciLexerPerl();
else if (type == "postscript")
senderLexer = new QsciLexerPostScript();
else if (type == "POV")
senderLexer = new QsciLexerPOV();
else if (type == "properties")
senderLexer = new QsciLexerProperties();
else if (type == "python")
senderLexer = new QsciLexerPython();
else if (type == "ruby")
senderLexer = new QsciLexerRuby();
else if (type == "spice")
senderLexer = new QsciLexerSpice();
else if (type == "SQL")
senderLexer = new QsciLexerSQL();
else if (type == "TCL")
senderLexer = new QsciLexerTCL();
else if (type == "tex")
senderLexer = new QsciLexerTeX();
else if (type == "verilog")
senderLexer = new QsciLexerVerilog();
else if (type == "VHDL")
senderLexer = new QsciLexerVHDL();
else if (type == "YAML")
senderLexer = new QsciLexerYAML();
else
senderLexer = NULL;
ui->senderScintilla->setLexer(senderLexer);
}
/**
* Set receiver syntax highlight
*/
void MainWindow::on_receiverSyntaxHighlightCombo_currentIndexChanged(QString type)
{
if(receiverLexer)
receiverLexer->deleteLater();
if (type == "HTML")
receiverLexer = new QsciLexerHTML();
else if (type == "bash")
receiverLexer = new QsciLexerBash();
else if (type == "batch")
receiverLexer = new QsciLexerBatch();
else if (type == "cmake")
receiverLexer = new QsciLexerCMake();
else if (type == "c++")
receiverLexer = new QsciLexerCPP();
else if (type == "CSS")
receiverLexer = new QsciLexerCSS();
else if (type == "d")
receiverLexer = new QsciLexerD();
else if (type == "diff")
receiverLexer = new QsciLexerDiff();
else if (type == "fortran")
receiverLexer = new QsciLexerFortran();
else if (type == "lua")
receiverLexer = new QsciLexerLua();
else if (type == "makefile")
receiverLexer = new QsciLexerMakefile();
else if (type == "pascal")
receiverLexer = new QsciLexerPascal();
else if (type == "perl")
receiverLexer = new QsciLexerPerl();
else if (type == "postscript")
receiverLexer = new QsciLexerPostScript();
else if (type == "POV")
receiverLexer = new QsciLexerPOV();
else if (type == "properties")
receiverLexer = new QsciLexerProperties();
else if (type == "python")
receiverLexer = new QsciLexerPython();
else if (type == "ruby")
receiverLexer = new QsciLexerRuby();
else if (type == "spice")
receiverLexer = new QsciLexerSpice();
else if (type == "SQL")
receiverLexer = new QsciLexerSQL();
else if (type == "TCL")
receiverLexer = new QsciLexerTCL();
else if (type == "tex")
receiverLexer = new QsciLexerTeX();
else if (type == "verilog")
receiverLexer = new QsciLexerVerilog();
else if (type == "VHDL")
receiverLexer = new QsciLexerVHDL();
else if (type == "YAML")
receiverLexer = new QsciLexerYAML();
else
receiverLexer = NULL;
ui->receiverScintilla->setLexer(receiverLexer);
}
/**
* Sender connection is ready:
* Enable sender GUI and update status label
*/
void MainWindow::senderConnectionReady()
{
ui->senderStatusLabel->setText(statusConnected);
if (ui->forwardOnlyButton->isChecked() == false) {
ui->senderPassRequestButton->setEnabled(true);
ui->senderPassAllButton->setEnabled(true);
}
receiver.start();
}
/**
* Receiver connection is ready:
* Enable receiver GUI and update status label
*/
void MainWindow::receiverConnectionReady()
{
ui->receiverStatusLabel->setText(statusConnected);
if (ui->forwardOnlyButton->isChecked() == false) {
ui->receiverPassResponseButton->setEnabled(true);
ui->receiverPassAllButton->setEnabled(true);
}
}
/**
* Sender connection is not ready:
* Disable sender GUI, update status label, and reconnect
*/
void MainWindow::senderConnectionNotReady()
{
ui->senderStatusLabel->setText(statusNotConnected);
ui->senderPassAllButton->setEnabled(false);
ui->senderPassRequestButton->setEnabled(false);
// disconnect receiver
if (reconnectMutex.tryLock()) {
receiver.stop();
reconnectMutex.unlock();
}
}
/**
* Receiver connection is not ready:
* Disable receiver GUI, update status label, and reconnect
*/
void MainWindow::receiverConnectionNotReady()
{
ui->receiverStatusLabel->setText(statusNotConnected);
ui->receiverPassAllButton->setEnabled(false);
ui->receiverPassResponseButton->setEnabled(false);
// if sender is GUI reconnect
if (senderIsGUI()) {
receiver.stop();
}
// disconnect sender
if (reconnectMutex.tryLock()) {
server.clientStop();
reconnectMutex.unlock();
}
}
/**
* Delete sender selected item
*/
void MainWindow::on_senderDeleteItemButton_clicked()
{
QListWidgetItem *item;
ui->senderMessageList->blockSignals(true);
item = ui->senderMessageList->currentItem();
if (item) {
senderMessages.remove(item->text());
delete item;
}
ui->senderMessageList->blockSignals(false);
}
/**
* Delete receiver selected item
*/
void MainWindow::on_receiverDeleteItemButton_clicked()
{
QListWidgetItem *item;
ui->receiverMessageList->blockSignals(true);
item = ui->receiverMessageList->currentItem();
if (item) {
receiverMessages.remove(item->text());
delete item;
}
ui->receiverMessageList->blockSignals(false);
}
/**
* Pass all sender requests
*/
void MainWindow::on_senderPassAllButton_toggled(bool checked)
{
if (checked) {
passAllSender = true;
ui->senderPassRequestButton->setEnabled(false);
} else {
passAllSender = false;
ui->senderPassRequestButton->setEnabled(true);
}
}
/**
* Pass all receiver responses
*/
void MainWindow::on_receiverPassAllButton_toggled(bool checked)
{
if (checked) {
passAllReceiver = true;
ui->receiverPassResponseButton->setEnabled(false);
} else {
passAllReceiver = false;
ui->receiverPassResponseButton->setEnabled(true);
}
}
/**
* Follow sender request stream in the GUI
*/
void MainWindow::on_senderFollowButton_toggled(bool checked)
{
if (checked) {
senderFollow = true;
} else {
senderFollow = false;
}
}
/**
* Follow receiver response stream in the GUI
*/
void MainWindow::on_receiverFollowButton_toggled(bool checked)
{
if (checked) {
receiverFollow = true;
} else {
receiverFollow = false;
}
}
/**
* Forward every message
*/
void MainWindow::on_forwardOnlyButton_toggled(bool checked)
{
if (checked) {
passAllSender = true;
passAllReceiver = true;
ui->senderPassAllButton->setEnabled(false);
ui->receiverPassAllButton->setEnabled(false);
ui->senderPassRequestButton->setEnabled(false);
ui->receiverPassResponseButton->setEnabled(false);
}
else {
passAllSender = false;
passAllReceiver = false;
if (ui->connectButton->isChecked()) {
if (ui->senderStatusLabel->text() == statusConnected) {
ui->senderPassAllButton->setEnabled(true);
ui->senderPassRequestButton->setEnabled(true);
}
if (ui->receiverStatusLabel->text() == statusConnected) {
ui->receiverPassAllButton->setEnabled(true);
ui->receiverPassResponseButton->setEnabled(true);
}
}
}
}
/**
* Add a block regexp (sender)
*/
void MainWindow::on_senderRegexpBlockAdd_clicked()
{
ui->senderRegexpBlockList->addItem(ui->senderRegexpBlockEdit->text());
}
/**
* Remove a block regexp (sender)
*/
void MainWindow::on_senderRegexpBlockRemove_clicked()
{
if (ui->senderRegexpBlockList->currentItem()) {
delete ui->senderRegexpBlockList->currentItem();
}
}
/**
* Add a block regexp (receiver)
*/
void MainWindow::on_receiverRegexpBlockAdd_clicked()
{
ui->receiverRegexpBlockList->addItem(ui->receiverRegexpBlockEdit->text());
}
/**
* Remove a block regexp (receiver)
*/
void MainWindow::on_receiverRegexpBlockRemove_clicked()
{
if (ui->receiverRegexpBlockList->currentItem()) {
delete ui->receiverRegexpBlockList->currentItem();
}
}
/**
* Add a replace regexp (sender)
*/
void MainWindow::on_senderRegexpReplaceAdd_clicked()
{
ui->senderRegexpReplaceList->addItem(ui->senderRegexpSearchEdit->text() +
" -- " +
ui->senderRegexpReplaceEdit->text());
}
/**
* Remove a replace regexp (sender)
*/
void MainWindow::on_senderRegexpReplaceRemove_clicked()
{
if (ui->senderRegexpReplaceList->currentItem()) {
delete ui->senderRegexpReplaceList->currentItem();
}
}
/**
* Add a replace regexp (receiver)
*/
void MainWindow::on_receiverRegexpReplaceAdd_clicked()
{
ui->receiverRegexpReplaceList->addItem(ui->receiverRegexpSearchEdit->text() +
" -- " +
ui->receiverRegexpReplaceEdit->text());
}
/**
* Remove a replace regexp (receiver)
*/
void MainWindow::on_receiverRegexpReplaceRemove_clicked()
{
if (ui->receiverRegexpReplaceList->currentItem()) {
delete ui->receiverRegexpReplaceList->currentItem();
}
}
/**
* Display application info
*/
void MainWindow::displayInfo()
{
QMessageBox::about(this, "Info", "<strong>PassiveNetwork v1.0</strong><br /><br />Developed by Emanuele Acri (<a href= \"mailto:[email protected]\">[email protected]</a>)<br />Project site: <a href= \"https://github.com/TigerSecurity/PassiveNetwork\">https://github.com/TigerSecurity/PassiveNetwork</a>");
}
/**
* Display application help
*/
void MainWindow::displayHelp()
{
QMessageBox::about(this, "Help", "PassiveNetwork is very easy to use:<br /><br />The <strong>Sender</strong> represents the client. You can listen for connections on a local port, or simulate the client with GUI.<br /><br />The <strong>Receiver</strong> represents the server. You can connect to a remote service, or use GUI to simulate it.<br /><br />To intercept or manipulate data you can use various features:<br /><br /><strong>RegExp Block</strong> stops the automatic forwarding of matched messages. When a message is blocked you can modify it before passing it.<br /><br /><strong>RegExp Replace</strong> automatically modifies data stream according to the configured regular expressions.<br /><br />We tried to make PassiveNetwork as intuitive as possible: <em>feel free to experiment with the various features</em>! ;)");
}
/**
* Save current session
*/
void MainWindow::saveSession()
{
QString filename = QFileDialog::getSaveFileName(this,
tr("Save current session"), "", tr("PassiveNetwork File (*.pn *)"));
if (filename.isEmpty())
return;
QFile file(filename);
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
// save connection data
out << ui->senderConnectionOptionGroup->isChecked();
out << ui->senderSocketTypeCombo->currentText();
out << ui->senderHostCombo->currentText();
out << ui->senderPortSpin->value();
out << ui->receiverConnectionOptionGroup->isChecked();
out << ui->receiverSocketTypeCombo->currentText();
out << ui->receiverHostCombo->currentText();
out << ui->receiverPortSpin->value();
// save messages
QStringList list;
out << senderMessages;
out << receiverMessages;
// save block regexps
for (int i=0; i<ui->senderRegexpBlockList->count(); i++) {
list.append(ui->senderRegexpBlockList->item(i)->text());
}
out << list;
list.clear();
for (int i=0; i<ui->receiverRegexpBlockList->count(); i++) {
list.append(ui->receiverRegexpBlockList->item(i)->text());
}
out << list;
list.clear();
// save replace regexps
for (int i=0; i<ui->senderRegexpReplaceList->count(); i++) {
list.append(ui->senderRegexpReplaceList->item(i)->text());
}
out << list;
list.clear();
for (int i=0; i<ui->receiverRegexpReplaceList->count(); i++) {
list.append(ui->receiverRegexpReplaceList->item(i)->text());
}
out << list;
list.clear();
}
/**
* Load previous session
*/
void MainWindow::loadSession()
{
if (ui->connectButton->isChecked()) {
ui->connectButton->toggle();
}
QString filename = QFileDialog::getOpenFileName(this,
tr("Load saved session"), "", tr("PassiveNetwork File (*.pn *)"));
if (filename.isEmpty())
return;
QFile file(filename);
file.open(QIODevice::ReadOnly);
QDataStream out(&file);
// read connection data
QString tstr;
int tint;
bool tbool;
out >> tbool;
ui->senderConnectionOptionGroup->setChecked(tbool);
out >> tstr;
ui->senderSocketTypeCombo->addItem(tstr);
ui->senderSocketTypeCombo->setCurrentIndex(ui->senderSocketTypeCombo->count()-1);
out >> tstr;
ui->senderHostCombo->addItem(tstr);
ui->senderHostCombo->setCurrentIndex(ui->senderHostCombo->count()-1);
out >> tint;
ui->senderPortSpin->setValue(tint);
out >> tbool;
ui->receiverConnectionOptionGroup->setChecked(tbool);
out >> tstr;