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
|
// -*- mode: c++; c-basic-offset: 2; -*-
#include "common.hh"
#include <fcntl.h>
#include <fstream>
#include <memory>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <thread>
#include <unistd.h>
#include <unordered_map>
#include <vector>
#include "config.hh"
#include "gui_about.hh"
#include "gui_config.hh"
#include "gui_formapply.hh"
#include "gui_hexdump.hh"
#include "gui_listmodel.hh"
#include "gui_menu.hh"
#include "gui_main.hh"
#include "gui_statusbar.hh"
#include "gui_textwnd.hh"
#include "io.hh"
#include "logger.hh"
#include "looper.hh"
#include "monitor.hh"
#include "observers.hh"
#include "proxy.hh"
#include "resolver.hh"
#include "ssl.hh"
namespace {
std::string const ACTION_SETUP = "setup";
std::string const ACTION_CONNECT = "connect";
std::string const ACTION_DISCONNECT = "disconnect";
std::string const ACTION_EXIT = "exit";
std::string const ACTION_ABOUT = "about";
std::string const ACTION_COPY_RAW = "copy_raw";
std::string const ACTION_COPY_TEXT = "copy_text";
std::string const ACTION_CLEAR = "clear";
std::string const ACTION_PROXY_LOG = "proxy_log";
std::string const ACTION_GENERATE_CA = "genca";
bool valid_hostname(std::string const& host) {
return !host.empty();
}
bool parse_address(std::string const& addr, std::string* host, uint16_t* port) {
auto i = addr.find(':');
if (i == std::string::npos) {
if (!valid_hostname(addr)) return false;
if (host) *host = addr;
if (port) *port = 9000;
return true;
}
auto h = addr.substr(0, i);
if (!valid_hostname(h)) return false;
char* end = nullptr;
auto p = strtoul(addr.c_str() + i + 1, &end, 10);
if (p == 0 || p > 65535 || !end || *end) return false;
if (host) *host = h;
if (port) *port = p;
return true;
}
#if HAVE_SSL
const char* CERT_BUNDLE[] = {
"/etc/ssl/certs/ca-certificates.crt",
NULL
};
std::string default_cert_bundle() {
static std::string cache;
if (cache.empty()) {
for (auto bundle = CERT_BUNDLE; *bundle; ++bundle) {
if (access(*bundle, R_OK) == 0) {
cache.assign(*bundle);
break;
}
}
}
return cache;
}
#endif // HAVE_SSL
class PackageList : public GuiListModel {
public:
struct Package {
std::string timestamp;
std::string from;
std::string to;
std::string size;
std::string data;
};
size_t rows() const override {
return packages_.size();
}
size_t columns() const override {
return 4;
}
std::string header(size_t column) const override {
switch (column) {
case 0:
return "Time";
case 1:
return "From";
case 2:
return "To";
case 3:
return "Data";
}
assert(false);
return "";
}
std::string data(size_t row, size_t column) const override {
if (row < packages_.size()) {
auto const& pkg = packages_[row];
switch (column) {
case 0: return pkg.timestamp;
case 1: return pkg.from;
case 2: return pkg.to;
case 3: return pkg.size;
}
}
assert(false);
return "";
}
void add_listener(Listener* listener) override {
listeners_.insert(listener);
}
void remove_listener(Listener* listener) override {
listeners_.erase(listener);
}
void package(Monitor::Package const& package) {
auto const index = packages_.size();
open_.emplace(package.id, index);
packages_.emplace_back();
auto& pkg = packages_.back();
format_timestamp(&pkg.timestamp, package.timestamp);
format_host_port(&pkg.from, package.source_host, package.source_port);
format_host_port(&pkg.to, package.target_host, package.target_port);
format_size(&pkg.size, 0, false);
notify_added(index, index);
}
size_t package_data(uint32_t id, char const* data, size_t size, bool last) {
auto it = open_.find(id);
if (it == open_.end()) {
assert(false);
return std::string::npos;
}
auto const index = it->second;
auto& pkg = packages_[index];
pkg.data.append(data, size);
format_size(&pkg.size, pkg.data.size(), last);
if (last) open_.erase(it);
notify_changed(index, index);
return index;
}
Package const& package(size_t index) const {
if (index < packages_.size()) {
return packages_[index];
}
assert(false);
return EMPTY;
}
void clear() {
auto last = packages_.size();
epoch_.tv_sec = 0;
epoch_.tv_nsec = 0;
if (last == 0) return;
packages_.clear();
open_.clear();
notify_removed(0, last - 1);
}
private:
static const Package EMPTY;
static void format_size(std::string* out, size_t size, bool done) {
char tmp[50];
auto len = snprintf(tmp, sizeof(tmp), "%llu bytes", static_cast<unsigned long long>(size));
out->assign(tmp, len);
if (!done) out->append(" ...", 4);
}
static void format_host_port(std::string* out, std::string const& host, uint16_t port) {
out->assign(host);
out->push_back(':');
char tmp[10];
auto len = snprintf(tmp, sizeof(tmp), "%u", static_cast<unsigned>(port));
out->append(tmp, len);
}
void format_timestamp(std::string* out, struct timespec const& time) {
if (epoch_.tv_sec == 0 && epoch_.tv_nsec == 0) {
epoch_ = time;
}
auto s = time.tv_sec - epoch_.tv_sec;
auto n = time.tv_nsec - epoch_.tv_nsec;
if (n < 0) {
--s;
n += 1000000000ull;
}
char tmp[50];
auto len = snprintf(tmp, sizeof(tmp), "%ld.%09lu", static_cast<long>(s), static_cast<unsigned long>(n));
out->assign(tmp, len);
}
void notify_added(size_t first, size_t last) {
auto it = listeners_.notify();
while (it.has_next()) {
it.next()->rows_added(this, first, last);
}
}
void notify_changed(size_t first, size_t last) {
auto it = listeners_.notify();
while (it.has_next()) {
it.next()->rows_changed(this, first, last);
}
}
void notify_removed(size_t first, size_t last) {
auto it = listeners_.notify();
while (it.has_next()) {
it.next()->rows_removed(this, first, last);
}
}
std::vector<Package> packages_;
std::unordered_map<uint32_t, size_t> open_;
Observers<Listener*> listeners_;
struct timespec epoch_;
static const std::string empty_;
};
// static
const PackageList::Package PackageList::EMPTY;
// static
const std::string PackageList::empty_;
class MemoryConfig : public GuiConfig {
public:
using Config::get;
std::string const& get(std::string const& key,
std::string const& fallback) override {
auto it = memory_.find(key);
if (it == memory_.end()) return fallback;
return it->second;
}
char const* get(std::string const& key, char const* fallback) override {
auto it = memory_.find(key);
if (it == memory_.end()) return fallback;
return it->second.c_str();
}
bool is_set(std::string const& key) override {
return memory_.count(key);
}
void set(std::string const& key, std::string const& value) override {
memory_[key] = value;
}
void remove(std::string const& key) override {
memory_.erase(key);
}
private:
std::unordered_map<std::string, std::string> memory_;
};
class StringLogger : public Logger {
public:
void out(Level lvl, char const* format, ...) override {
if (lvl == DBG) {
#ifdef NDEBUG
return;
#endif
}
char* tmp;
va_list args;
va_start(args, format);
auto ret = vasprintf(&tmp, format, args);
va_end(args);
if (ret == -1) return;
switch (lvl) {
case ERR:
text_->append("Error", ATTR_ERROR);
break;
case WARN:
text_->append("Warning", ATTR_WARNING);
break;
case INFO:
text_->append("Info", ATTR_INFO);
break;
case DBG:
text_->append("Debug", ATTR_DEBUG);
break;
}
text_->append(": ");
text_->append(tmp, ret);
text_->append("\n");
free(tmp);
}
AttributedText const* text() const {
return text_.get();
}
void clear() {
text_->reset();
}
StringLogger()
: ATTR_ERROR(0xff, 0, 0),
ATTR_WARNING(0xff, 0xff, 0),
ATTR_INFO(),
ATTR_DEBUG(0, 0xff, 0),
text_(AttributedText::create()) {
}
private:
const AttributedText::Attribute ATTR_ERROR;
const AttributedText::Attribute ATTR_WARNING;
const AttributedText::Attribute ATTR_INFO;
const AttributedText::Attribute ATTR_DEBUG;
std::unique_ptr<AttributedText> text_;
};
class MonitorGui : GuiMenu::Listener, GuiMain::Listener, Monitor::Delegate,
GuiTextWindow::Listener {
private:
class ConnectFormListener : public GuiFormApply::Listener {
public:
void changed(GuiForm* UNUSED(form), std::string const& UNUSED(id)) override {
}
bool about_to_close(GuiForm* form) override {
auto address = form->get_string("address");
if (address.empty()) {
form->set_error("Empty address");
return false;
}
if (!parse_address(address, nullptr, nullptr)) {
form->set_error("Invalid address, expects HOST[:PORT]");
return false;
}
return true;
}
};
class ConnectFormDelegate : public GuiFormApply::Delegate {
public:
ConnectFormDelegate(Monitor* monitor, Config* config)
: monitor_(monitor), config_(config) {
}
void apply(GuiFormApply* form) override {
std::string host;
uint16_t port;
auto const& addr = form->get_string("address");
if (!parse_address(addr, &host, &port)) {
form->set_error("Invalid address, expects HOST[:PORT]");
form->applied(false);
return;
}
config_->set("connect", addr);
monitor_->connect(host, port);
}
private:
Monitor* monitor_;
Config* config_;
};
class SetupFormListener : public GuiFormApply::Listener {
public:
void changed(GuiForm* form, std::string const& id) override {
if (id.compare("mitm") == 0) {
auto enable = form->get_bool(id);
form->enable("ssl-certs", enable);
form->enable("ssl-ca", enable);
form->enable("unsecure", enable);
}
}
bool about_to_close(GuiForm* form) override {
auto const& port = form->get_string("port");
if (port.empty()) {
form->set_error("Empty proxy port");
return false;
}
auto const& mitm = form->get_bool("mitm");
if (mitm) {
auto const& certs = form->get_file("ssl-certs");
if (certs.empty()) {
form->set_error("No SSL certificates file set");
return false;
}
auto const& ca = form->get_file("ssl-ca");
if (ca.empty()) {
form->set_error("No SSL CA file set");
return false;
}
}
return true;
}
};
class SetupFormDelegate : public GuiFormApply::Delegate {
public:
SetupFormDelegate(std::unique_ptr<Proxy>& proxy,
Config* proxy_config,
StringLogger* proxy_logger,
Monitor* monitor,
Config* config,
Looper* looper,
Resolver* resolver)
: proxy_(proxy), proxy_config_(proxy_config), proxy_logger_(proxy_logger),
monitor_(monitor), config_(config), looper_(looper),
resolver_(resolver) {
}
void apply(GuiFormApply* form) override {
auto const& bind = form->get_string("bind");
auto const& port = form->get_string("port");
if (port.empty()) {
form->set_error("Empty proxy port");
form->applied(false);
return;
}
config_->set("bind", bind);
config_->set("port", port);
#if HAVE_SSL
auto const& mitm = form->get_bool("mitm");
config_->set("mitm", mitm);
proxy_config_->set("proxy_bind", bind);
proxy_config_->set("proxy_port", port);
if (mitm) {
auto const& certs = form->get_file("ssl-certs");
auto const& ca = form->get_file("ssl-ca");
auto const& unsecure = form->get_bool("unsecure");
if (certs.empty()) {
form->set_error("No SSL certificates file set");
form->applied(false);
return;
}
if (access(certs.c_str(), R_OK)) {
form->set_error("SSL certificates file not readable");
form->applied(false);
return;
}
if (ca.empty()) {
form->set_error("No SSL CA file set");
form->applied(false);
return;
}
if (access(ca.c_str(), R_OK)) {
form->set_error("SSL CA file not readable");
form->applied(false);
return;
}
config_->set("ssl-certs", certs);
config_->set("ssl-ca", ca);
config_->set("unsecure", unsecure);
proxy_config_->set("ssl_cert_bundle", certs);
proxy_config_->set("ssl_ca_cert", ca);
proxy_config_->set("ssl_ca_key", ca);
proxy_config_->set("ssl_unsecure", unsecure);
} else {
proxy_config_->remove("ssl_cert_bundle");
proxy_config_->remove("ssl_ca_cert");
proxy_config_->remove("ssl_ca_key");
proxy_config_->remove("ssl_unsecure");
}
#endif // HAVE_SSL
proxy_config_->set("__one_single_monitor", "true");
io::auto_fd proxy_fd(
Proxy::setup_accept_socket(proxy_config_, proxy_logger_));
if (!proxy_fd) {
auto error = proxy_logger_->text()->text();
proxy_logger_->clear();
std::string errmsg =
"Unable to bind " + (bind.empty() ? "*" : bind) + ":" + port;
if (!error.empty()) errmsg += "\n" + error;
form->set_error(errmsg);
form->applied(false);
return;
}
int monitor_fd[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, monitor_fd)) {
form->set_error("Unable to create socketpair");
form->applied(false);
return;
}
fcntl(monitor_fd[0], F_SETFL, O_NONBLOCK);
fcntl(monitor_fd[1], F_SETFL, O_NONBLOCK);
proxy_.reset(Proxy::create(proxy_config_, "",
nullptr, nullptr, proxy_logger_,
proxy_fd.release(), monitor_fd[0],
looper_, resolver_));
monitor_->connect(monitor_fd[1]);
}
private:
std::unique_ptr<Proxy>& proxy_;
Config* proxy_config_;
StringLogger* proxy_logger_;
Monitor* monitor_;
Config* config_;
Looper* looper_;
Resolver* resolver_;
};
class GenerateFormListener : public GuiFormApply::Listener {
public:
void changed(GuiForm* UNUSED(form), std::string const& UNUSED(id)) override {
}
bool about_to_close(GuiForm* form) override {
auto const& output = form->get_file("output");
if (output.empty()) {
form->set_error("No output file selected.");
return false;
}
return true;
}
};
class GenerateFormDelegate : public GuiFormApply::Delegate {
public:
GenerateFormDelegate(Looper* looper, Config* config)
: looper_(looper), config_(config) {
}
~GenerateFormDelegate() {
if (read_) looper_->remove(read_.get());
}
void apply(GuiFormApply* form) override {
auto const& issuer = form->get_string("issuer");
auto const& output = form->get_file("output");
if (output.empty()) {
form->set_error("No ouput file selected.");
form->applied(false);
return;
}
config_->set("issuer", issuer);
config_->set("genca-output", output);
int fd[2];
if (pipe(fd)) {
form->set_error("Unable to create pipe.");
form->applied(false);
return;
}
read_.reset(fd[0]);
logger_.reset(new StringLogger());
looper_->add(read_.get(), Looper::EVENT_READ, [=](int, uint8_t) -> void {
char c;
if (read(read_.get(), &c, 1) != 1) {
c = 0;
}
looper_->remove(read_.get());
read_.reset();
if (c) {
form->applied(true);
} else {
form->set_error(logger_->text()->text());
form->applied(false);
}
});
// Hack to get SSL lib to initialize on the right thread
SSLCert::load(Logger::null(), "");
std::thread(generate, issuer, output, logger_.get(), fd[1]).detach();
}
private:
static void generate(std::string const& issuer, std::string const& output,
Logger* logger, int fd) {
char c = doGenerate(issuer, output, logger) ? 1 : 0;
write(fd, &c, 1);
close(fd);
}
static bool doGenerate(std::string const& issuer, std::string const& output,
Logger* logger) {
std::unique_ptr<SSLEntropy> entropy(SSLEntropy::create(logger));
if (!entropy) return false;
std::string key;
if (!SSLKey::generate(logger, entropy.get(), &key)) return false;
std::string cert;
std::unique_ptr<SSLKey> pkey(SSLKey::load(logger, key));
if (!SSLCert::generate(logger, entropy.get(), nullptr, nullptr, issuer,
pkey.get(), &cert)) return false;
std::ofstream of(output);
of << cert << '\n' << key << std::endl;
return of.good();
}
io::auto_fd read_;
Looper* looper_;
Config* config_;
std::unique_ptr<StringLogger> logger_;
};
public:
MonitorGui()
: packages_(new PackageList()),
main_(GuiMain::create("TransparentProxy Monitor", 800, 500)),
menu_(GuiMenu::create()),
statusbar_(GuiStatusBar::create()),
looper_(main_->createLooper()),
has_selection_(false),
selection_(0) {
auto file = menu_->add_menu("File");
file->add_item(ACTION_SETUP, "Setup...");
file->add_item(ACTION_CONNECT, "Connect...");
file->add_item(ACTION_DISCONNECT, "Disconnect");
file->add_separator();
file->add_item(ACTION_EXIT, "Exit");
auto edit = menu_->add_menu("Edit");
edit->add_item(ACTION_COPY_TEXT, "Copy");
edit->add_item(ACTION_COPY_RAW, "Copy binary");
edit->add_separator();
edit->add_item(ACTION_CLEAR, "Clear");
auto tools = menu_->add_menu("Tools");
tools->add_item(ACTION_GENERATE_CA, "Generate CA...");
auto help = menu_->add_menu("Help");
help->add_item(ACTION_ABOUT, "About...");
help->add_item(ACTION_PROXY_LOG, "Proxy log...");
main_->set_menu(menu_.get());
main_->set_statusbar(statusbar_.get());
main_->set_split(0.7);
main_->set_listmodel(packages_.get());
statusbar_->set_status("Not connected");
menu_->enable_item(ACTION_DISCONNECT, false);
menu_->enable_item(ACTION_COPY_RAW, false);
menu_->enable_item(ACTION_COPY_TEXT, false);
menu_->add_listener(this);
main_->add_listener(this);
}
bool run(int argc, char** argv) {
return main_->run(argc, argv);
}
// GuiMenu::Listener
void item_activated(const std::string& id) override {
if (id == ACTION_EXIT) {
main_->exit();
} else if (id == ACTION_ABOUT) {
if (!about_) {
about_.reset(GuiAbout::create("TransparentProxy Monitor",
VERSION,
"Joel Klinghed", "the_jk@yahoo.com",
nullptr));
}
main_->show(about_.get());
} else if (id == ACTION_SETUP) {
setup_monitor();
setup_proxy();
proxy_logger_->clear();
auto dlg = std::unique_ptr<GuiFormApply::Delegate>(
new SetupFormDelegate(proxy_, proxy_config_.get(), proxy_logger_.get(),
monitor_.get(), main_->config(),
looper_.get(), resolver_.get()));
auto lst = std::unique_ptr<GuiFormApply::Listener>(
new SetupFormListener());
connect_.reset(
GuiFormApply::create("Setup...",
"Setup a proxy to start monitoring traffic.",
"Setup",
dlg.get()));
connect_->add_string("bind", "Address",
main_->config()->get("bind", ""),
"Address to listen for proxy connections on."
" Leave empty to listen on all interfaces.");
connect_->add_string("port", "Port",
main_->config()->get("port", "8080"),
"Port to listen for proxy connections on.");
#if HAVE_SSL
bool mitm = main_->config()->get("mitm", false);
connect_->add_bool("mitm", "Intercept SSL traffic",
mitm,
"If enabled SSL connections will be intercepted"
" by the proxy to log unencrypted traffic.");
std::vector<GuiFormApply::Filter> filter;
filter.emplace_back();
filter.back().name = "PEM";
filter.back().masks.emplace_back("*.pem");
connect_->add_file("ssl-ca", "Certificate Authority",
main_->config()->get("ssl-ca",
main_->config()->get("genca-output", "")),
"CA and key to sign all fake server certificates with",
GuiForm::FILE_OPEN, filter);
connect_->enable("ssl-ca", mitm);
filter.back().name = "CRT";
filter.back().masks.emplace_back("*.crt");
connect_->add_file("ssl-certs", "Certificate bundle",
main_->config()->get("ssl-certs",
default_cert_bundle()),
"Certificate bundle to verify remote SSL connections",
GuiForm::FILE_OPEN, filter);
connect_->enable("ssl-certs", mitm);
connect_->add_bool("unsecure", "Allow unsecure remote connections",
main_->config()->get("unsecure", false),
"Allow deprecated protocols such as SSLv3 and "
" self-signed or missmatched certificates");
connect_->enable("unsecure", mitm);
#endif // HAVE_SSL
connect_->add_listener(lst.get());
if (connect_->show(main_.get())) {
monitor_->attach();
connect_.reset();
} else {
connect_.reset();
monitor_->disconnect();
proxy_.reset();
}
} else if (id == ACTION_CONNECT) {
setup_monitor();
auto dlg = std::unique_ptr<GuiFormApply::Delegate>(
new ConnectFormDelegate(monitor_.get(), main_->config()));
auto lst = std::unique_ptr<GuiFormApply::Listener>(
new ConnectFormListener());
connect_.reset(
GuiFormApply::create("Connect...",
"Enter address for monitor to connect to",
"Connect",
dlg.get()));
connect_->add_string("address", "Address",
main_->config()->get("connect", ""),
"Host and optional port (defaults to 9000)");
connect_->add_listener(lst.get());
if (connect_->show(main_.get())) {
monitor_->attach();
} else {
monitor_->disconnect();
}
connect_.reset();
} else if (id == ACTION_DISCONNECT) {
assert(monitor_);
if (monitor_) {
monitor_->disconnect();
}
proxy_.reset();
} else if (id == ACTION_COPY_TEXT) {
if (!has_selection_) {
assert(false);
return;
}
auto& pkg = packages_->package(selection_);
std::unique_ptr<AttributedText> text(AttributedText::create());
HexDump::write(text.get(), HexDump::ADDRESS | HexDump::CHARS, pkg.data);
std::string str;
str.append("From: ").append(pkg.from).push_back('\n');
str.append("To : ").append(pkg.to).push_back('\n');
str.append("At : ").append(pkg.timestamp).push_back('\n');
str.append(text->text());
main_->add_to_clipboard(str);
} else if (id == ACTION_COPY_RAW) {
if (!has_selection_) {
assert(false);
return;
}
auto& pkg = packages_->package(selection_);
main_->add_to_clipboard(pkg.data, "application/octet-stream");
} else if (id == ACTION_CLEAR) {
packages_->clear();
} else if (id == ACTION_PROXY_LOG) {
if (!proxy_logwnd_) {
if (!proxy_logger_) proxy_logger_.reset(new StringLogger());
proxy_logwnd_.reset(GuiTextWindow::create(
"Proxy log", 500, 200, proxy_logger_->text()));
proxy_logwnd_->add_listener(this);
proxy_logwnd_->show(main_.get());
} else {
proxy_logwnd_->focus();
}
} else if (id == ACTION_GENERATE_CA) {
auto del = std::unique_ptr<GuiFormApply::Delegate>(
new GenerateFormDelegate(looper_.get(), main_->config()));
auto lst = std::unique_ptr<GuiFormApply::Listener>(
new GenerateFormListener());
auto dlg = std::unique_ptr<GuiFormApply>(
GuiFormApply::create("Generate CA...",
"Generate a certificate and key pair "
"and save to file.",
"Generate",
del.get()));
dlg->add_string("issuer", "Issuer name",
main_->config()->get("issuer", ""),
"Issuer name to user instead of default.");
std::vector<GuiFormApply::Filter> filter;
filter.emplace_back();
filter.back().name = "PEM";
filter.back().masks.emplace_back("*.pem");
dlg->add_file("output", "File", "",
"File to save certificate and key pair to.",
GuiForm::FILE_SAVE, filter);
dlg->add_listener(lst.get());
dlg->show(main_.get());
} else {
assert(false);
}
}
// GuiMain::Listener
bool about_to_exit(GuiMain* main) override {
assert(main_.get() == main);
return true;
}
void selected_row(GuiMain* main, size_t index) override {
assert(main_.get() == main);
if (has_selection_ && selection_ == index) return;
has_selection_ = true;
selection_ = index;
auto& pkg = packages_->package(index);
std::unique_ptr<AttributedText> text(AttributedText::create());
HexDump::write(text.get(), HexDump::ADDRESS | HexDump::CHARS, pkg.data);
main_->set_package(std::move(text));
menu_->enable_item(ACTION_COPY_RAW, true);
menu_->enable_item(ACTION_COPY_TEXT, true);
}
void lost_selection(GuiMain* main) override {
assert(main_.get() == main);
has_selection_ = false;
main_->set_package(nullptr);
menu_->enable_item(ACTION_COPY_RAW, false);
menu_->enable_item(ACTION_COPY_TEXT, false);
}
// GuiTextWindow::Listener
bool about_to_close(GuiTextWindow* wnd) override {
assert(proxy_logwnd_.get() == wnd);
proxy_logwnd_.reset();
return true;
}
// Monitor::Delegate
void state(Monitor* monitor, Monitor::State state) override {
assert(monitor == monitor_.get());
if (connect_) {
switch (state) {
case Monitor::DISCONNECTED:
connect_->set_error("Unable to connect, is the proxy running"
" and listening for monitors?");
connect_->applied(false);
break;
case Monitor::CONNECTED:
connect_->applied(true);
break;
case Monitor::CONNECTING:
case Monitor::ATTACHED:
break;
}
}
bool allow_connect, allow_disconnect;
switch (state) {
case Monitor::DISCONNECTED:
statusbar_->set_status("Not connected");
allow_connect = true;
allow_disconnect = false;
break;
case Monitor::CONNECTED:
statusbar_->set_status("Connected");
allow_connect = false;
allow_disconnect = true;
break;
case Monitor::CONNECTING:
statusbar_->set_status("Connecting...");
allow_connect = false;
allow_disconnect = false;
break;
case Monitor::ATTACHED:
statusbar_->set_status("Connected and attached");
allow_connect = false;
allow_disconnect = true;
break;
}
menu_->enable_item(ACTION_SETUP, allow_connect);
menu_->enable_item(ACTION_CONNECT, allow_connect);
menu_->enable_item(ACTION_DISCONNECT, allow_disconnect);
}
void error(Monitor* monitor, std::string const& UNUSED(error)) override {
assert(monitor == monitor_.get());
}
void package(Monitor* monitor, Monitor::Package const& package) override {
assert(monitor == monitor_.get());
packages_->package(package);
}
void package_data(Monitor* monitor, uint32_t id,
char const* data, size_t size, bool last) override {
assert(monitor == monitor_.get());
auto index = packages_->package_data(id, data, size, last);
if (has_selection_ && index == selection_) {
selected_row(nullptr, index);
}
}
private:
void setup_monitor() {
if (!resolver_) {
resolver_.reset(Resolver::create(looper_.get()));
}
if (!monitor_) {
monitor_.reset(Monitor::create(looper_.get(), resolver_.get(), this));
}
}
void setup_proxy() {
if (!proxy_config_) {
proxy_config_.reset(new MemoryConfig());
}
if (!proxy_logger_) {
proxy_logger_.reset(new StringLogger());
}
}
std::unique_ptr<PackageList> packages_;
std::unique_ptr<GuiMain> main_;
std::unique_ptr<GuiMenu> menu_;
std::unique_ptr<GuiStatusBar> statusbar_;
std::unique_ptr<GuiAbout> about_;
std::unique_ptr<GuiFormApply> connect_;
std::unique_ptr<Looper> looper_;
std::unique_ptr<Resolver> resolver_;
std::unique_ptr<Monitor> monitor_;
std::unique_ptr<Config> proxy_config_;
std::unique_ptr<StringLogger> proxy_logger_;
std::unique_ptr<Proxy> proxy_;
std::unique_ptr<GuiTextWindow> proxy_logwnd_;
bool has_selection_;
size_t selection_;
};
} // namespace
int main(int argc, char** argv) {
auto gui = std::unique_ptr<MonitorGui>(new MonitorGui());
return gui->run(argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE;
}
|