1 //
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9
10 //------------------------------------------------------------------------------
11 //
12 // Example: WebSocket SSL server, synchronous
13 //
14 //------------------------------------------------------------------------------
15
16 #include "example/common/server_certificate.hpp"
17
18 #include <boost/beast/core.hpp>
19 #include <boost/beast/ssl.hpp>
20 #include <boost/beast/websocket.hpp>
21 #include <boost/beast/websocket/ssl.hpp>
22 #include <boost/asio/ip/tcp.hpp>
23 #include <boost/asio/ssl/stream.hpp>
24 #include <cstdlib>
25 #include <functional>
26 #include <iostream>
27 #include <string>
28 #include <thread>
29
30 namespace beast = boost::beast; // from <boost/beast.hpp>
31 namespace http = beast::http; // from <boost/beast/http.hpp>
32 namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
33 namespace net = boost::asio; // from <boost/asio.hpp>
34 namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
35 using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
36
37 //------------------------------------------------------------------------------
38
39 // Echoes back all received WebSocket messages
40 void
do_session(tcp::socket socket,ssl::context & ctx)41 do_session(tcp::socket socket, ssl::context& ctx)
42 {
43 try
44 {
45 // Construct the websocket stream around the socket
46 websocket::stream<beast::ssl_stream<tcp::socket&>> ws{socket, ctx};
47
48 // Perform the SSL handshake
49 ws.next_layer().handshake(ssl::stream_base::server);
50
51 // Set a decorator to change the Server of the handshake
52 ws.set_option(websocket::stream_base::decorator(
53 [](websocket::response_type& res)
54 {
55 res.set(http::field::server,
56 std::string(BOOST_BEAST_VERSION_STRING) +
57 " websocket-server-sync-ssl");
58 }));
59
60 // Accept the websocket handshake
61 ws.accept();
62
63 for(;;)
64 {
65 // This buffer will hold the incoming message
66 beast::flat_buffer buffer;
67
68 // Read a message
69 ws.read(buffer);
70
71 // Echo the message back
72 ws.text(ws.got_text());
73 ws.write(buffer.data());
74 }
75 }
76 catch(beast::system_error const& se)
77 {
78 // This indicates that the session was closed
79 if(se.code() != websocket::error::closed)
80 std::cerr << "Error: " << se.code().message() << std::endl;
81 }
82 catch(std::exception const& e)
83 {
84 std::cerr << "Error: " << e.what() << std::endl;
85 }
86 }
87
88 //------------------------------------------------------------------------------
89
main(int argc,char * argv[])90 int main(int argc, char* argv[])
91 {
92 try
93 {
94 // Check command line arguments.
95 if (argc != 3)
96 {
97 std::cerr <<
98 "Usage: websocket-server-sync-ssl <address> <port>\n" <<
99 "Example:\n" <<
100 " websocket-server-sync-ssl 0.0.0.0 8080\n";
101 return EXIT_FAILURE;
102 }
103 auto const address = net::ip::make_address(argv[1]);
104 auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
105
106 // The io_context is required for all I/O
107 net::io_context ioc{1};
108
109 // The SSL context is required, and holds certificates
110 ssl::context ctx{ssl::context::tlsv12};
111
112 // This holds the self-signed certificate used by the server
113 load_server_certificate(ctx);
114
115 // The acceptor receives incoming connections
116 tcp::acceptor acceptor{ioc, {address, port}};
117 for(;;)
118 {
119 // This will receive the new connection
120 tcp::socket socket{ioc};
121
122 // Block until we get a connection
123 acceptor.accept(socket);
124
125 // Launch the session, transferring ownership of the socket
126 std::thread(
127 &do_session,
128 std::move(socket),
129 std::ref(ctx)).detach();
130 }
131 }
132 catch (const std::exception& e)
133 {
134 std::cerr << "Error: " << e.what() << std::endl;
135 return EXIT_FAILURE;
136 }
137 }
138