Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
echo_server_tls.cpp File Reference
#include <websocketpp/config/asio.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
Include dependency graph for echo_server_tls.cpp:

Go to the source code of this file.

Typedefs

typedef websocketpp::server< websocketpp::config::asio_tlsserver
 
typedef websocketpp::config::asio::message_type::ptr message_ptr
 
typedef websocketpp::lib::shared_ptr< websocketpp::lib::asio::ssl::context > context_ptr
 

Enumerations

enum  tls_mode { MOZILLA_INTERMEDIATE = 1 , MOZILLA_MODERN = 2 }
 

Functions

void on_message (server *s, websocketpp::connection_hdl hdl, message_ptr msg)
 
void on_http (server *s, websocketpp::connection_hdl hdl)
 
std::string get_password ()
 
context_ptr on_tls_init (tls_mode mode, websocketpp::connection_hdl hdl)
 
int main ()
 

Typedef Documentation

◆ context_ptr

typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context> context_ptr

Definition at line 51 of file echo_server_tls.cpp.

◆ message_ptr

◆ server

NOTES

This example uses a number of standard classes through the websocketpp::lib namespace. This is to allow easy switching between Boost, the C++11 STL, and the standalone Asio library. Your program need not use these namespaces if you do not need this sort of flexibility.

Definition at line 43 of file echo_server_tls.cpp.

Enumeration Type Documentation

◆ tls_mode

enum tls_mode
Enumerator
MOZILLA_INTERMEDIATE 
MOZILLA_MODERN 

Definition at line 79 of file echo_server_tls.cpp.

79 {
82};
@ MOZILLA_MODERN
@ MOZILLA_INTERMEDIATE

Function Documentation

◆ get_password()

std::string get_password ( )

Definition at line 73 of file echo_server_tls.cpp.

73 {
74 return "test";
75}
Here is the caller graph for this function:

◆ main()

int main ( void )

Definition at line 133 of file echo_server_tls.cpp.

133 {
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
void start_accept(lib::error_code &ec)
Starts the server's async connection acceptance loop (exception free)
void on_message(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
context_ptr on_tls_init(tls_mode mode, websocketpp::connection_hdl hdl)
void on_http(server *s, websocketpp::connection_hdl hdl)
Here is the call graph for this function:

◆ on_http()

void on_http ( server * s,
websocketpp::connection_hdl hdl )

Definition at line 66 of file echo_server_tls.cpp.

66 {
67 server::connection_ptr con = s->get_con_from_hdl(hdl);
68
69 con->set_body("Hello World!");
71}
char * s
Here is the caller graph for this function:

◆ on_message()

void on_message ( server * s,
websocketpp::connection_hdl hdl,
message_ptr msg )

Definition at line 53 of file echo_server_tls.cpp.

53 {
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}
Here is the caller graph for this function:

◆ on_tls_init()

context_ptr on_tls_init ( tls_mode mode,
websocketpp::connection_hdl hdl )

Definition at line 84 of file echo_server_tls.cpp.

84 {
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}
websocketpp::lib::shared_ptr< boost::asio::ssl::context > context_ptr
std::string get_password()
Here is the call graph for this function:
Here is the caller graph for this function: