mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-23 12:35:50 +00:00
Make PeerDoor implementation private
This commit is contained in:
@@ -6,150 +6,102 @@
|
||||
|
||||
SETUP_LOG (PeerDoor)
|
||||
|
||||
// PEER_IP, PEER_PORT, PEER_SSL_CIPHER_LIST
|
||||
PeerDoor::PeerDoor (
|
||||
class PeerDoorImp : public PeerDoor, LeakChecked <PeerDoorImp>
|
||||
{
|
||||
public:
|
||||
PeerDoorImp (std::string const& ip,
|
||||
int port,
|
||||
std::string const& sslCiphers,
|
||||
boost::asio::io_service& io_service)
|
||||
: mAcceptor (
|
||||
io_service,
|
||||
boost::asio::ip::tcp::endpoint (boost::asio::ip::address ().from_string (ip.empty () ? "0.0.0.0" : ip),
|
||||
port))
|
||||
, mCtx (boost::asio::ssl::context::sslv23)
|
||||
, mDelayTimer (io_service)
|
||||
{
|
||||
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);
|
||||
|
||||
if (SSL_CTX_set_cipher_list (mCtx.native_handle (), sslCiphers.c_str ()) != 1)
|
||||
std::runtime_error ("Error setting cipher list (no valid ciphers).");
|
||||
|
||||
if (! ip.empty () && port != 0)
|
||||
{
|
||||
Log (lsINFO) << "Peer port: " << ip << " " << port;
|
||||
startListening ();
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
boost::asio::ssl::context& getSSLContext ()
|
||||
{
|
||||
return mCtx;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void startListening ()
|
||||
{
|
||||
Peer::pointer new_connection = Peer::New (
|
||||
mAcceptor.get_io_service (),
|
||||
mCtx,
|
||||
getApp().getPeers ().assignPeerId (),
|
||||
true);
|
||||
|
||||
mAcceptor.async_accept (new_connection->getSocket (),
|
||||
boost::bind (&PeerDoorImp::handleConnect, this, new_connection,
|
||||
boost::asio::placeholders::error));
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void handleConnect (Peer::pointer new_connection,
|
||||
const boost::system::error_code& error)
|
||||
{
|
||||
bool delay = false;
|
||||
|
||||
if (!error)
|
||||
{
|
||||
new_connection->connected (error);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (error == boost::system::errc::too_many_files_open)
|
||||
delay = true;
|
||||
|
||||
WriteLog (lsERROR, PeerDoor) << error;
|
||||
}
|
||||
|
||||
if (delay)
|
||||
{
|
||||
mDelayTimer.expires_from_now (boost::posix_time::milliseconds (500));
|
||||
mDelayTimer.async_wait (boost::bind (&PeerDoorImp::startListening, this));
|
||||
}
|
||||
else
|
||||
{
|
||||
startListening ();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
boost::asio::ip::tcp::acceptor mAcceptor;
|
||||
boost::asio::ssl::context mCtx;
|
||||
boost::asio::deadline_timer mDelayTimer;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
PeerDoor* PeerDoor::New (
|
||||
std::string const& ip,
|
||||
int port,
|
||||
std::string const& sslCiphers,
|
||||
boost::asio::io_service& io_service)
|
||||
: mAcceptor (
|
||||
io_service,
|
||||
boost::asio::ip::tcp::endpoint (boost::asio::ip::address ().from_string (ip.empty () ? "0.0.0.0" : ip),
|
||||
port))
|
||||
, mCtx (boost::asio::ssl::context::sslv23)
|
||||
, mDelayTimer (io_service)
|
||||
{
|
||||
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);
|
||||
|
||||
if (SSL_CTX_set_cipher_list (mCtx.native_handle (), sslCiphers.c_str ()) != 1)
|
||||
std::runtime_error ("Error setting cipher list (no valid ciphers).");
|
||||
|
||||
if (! ip.empty () && port != 0)
|
||||
{
|
||||
Log (lsINFO) << "Peer port: " << ip << " " << port;
|
||||
startListening ();
|
||||
}
|
||||
return new PeerDoorImp (ip, port, sslCiphers, io_service);
|
||||
}
|
||||
|
||||
void PeerDoor::startListening ()
|
||||
{
|
||||
Peer::pointer new_connection = Peer::New (
|
||||
mAcceptor.get_io_service (),
|
||||
mCtx,
|
||||
getApp().getPeers ().assignPeerId (),
|
||||
true);
|
||||
|
||||
mAcceptor.async_accept (new_connection->getSocket (),
|
||||
boost::bind (&PeerDoor::handleConnect, this, new_connection,
|
||||
boost::asio::placeholders::error));
|
||||
}
|
||||
|
||||
void PeerDoor::handleConnect (Peer::pointer new_connection,
|
||||
const boost::system::error_code& error)
|
||||
{
|
||||
bool delay = false;
|
||||
|
||||
if (!error)
|
||||
{
|
||||
new_connection->connected (error);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (error == boost::system::errc::too_many_files_open)
|
||||
delay = true;
|
||||
|
||||
WriteLog (lsERROR, PeerDoor) << error;
|
||||
}
|
||||
|
||||
if (delay)
|
||||
{
|
||||
mDelayTimer.expires_from_now (boost::posix_time::milliseconds (500));
|
||||
mDelayTimer.async_wait (boost::bind (&PeerDoor::startListening, this));
|
||||
}
|
||||
else
|
||||
{
|
||||
startListening ();
|
||||
}
|
||||
}
|
||||
|
||||
void initSSLContext (boost::asio::ssl::context& context,
|
||||
std::string key_file, std::string cert_file, std::string chain_file)
|
||||
{
|
||||
SSL_CTX* sslContext = context.native_handle ();
|
||||
|
||||
context.set_options (boost::asio::ssl::context::default_workarounds |
|
||||
boost::asio::ssl::context::no_sslv2 |
|
||||
boost::asio::ssl::context::single_dh_use);
|
||||
|
||||
bool cert_set = false;
|
||||
|
||||
if (!cert_file.empty ())
|
||||
{
|
||||
boost::system::error_code error;
|
||||
context.use_certificate_file (cert_file, boost::asio::ssl::context::pem, error);
|
||||
|
||||
if (error)
|
||||
throw std::runtime_error ("Unable to use certificate file");
|
||||
|
||||
cert_set = true;
|
||||
}
|
||||
|
||||
if (!chain_file.empty ())
|
||||
{
|
||||
// VFALCO Replace fopen() with RAII
|
||||
FILE* f = fopen (chain_file.c_str (), "r");
|
||||
|
||||
if (!f)
|
||||
throw std::runtime_error ("Unable to open chain file");
|
||||
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
X509* x = PEM_read_X509 (f, NULL, NULL, NULL);
|
||||
|
||||
if (x == NULL)
|
||||
break;
|
||||
|
||||
if (!cert_set)
|
||||
{
|
||||
if (SSL_CTX_use_certificate (sslContext, x) != 1)
|
||||
throw std::runtime_error ("Unable to get certificate from chain file");
|
||||
|
||||
cert_set = true;
|
||||
}
|
||||
else if (SSL_CTX_add_extra_chain_cert (sslContext, x) != 1)
|
||||
{
|
||||
X509_free (x);
|
||||
throw std::runtime_error ("Unable to add chain certificate");
|
||||
}
|
||||
}
|
||||
|
||||
fclose (f);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
fclose (f);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
if (!key_file.empty ())
|
||||
{
|
||||
boost::system::error_code error;
|
||||
context.use_private_key_file (key_file, boost::asio::ssl::context::pem, error);
|
||||
|
||||
if (error)
|
||||
throw std::runtime_error ("Unable to use private key file");
|
||||
}
|
||||
|
||||
if (SSL_CTX_check_private_key (sslContext) != 1)
|
||||
throw std::runtime_error ("Private key not valid");
|
||||
}
|
||||
|
||||
// vim:ts=4
|
||||
|
||||
@@ -4,35 +4,23 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef __PEERDOOR__
|
||||
#define __PEERDOOR__
|
||||
#ifndef RIPPLE_PEERDOOR_H_INCLUDED
|
||||
#define RIPPLE_PEERDOOR_H_INCLUDED
|
||||
|
||||
/*
|
||||
Handles incoming connections from other Peers
|
||||
/** Handles incoming connections from peers.
|
||||
*/
|
||||
|
||||
class PeerDoor : LeakChecked <PeerDoor>
|
||||
{
|
||||
public:
|
||||
PeerDoor (std::string const& ip,
|
||||
int port,
|
||||
std::string const& sslCiphers,
|
||||
boost::asio::io_service& io_service);
|
||||
virtual ~PeerDoor () { }
|
||||
|
||||
boost::asio::ssl::context& getSSLContext ()
|
||||
{
|
||||
return mCtx;
|
||||
}
|
||||
static PeerDoor* New (
|
||||
std::string const& ip,
|
||||
int port,
|
||||
std::string const& sslCiphers,
|
||||
boost::asio::io_service& io_service);
|
||||
|
||||
private:
|
||||
boost::asio::ip::tcp::acceptor mAcceptor;
|
||||
boost::asio::ssl::context mCtx;
|
||||
boost::asio::deadline_timer mDelayTimer;
|
||||
|
||||
void startListening ();
|
||||
void handleConnect (Peer::pointer new_connection, const boost::system::error_code& error);
|
||||
virtual boost::asio::ssl::context& getSSLContext () = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
// vim:ts=4
|
||||
|
||||
@@ -6,11 +6,6 @@
|
||||
|
||||
SETUP_LOG (RPCDoor)
|
||||
|
||||
// VFALCO TODO Clean up this loose extern
|
||||
//
|
||||
extern void initSSLContext (boost::asio::ssl::context& context,
|
||||
std::string key_file, std::string cert_file, std::string chain_file);
|
||||
|
||||
RPCDoor::RPCDoor (boost::asio::io_service& io_service, RPCServer::Handler& handler)
|
||||
: m_rpcServerHandler (handler)
|
||||
, mAcceptor (io_service,
|
||||
@@ -21,7 +16,15 @@ RPCDoor::RPCDoor (boost::asio::io_service& io_service, RPCServer::Handler& handl
|
||||
WriteLog (lsINFO, RPCDoor) << "RPC port: " << theConfig.getRpcAddress().toRawUTF8() << " allow remote: " << theConfig.RPC_ALLOW_REMOTE;
|
||||
|
||||
if (theConfig.RPC_SECURE != 0)
|
||||
initSSLContext (mSSLContext, theConfig.RPC_SSL_KEY, theConfig.RPC_SSL_CERT, theConfig.RPC_SSL_CHAIN);
|
||||
{
|
||||
// VFALCO TODO This could be a method of theConfig
|
||||
//
|
||||
basio::SslContext::initializeFromFile (
|
||||
mSSLContext,
|
||||
theConfig.RPC_SSL_KEY,
|
||||
theConfig.RPC_SSL_CERT,
|
||||
theConfig.RPC_SSL_CHAIN);
|
||||
}
|
||||
|
||||
startListening ();
|
||||
}
|
||||
|
||||
@@ -569,7 +569,7 @@ void Application::setup ()
|
||||
{
|
||||
try
|
||||
{
|
||||
mPeerDoor = new PeerDoor (
|
||||
mPeerDoor = PeerDoor::New (
|
||||
theConfig.PEER_IP,
|
||||
theConfig.PEER_PORT,
|
||||
theConfig.PEER_SSL_CIPHER_LIST,
|
||||
|
||||
@@ -91,8 +91,8 @@ private:
|
||||
public:
|
||||
UniqueNodeListImp ()
|
||||
: m_scoreTimer (this)
|
||||
, m_fetchTimer (this)
|
||||
, mFetchActive (0)
|
||||
, m_fetchTimer (this)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ public:
|
||||
|
||||
// This is called when the application is started.
|
||||
// Get update times and start fetching and scoring as needed.
|
||||
void UniqueNodeListImp::start ()
|
||||
void start ()
|
||||
{
|
||||
miscLoad ();
|
||||
|
||||
@@ -414,7 +414,7 @@ public:
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
bool UniqueNodeListImp::nodeLoad (boost::filesystem::path pConfig)
|
||||
bool nodeLoad (boost::filesystem::path pConfig)
|
||||
{
|
||||
if (pConfig.empty ())
|
||||
{
|
||||
@@ -1084,12 +1084,7 @@ private:
|
||||
+ boost::posix_time::seconds (secondsFromNow);
|
||||
|
||||
// WriteLog (lsTRACE, UniqueNodeList) << str(boost::format("scoreNext: @%s") % mtpScoreNext);
|
||||
#if 1
|
||||
m_scoreTimer.setExpiration (secondsFromNow);
|
||||
#else
|
||||
mdtScoreTimer.expires_at (mtpScoreNext);
|
||||
mdtScoreTimer.async_wait (BIND_TYPE (&UniqueNodeListImp::scoreTimerHandler, this, P_1));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1281,14 +1276,9 @@ private:
|
||||
// Fetch needs to happen in the future. Set a timer to wake us.
|
||||
mtpFetchNext = tpNext;
|
||||
|
||||
#if 1
|
||||
double const seconds = (tpNext - tpNow).seconds ();
|
||||
|
||||
m_fetchTimer.setExpiration (seconds);
|
||||
#else
|
||||
mdtFetchTimer.expires_at (mtpFetchNext);
|
||||
mdtFetchTimer.async_wait (BIND_TYPE (&UniqueNodeListImp::fetchTimerHandler, this, P_1));
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -7,9 +7,6 @@
|
||||
#ifndef RIPPLE_WSHANDLER_H_INCLUDED
|
||||
#define RIPPLE_WSHANDLER_H_INCLUDED
|
||||
|
||||
extern void initSSLContext (boost::asio::ssl::context& context,
|
||||
std::string key_file, std::string cert_file, std::string chain_file);
|
||||
|
||||
extern bool serverOkay (std::string& reason);
|
||||
|
||||
template <typename endpoint_type>
|
||||
@@ -52,8 +49,11 @@ public:
|
||||
{
|
||||
if (theConfig.WEBSOCKET_SECURE != 0)
|
||||
{
|
||||
initSSLContext (*mCtx, theConfig.WEBSOCKET_SSL_KEY,
|
||||
theConfig.WEBSOCKET_SSL_CERT, theConfig.WEBSOCKET_SSL_CHAIN);
|
||||
basio::SslContext::initializeFromFile (
|
||||
*mCtx,
|
||||
theConfig.WEBSOCKET_SSL_KEY,
|
||||
theConfig.WEBSOCKET_SSL_CERT,
|
||||
theConfig.WEBSOCKET_SSL_CHAIN);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user