Skip to content

Commit 4408bb1

Browse files
authored
Merge pull request #17 from atelier-saulx/feature/tls-flag
Make TLS a runtime flag
2 parents 6f61cf8 + 9b20c1e commit 4408bb1

File tree

7 files changed

+147
-85
lines changed

7 files changed

+147
-85
lines changed

packages/cpp-client/example/example.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ void based_observe_cb(const char* data, uint64_t checksum, const char* error, in
3535
}
3636

3737
int main(int argc, char** argv) {
38-
int client1 = Based__new_client();
38+
int client1 = Based__new_client(false);
3939

4040
Based__connect(client1, (char*)"local", (char*)"saulx", (char*)"test", (char*)"ci", (char*)"",
41-
(char*)"", false, (char*)"", (char*)"http://192.168.1.10:24587");
41+
(char*)"", false, (char*)"", (char*)"192.168.1.10:24587");
4242

4343
// Based__connect_to_url(client1, (char*)"ws://localhost:9999");
4444

packages/cpp-client/include/based.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ typedef int32_t based_id;
88
// using based_observe_cb = void (*)(char*, uint64_t, char*);
99
// using based_cb = void (*)(char*, char*);
1010

11-
extern "C" based_id Based__new_client();
11+
extern "C" based_id Based__new_client(bool enable_tls);
1212
extern "C" void Based__delete_client(based_id client_id);
1313

1414
extern "C" char* Based__get_service(based_id client_id,

packages/cpp-client/src/based.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ based_id idx = 0;
88
char get_service_buf[1024];
99
char auth_state_buf[1048576];
1010

11-
extern "C" based_id Based__new_client() {
12-
BasedClient* cl = new BasedClient;
11+
extern "C" based_id Based__new_client(bool enable_tls) {
12+
BasedClient* cl = new BasedClient(enable_tls);
1313
idx++;
1414

1515
if (clients.find(idx) != clients.end()) {

packages/cpp-client/src/basedclient.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ enum IncomingType {
2020
CHANNEL_MESSAGE = 7,
2121
};
2222

23-
BasedClient::BasedClient() : m_request_id(0), m_sub_id(0), m_auth_in_progress(false){};
23+
BasedClient::BasedClient(bool enable_tls)
24+
: m_request_id(0),
25+
m_sub_id(0),
26+
m_auth_in_progress(false),
27+
m_con(enable_tls){};
2428

2529
/////////////////
2630
// Helper functions

packages/cpp-client/src/basedclient.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class BasedClient {
140140
std::map<sub_id_t, void (*)(const char*, const char*, int)> m_get_sub_callbacks;
141141

142142
public:
143-
BasedClient();
143+
BasedClient(bool enable_tls);
144144

145145
/**
146146
* @brief Function to retrieve the url of a specific service.

packages/cpp-client/src/connection.cpp

Lines changed: 125 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -204,36 +204,36 @@ std::pair<std::string, std::string> WsConnection::make_request(std::string url,
204204
///////////////////////// Class methods /////////////////////////////////
205205
//////////////////////////////////////////////////////////////////////////
206206

207-
WsConnection::WsConnection()
207+
WsConnection::WsConnection(bool enable_tls)
208208
: m_status(ConnectionStatus::CLOSED),
209209
m_on_open(NULL),
210210
m_on_message(NULL),
211211
m_reconnect_attempts(0) {
212-
// set the endpoint logging behavior to silent by clearing all of the access and error
213-
// logging channels
214-
m_endpoint.clear_access_channels(websocketpp::log::alevel::all);
215-
m_endpoint.clear_error_channels(websocketpp::log::elevel::all);
216-
217-
// try {
218-
m_endpoint.init_asio();
219-
// } catch (const websocketpp::exception& e) {
220-
// std::cout << "CAUGHT ERROR!!!" << e.m_msg << std::endl;
221-
// }
222-
#ifdef BASED_TLS
223-
m_endpoint.set_tls_init_handler(websocketpp::lib::bind(&WsConnection::on_tls_init));
224-
#endif
225-
226-
// perpetual mode = the endpoint's processing loop will not exit automatically when it has
227-
// no connections
228-
m_endpoint.start_perpetual();
229-
// run perpetually in a thread
230-
231-
m_thread = std::make_shared<std::thread>(&ws_client::run, &m_endpoint);
212+
m_enable_tls = enable_tls;
213+
BASED_LOG("ENABLE TLS = %d", m_enable_tls);
214+
if (m_enable_tls) {
215+
m_wss_endpoint.clear_access_channels(websocketpp::log::alevel::all);
216+
m_wss_endpoint.clear_error_channels(websocketpp::log::elevel::all);
217+
m_wss_endpoint.init_asio();
218+
m_wss_endpoint.set_tls_init_handler(websocketpp::lib::bind(&WsConnection::on_tls_init));
219+
m_wss_endpoint.start_perpetual();
220+
m_thread = std::make_shared<std::thread>(&wss_client::run, &m_wss_endpoint);
221+
} else {
222+
m_ws_endpoint.clear_access_channels(websocketpp::log::alevel::all);
223+
m_ws_endpoint.clear_error_channels(websocketpp::log::elevel::all);
224+
m_ws_endpoint.init_asio();
225+
m_ws_endpoint.start_perpetual();
226+
m_thread = std::make_shared<std::thread>(&ws_client::run, &m_ws_endpoint);
227+
}
232228
};
233229

234230
WsConnection::~WsConnection() {
235231
disconnect();
236-
m_endpoint.stop_perpetual();
232+
if (m_enable_tls) {
233+
m_wss_endpoint.stop_perpetual();
234+
} else {
235+
m_ws_endpoint.stop_perpetual();
236+
}
237237
m_thread->join();
238238

239239
BASED_LOG("Destroyed WsConnection obj");
@@ -256,11 +256,11 @@ std::string WsConnection::discover_service(BasedConnectOpt opts, bool http) {
256256
auto service_url = req.first;
257257
auto access_key = req.second;
258258
url = service_url;
259-
#ifdef BASED_TLS
260-
return http ? "https://" + url : "wss://" + url + "/" + access_key;
261-
#else
262-
return http ? "http://" + url : "ws://" + url + "/" + access_key;
263-
#endif
259+
if (m_enable_tls) {
260+
return http ? "https://" + url : "wss://" + url + "/" + access_key;
261+
} else {
262+
return http ? "http://" + url : "ws://" + url + "/" + access_key;
263+
}
264264
}
265265

266266
void WsConnection::connect(std::string cluster,
@@ -303,27 +303,51 @@ void WsConnection::connect_to_uri(std::string uri) {
303303

304304
m_uri = uri;
305305
websocketpp::lib::error_code ec;
306-
ws_client::connection_ptr con = m_endpoint.get_connection(m_uri, ec);
307306

308-
if (ec) {
309-
BASED_LOG("Error trying to initialize connection, message = \"%s\"", ec.message().c_str());
310-
m_status = ConnectionStatus::FAILED;
311-
return;
312-
}
307+
if (m_enable_tls) {
308+
wss_client::connection_ptr con = m_wss_endpoint.get_connection(m_uri, ec);
309+
310+
if (ec) {
311+
BASED_LOG("Error trying to initialize connection, message = \"%s\"",
312+
ec.message().c_str());
313+
m_status = ConnectionStatus::FAILED;
314+
return;
315+
}
316+
317+
m_status = ConnectionStatus::CONNECTING;
313318

314-
m_status = ConnectionStatus::CONNECTING;
319+
// Add special headers
315320

316-
// Add special headers
321+
con->append_header("sec-websocket-protocol", "{}");
317322

318-
con->append_header("sec-websocket-protocol", "{}");
323+
m_hdl = con->get_handle();
319324

320-
m_hdl = con->get_handle();
325+
set_handlers(con);
321326

322-
set_handlers(con);
327+
m_wss_endpoint.connect(con);
323328

324-
m_endpoint.connect(con);
329+
} else {
330+
ws_client::connection_ptr con = m_ws_endpoint.get_connection(m_uri, ec);
325331

326-
return;
332+
if (ec) {
333+
BASED_LOG("Error trying to initialize connection, message = \"%s\"",
334+
ec.message().c_str());
335+
m_status = ConnectionStatus::FAILED;
336+
return;
337+
}
338+
339+
m_status = ConnectionStatus::CONNECTING;
340+
341+
// Add special headers
342+
343+
con->append_header("sec-websocket-protocol", "{}");
344+
345+
m_hdl = con->get_handle();
346+
347+
set_handlers(con);
348+
349+
m_ws_endpoint.connect(con);
350+
}
327351
};
328352

329353
void WsConnection::set_open_handler(std::function<void()> on_open) {
@@ -343,7 +367,11 @@ void WsConnection::disconnect() {
343367
m_status = ConnectionStatus::TERMINATED_BY_USER;
344368

345369
websocketpp::lib::error_code ec;
346-
m_endpoint.close(m_hdl, websocketpp::close::status::going_away, "", ec);
370+
if (m_enable_tls) {
371+
m_wss_endpoint.close(m_hdl, websocketpp::close::status::going_away, "", ec);
372+
} else {
373+
m_ws_endpoint.close(m_hdl, websocketpp::close::status::going_away, "", ec);
374+
}
347375
if (ec) {
348376
BASED_LOG("Error trying to close connection, message = \"%s\"", ec.message().c_str());
349377

@@ -356,7 +384,13 @@ void WsConnection::send(std::vector<uint8_t> message) {
356384

357385
if (m_status != ConnectionStatus::OPEN) throw(std::runtime_error("Connection is not open."));
358386

359-
m_endpoint.send(m_hdl, message.data(), message.size(), websocketpp::frame::opcode::binary, ec);
387+
if (m_enable_tls) {
388+
m_wss_endpoint.send(m_hdl, message.data(), message.size(),
389+
websocketpp::frame::opcode::binary, ec);
390+
} else {
391+
m_ws_endpoint.send(m_hdl, message.data(), message.size(),
392+
websocketpp::frame::opcode::binary, ec);
393+
}
360394
if (ec) {
361395
BASED_LOG("Error trying to send message, message = \"%s\"", ec.message().c_str());
362396
return;
@@ -367,10 +401,7 @@ ConnectionStatus WsConnection::status() {
367401
return m_status;
368402
};
369403

370-
#ifdef BASED_TLS
371-
#ifdef ASIO_STANDALONE
372404
using context_ptr = std::shared_ptr<asio::ssl::context>;
373-
374405
context_ptr WsConnection::on_tls_init() {
375406
context_ptr ctx = std::make_shared<asio::ssl::context>(asio::ssl::context::sslv23);
376407

@@ -383,27 +414,59 @@ context_ptr WsConnection::on_tls_init() {
383414
}
384415
return ctx;
385416
}
386-
#else
387-
using context_ptr = std::shared_ptr<boost::asio::ssl::context>;
388417

389-
context_ptr WsConnection::on_tls_init() {
390-
context_ptr ctx =
391-
std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
418+
void WsConnection::set_handlers(ws_client::connection_ptr con) {
419+
// bind must be used if the function we're binding to doest have the right number of
420+
// arguments (hence the placeholders) these handlers must be set before calling connect, and
421+
// can't be changed after (i think)
422+
con->set_open_handler([this](websocketpp::connection_hdl) {
423+
BASED_LOG("Connection opened");
424+
m_status = ConnectionStatus::OPEN;
425+
m_reconnect_attempts = 0;
426+
if (m_on_open) {
427+
m_on_open();
428+
}
429+
});
392430

393-
try {
394-
ctx->set_options(boost::asio::ssl::context::default_workarounds |
395-
boost::asio::ssl::context::no_sslv2 | boost::asio::ssl::context::no_sslv3 |
396-
boost::asio::ssl::context::single_dh_use);
431+
con->set_message_handler([this](websocketpp::connection_hdl hdl, ws_client::message_ptr msg) {
432+
// here we will pass the message to the decoder, which, based on the header, will
433+
// call the appropriate callback
397434

398-
} catch (std::exception& e) {
399-
std::cerr << "Error in context pointer: " << e.what() << std::endl;
400-
}
401-
return ctx;
435+
std::string payload = msg->get_payload();
436+
if (m_on_message) {
437+
m_on_message(payload);
438+
}
439+
});
440+
441+
con->set_close_handler([this](websocketpp::connection_hdl) {
442+
if (m_status != ConnectionStatus::TERMINATED_BY_USER) {
443+
m_status = ConnectionStatus::CLOSED;
444+
m_reconnect_attempts++;
445+
446+
if (!m_opts.name.empty()) {
447+
connect(m_opts.cluster, m_opts.org, m_opts.project, m_opts.env, m_opts.key,
448+
m_opts.optional_key, m_opts.host, m_opts.discovery_url);
449+
} else {
450+
connect_to_uri(m_uri);
451+
}
452+
}
453+
});
454+
455+
con->set_fail_handler([this](websocketpp::connection_hdl) {
456+
BASED_LOG("Received FAIL event");
457+
m_status = ConnectionStatus::FAILED;
458+
m_reconnect_attempts++;
459+
460+
if (m_uri.size() == 0) {
461+
connect(m_opts.cluster, m_opts.org, m_opts.project, m_opts.env, m_opts.key,
462+
m_opts.optional_key, m_opts.host, m_opts.discovery_url);
463+
} else {
464+
connect_to_uri(m_uri);
465+
}
466+
});
402467
}
403-
#endif
404-
#endif
405468

406-
void WsConnection::set_handlers(ws_client::connection_ptr con) {
469+
void WsConnection::set_handlers(wss_client::connection_ptr con) {
407470
// bind must be used if the function we're binding to doest have the right number of
408471
// arguments (hence the placeholders) these handlers must be set before calling connect, and
409472
// can't be changed after (i think)
@@ -416,7 +479,7 @@ void WsConnection::set_handlers(ws_client::connection_ptr con) {
416479
}
417480
});
418481

419-
con->set_message_handler([this](websocketpp::connection_hdl hdl, ws_client::message_ptr msg) {
482+
con->set_message_handler([this](websocketpp::connection_hdl hdl, wss_client::message_ptr msg) {
420483
// here we will pass the message to the decoder, which, based on the header, will
421484
// call the appropriate callback
422485

packages/cpp-client/src/connection.hpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
#include <thread>
77
#include <websocketpp/client.hpp>
88

9-
#ifdef BASED_TLS
10-
#include <websocketpp/config/asio_client.hpp> // SSL
11-
typedef websocketpp::client<websocketpp::config::asio_tls_client> ws_client; // SSL
12-
#else
9+
#include <websocketpp/config/asio_client.hpp> // SSL
1310
#include <websocketpp/config/asio_no_tls_client.hpp> // No SSL
14-
typedef websocketpp::client<websocketpp::config::asio_client> ws_client; // No SSL
15-
#endif
11+
12+
typedef websocketpp::client<websocketpp::config::asio_tls_client> wss_client; // SSL
13+
typedef websocketpp::client<websocketpp::config::asio_client> ws_client; // No SSL
1614

1715
enum ConnectionStatus { OPEN = 0, CONNECTING, CLOSED, FAILED, TERMINATED_BY_USER };
1816

@@ -33,7 +31,7 @@ struct BasedConnectOpt {
3331
class WsConnection {
3432
// eventually there should be some logic here to handle inactivity.
3533
public:
36-
WsConnection();
34+
WsConnection(bool enable_tls);
3735
~WsConnection();
3836
void connect(std::string cluster,
3937
std::string org,
@@ -53,18 +51,15 @@ class WsConnection {
5351
std::string discover_service(BasedConnectOpt opts, bool http);
5452

5553
void set_handlers(ws_client::connection_ptr con);
56-
#ifdef BASED_TLS
57-
#ifdef ASIO_STANDALONE
54+
void set_handlers(wss_client::connection_ptr con);
55+
5856
using context_ptr = std::shared_ptr<asio::ssl::context>;
5957
static context_ptr on_tls_init();
60-
#else
61-
// Use Boost
62-
using context_ptr = std::shared_ptr<boost::asio::ssl::context>;
63-
static context_ptr on_tls_init();
64-
#endif
65-
#endif
58+
6659
private: // Members
67-
ws_client m_endpoint;
60+
bool m_enable_tls;
61+
ws_client m_ws_endpoint;
62+
wss_client m_wss_endpoint;
6863
websocketpp::connection_hdl m_hdl;
6964
ConnectionStatus m_status;
7065
std::string m_uri;

0 commit comments

Comments
 (0)