diff --git a/src/WSDoor.cpp b/src/WSDoor.cpp new file mode 100644 index 0000000000..3b1dc53681 --- /dev/null +++ b/src/WSDoor.cpp @@ -0,0 +1,102 @@ + +#include "WSDoor.h" + +#include + +#include +#include + +#include "Application.h" +#include "Config.h" +#include "Log.h" +#include "utils.h" + +using namespace std; +using namespace boost::asio::ip; + +// Generate DH for SSL connection. +static DH* handleTmpDh(SSL* ssl, int is_export, int iKeyLength) +{ + return 512 == iKeyLength ? theApp->getWallet().getDh512() : theApp->getWallet().getDh1024(); +} + +// A single instance of this object is made. +// This instance dispatches all events. There is no per connection persistency. +template +class WSServerHandler : public endpoint_type::handler { +private: + boost::shared_ptr mCtx; + +public: + typedef typename endpoint_type::handler::connection_ptr connection_ptr; + typedef typename endpoint_type::handler::message_ptr message_ptr; + + WSServerHandler(boost::shared_ptr spCtx) : mCtx(spCtx) {} + + boost::shared_ptr on_tls_init() + { + return mCtx; + } + + void on_message(connection_ptr con, message_ptr msg) { + con->send(msg->get_payload(), msg->get_opcode()); + } + + void http(connection_ptr con) { + con->set_body("WebSocket++ TLS certificate test

WebSocket++ TLS certificate test

This is an HTTP(S) page served by a WebSocket++ server for the purposes of confirming that certificates are working since browsers normally silently ignore certificate issues.

"); + } +}; + +void WSDoor::startListening() +{ + boost::shared_ptr mCtx; + mCtx = boost::make_shared(boost::asio::ssl::context::sslv23); + + mCtx->set_options( + boost::asio::ssl::context::default_workarounds + | boost::asio::ssl::context::no_sslv2 + | boost::asio::ssl::context::single_dh_use); + + SSL_CTX_set_tmp_dh_callback(mCtx->native_handle(), handleTmpDh); + + websocketpp::server_tls::handler::ptr handler(new WSServerHandler(mCtx)); + mEndpoint = new websocketpp::server_tls(handler); + + Log(lsINFO) << "listening>"; + + mEndpoint->listen(boost::asio::ip::tcp::endpoint(address().from_string(theConfig.WEBSOCKET_IP), theConfig.WEBSOCKET_PORT)); + + free(mEndpoint); + + Log(lsINFO) << "listening<"; +} + +WSDoor* WSDoor::createWSDoor() +{ + WSDoor* wdpResult = new WSDoor(); + + if (!theConfig.WEBSOCKET_IP.empty() && theConfig.WEBSOCKET_PORT) + { + Log(lsINFO) << "Websocket: Listening: " << theConfig.WEBSOCKET_IP << " " << theConfig.WEBSOCKET_PORT; + + wdpResult->mThread = new boost::thread(boost::bind(&WSDoor::startListening, wdpResult)); + } + else + { + Log(lsINFO) << "Websocket: Disabled"; + } + + return wdpResult; +} + +void WSDoor::stop() +{ + if (mThread) + { + mEndpoint->stop(); // XXX Make this thread safe + + mThread->join(); + } +} + +// vim:ts=4 diff --git a/src/WSDoor.h b/src/WSDoor.h new file mode 100644 index 0000000000..86260ca851 --- /dev/null +++ b/src/WSDoor.h @@ -0,0 +1,32 @@ +#ifndef __WSDOOR__ +#define __WSDOOR__ + +#include "../websocketpp/src/sockets/tls.hpp" +#include "../websocketpp/src/websocketpp.hpp" + +#include +#include +#include +#include +#include + +class WSDoor +{ +private: + websocketpp::server_tls* mEndpoint; + boost::thread* mThread; + + void startListening(); + +public: + + WSDoor() : mEndpoint(0), mThread(0) { ; } + + void stop(); + + static WSDoor* createWSDoor(); +}; + +#endif + +// vim:ts=4