broken websocket code

This commit is contained in:
jed
2012-12-06 16:48:04 -08:00
parent 73db82ac56
commit 4ec4ccf490
9 changed files with 161 additions and 40 deletions

View File

@@ -143,6 +143,7 @@
<ClCompile Include="src\cpp\ripple\Peer.cpp" />
<ClCompile Include="src\cpp\ripple\PeerDoor.cpp" />
<ClCompile Include="src\cpp\ripple\PlatRand.cpp" />
<ClCompile Include="src\cpp\ripple\ProofOfWork.cpp" />
<ClCompile Include="src\cpp\ripple\PubKeyCache.cpp" />
<ClCompile Include="src\cpp\ripple\RangeSet.cpp" />
<ClCompile Include="src\cpp\ripple\RegularKeySetTransactor.cpp" />
@@ -184,6 +185,7 @@
<ClCompile Include="src\cpp\ripple\WalletAddTransactor.cpp" />
<ClCompile Include="src\cpp\ripple\WSConnection.cpp" />
<ClCompile Include="src\cpp\ripple\WSDoor.cpp" />
<ClCompile Include="src\cpp\ripple\WSHandler.cpp" />
<ClCompile Include="src\cpp\websocketpp\src\base64\base64.cpp" />
<ClCompile Include="src\cpp\websocketpp\src\md5\md5.c" />
<ClCompile Include="src\cpp\websocketpp\src\messages\data.cpp" />
@@ -295,6 +297,17 @@
<ClInclude Include="src\cpp\ripple\WSConnection.h" />
<ClInclude Include="src\cpp\ripple\WSDoor.h" />
<ClInclude Include="src\cpp\ripple\WSHandler.h" />
<ClInclude Include="src\cpp\websocketpp\src\common.hpp" />
<ClInclude Include="src\cpp\websocketpp\src\connection.hpp" />
<ClInclude Include="src\cpp\websocketpp\src\endpoint.hpp" />
<ClInclude Include="src\cpp\websocketpp\src\network_utilities.hpp" />
<ClInclude Include="src\cpp\websocketpp\src\shared_const_buffer.hpp" />
<ClInclude Include="src\cpp\websocketpp\src\sockets\plain.hpp" />
<ClInclude Include="src\cpp\websocketpp\src\sockets\socket_base.hpp" />
<ClInclude Include="src\cpp\websocketpp\src\sockets\tls.hpp" />
<ClInclude Include="src\cpp\websocketpp\src\uri.hpp" />
<ClInclude Include="src\cpp\websocketpp\src\websocketpp.hpp" />
<ClInclude Include="src\cpp\websocketpp\src\websocket_frame.hpp" />
<ClInclude Include="util\pugiconfig.hpp" />
<ClInclude Include="util\pugixml.hpp" />
</ItemGroup>

View File

@@ -31,6 +31,9 @@
<Filter Include="Source Files\websocketpp">
<UniqueIdentifier>{92775c5f-dc9f-4a97-a9a6-6d4bd4e424b4}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\websocket">
<UniqueIdentifier>{cd4c41c0-3ee6-49f8-8322-d11422b892f9}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\cpp\database\database.cpp">
@@ -351,6 +354,12 @@
<ClCompile Include="src\cpp\ripple\AccountItems.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\cpp\ripple\ProofOfWork.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\cpp\ripple\WSHandler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="util\pugiconfig.hpp">
@@ -656,6 +665,39 @@
<ClInclude Include="src\cpp\ripple\AccountItems.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\cpp\websocketpp\src\common.hpp">
<Filter>Header Files\websocket</Filter>
</ClInclude>
<ClInclude Include="src\cpp\websocketpp\src\connection.hpp">
<Filter>Header Files\websocket</Filter>
</ClInclude>
<ClInclude Include="src\cpp\websocketpp\src\endpoint.hpp">
<Filter>Header Files\websocket</Filter>
</ClInclude>
<ClInclude Include="src\cpp\websocketpp\src\network_utilities.hpp">
<Filter>Header Files\websocket</Filter>
</ClInclude>
<ClInclude Include="src\cpp\websocketpp\src\shared_const_buffer.hpp">
<Filter>Header Files\websocket</Filter>
</ClInclude>
<ClInclude Include="src\cpp\websocketpp\src\uri.hpp">
<Filter>Header Files\websocket</Filter>
</ClInclude>
<ClInclude Include="src\cpp\websocketpp\src\websocket_frame.hpp">
<Filter>Header Files\websocket</Filter>
</ClInclude>
<ClInclude Include="src\cpp\websocketpp\src\websocketpp.hpp">
<Filter>Header Files\websocket</Filter>
</ClInclude>
<ClInclude Include="src\cpp\websocketpp\src\sockets\plain.hpp">
<Filter>Header Files\websocket</Filter>
</ClInclude>
<ClInclude Include="src\cpp\websocketpp\src\sockets\socket_base.hpp">
<Filter>Header Files\websocket</Filter>
</ClInclude>
<ClInclude Include="src\cpp\websocketpp\src\sockets\tls.hpp">
<Filter>Header Files\websocket</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="wallet.xml" />

View File

@@ -39,6 +39,8 @@
#define SECTION_WEBSOCKET_PUBLIC_PORT "websocket_public_port"
#define SECTION_WEBSOCKET_IP "websocket_ip"
#define SECTION_WEBSOCKET_PORT "websocket_port"
#define SECTION_WEBSOCKET_SECURE "websocket_secure"
#define SECTION_WEBSOCKET_SSL_CERT "websocket_ssl_cert"
#define SECTION_VALIDATORS "validators"
#define SECTION_VALIDATORS_SITE "validators_site"
@@ -134,6 +136,7 @@ void Config::setup(const std::string& strConf, bool bQuiet)
RPC_PORT = 5001;
WEBSOCKET_PORT = SYSTEM_WEBSOCKET_PORT;
WEBSOCKET_PUBLIC_PORT = SYSTEM_WEBSOCKET_PUBLIC_PORT;
WEBSOCKET_SECURE = false;
NUMBER_CONNECTIONS = 30;
// a new ledger every minute
@@ -256,6 +259,12 @@ void Config::load()
if (sectionSingleB(secConfig, SECTION_WEBSOCKET_PUBLIC_PORT, strTemp))
WEBSOCKET_PUBLIC_PORT = boost::lexical_cast<int>(strTemp);
if (sectionSingleB(secConfig, SECTION_WEBSOCKET_SECURE, strTemp))
WEBSOCKET_SECURE = boost::lexical_cast<bool>(strTemp);
sectionSingleB(secConfig, SECTION_WEBSOCKET_SSL_CERT, WEBSOCKET_SSL_CERT);
if (sectionSingleB(secConfig, SECTION_VALIDATION_SEED, strTemp))
{
VALIDATION_SEED.setSeedGeneric(strTemp);

View File

@@ -91,6 +91,8 @@ public:
std::string WEBSOCKET_IP;
int WEBSOCKET_PORT;
bool WEBSOCKET_SECURE;
std::string WEBSOCKET_SSL_CERT;
// RPC parameters
std::string RPC_IP;

View File

@@ -13,25 +13,25 @@ SETUP_LOG();
#include "../json/reader.h"
#include "../json/writer.h"
WSConnection::~WSConnection()
{
mNetwork.unsubTransactions(this);
mNetwork.unsubRTTransactions(this);
mNetwork.unsubLedger(this);
mNetwork.unsubServer(this);
mNetwork.unsubAccount(this, mSubAccountInfo, true);
mNetwork.unsubAccount(this, mSubAccountInfo, false);
}
void WSConnection::send(const Json::Value& jvObj)
//template <typename endpoint_type>
//WSConnection::~WSConnection()
//template WSConnection::~WSConnection<server>();
//template WSConnection::~WSConnection<server_tls>();
template <typename endpoint_type>
void WSConnection<endpoint_type>::send(const Json::Value& jvObj)
{
mHandler->send(mConnection, jvObj);
}
template void WSConnection::send<server>(const Json::Value& jvObj);
template void WSConnection::send<server_tls>(const Json::Value& jvObj);
//
// Utilities
//
template <typename endpoint_type>
Json::Value WSConnection::invokeCommand(Json::Value& jvRequest)
{
if (!jvRequest.isMember("command"))

View File

@@ -12,16 +12,17 @@ class WSServerHandler;
// Storage for connection specific info
// - Subscriptions
//
template <typename endpoint_type>
class WSConnection : public InfoSub
{
public:
typedef websocketpp::WSDOOR_SERVER::handler::connection_ptr connection_ptr;
typedef websocketpp::WSDOOR_SERVER::handler::message_ptr message_ptr;
typedef typename endpoint_type::handler::connection_ptr connection_ptr;
typedef typename endpoint_type::handler::message_ptr message_ptr;
protected:
typedef void (WSConnection::*doFuncPtr)(Json::Value& jvResult, Json::Value &jvRequest);
WSServerHandler<websocketpp::WSDOOR_SERVER>* mHandler;
WSServerHandler<endpoint_type>* mHandler;
connection_ptr mConnection;
NetworkOPs& mNetwork;
@@ -30,17 +31,29 @@ public:
// : mHandler((WSServerHandler<websocketpp::WSDOOR_SERVER>*)(NULL)),
// mConnection(connection_ptr()) { ; }
WSConnection(WSServerHandler<websocketpp::WSDOOR_SERVER>* wshpHandler, connection_ptr cpConnection)
WSConnection(WSServerHandler<endpoint_type>* wshpHandler, connection_ptr cpConnection)
: mHandler(wshpHandler), mConnection(cpConnection), mNetwork(theApp->getOPs()) { ; }
virtual ~WSConnection();
virtual ~WSConnection()
{
mNetwork.unsubTransactions(this);
mNetwork.unsubRTTransactions(this);
mNetwork.unsubLedger(this);
mNetwork.unsubServer(this);
mNetwork.unsubAccount(this, mSubAccountInfo, true);
mNetwork.unsubAccount(this, mSubAccountInfo, false);
}
// Implement overridden functions from base class:
template <typename endpoint_type>
void send(const Json::Value& jvObj);
// Utilities
template <typename endpoint_type>
Json::Value invokeCommand(Json::Value& jvRequest);
};
// vim:ts=4

View File

@@ -9,6 +9,7 @@ SETUP_LOG();
#include "utils.h"
#include "WSConnection.h"
#include "WSHandler.h"
#include "Config.h"
#include "WSDoor.h"
@@ -53,11 +54,30 @@ void WSDoor::startListening()
SSL_CTX_set_tmp_dh_callback(mCtx->native_handle(), handleTmpDh);
if(theConfig.WEBSOCKET_SECURE)
{
// Construct a single handler for all requests.
websocketpp::WSDOOR_SERVER::handler::ptr handler(new WSServerHandler<websocketpp::WSDOOR_SERVER>(mCtx, mPublic));
websocketpp::server_tls::handler::ptr handler(new WSServerHandler<websocketpp::server_tls>(mCtx, mPublic));
// Construct a websocket server.
mEndpoint = new websocketpp::WSDOOR_SERVER(handler);
mSEndpoint = new websocketpp::server_tls(handler);
// mEndpoint->alog().unset_level(websocketpp::log::alevel::ALL);
// mEndpoint->elog().unset_level(websocketpp::log::elevel::ALL);
// Call the main-event-loop of the websocket server.
mSEndpoint->listen(
boost::asio::ip::tcp::endpoint(
boost::asio::ip::address().from_string(mIp), mPort));
delete mSEndpoint;
}else
{
// Construct a single handler for all requests.
websocketpp::server::handler::ptr handler(new WSServerHandler<websocketpp::server>(mCtx, mPublic));
// Construct a websocket server.
mEndpoint = new websocketpp::server(handler);
// mEndpoint->alog().unset_level(websocketpp::log::alevel::ALL);
// mEndpoint->elog().unset_level(websocketpp::log::elevel::ALL);
@@ -68,6 +88,8 @@ void WSDoor::startListening()
boost::asio::ip::address().from_string(mIp), mPort));
delete mEndpoint;
}
}
WSDoor* WSDoor::createWSDoor(const std::string& strIp, const int iPort, bool bPublic)

View File

@@ -10,16 +10,12 @@
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#if 1
#define WSDOOR_SERVER server
#else
#define WSDOOR_SERVER server_tls
#endif
class WSDoor
{
private:
websocketpp::WSDOOR_SERVER* mEndpoint;
websocketpp::server* mEndpoint;
websocketpp::server_tls* mSEndpoint;
boost::thread* mThread;
bool mPublic;
std::string mIp;
@@ -29,7 +25,7 @@ private:
public:
WSDoor(const std::string& strIp, int iPort, bool bPublic) : mEndpoint(0), mThread(0), mPublic(bPublic), mIp(strIp), mPort(iPort) { ; }
WSDoor(const std::string& strIp, int iPort, bool bPublic) : mEndpoint(0), mSEndpoint(0), mThread(0), mPublic(bPublic), mIp(strIp), mPort(iPort) { ; }
void stop();

View File

@@ -2,7 +2,9 @@
#define __WSHANDLER__
#include "Application.h"
#include "Config.h"
template <typename endpoint_type>
class WSConnection;
// A single instance of this object is made.
@@ -25,7 +27,7 @@ private:
protected:
boost::mutex mMapLock;
// For each connection maintain an associated object to track subscriptions.
boost::unordered_map<connection_ptr, boost::shared_ptr<WSConnection> > mMap;
boost::unordered_map<connection_ptr, boost::shared_ptr< WSConnection<endpoint_type> > > mMap;
bool mPublic;
public:
@@ -33,10 +35,7 @@ public:
bool getPublic() { return mPublic; };
boost::shared_ptr<boost::asio::ssl::context> on_tls_init()
{
return mCtx;
}
void send(connection_ptr cpClient, message_ptr mpMessage)
{
@@ -127,6 +126,31 @@ public:
}
}
boost::shared_ptr<boost::asio::ssl::context> on_tls_init()
{
if(theConfig.WEBSOCKET_SECURE)
{
// create a tls context, init, and return.
boost::shared_ptr<boost::asio::ssl::context> context(new boost::asio::ssl::context(boost::asio::ssl::context::tlsv1));
try {
context->set_options(boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::single_dh_use);
context->set_password_callback(boost::bind(&type::get_password, this));
context->use_certificate_chain_file(theConfig.WEBSOCKET_SSL_CERT);
context->use_private_key_file(theConfig.WEBSOCKET_SSL_CERT, boost::asio::ssl::context::pem);
//context->use_tmp_dh_file("../../src/ssl/dh512.pem");
} catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
return context;
}else
{
return mCtx;
}
}
// Respond to http requests.
void http(connection_ptr cpClient)
{