From bd458fd4507c9eb20818190028298786406dd10a Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Mon, 5 Nov 2012 14:18:34 -0800 Subject: [PATCH] Split websocket connections to two ports. --- src/Application.cpp | 36 +++++++++++++++++++++++++++++++----- src/Application.h | 3 ++- src/Config.cpp | 8 ++++++++ src/Config.h | 8 ++++++-- src/WSConnection.cpp | 10 ++++++---- src/WSConnection.h | 4 +++- src/WSDoor.cpp | 27 ++++++++++----------------- src/WSDoor.h | 7 +++++-- src/WSHandler.h | 11 ++++++++--- 9 files changed, 79 insertions(+), 35 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index 8133fa271..a6b76b793 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -148,7 +148,7 @@ void Application::run() } else { - std::cerr << "Peer interface: disabled" << std::endl; + cLog(lsINFO) << "Peer interface: disabled"; } // @@ -160,10 +160,32 @@ void Application::run() } else { - std::cerr << "RPC interface: disabled" << std::endl; + cLog(lsINFO) << "RPC interface: disabled"; } - mWSDoor = WSDoor::createWSDoor(); + // + // Allow private WS connections. + // + if (!theConfig.WEBSOCKET_IP.empty() && theConfig.WEBSOCKET_PORT) + { + mWSPrivateDoor = WSDoor::createWSDoor(theConfig.WEBSOCKET_IP, theConfig.WEBSOCKET_PORT, false); + } + else + { + cLog(lsINFO) << "WS private interface: disabled"; + } + + // + // Allow public WS connections. + // + if (!theConfig.WEBSOCKET_PUBLIC_IP.empty() && theConfig.WEBSOCKET_PUBLIC_PORT) + { + mWSPublicDoor = WSDoor::createWSDoor(theConfig.WEBSOCKET_PUBLIC_IP, theConfig.WEBSOCKET_PUBLIC_PORT, true); + } + else + { + cLog(lsINFO) << "WS public interface: disabled"; + } // // Begin connecting to network. @@ -182,9 +204,13 @@ void Application::run() mIOService.run(); // This blocks - mWSDoor->stop(); + if (mWSPublicDoor) + mWSPublicDoor->stop(); - std::cout << "Done." << std::endl; + if (mWSPrivateDoor) + mWSPrivateDoor->stop(); + + cLog(lsINFO) << "Done."; } void Application::sweep() diff --git a/src/Application.h b/src/Application.h index da956fae2..a10019096 100644 --- a/src/Application.h +++ b/src/Application.h @@ -62,7 +62,8 @@ class Application ConnectionPool mConnectionPool; PeerDoor* mPeerDoor; RPCDoor* mRPCDoor; - WSDoor* mWSDoor; + WSDoor* mWSPublicDoor; + WSDoor* mWSPrivateDoor; uint256 mNonce256; std::size_t mNonceST; diff --git a/src/Config.cpp b/src/Config.cpp index f69e58446..8a665ff13 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -30,6 +30,8 @@ #define SECTION_UNL_DEFAULT "unl_default" #define SECTION_VALIDATION_QUORUM "validation_quorum" #define SECTION_VALIDATION_SEED "validation_seed" +#define SECTION_WEBSOCKET_PUBLIC_IP "websocket_public_ip" +#define SECTION_WEBSOCKET_PUBLIC_PORT "websocket_public_port" #define SECTION_WEBSOCKET_IP "websocket_ip" #define SECTION_WEBSOCKET_PORT "websocket_port" #define SECTION_VALIDATORS "validators" @@ -124,6 +126,7 @@ void Config::setup(const std::string& strConf) PEER_PORT = SYSTEM_PEER_PORT; RPC_PORT = 5001; WEBSOCKET_PORT = SYSTEM_WEBSOCKET_PORT; + WEBSOCKET_PUBLIC_PORT = SYSTEM_WEBSOCKET_PUBLIC_PORT; NUMBER_CONNECTIONS = 30; // a new ledger every minute @@ -235,6 +238,11 @@ void Config::load() if (sectionSingleB(secConfig, SECTION_WEBSOCKET_PORT, strTemp)) WEBSOCKET_PORT = boost::lexical_cast(strTemp); + (void) sectionSingleB(secConfig, SECTION_WEBSOCKET_PUBLIC_IP, WEBSOCKET_PUBLIC_IP); + + if (sectionSingleB(secConfig, SECTION_WEBSOCKET_PUBLIC_PORT, strTemp)) + WEBSOCKET_PUBLIC_PORT = boost::lexical_cast(strTemp); + if (sectionSingleB(secConfig, SECTION_VALIDATION_SEED, strTemp)) { VALIDATION_SEED.setSeedGeneric(strTemp); diff --git a/src/Config.h b/src/Config.h index cb17b46ed..3807b51df 100644 --- a/src/Config.h +++ b/src/Config.h @@ -24,8 +24,9 @@ #define DEFAULT_VALIDATORS_SITE "redstem.com" #define VALIDATORS_FILE_NAME "validators.txt" -const int SYSTEM_PEER_PORT = 6561; -const int SYSTEM_WEBSOCKET_PORT = 6562; +const int SYSTEM_PEER_PORT = 6561; +const int SYSTEM_WEBSOCKET_PORT = 6562; +const int SYSTEM_WEBSOCKET_PUBLIC_PORT = 6563; // XXX Going away. // Allow anonymous DH. #define DEFAULT_PEER_SSL_CIPHER_LIST "ALL:!LOW:!EXP:!MD5:@STRENGTH" @@ -82,6 +83,9 @@ public: unsigned int PEER_CONNECT_LOW_WATER; // Websocket networking parameters + std::string WEBSOCKET_PUBLIC_IP; // XXX Going away. Merge with the inbound peer connction. + int WEBSOCKET_PUBLIC_PORT; + std::string WEBSOCKET_IP; int WEBSOCKET_PORT; diff --git a/src/WSConnection.cpp b/src/WSConnection.cpp index 121bdead1..dc392cb85 100644 --- a/src/WSConnection.cpp +++ b/src/WSConnection.cpp @@ -143,7 +143,7 @@ void WSConnection::doSubscribe(Json::Value& jvResult, Json::Value& jvRequest) mNetwork.subServer(this); }else if(streamName=="ledger") { - mNetwork.subLedger(this); + mNetwork.subLedger(this, jvResult); }else if(streamName=="transactions") { mNetwork.subTransactions(this); @@ -276,13 +276,14 @@ void WSConnection::doUnsubscribe(Json::Value& jvResult, Json::Value& jvRequest) } } - - void WSConnection::doRPC(Json::Value& jvResult, Json::Value& jvRequest) { if (jvRequest.isMember("rpc_command") ) { - jvResult=theApp->getRPCHandler().doCommand(jvRequest["rpc_command"].asString(),jvRequest["params"],RPCHandler::GUEST); + jvResult=theApp->getRPCHandler().doCommand( + jvRequest["rpc_command"].asString(), + jvRequest["params"], + mHandler->getPublic() ? RPCHandler::GUEST : RPCHandler::ADMIN); }else jvResult["error"] = "fieldNotCommand"; @@ -304,4 +305,5 @@ void WSConnection::doSubmit(Json::Value& jvResult, Json::Value& jvRequest) // TODO: track the transaction mNetwork.subSubmit(this, jvResult["tx hash"] ); } } + // vim:ts=4 diff --git a/src/WSConnection.h b/src/WSConnection.h index e93609c1e..47cdf898b 100644 --- a/src/WSConnection.h +++ b/src/WSConnection.h @@ -52,4 +52,6 @@ public: void doSubscribe(Json::Value& jvResult, Json::Value& jvRequest); void doUnsubscribe(Json::Value& jvResult, Json::Value& jvRequest); -}; \ No newline at end of file +}; + +// vim:ts=4 diff --git a/src/WSDoor.cpp b/src/WSDoor.cpp index 58c8f68dd..55e46b58a 100644 --- a/src/WSDoor.cpp +++ b/src/WSDoor.cpp @@ -40,10 +40,6 @@ static DH* handleTmpDh(SSL* ssl, int is_export, int iKeyLength) return 512 == iKeyLength ? theApp->getWallet().getDh512() : theApp->getWallet().getDh1024(); } - - - - void WSDoor::startListening() { // Generate a single SSL context for use by all connections. @@ -58,7 +54,7 @@ void WSDoor::startListening() SSL_CTX_set_tmp_dh_callback(mCtx->native_handle(), handleTmpDh); // Construct a single handler for all requests. - websocketpp::WSDOOR_SERVER::handler::ptr handler(new WSServerHandler(mCtx)); + websocketpp::WSDOOR_SERVER::handler::ptr handler(new WSServerHandler(mCtx, mPublic)); // Construct a websocket server. mEndpoint = new websocketpp::WSDOOR_SERVER(handler); @@ -69,25 +65,22 @@ void WSDoor::startListening() // Call the main-event-loop of the websocket server. mEndpoint->listen( boost::asio::ip::tcp::endpoint( - boost::asio::ip::address().from_string(theConfig.WEBSOCKET_IP), theConfig.WEBSOCKET_PORT)); + boost::asio::ip::address().from_string(mIp), mPort)); delete mEndpoint; } -WSDoor* WSDoor::createWSDoor() +WSDoor* WSDoor::createWSDoor(const std::string& strIp, const int iPort, bool bPublic) { - WSDoor* wdpResult = new WSDoor(); + WSDoor* wdpResult = new WSDoor(strIp, iPort, bPublic); - if (!theConfig.WEBSOCKET_IP.empty() && theConfig.WEBSOCKET_PORT) - { - Log(lsINFO) << "Websocket: Listening: " << theConfig.WEBSOCKET_IP << " " << theConfig.WEBSOCKET_PORT; + cLog(lsINFO) << + boost::str(boost::format("Websocket: %s: Listening: %s %d ") + % (bPublic ? "Public" : "Private") + % strIp + % iPort); - wdpResult->mThread = new boost::thread(boost::bind(&WSDoor::startListening, wdpResult)); - } - else - { - Log(lsINFO) << "Websocket: Disabled"; - } + wdpResult->mThread = new boost::thread(boost::bind(&WSDoor::startListening, wdpResult)); return wdpResult; } diff --git a/src/WSDoor.h b/src/WSDoor.h index af90ddaf8..fe41d1bc5 100644 --- a/src/WSDoor.h +++ b/src/WSDoor.h @@ -21,16 +21,19 @@ class WSDoor private: websocketpp::WSDOOR_SERVER* mEndpoint; boost::thread* mThread; + bool mPublic; + std::string mIp; + int mPort; void startListening(); public: - WSDoor() : mEndpoint(0), mThread(0) { ; } + WSDoor(const std::string& strIp, int iPort, bool bPublic) : mEndpoint(0), mThread(0), mPublic(bPublic), mIp(strIp), mPort(iPort) { ; } void stop(); - static WSDoor* createWSDoor(); + static WSDoor* createWSDoor(const std::string& strIp, const int iPort, bool bPublic); }; #endif diff --git a/src/WSHandler.h b/src/WSHandler.h index e400da35f..f12a09eee 100644 --- a/src/WSHandler.h +++ b/src/WSHandler.h @@ -18,15 +18,18 @@ public: }; private: - boost::shared_ptr mCtx; + boost::shared_ptr mCtx; protected: - boost::mutex mMapLock; + boost::mutex mMapLock; // For each connection maintain an associated object to track subscriptions. boost::unordered_map > mMap; + bool mPublic; public: - WSServerHandler(boost::shared_ptr spCtx) : mCtx(spCtx) {} + WSServerHandler(boost::shared_ptr spCtx, bool bPublic) : mCtx(spCtx), mPublic(bPublic) {} + + bool getPublic() { return mPublic; }; boost::shared_ptr on_tls_init() { @@ -122,3 +125,5 @@ public: }; #endif + +// vim:ts=4