-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWebServer.cpp
More file actions
884 lines (775 loc) · 24.5 KB
/
Copy pathWebServer.cpp
File metadata and controls
884 lines (775 loc) · 24.5 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
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
#include "WebServer.h"
#include "index.hpp"
#include "detect_ssl.hpp"
#include "server_certificate.hpp"
#include "ssl_stream.hpp"
#include "root_certificates.hpp"
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/signal_set.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/asio/strand.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/make_unique.hpp>
#include <boost/config.hpp>
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include <iostream>
#include "Serialize.h"
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>
// Report a failure
void
fail(boost::system::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
// Handles a plain WebSocket connection
struct plain_websocket_session : std::enable_shared_from_this<plain_websocket_session> {
websocket::stream<tcp::socket> ws_;
bool close_ = false;
size_t callback_id;
WebBroker& web_broker;
boost::beast::multi_buffer buffer_;
char ping_state_ = 0;
boost::asio::strand<boost::asio::io_context::executor_type> strand_;
boost::asio::steady_timer timer_;
// Start the asynchronous operation
template<class Body, class Allocator>
void
do_accept(http::request<Body, http::basic_fields<Allocator>> req)
{
// Set the control callback. This will be called
// on every incoming ping, pong, and close frame.
ws_.control_callback(
std::bind(
&plain_websocket_session::on_control_callback,
this,
std::placeholders::_1,
std::placeholders::_2));
// Set the timer
timer_.expires_after(std::chrono::seconds(15));
// Accept the websocket handshake
ws_.async_accept(
req,
boost::asio::bind_executor(
strand_,
std::bind(
&plain_websocket_session::on_accept,
shared_from_this(),
std::placeholders::_1)));
}
void write(const std::string& x) {
ws_.write(boost::asio::buffer(x));
}
void
on_accept(boost::system::error_code ec)
{
// Happens when the timer closes the socket
if(ec == boost::asio::error::operation_aborted)
return;
if(ec)
return fail(ec, "accept");
// Read a message
do_read();
}
// Called when the timer expires.
void
on_timer(boost::system::error_code ec)
{
if(ec && ec != boost::asio::error::operation_aborted)
return fail(ec, "timer");
// See if the timer really expired since the deadline may have moved.
if(timer_.expiry() <= std::chrono::steady_clock::now())
{
// If this is the first time the timer expired,
// send a ping to see if the other end is there.
if(ws_.is_open() && ping_state_ == 0)
{
// Note that we are sending a ping
ping_state_ = 1;
// Set the timer
timer_.expires_after(std::chrono::seconds(15));
// Now send the ping
ws_.async_ping({},
boost::asio::bind_executor(
strand_,
std::bind(
&plain_websocket_session::on_ping,
shared_from_this(),
std::placeholders::_1)));
}
else
{
// The timer expired while trying to handshake,
// or we sent a ping and it never completed or
// we never got back a control frame, so close.
do_timeout();
return;
}
}
// Wait on the timer
timer_.async_wait(
boost::asio::bind_executor(
strand_,
std::bind(
&plain_websocket_session::on_timer,
shared_from_this(),
std::placeholders::_1)));
}
// Called to indicate activity from the remote peer
void
activity()
{
// Note that the connection is alive
ping_state_ = 0;
// Set the timer
timer_.expires_after(std::chrono::seconds(15));
}
// Called after a ping is sent.
void
on_ping(boost::system::error_code ec)
{
// Happens when the timer closes the socket
if(ec == boost::asio::error::operation_aborted)
return;
if(ec)
return fail(ec, "ping");
// Note that the ping was sent.
if(ping_state_ == 1)
{
ping_state_ = 2;
}
else
{
// ping_state_ could have been set to 0
// if an incoming control frame was received
// at exactly the same time we sent a ping.
BOOST_ASSERT(ping_state_ == 0);
}
}
void
on_control_callback(
websocket::frame_type kind,
boost::beast::string_view payload)
{
boost::ignore_unused(kind, payload);
// Note that there is activity
activity();
}
void
do_read()
{
// Read a message into our buffer
ws_.async_read(
buffer_,
boost::asio::bind_executor(
strand_,
std::bind(
&plain_websocket_session::on_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
}
void
on_read(
boost::system::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
// Happens when the timer closes the socket
if(ec == boost::asio::error::operation_aborted)
return;
// This indicates that the websocket_session was closed
if(ec == websocket::error::closed)
return;
if(ec)
fail(ec, "read");
// Note that there is activity
activity();
// Echo the message
ws_.text(ws_.got_text());
ws_.async_write(
buffer_.data(),
boost::asio::bind_executor(
strand_,
std::bind(
&plain_websocket_session::on_write,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
}
void
on_write(
boost::system::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
// Happens when the timer closes the socket
if(ec == boost::asio::error::operation_aborted)
return;
if(ec)
return fail(ec, "write");
// Clear the buffer
buffer_.consume(buffer_.size());
// Do another read
do_read();
}
explicit plain_websocket_session(tcp::socket socket, WebBroker& web_broker)
: strand_(socket.get_executor())
, timer_(socket.get_executor().context(), (std::chrono::steady_clock::time_point::max)()),
ws_{ std::move(socket) },
web_broker{ web_broker },
callback_id{ web_broker.register_callback([&](const std::string& x){ write(x); }) }{
}
~plain_websocket_session() {
web_broker.unregister_callback(callback_id);
}
websocket::stream<tcp::socket>&
ws()
{
return ws_;
}
// Start the asynchronous operation
template<class Body, class Allocator>
void
run(http::request<Body, http::basic_fields<Allocator>> req)
{
// Run the timer. The timer is operated
// continuously, this simplifies the code.
on_timer({});
// Accept the WebSocket upgrade request
do_accept(std::move(req));
}
void
do_timeout()
{
// This is so the close can have a timeout
if(close_)
return;
close_ = true;
// Set the timer
timer_.expires_after(std::chrono::seconds(15));
// Close the WebSocket Connection
ws_.async_close(
websocket::close_code::normal,
boost::asio::bind_executor(
strand_,
std::bind(
&plain_websocket_session::on_close,
shared_from_this(),
std::placeholders::_1)));
}
void
on_close(boost::system::error_code ec)
{
// Happens when close times out
if(ec == boost::asio::error::operation_aborted)
return;
if(ec)
return fail(ec, "close");
// At this point the connection is gracefully closed
}
};
template<class Derived>
class http_session
{
// Access the derived class, this is part of
// the Curiously Recurring Template Pattern idiom.
Derived&
derived()
{
return static_cast<Derived&>(*this);
}
// This queue is used for HTTP pipelining.
class queue
{
enum
{
// Maximum number of responses we will queue
limit = 8
};
// The type-erased, saved work item
struct work
{
virtual ~work() = default;
virtual void operator()() = 0;
};
http_session& self_;
std::vector<std::unique_ptr<work>> items_;
public:
explicit
queue(http_session& self)
: self_(self)
{
static_assert(limit > 0, "queue limit must be positive");
items_.reserve(limit);
}
// Returns `true` if we have reached the queue limit
bool
is_full() const
{
return items_.size() >= limit;
}
// Called when a message finishes sending
// Returns `true` if the caller should initiate a read
bool
on_write()
{
BOOST_ASSERT(! items_.empty());
auto const was_full = is_full();
items_.erase(items_.begin());
if(! items_.empty())
(*items_.front())();
return was_full;
}
// Called by the HTTP handler to send a response.
template<bool isRequest, class Body, class Fields>
void
operator()(http::message<isRequest, Body, Fields>&& msg)
{
// This holds a work item
struct work_impl : work
{
http_session& self_;
http::message<isRequest, Body, Fields> msg_;
work_impl(
http_session& self,
http::message<isRequest, Body, Fields>&& msg)
: self_(self)
, msg_(std::move(msg))
{
}
void
operator()()
{
http::async_write(
self_.derived().stream(),
msg_,
boost::asio::bind_executor(
self_.strand_,
std::bind(
&http_session::on_write,
self_.derived().shared_from_this(),
std::placeholders::_1,
msg_.need_eof())));
}
};
// Allocate and store the work
items_.push_back(
boost::make_unique<work_impl>(self_, std::move(msg)));
// If there was no previous work, start this one
if(items_.size() == 1)
(*items_.front())();
}
};
Store& store;
WebBroker& web_broker;
http::request<http::string_body> req_;
queue queue_;
http::response<http::string_body> bad_request(boost::beast::string_view why) {
http::response<http::string_body> res{ http::status::bad_request, req_.version() };
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.keep_alive(req_.keep_alive());
res.body() = why.to_string();
res.prepare_payload();
return res;
};
http::response<http::string_body> not_found(boost::beast::string_view target) {
http::response<http::string_body> res{ http::status::not_found, req_.version() };
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.keep_alive(req_.keep_alive());
res.body() = "The resource '" + target.to_string() + "' was not found.";
res.prepare_payload();
return res;
};
http::response<http::string_body> server_error(boost::beast::string_view what) {
http::response<http::string_body> res{ http::status::internal_server_error, req_.version() };
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.keep_alive(req_.keep_alive());
res.body() = "An error occurred: '" + what.to_string() + "'";
res.prepare_payload();
return res;
};
protected:
boost::asio::steady_timer timer_;
boost::asio::strand<boost::asio::io_context::executor_type> strand_;
boost::beast::flat_buffer buffer_;
public:
http_session(
boost::asio::io_context& ioc,
boost::beast::flat_buffer buffer,
Store& store,
WebBroker& web_broker)
: store{ store }
, web_broker{ web_broker }
, queue_(*this)
, timer_(ioc, (std::chrono::steady_clock::time_point::max)())
, strand_(ioc.get_executor())
, buffer_(std::move(buffer))
{
}
void
do_read()
{
// Set the timer
timer_.expires_after(std::chrono::seconds(15));
// Make the request empty before reading,
// otherwise the operation behavior is undefined.
req_ = {};
// Read a request
http::async_read(
derived().stream(),
buffer_,
req_,
boost::asio::bind_executor(
strand_,
std::bind(
&http_session::on_read,
derived().shared_from_this(),
std::placeholders::_1)));
}
// Called when the timer expires.
void
on_timer(boost::system::error_code ec)
{
if(ec && ec != boost::asio::error::operation_aborted)
return fail(ec, "timer");
// Verify that the timer really expired since the deadline may have moved.
if(timer_.expiry() <= std::chrono::steady_clock::now())
return derived().do_timeout();
// Wait on the timer
timer_.async_wait(
boost::asio::bind_executor(
strand_,
std::bind(
&http_session::on_timer,
derived().shared_from_this(),
std::placeholders::_1)));
}
void index() {
if (req_.method() == http::verb::head) {
http::response<http::empty_body> res{ http::status::ok, req_.version() };
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.content_length(index_body.size());
res.keep_alive(req_.keep_alive());
return queue_(std::move(res));
}
http::response<http::string_body> res{
std::piecewise_construct,
std::make_tuple(index_body),
std::make_tuple(http::status::ok, req_.version()) };
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.content_length(index_body.size());
res.keep_alive(req_.keep_alive());
return queue_(std::move(res));
}
void json_response(std::string body) {
if (req_.method() == http::verb::head) {
http::response<http::empty_body> res{ http::status::ok, req_.version() };
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "application/json");
res.content_length(body.size());
res.keep_alive(req_.keep_alive());
return queue_(std::move(res));
}
http::response<http::string_body> res{
std::piecewise_construct,
std::make_tuple(body),
std::make_tuple(http::status::ok, req_.version()) };
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "application/json");
res.content_length(body.size());
res.keep_alive(req_.keep_alive());
return queue_(std::move(res));
}
void handle_request() {
if (req_.method() != http::verb::get && req_.method() != http::verb::head)
return queue_(bad_request("Unknown HTTP-method"));
if (req_.target() == "/") return index();
if (req_.target() == "/dns") return json_response(serialize(store.dns_requests()));
if (req_.target() == "/connections") return json_response(serialize(store.connections()));
if (req_.target() == "/requests") return json_response(serialize(store.requests()));
if (req_.target() == "/netflows") return json_response(serialize(store.netflows()));
return queue_(bad_request("Not found."));
}
void
on_read(boost::system::error_code ec)
{
// Happens when the timer closes the socket
if(ec == boost::asio::error::operation_aborted)
return;
// This means they closed the connection
if(ec == http::error::end_of_stream)
return derived().do_eof();
if(ec)
return fail(ec, "read");
// See if it is a WebSocket Upgrade
if(websocket::is_upgrade(req_)) {
std::make_shared<plain_websocket_session>(derived().release_stream(), web_broker)->run(std::move(req_));
return;
}
// Send the response
handle_request();
// If we aren't at the queue limit, try to pipeline another request
if(! queue_.is_full())
do_read();
}
void
on_write(boost::system::error_code ec, bool close)
{
// Happens when the timer closes the socket
if(ec == boost::asio::error::operation_aborted)
return;
if(ec)
return fail(ec, "write");
if(close)
{
// This means we should close the connection, usually because
// the response indicated the "Connection: close" semantic.
return derived().do_eof();
}
// Inform the queue that a write completed
if(queue_.on_write())
{
// Read another request
do_read();
}
}
};
// Handles a plain HTTP connection
class plain_http_session
: public http_session<plain_http_session>
, public std::enable_shared_from_this<plain_http_session>
{
tcp::socket socket_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
Store& store;
WebBroker& web_broker;
public:
// Create the http_session
plain_http_session(
tcp::socket socket,
boost::beast::flat_buffer buffer,
Store& store,
WebBroker& web_broker)
: http_session<plain_http_session>(
socket.get_executor().context(),
std::move(buffer),
store, web_broker)
, socket_(std::move(socket))
, strand_(socket_.get_executor())
, store{ store }
, web_broker{ web_broker }
{
}
// Called by the base class
tcp::socket&
stream()
{
return socket_;
}
// Called by the base class
tcp::socket
release_stream()
{
return std::move(socket_);
}
// Start the asynchronous operation
void
run()
{
// Run the timer. The timer is operated
// continuously, this simplifies the code.
on_timer({});
do_read();
}
void
do_eof()
{
// Send a TCP shutdown
boost::system::error_code ec;
socket_.shutdown(tcp::socket::shutdown_send, ec);
// At this point the connection is closed gracefully
}
void
do_timeout()
{
// Closing the socket cancels all outstanding operations. They
// will complete with boost::asio::error::operation_aborted
boost::system::error_code ec;
socket_.shutdown(tcp::socket::shutdown_both, ec);
socket_.close(ec);
}
};
// Detects SSL handshakes
class detect_session : public std::enable_shared_from_this<detect_session>
{
tcp::socket socket_;
ssl::context& ctx_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
boost::beast::flat_buffer buffer_;
Store& store;
WebBroker& web_broker;
public:
explicit
detect_session(
tcp::socket socket,
ssl::context& ctx,
Store& store,
WebBroker& web_broker)
: socket_(std::move(socket))
, ctx_(ctx)
, strand_(socket_.get_executor())
, store{ store }
, web_broker{ web_broker }
{
}
// Launch the detector
void
run()
{
async_detect_ssl(
socket_,
buffer_,
boost::asio::bind_executor(
strand_,
std::bind(
&detect_session::on_detect,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2)));
}
void
on_detect(boost::system::error_code ec, boost::tribool result)
{
if(ec)
return fail(ec, "detect");
std::make_shared<plain_http_session>(
std::move(socket_),
std::move(buffer_),
store,
web_broker)->run();
}
};
// Accepts incoming connections and launches the sessions
class listener : public std::enable_shared_from_this<listener>
{
ssl::context& ctx_;
tcp::acceptor acceptor_;
tcp::socket socket_;
Store& store;
WebBroker& web_broker;
public:
listener(
boost::asio::io_context& ioc,
ssl::context& ctx,
tcp::endpoint endpoint,
Store& store,
WebBroker& web_broker)
: ctx_(ctx)
, acceptor_(ioc)
, socket_(ioc)
, store{ store }
, web_broker{ web_broker }
{
boost::system::error_code ec;
// Open the acceptor
acceptor_.open(endpoint.protocol(), ec);
if(ec)
{
fail(ec, "open");
return;
}
// Allow address reuse
acceptor_.set_option(boost::asio::socket_base::reuse_address(true));
if(ec)
{
fail(ec, "set_option");
return;
}
// Bind to the server address
acceptor_.bind(endpoint, ec);
if(ec)
{
fail(ec, "bind");
return;
}
// Start listening for connections
acceptor_.listen(
boost::asio::socket_base::max_listen_connections, ec);
if(ec)
{
fail(ec, "listen");
return;
}
}
// Start accepting incoming connections
void
run()
{
if(! acceptor_.is_open())
return;
do_accept();
}
void
do_accept()
{
acceptor_.async_accept(
socket_,
std::bind(
&listener::on_accept,
shared_from_this(),
std::placeholders::_1));
}
void
on_accept(boost::system::error_code ec)
{
if(ec)
{
fail(ec, "accept");
}
else
{
// Create the detector http_session and run it
std::make_shared<detect_session>(
std::move(socket_),
ctx_,
store,
web_broker)->run();
}
// Accept another connection
do_accept();
}
};
WebServer::WebServer(Store& store, WebBroker& web_broker, boost::asio::io_context& io_context,
const std::string& address, uint16_t port) : store{ store }, web_broker{ web_broker } {
ssl::context ctx{ssl::context::sslv23};
load_server_certificate(ctx);
auto ip = boost::asio::ip::make_address(address);
std::make_shared<listener>(
io_context,
ctx,
tcp::endpoint{ip, port},
store,
web_broker)->run();
}