mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Refactor the structure of ServerHandler:
This is a cleanup to the structure of the sources. * Rename to ServerHandler * Move private implementation declaration to separate header * De-inline function definitions in the class declaration.
This commit is contained in:
@@ -1864,10 +1864,12 @@
|
|||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClInclude Include="..\..\src\ripple\app\main\ParameterTable.h">
|
<ClInclude Include="..\..\src\ripple\app\main\ParameterTable.h">
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClCompile Include="..\..\src\ripple\app\main\RPCHTTPServer.cpp">
|
<ClInclude Include="..\..\src\ripple\app\main\ServerHandler.h">
|
||||||
|
</ClInclude>
|
||||||
|
<ClCompile Include="..\..\src\ripple\app\main\ServerHandlerImp.cpp">
|
||||||
<ExcludedFromBuild>True</ExcludedFromBuild>
|
<ExcludedFromBuild>True</ExcludedFromBuild>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClInclude Include="..\..\src\ripple\app\main\RPCHTTPServer.h">
|
<ClInclude Include="..\..\src\ripple\app\main\ServerHandlerImp.h">
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="..\..\src\ripple\app\main\Tuning.h">
|
<ClInclude Include="..\..\src\ripple\app\main\Tuning.h">
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
|||||||
@@ -2769,10 +2769,13 @@
|
|||||||
<ClInclude Include="..\..\src\ripple\app\main\ParameterTable.h">
|
<ClInclude Include="..\..\src\ripple\app\main\ParameterTable.h">
|
||||||
<Filter>ripple\app\main</Filter>
|
<Filter>ripple\app\main</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClCompile Include="..\..\src\ripple\app\main\RPCHTTPServer.cpp">
|
<ClInclude Include="..\..\src\ripple\app\main\ServerHandler.h">
|
||||||
|
<Filter>ripple\app\main</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClCompile Include="..\..\src\ripple\app\main\ServerHandlerImp.cpp">
|
||||||
<Filter>ripple\app\main</Filter>
|
<Filter>ripple\app\main</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClInclude Include="..\..\src\ripple\app\main\RPCHTTPServer.h">
|
<ClInclude Include="..\..\src\ripple\app\main\ServerHandlerImp.h">
|
||||||
<Filter>ripple\app\main</Filter>
|
<Filter>ripple\app\main</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="..\..\src\ripple\app\main\Tuning.h">
|
<ClInclude Include="..\..\src\ripple\app\main\Tuning.h">
|
||||||
|
|||||||
@@ -174,7 +174,7 @@ public:
|
|||||||
std::unique_ptr <InboundLedgers> m_inboundLedgers;
|
std::unique_ptr <InboundLedgers> m_inboundLedgers;
|
||||||
std::unique_ptr <NetworkOPs> m_networkOPs;
|
std::unique_ptr <NetworkOPs> m_networkOPs;
|
||||||
std::unique_ptr <UniqueNodeList> m_deprecatedUNL;
|
std::unique_ptr <UniqueNodeList> m_deprecatedUNL;
|
||||||
std::unique_ptr <RPCHTTPServer> m_rpcHTTPServer;
|
std::unique_ptr <ServerHandler> serverHandler_;
|
||||||
std::unique_ptr <NodeStore::Database> m_nodeStore;
|
std::unique_ptr <NodeStore::Database> m_nodeStore;
|
||||||
std::unique_ptr <SNTPClient> m_sntpClient;
|
std::unique_ptr <SNTPClient> m_sntpClient;
|
||||||
std::unique_ptr <TxQueue> m_txQueue;
|
std::unique_ptr <TxQueue> m_txQueue;
|
||||||
@@ -311,7 +311,7 @@ public:
|
|||||||
// VFALCO NOTE LocalCredentials starts the deprecated UNL service
|
// VFALCO NOTE LocalCredentials starts the deprecated UNL service
|
||||||
, m_deprecatedUNL (make_UniqueNodeList (*m_jobQueue))
|
, m_deprecatedUNL (make_UniqueNodeList (*m_jobQueue))
|
||||||
|
|
||||||
, m_rpcHTTPServer (make_RPCHTTPServer (*m_networkOPs,
|
, serverHandler_ (make_RPCHTTPServer (*m_networkOPs,
|
||||||
*m_jobQueue, *m_networkOPs, *m_resourceManager,
|
*m_jobQueue, *m_networkOPs, *m_resourceManager,
|
||||||
setup_RPC(getConfig()["rpc"])))
|
setup_RPC(getConfig()["rpc"])))
|
||||||
|
|
||||||
@@ -372,7 +372,7 @@ public:
|
|||||||
m_nodeStoreScheduler.setJobQueue (*m_jobQueue);
|
m_nodeStoreScheduler.setJobQueue (*m_jobQueue);
|
||||||
|
|
||||||
add (m_ledgerMaster->getPropertySource ());
|
add (m_ledgerMaster->getPropertySource ());
|
||||||
add (*m_rpcHTTPServer);
|
add (*serverHandler_);
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
@@ -806,7 +806,7 @@ public:
|
|||||||
|
|
||||||
//----------------------------------------------------------------------
|
//----------------------------------------------------------------------
|
||||||
|
|
||||||
m_rpcHTTPServer->setup (m_journal);
|
serverHandler_->setup (m_journal);
|
||||||
|
|
||||||
// Begin connecting to network.
|
// Begin connecting to network.
|
||||||
if (!getConfig ().RUN_STANDALONE)
|
if (!getConfig ().RUN_STANDALONE)
|
||||||
|
|||||||
@@ -1,304 +0,0 @@
|
|||||||
//------------------------------------------------------------------------------
|
|
||||||
/*
|
|
||||||
This file is part of rippled: https://github.com/ripple/rippled
|
|
||||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
|
||||||
|
|
||||||
Permission to use, copy, modify, and/or distribute this software for any
|
|
||||||
purpose with or without fee is hereby granted, provided that the above
|
|
||||||
copyright notice and this permission notice appear in all copies.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
||||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
||||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
||||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
||||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
||||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
||||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
||||||
*/
|
|
||||||
//==============================================================================
|
|
||||||
|
|
||||||
#include <ripple/common/RippleSSLContext.h>
|
|
||||||
#include <ripple/http/Session.h>
|
|
||||||
#include <ripple/app/main/RPCHTTPServer.h>
|
|
||||||
#include <ripple/rpc/RPCHandler.h>
|
|
||||||
|
|
||||||
namespace ripple {
|
|
||||||
|
|
||||||
class RPCHTTPServerImp
|
|
||||||
: public RPCHTTPServer
|
|
||||||
, public beast::LeakChecked <RPCHTTPServerImp>
|
|
||||||
, public HTTP::Handler
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Resource::Manager& m_resourceManager;
|
|
||||||
beast::Journal m_journal;
|
|
||||||
JobQueue& m_jobQueue;
|
|
||||||
NetworkOPs& m_networkOPs;
|
|
||||||
HTTP::Server m_server;
|
|
||||||
std::unique_ptr <RippleSSLContext> m_context;
|
|
||||||
RPC::Setup setup_;
|
|
||||||
|
|
||||||
RPCHTTPServerImp (Stoppable& parent, JobQueue& jobQueue,
|
|
||||||
NetworkOPs& networkOPs, Resource::Manager& resourceManager,
|
|
||||||
RPC::Setup const& setup)
|
|
||||||
: RPCHTTPServer (parent)
|
|
||||||
, m_resourceManager (resourceManager)
|
|
||||||
, m_journal (deprecatedLogs().journal("HTTP-RPC"))
|
|
||||||
, m_jobQueue (jobQueue)
|
|
||||||
, m_networkOPs (networkOPs)
|
|
||||||
, m_server (*this, deprecatedLogs().journal("HTTP"))
|
|
||||||
, setup_ (setup)
|
|
||||||
{
|
|
||||||
if (setup_.secure)
|
|
||||||
m_context.reset (RippleSSLContext::createAuthenticated (
|
|
||||||
setup_.ssl_key, setup_.ssl_cert, setup_.ssl_chain));
|
|
||||||
else
|
|
||||||
m_context.reset (RippleSSLContext::createBare());
|
|
||||||
}
|
|
||||||
|
|
||||||
~RPCHTTPServerImp()
|
|
||||||
{
|
|
||||||
m_server.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
setup (beast::Journal journal) override
|
|
||||||
{
|
|
||||||
if (! setup_.ip.empty() && setup_.port != 0)
|
|
||||||
{
|
|
||||||
auto ep = beast::IP::Endpoint::from_string (setup_.ip);
|
|
||||||
|
|
||||||
// VFALCO TODO IP address should not have an "unspecified" state
|
|
||||||
//if (! is_unspecified (ep))
|
|
||||||
{
|
|
||||||
HTTP::Port port;
|
|
||||||
|
|
||||||
if (setup_.secure == 0)
|
|
||||||
port.security = HTTP::Port::Security::no_ssl;
|
|
||||||
else if (setup_.secure == 1)
|
|
||||||
port.security = HTTP::Port::Security::allow_ssl;
|
|
||||||
else
|
|
||||||
port.security = HTTP::Port::Security::require_ssl;
|
|
||||||
port.addr = ep.at_port(0);
|
|
||||||
if (setup_.port != 0)
|
|
||||||
port.port = setup_.port;
|
|
||||||
else
|
|
||||||
port.port = ep.port();
|
|
||||||
port.context = m_context.get ();
|
|
||||||
|
|
||||||
HTTP::Ports ports;
|
|
||||||
ports.push_back (port);
|
|
||||||
m_server.setPorts (ports);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
journal.info << "RPC interface: disabled";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
|
||||||
//
|
|
||||||
// Stoppable
|
|
||||||
//
|
|
||||||
|
|
||||||
void
|
|
||||||
onStop() override
|
|
||||||
{
|
|
||||||
m_server.stopAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
onChildrenStopped() override
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
|
||||||
//
|
|
||||||
// HTTP::Handler
|
|
||||||
//
|
|
||||||
|
|
||||||
void
|
|
||||||
onAccept (HTTP::Session& session) override
|
|
||||||
{
|
|
||||||
// Reject non-loopback connections if RPC_ALLOW_REMOTE is not set
|
|
||||||
if (! setup_.allow_remote &&
|
|
||||||
! beast::IP::is_loopback (session.remoteAddress()))
|
|
||||||
{
|
|
||||||
session.close (false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
onRequest (HTTP::Session& session) override
|
|
||||||
{
|
|
||||||
// Check user/password authorization
|
|
||||||
auto const headers (build_map (session.message().headers));
|
|
||||||
if (! HTTPAuthorized (headers))
|
|
||||||
{
|
|
||||||
session.write (HTTPReply (403, "Forbidden"));
|
|
||||||
session.close (true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
session.detach();
|
|
||||||
|
|
||||||
m_jobQueue.addJob (jtCLIENT, "RPC-Client", std::bind (
|
|
||||||
&RPCHTTPServerImp::processSession, this, std::placeholders::_1,
|
|
||||||
std::ref (session)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
onClose (HTTP::Session& session,
|
|
||||||
boost::system::error_code const&) override
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
onStopped (HTTP::Server&) override
|
|
||||||
{
|
|
||||||
stopped();
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// Dispatched on the job queue
|
|
||||||
void processSession (Job& job, HTTP::Session& session)
|
|
||||||
{
|
|
||||||
auto const s (to_string(session.message().body));
|
|
||||||
session.write (processRequest (to_string(session.message().body),
|
|
||||||
session.remoteAddress().at_port(0)));
|
|
||||||
|
|
||||||
if (session.message().keep_alive())
|
|
||||||
{
|
|
||||||
session.complete();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
session.close (true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string createResponse (
|
|
||||||
int statusCode,
|
|
||||||
std::string const& description)
|
|
||||||
{
|
|
||||||
return HTTPReply (statusCode, description);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stolen directly from RPCServerHandler
|
|
||||||
std::string
|
|
||||||
processRequest (std::string const& request,
|
|
||||||
beast::IP::Endpoint const& remoteIPAddress)
|
|
||||||
{
|
|
||||||
Json::Value jvRequest;
|
|
||||||
{
|
|
||||||
Json::Reader reader;
|
|
||||||
|
|
||||||
if ((request.size () > 1000000) ||
|
|
||||||
! reader.parse (request, jvRequest) ||
|
|
||||||
jvRequest.isNull () ||
|
|
||||||
! jvRequest.isObject ())
|
|
||||||
{
|
|
||||||
return createResponse (400, "Unable to parse request");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
auto const role = getConfig ().getAdminRole (jvRequest, remoteIPAddress);
|
|
||||||
|
|
||||||
Resource::Consumer usage;
|
|
||||||
|
|
||||||
if (role == Config::ADMIN)
|
|
||||||
usage = m_resourceManager.newAdminEndpoint (remoteIPAddress.to_string());
|
|
||||||
else
|
|
||||||
usage = m_resourceManager.newInboundEndpoint(remoteIPAddress);
|
|
||||||
|
|
||||||
if (usage.disconnect ())
|
|
||||||
return createResponse (503, "Server is overloaded");
|
|
||||||
|
|
||||||
// Parse id now so errors from here on will have the id
|
|
||||||
//
|
|
||||||
// VFALCO NOTE Except that "id" isn't included in the following errors.
|
|
||||||
//
|
|
||||||
Json::Value const id = jvRequest ["id"];
|
|
||||||
|
|
||||||
Json::Value const method = jvRequest ["method"];
|
|
||||||
|
|
||||||
if (method.isNull ())
|
|
||||||
return createResponse (400, "Null method");
|
|
||||||
|
|
||||||
if (! method.isString ())
|
|
||||||
return createResponse (400, "method is not string");
|
|
||||||
|
|
||||||
std::string strMethod = method.asString ();
|
|
||||||
if (strMethod.empty())
|
|
||||||
return createResponse (400, "method is empty");
|
|
||||||
|
|
||||||
// Parse params
|
|
||||||
Json::Value params = jvRequest ["params"];
|
|
||||||
|
|
||||||
if (params.isNull ())
|
|
||||||
params = Json::Value (Json::arrayValue);
|
|
||||||
|
|
||||||
else if (!params.isArray ())
|
|
||||||
return HTTPReply (400, "params unparseable");
|
|
||||||
|
|
||||||
// VFALCO TODO Shouldn't we handle this earlier?
|
|
||||||
//
|
|
||||||
if (role == Config::FORBID)
|
|
||||||
{
|
|
||||||
// VFALCO TODO Needs implementing
|
|
||||||
// FIXME Needs implementing
|
|
||||||
// XXX This needs rate limiting to prevent brute forcing password.
|
|
||||||
return HTTPReply (403, "Forbidden");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
std::string response;
|
|
||||||
RPCHandler rpcHandler (m_networkOPs);
|
|
||||||
Resource::Charge loadType = Resource::feeReferenceRPC;
|
|
||||||
|
|
||||||
m_journal.debug << "Query: " << strMethod << params;
|
|
||||||
|
|
||||||
Json::Value const result (rpcHandler.doRpcCommand (
|
|
||||||
strMethod, params, role, loadType));
|
|
||||||
m_journal.debug << "Reply: " << result;
|
|
||||||
|
|
||||||
usage.charge (loadType);
|
|
||||||
|
|
||||||
response = JSONRPCReply (result, Json::Value (), id);
|
|
||||||
|
|
||||||
return createResponse (200, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// PropertyStream
|
|
||||||
//
|
|
||||||
|
|
||||||
void
|
|
||||||
onWrite (beast::PropertyStream::Map& map) override
|
|
||||||
{
|
|
||||||
m_server.onWrite (map);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
RPCHTTPServer::RPCHTTPServer (Stoppable& parent)
|
|
||||||
: Stoppable ("RPCHTTPServer", parent)
|
|
||||||
, Source ("http")
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
std::unique_ptr <RPCHTTPServer>
|
|
||||||
make_RPCHTTPServer (beast::Stoppable& parent, JobQueue& jobQueue,
|
|
||||||
NetworkOPs& networkOPs, Resource::Manager& resourceManager,
|
|
||||||
RPC::Setup const& setup)
|
|
||||||
{
|
|
||||||
return std::make_unique <RPCHTTPServerImp> (
|
|
||||||
parent, jobQueue, networkOPs, resourceManager, setup);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
/*
|
/*
|
||||||
This file is part of rippled: https://github.com/ripple/rippled
|
This file is part of rippled: https://github.com/ripple/rippled
|
||||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||||
@@ -17,8 +17,8 @@
|
|||||||
*/
|
*/
|
||||||
//==============================================================================
|
//==============================================================================
|
||||||
|
|
||||||
#ifndef RIPPLE_APP_RPCHTTPSERVER_H_INCLUDED
|
#ifndef RIPPLE_APP_MAIN_SERVERHANDLER_H_INCLUDED
|
||||||
#define RIPPLE_APP_RPCHTTPSERVER_H_INCLUDED
|
#define RIPPLE_APP_MAIN_SERVERHANDLER_H_INCLUDED
|
||||||
|
|
||||||
#include <ripple/core/Config.h>
|
#include <ripple/core/Config.h>
|
||||||
#include <beast/utility/Journal.h>
|
#include <beast/utility/Journal.h>
|
||||||
@@ -27,16 +27,16 @@
|
|||||||
|
|
||||||
namespace ripple {
|
namespace ripple {
|
||||||
|
|
||||||
class RPCHTTPServer
|
class ServerHandler
|
||||||
: public beast::Stoppable
|
: public beast::Stoppable
|
||||||
, public beast::PropertyStream::Source
|
, public beast::PropertyStream::Source
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
RPCHTTPServer (Stoppable& parent);
|
ServerHandler (Stoppable& parent);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual
|
virtual
|
||||||
~RPCHTTPServer() = default;
|
~ServerHandler() = default;
|
||||||
|
|
||||||
/** Opens listening ports based on the Config settings
|
/** Opens listening ports based on the Config settings
|
||||||
This is implemented outside the constructor to support
|
This is implemented outside the constructor to support
|
||||||
@@ -47,8 +47,8 @@ public:
|
|||||||
setup (beast::Journal journal) = 0;
|
setup (beast::Journal journal) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::unique_ptr <RPCHTTPServer>
|
std::unique_ptr <ServerHandler>
|
||||||
make_RPCHTTPServer (beast::Stoppable& parent, JobQueue& jobQueue,
|
make_ServerHandler (beast::Stoppable& parent, JobQueue& jobQueue,
|
||||||
NetworkOPs& networkOPs, Resource::Manager& resourceManager,
|
NetworkOPs& networkOPs, Resource::Manager& resourceManager,
|
||||||
RPC::Setup const& setup);
|
RPC::Setup const& setup);
|
||||||
|
|
||||||
274
src/ripple/app/main/ServerHandlerImp.cpp
Normal file
274
src/ripple/app/main/ServerHandlerImp.cpp
Normal file
@@ -0,0 +1,274 @@
|
|||||||
|
//------------------------------------------------------------------------------
|
||||||
|
/*
|
||||||
|
This file is part of rippled: https://github.com/ripple/rippled
|
||||||
|
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
|
copyright notice and this permission notice appear in all copies.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
//==============================================================================
|
||||||
|
|
||||||
|
#include <ripple/app/main/ServerHandlerImp.h>
|
||||||
|
|
||||||
|
namespace ripple {
|
||||||
|
|
||||||
|
ServerHandler::ServerHandler (Stoppable& parent)
|
||||||
|
: Stoppable ("ServerHandler", parent)
|
||||||
|
, Source ("server")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
ServerHandlerImp::ServerHandlerImp (Stoppable& parent, JobQueue& jobQueue,
|
||||||
|
NetworkOPs& networkOPs, Resource::Manager& resourceManager,
|
||||||
|
RPC::Setup const& setup)
|
||||||
|
: ServerHandler (parent)
|
||||||
|
, m_resourceManager (resourceManager)
|
||||||
|
, m_journal (deprecatedLogs().journal("Server"))
|
||||||
|
, m_jobQueue (jobQueue)
|
||||||
|
, m_networkOPs (networkOPs)
|
||||||
|
, m_server (*this, deprecatedLogs().journal("Server"))
|
||||||
|
, setup_ (setup)
|
||||||
|
{
|
||||||
|
if (setup_.secure)
|
||||||
|
m_context.reset (RippleSSLContext::createAuthenticated (
|
||||||
|
setup_.ssl_key, setup_.ssl_cert, setup_.ssl_chain));
|
||||||
|
else
|
||||||
|
m_context.reset (RippleSSLContext::createBare());
|
||||||
|
}
|
||||||
|
|
||||||
|
ServerHandlerImp::~ServerHandlerImp()
|
||||||
|
{
|
||||||
|
m_server.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ServerHandlerImp::setup (beast::Journal journal)
|
||||||
|
{
|
||||||
|
if (! setup_.ip.empty() && setup_.port != 0)
|
||||||
|
{
|
||||||
|
auto ep = beast::IP::Endpoint::from_string (setup_.ip);
|
||||||
|
|
||||||
|
// VFALCO TODO IP address should not have an "unspecified" state
|
||||||
|
//if (! is_unspecified (ep))
|
||||||
|
{
|
||||||
|
HTTP::Port port;
|
||||||
|
|
||||||
|
if (setup_.secure == 0)
|
||||||
|
port.security = HTTP::Port::Security::no_ssl;
|
||||||
|
else if (setup_.secure == 1)
|
||||||
|
port.security = HTTP::Port::Security::allow_ssl;
|
||||||
|
else
|
||||||
|
port.security = HTTP::Port::Security::require_ssl;
|
||||||
|
port.addr = ep.at_port(0);
|
||||||
|
if (setup_.port != 0)
|
||||||
|
port.port = setup_.port;
|
||||||
|
else
|
||||||
|
port.port = ep.port();
|
||||||
|
port.context = m_context.get ();
|
||||||
|
|
||||||
|
HTTP::Ports ports;
|
||||||
|
ports.push_back (port);
|
||||||
|
m_server.setPorts (ports);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
journal.info << "RPC interface: disabled";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void
|
||||||
|
ServerHandlerImp::onStop()
|
||||||
|
{
|
||||||
|
m_server.stopAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void
|
||||||
|
ServerHandlerImp::onAccept (HTTP::Session& session)
|
||||||
|
{
|
||||||
|
// Reject non-loopback connections if RPC_ALLOW_REMOTE is not set
|
||||||
|
if (! setup_.allow_remote &&
|
||||||
|
! beast::IP::is_loopback (session.remoteAddress()))
|
||||||
|
{
|
||||||
|
session.close (false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ServerHandlerImp::onRequest (HTTP::Session& session)
|
||||||
|
{
|
||||||
|
// Check user/password authorization
|
||||||
|
auto const headers (build_map (session.message().headers));
|
||||||
|
if (! HTTPAuthorized (headers))
|
||||||
|
{
|
||||||
|
session.write (HTTPReply (403, "Forbidden"));
|
||||||
|
session.close (true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
session.detach();
|
||||||
|
|
||||||
|
m_jobQueue.addJob (jtCLIENT, "RPC-Client", std::bind (
|
||||||
|
&ServerHandlerImp::processSession, this, std::placeholders::_1,
|
||||||
|
std::ref (session)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ServerHandlerImp::onClose (HTTP::Session& session,
|
||||||
|
boost::system::error_code const&)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ServerHandlerImp::onStopped (HTTP::Server&)
|
||||||
|
{
|
||||||
|
stopped();
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Dispatched on the job queue
|
||||||
|
void
|
||||||
|
ServerHandlerImp::processSession (Job& job, HTTP::Session& session)
|
||||||
|
{
|
||||||
|
auto const s (to_string(session.message().body));
|
||||||
|
session.write (processRequest (to_string(session.message().body),
|
||||||
|
session.remoteAddress().at_port(0)));
|
||||||
|
|
||||||
|
if (session.message().keep_alive())
|
||||||
|
{
|
||||||
|
session.complete();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
session.close (true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
ServerHandlerImp::createResponse (
|
||||||
|
int statusCode,
|
||||||
|
std::string const& description)
|
||||||
|
{
|
||||||
|
return HTTPReply (statusCode, description);
|
||||||
|
}
|
||||||
|
|
||||||
|
// VFALCO ARGH! returning a single std::string for the entire response?
|
||||||
|
std::string
|
||||||
|
ServerHandlerImp::processRequest (std::string const& request,
|
||||||
|
beast::IP::Endpoint const& remoteIPAddress)
|
||||||
|
{
|
||||||
|
Json::Value jvRequest;
|
||||||
|
{
|
||||||
|
Json::Reader reader;
|
||||||
|
if ((request.size () > 1000000) ||
|
||||||
|
! reader.parse (request, jvRequest) ||
|
||||||
|
jvRequest.isNull () ||
|
||||||
|
! jvRequest.isObject ())
|
||||||
|
{
|
||||||
|
return createResponse (400, "Unable to parse request");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto const role = getConfig ().getAdminRole (jvRequest, remoteIPAddress);
|
||||||
|
|
||||||
|
Resource::Consumer usage;
|
||||||
|
|
||||||
|
if (role == Config::ADMIN)
|
||||||
|
usage = m_resourceManager.newAdminEndpoint (remoteIPAddress.to_string());
|
||||||
|
else
|
||||||
|
usage = m_resourceManager.newInboundEndpoint(remoteIPAddress);
|
||||||
|
|
||||||
|
if (usage.disconnect ())
|
||||||
|
return createResponse (503, "Server is overloaded");
|
||||||
|
|
||||||
|
// Parse id now so errors from here on will have the id
|
||||||
|
//
|
||||||
|
// VFALCO NOTE Except that "id" isn't included in the following errors.
|
||||||
|
//
|
||||||
|
Json::Value const id = jvRequest ["id"];
|
||||||
|
|
||||||
|
Json::Value const method = jvRequest ["method"];
|
||||||
|
|
||||||
|
if (method.isNull ())
|
||||||
|
return createResponse (400, "Null method");
|
||||||
|
|
||||||
|
if (! method.isString ())
|
||||||
|
return createResponse (400, "method is not string");
|
||||||
|
|
||||||
|
std::string strMethod = method.asString ();
|
||||||
|
if (strMethod.empty())
|
||||||
|
return createResponse (400, "method is empty");
|
||||||
|
|
||||||
|
// Parse params
|
||||||
|
Json::Value params = jvRequest ["params"];
|
||||||
|
|
||||||
|
if (params.isNull ())
|
||||||
|
params = Json::Value (Json::arrayValue);
|
||||||
|
|
||||||
|
else if (!params.isArray ())
|
||||||
|
return HTTPReply (400, "params unparseable");
|
||||||
|
|
||||||
|
// VFALCO TODO Shouldn't we handle this earlier?
|
||||||
|
//
|
||||||
|
if (role == Config::FORBID)
|
||||||
|
{
|
||||||
|
// VFALCO TODO Needs implementing
|
||||||
|
// FIXME Needs implementing
|
||||||
|
// XXX This needs rate limiting to prevent brute forcing password.
|
||||||
|
return HTTPReply (403, "Forbidden");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string response;
|
||||||
|
RPCHandler rpcHandler (m_networkOPs);
|
||||||
|
Resource::Charge loadType = Resource::feeReferenceRPC;
|
||||||
|
|
||||||
|
m_journal.debug << "Query: " << strMethod << params;
|
||||||
|
|
||||||
|
Json::Value const result (rpcHandler.doRpcCommand (
|
||||||
|
strMethod, params, role, loadType));
|
||||||
|
m_journal.debug << "Reply: " << result;
|
||||||
|
|
||||||
|
usage.charge (loadType);
|
||||||
|
|
||||||
|
response = JSONRPCReply (result, Json::Value (), id);
|
||||||
|
|
||||||
|
return createResponse (200, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void
|
||||||
|
ServerHandlerImp::onWrite (beast::PropertyStream::Map& map)
|
||||||
|
{
|
||||||
|
m_server.onWrite (map);
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
std::unique_ptr <ServerHandler>
|
||||||
|
make_RPCHTTPServer (beast::Stoppable& parent, JobQueue& jobQueue,
|
||||||
|
NetworkOPs& networkOPs, Resource::Manager& resourceManager,
|
||||||
|
RPC::Setup const& setup)
|
||||||
|
{
|
||||||
|
return std::make_unique <ServerHandlerImp> (
|
||||||
|
parent, jobQueue, networkOPs, resourceManager, setup);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
109
src/ripple/app/main/ServerHandlerImp.h
Normal file
109
src/ripple/app/main/ServerHandlerImp.h
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
//------------------------------------------------------------------------------
|
||||||
|
/*
|
||||||
|
This file is part of rippled: https://github.com/ripple/rippled
|
||||||
|
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
|
copyright notice and this permission notice appear in all copies.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
//==============================================================================
|
||||||
|
|
||||||
|
#ifndef RIPPLE_APP_MAIN_SERVERHANDLERIMP_H_INCLUDED
|
||||||
|
#define RIPPLE_APP_MAIN_SERVERHANDLERIMP_H_INCLUDED
|
||||||
|
|
||||||
|
#include <ripple/app/main/ServerHandler.h>
|
||||||
|
#include <ripple/common/RippleSSLContext.h>
|
||||||
|
#include <ripple/http/Session.h>
|
||||||
|
#include <ripple/rpc/RPCHandler.h>
|
||||||
|
|
||||||
|
namespace ripple {
|
||||||
|
|
||||||
|
// Private implementation
|
||||||
|
class ServerHandlerImp
|
||||||
|
: public ServerHandler
|
||||||
|
, public beast::LeakChecked <ServerHandlerImp>
|
||||||
|
, public HTTP::Handler
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
Resource::Manager& m_resourceManager;
|
||||||
|
beast::Journal m_journal;
|
||||||
|
JobQueue& m_jobQueue;
|
||||||
|
NetworkOPs& m_networkOPs;
|
||||||
|
HTTP::Server m_server;
|
||||||
|
std::unique_ptr <RippleSSLContext> m_context;
|
||||||
|
RPC::Setup setup_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
ServerHandlerImp (Stoppable& parent, JobQueue& jobQueue,
|
||||||
|
NetworkOPs& networkOPs, Resource::Manager& resourceManager,
|
||||||
|
RPC::Setup const& setup);
|
||||||
|
|
||||||
|
~ServerHandlerImp();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void
|
||||||
|
setup (beast::Journal journal) override;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Stoppable
|
||||||
|
//
|
||||||
|
|
||||||
|
void
|
||||||
|
onStop() override;
|
||||||
|
|
||||||
|
//
|
||||||
|
// HTTP::Handler
|
||||||
|
//
|
||||||
|
|
||||||
|
void
|
||||||
|
onAccept (HTTP::Session& session) override;
|
||||||
|
|
||||||
|
void
|
||||||
|
onRequest (HTTP::Session& session) override;
|
||||||
|
|
||||||
|
void
|
||||||
|
onClose (HTTP::Session& session,
|
||||||
|
boost::system::error_code const&) override;
|
||||||
|
|
||||||
|
void
|
||||||
|
onStopped (HTTP::Server&) override;
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void
|
||||||
|
processSession (Job& job, HTTP::Session& session);
|
||||||
|
|
||||||
|
std::string
|
||||||
|
createResponse (int statusCode, std::string const& description);
|
||||||
|
|
||||||
|
std::string
|
||||||
|
processRequest (std::string const& request,
|
||||||
|
beast::IP::Endpoint const& remoteIPAddress);
|
||||||
|
|
||||||
|
//
|
||||||
|
// PropertyStream
|
||||||
|
//
|
||||||
|
|
||||||
|
void
|
||||||
|
onWrite (beast::PropertyStream::Map& map) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
std::unique_ptr <ServerHandler>
|
||||||
|
make_RPCHTTPServer (beast::Stoppable& parent, JobQueue& jobQueue,
|
||||||
|
NetworkOPs& networkOPs, Resource::Manager& resourceManager,
|
||||||
|
RPC::Setup const& setup);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -18,7 +18,6 @@
|
|||||||
//==============================================================================
|
//==============================================================================
|
||||||
|
|
||||||
#include <ripple/common/jsonrpc_fields.h>
|
#include <ripple/common/jsonrpc_fields.h>
|
||||||
#include <ripple/app/main/RPCHTTPServer.h>
|
|
||||||
#include <ripple/rpc/RPCHandler.h>
|
#include <ripple/rpc/RPCHandler.h>
|
||||||
#include <ripple/rpc/impl/Tuning.h>
|
#include <ripple/rpc/impl/Tuning.h>
|
||||||
#include <ripple/rpc/impl/Context.h>
|
#include <ripple/rpc/impl/Context.h>
|
||||||
|
|||||||
@@ -29,7 +29,7 @@
|
|||||||
#include <ripple/app/main/NodeStoreScheduler.cpp>
|
#include <ripple/app/main/NodeStoreScheduler.cpp>
|
||||||
#include <ripple/app/main/IoServicePool.cpp>
|
#include <ripple/app/main/IoServicePool.cpp>
|
||||||
#include <ripple/app/main/FatalErrorReporter.cpp>
|
#include <ripple/app/main/FatalErrorReporter.cpp>
|
||||||
#include <ripple/app/main/RPCHTTPServer.cpp>
|
#include <ripple/app/main/ServerHandlerImp.cpp>
|
||||||
#include <ripple/app/tx/TxQueueEntry.h>
|
#include <ripple/app/tx/TxQueueEntry.h>
|
||||||
#include <ripple/app/tx/TxQueueEntry.cpp>
|
#include <ripple/app/tx/TxQueueEntry.cpp>
|
||||||
#include <ripple/app/tx/TxQueue.cpp>
|
#include <ripple/app/tx/TxQueue.cpp>
|
||||||
|
|||||||
Reference in New Issue
Block a user