Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
echo_server_tls.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, Peter Thorson. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of the WebSocket++ Project nor the
12 * names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
38
40
41#include <iostream>
42
44
45using websocketpp::lib::placeholders::_1;
46using websocketpp::lib::placeholders::_2;
47using websocketpp::lib::bind;
48
49// pull out the type of messages sent by our config
51typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context> context_ptr;
52
54 std::cout << "on_message called with hdl: " << hdl.lock().get()
55 << " and message: " << msg->get_payload()
56 << std::endl;
57
58 try {
59 s->send(hdl, msg->get_payload(), msg->get_opcode());
60 } catch (const websocketpp::lib::error_code& e) {
61 std::cout << "Echo failed because: " << e
62 << "(" << e.message() << ")" << std::endl;
63 }
64}
65
67 server::connection_ptr con = s->get_con_from_hdl(hdl);
68
69 con->set_body("Hello World!");
71}
72
73std::string get_password() {
74 return "test";
75}
76
77// See https://wiki.mozilla.org/Security/Server_Side_TLS for more details about
78// the TLS modes. The code below demonstrates how to implement both the modern
83
85 namespace asio = websocketpp::lib::asio;
86
87 std::cout << "on_tls_init called with hdl: " << hdl.lock().get() << std::endl;
88 std::cout << "using TLS mode: " << (mode == MOZILLA_MODERN ? "Mozilla Modern" : "Mozilla Intermediate") << std::endl;
89
90 context_ptr ctx = websocketpp::lib::make_shared<asio::ssl::context>(asio::ssl::context::sslv23);
91
92 try {
93 if (mode == MOZILLA_MODERN) {
94 // Modern disables TLSv1
95 ctx->set_options(asio::ssl::context::default_workarounds |
96 asio::ssl::context::no_sslv2 |
97 asio::ssl::context::no_sslv3 |
98 asio::ssl::context::no_tlsv1 |
99 asio::ssl::context::single_dh_use);
100 } else {
101 ctx->set_options(asio::ssl::context::default_workarounds |
102 asio::ssl::context::no_sslv2 |
103 asio::ssl::context::no_sslv3 |
104 asio::ssl::context::single_dh_use);
105 }
106 ctx->set_password_callback(bind(&get_password));
107 ctx->use_certificate_chain_file("server.pem");
108 ctx->use_private_key_file("server.pem", asio::ssl::context::pem);
109
110 // Example method of generating this file:
111 // `openssl dhparam -out dh.pem 2048`
112 // Mozilla Intermediate suggests 1024 as the minimum size to use
113 // Mozilla Modern suggests 2048 as the minimum size to use.
114 ctx->use_tmp_dh_file("dh.pem");
115
116 std::string ciphers;
117
118 if (mode == MOZILLA_MODERN) {
119 ciphers = "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK";
120 } else {
121 ciphers = "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA";
122 }
123
124 if (SSL_CTX_set_cipher_list(ctx->native_handle() , ciphers.c_str()) != 1) {
125 std::cout << "Error setting cipher list" << std::endl;
126 }
127 } catch (std::exception& e) {
128 std::cout << "Exception: " << e.what() << std::endl;
129 }
130 return ctx;
131}
132
133int main() {
134 // Create a server endpoint
135 server echo_server;
136
137 // Initialize ASIO
138 echo_server.init_asio();
139
140 // Register our message handler
141 echo_server.set_message_handler(bind(&on_message,&echo_server,::_1,::_2));
142 echo_server.set_http_handler(bind(&on_http,&echo_server,::_1));
143 echo_server.set_tls_init_handler(bind(&on_tls_init,MOZILLA_INTERMEDIATE,::_1));
144
145 // Listen on port 9002
146 echo_server.listen(9002);
147
148 // Start the server accept loop
149 echo_server.start_accept();
150
151 // Start the ASIO io_service run loop
152 echo_server.run();
153
154}
void set_message_handler(message_handler h)
Definition endpoint.hpp:322
void set_http_handler(http_handler h)
Definition endpoint.hpp:312
lib::shared_ptr< message > ptr
Definition message.hpp:86
Server endpoint role based on the given config.
void start_accept(lib::error_code &ec)
Starts the server's async connection acceptance loop (exception free)
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
websocketpp::lib::shared_ptr< boost::asio::ssl::context > context_ptr
@ MOZILLA_MODERN
@ MOZILLA_INTERMEDIATE
websocketpp::lib::shared_ptr< websocketpp::lib::asio::ssl::context > context_ptr
void on_message(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
websocketpp::server< websocketpp::config::asio_tls > server
websocketpp::config::asio::message_type::ptr message_ptr
int main()
std::string get_password()
context_ptr on_tls_init(tls_mode mode, websocketpp::connection_hdl hdl)
void on_http(server *s, websocketpp::connection_hdl hdl)
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
char * s