RPC: allow binding of restricted port in addition to core port

This commit is contained in:
Tim L 2017-11-15 22:58:11 -05:00
parent dc6a8014bd
commit 23b6f685de
4 changed files with 69 additions and 32 deletions

View File

@ -60,7 +60,7 @@ private:
public: public:
t_core core; t_core core;
t_p2p p2p; t_p2p p2p;
t_rpc rpc; std::vector<std::unique_ptr<t_rpc>> rpcs;
t_internals( t_internals(
boost::program_options::variables_map const & vm boost::program_options::variables_map const & vm
@ -68,11 +68,22 @@ public:
: core{vm} : core{vm}
, protocol{vm, core} , protocol{vm, core}
, p2p{vm, protocol} , p2p{vm, protocol}
, rpc{vm, core, p2p}
{ {
// Handle circular dependencies // Handle circular dependencies
protocol.set_p2p_endpoint(p2p.get()); protocol.set_p2p_endpoint(p2p.get());
core.set_protocol(protocol.get()); core.set_protocol(protocol.get());
const auto testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on);
const auto restricted = command_line::get_arg(vm, cryptonote::core_rpc_server::arg_restricted_rpc);
const auto main_rpc_port = command_line::get_arg(vm, testnet ? cryptonote::core_rpc_server::arg_testnet_rpc_bind_port : cryptonote::core_rpc_server::arg_rpc_bind_port);
rpcs.emplace_back(new t_rpc{vm, core, p2p, restricted, testnet, main_rpc_port, "core"});
auto restricted_rpc_port_arg = testnet ? cryptonote::core_rpc_server::arg_testnet_rpc_restricted_bind_port : cryptonote::core_rpc_server::arg_rpc_restricted_bind_port;
if(!command_line::is_arg_defaulted(vm, restricted_rpc_port_arg))
{
auto restricted_rpc_port = command_line::get_arg(vm, restricted_rpc_port_arg);
rpcs.emplace_back(new t_rpc{vm, core, p2p, true, testnet, restricted_rpc_port, "restricted"});
}
} }
}; };
@ -135,14 +146,15 @@ bool t_daemon::run(bool interactive)
{ {
if (!mp_internals->core.run()) if (!mp_internals->core.run())
return false; return false;
mp_internals->rpc.run();
for(auto& rpc: mp_internals->rpcs)
rpc->run();
std::unique_ptr<daemonize::t_command_server> rpc_commands; std::unique_ptr<daemonize::t_command_server> rpc_commands;
if (interactive && mp_internals->rpcs.size())
if (interactive)
{ {
// The first three variables are not used when the fourth is false // The first three variables are not used when the fourth is false
rpc_commands.reset(new daemonize::t_command_server(0, 0, boost::none, false, mp_internals->rpc.get_server())); rpc_commands.reset(new daemonize::t_command_server(0, 0, boost::none, false, mp_internals->rpcs.front()->get_server()));
rpc_commands->start_handling(std::bind(&daemonize::t_daemon::stop_p2p, this)); rpc_commands->start_handling(std::bind(&daemonize::t_daemon::stop_p2p, this));
} }
@ -154,12 +166,11 @@ bool t_daemon::run(bool interactive)
LOG_ERROR(std::string("Failed to add TCP Socket (") + zmq_rpc_bind_address LOG_ERROR(std::string("Failed to add TCP Socket (") + zmq_rpc_bind_address
+ ":" + zmq_rpc_bind_port + ") to ZMQ RPC Server"); + ":" + zmq_rpc_bind_port + ") to ZMQ RPC Server");
if (interactive) if (rpc_commands)
{
rpc_commands->stop_handling(); rpc_commands->stop_handling();
}
mp_internals->rpc.stop(); for(auto& rpc : mp_internals->rpcs)
rpc->stop();
return false; return false;
} }
@ -173,13 +184,12 @@ bool t_daemon::run(bool interactive)
mp_internals->p2p.run(); // blocks until p2p goes down mp_internals->p2p.run(); // blocks until p2p goes down
if (rpc_commands) if (rpc_commands)
{
rpc_commands->stop_handling(); rpc_commands->stop_handling();
}
zmq_server.stop(); zmq_server.stop();
mp_internals->rpc.stop(); for(auto& rpc : mp_internals->rpcs)
rpc->stop();
mp_internals->core.get().get_miner().stop(); mp_internals->core.get().get_miner().stop();
MGINFO("Node stopped."); MGINFO("Node stopped.");
return true; return true;
@ -204,7 +214,9 @@ void t_daemon::stop()
} }
mp_internals->core.get().get_miner().stop(); mp_internals->core.get().get_miner().stop();
mp_internals->p2p.stop(); mp_internals->p2p.stop();
mp_internals->rpc.stop(); for(auto& rpc : mp_internals->rpcs)
rpc->stop();
mp_internals.reset(nullptr); // Ensure resources are cleaned up before we return mp_internals.reset(nullptr); // Ensure resources are cleaned up before we return
} }

View File

@ -47,35 +47,41 @@ public:
} }
private: private:
cryptonote::core_rpc_server m_server; cryptonote::core_rpc_server m_server;
const std::string m_description;
public: public:
t_rpc( t_rpc(
boost::program_options::variables_map const & vm boost::program_options::variables_map const & vm
, t_core & core , t_core & core
, t_p2p & p2p , t_p2p & p2p
, const bool restricted
, const bool testnet
, const std::string & port
, const std::string & description
) )
: m_server{core.get(), p2p.get()} : m_server{core.get(), p2p.get()}, m_description{description}
{ {
MGINFO("Initializing core rpc server..."); MGINFO("Initializing " << m_description << " rpc server...");
if (!m_server.init(vm))
if (!m_server.init(vm, restricted, testnet, port))
{ {
throw std::runtime_error("Failed to initialize core rpc server."); throw std::runtime_error("Failed to initialize " + m_description + " rpc server.");
} }
MGINFO("Core rpc server initialized OK on port: " << m_server.get_binded_port()); MGINFO(m_description << " rpc server initialized OK on port: " << m_server.get_binded_port());
} }
void run() void run()
{ {
MGINFO("Starting core rpc server..."); MGINFO("Starting " << m_description << " rpc server...");
if (!m_server.run(2, false)) if (!m_server.run(2, false))
{ {
throw std::runtime_error("Failed to start core rpc server."); throw std::runtime_error("Failed to start " + m_description + " rpc server.");
} }
MGINFO("Core rpc server started ok"); MGINFO(m_description << " rpc server started ok");
} }
void stop() void stop()
{ {
MGINFO("Stopping core rpc server..."); MGINFO("Stopping " << m_description << " rpc server...");
m_server.send_stop_signal(); m_server.send_stop_signal();
m_server.timed_wait_server_stop(5000); m_server.timed_wait_server_stop(5000);
} }
@ -87,11 +93,11 @@ public:
~t_rpc() ~t_rpc()
{ {
MGINFO("Deinitializing rpc server..."); MGINFO("Deinitializing " << m_description << " rpc server...");
try { try {
m_server.deinit(); m_server.deinit();
} catch (...) { } catch (...) {
MERROR("Failed to deinitialize rpc server..."); MERROR("Failed to deinitialize " << m_description << " rpc server...");
} }
} }
}; };

View File

@ -68,7 +68,9 @@ namespace cryptonote
void core_rpc_server::init_options(boost::program_options::options_description& desc) void core_rpc_server::init_options(boost::program_options::options_description& desc)
{ {
command_line::add_arg(desc, arg_rpc_bind_port); command_line::add_arg(desc, arg_rpc_bind_port);
command_line::add_arg(desc, arg_rpc_restricted_bind_port);
command_line::add_arg(desc, arg_testnet_rpc_bind_port); command_line::add_arg(desc, arg_testnet_rpc_bind_port);
command_line::add_arg(desc, arg_testnet_rpc_restricted_bind_port);
command_line::add_arg(desc, arg_restricted_rpc); command_line::add_arg(desc, arg_restricted_rpc);
cryptonote::rpc_args::init_options(desc); cryptonote::rpc_args::init_options(desc);
} }
@ -83,21 +85,21 @@ namespace cryptonote
//------------------------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server::init( bool core_rpc_server::init(
const boost::program_options::variables_map& vm const boost::program_options::variables_map& vm
, const bool restricted
, const bool testnet
, const std::string& port
) )
{ {
m_testnet = command_line::get_arg(vm, cryptonote::arg_testnet_on); m_restricted = restricted;
m_testnet = testnet;
m_net_server.set_threads_prefix("RPC"); m_net_server.set_threads_prefix("RPC");
auto p2p_bind_arg = m_testnet ? arg_testnet_rpc_bind_port : arg_rpc_bind_port;
auto rpc_config = cryptonote::rpc_args::process(vm); auto rpc_config = cryptonote::rpc_args::process(vm);
if (!rpc_config) if (!rpc_config)
return false; return false;
m_restricted = command_line::get_arg(vm, arg_restricted_rpc);
boost::optional<epee::net_utils::http::login> http_login{}; boost::optional<epee::net_utils::http::login> http_login{};
std::string port = command_line::get_arg(vm, p2p_bind_arg);
if (rpc_config->login) if (rpc_config->login)
http_login.emplace(std::move(rpc_config->login->username), std::move(rpc_config->login->password).password()); http_login.emplace(std::move(rpc_config->login->username), std::move(rpc_config->login->password).password());
@ -1780,12 +1782,24 @@ namespace cryptonote
, std::to_string(config::RPC_DEFAULT_PORT) , std::to_string(config::RPC_DEFAULT_PORT)
}; };
const command_line::arg_descriptor<std::string> core_rpc_server::arg_rpc_restricted_bind_port = {
"rpc-restricted-bind-port"
, "Port for restricted RPC server"
, ""
};
const command_line::arg_descriptor<std::string> core_rpc_server::arg_testnet_rpc_bind_port = { const command_line::arg_descriptor<std::string> core_rpc_server::arg_testnet_rpc_bind_port = {
"testnet-rpc-bind-port" "testnet-rpc-bind-port"
, "Port for testnet RPC server" , "Port for testnet RPC server"
, std::to_string(config::testnet::RPC_DEFAULT_PORT) , std::to_string(config::testnet::RPC_DEFAULT_PORT)
}; };
const command_line::arg_descriptor<std::string> core_rpc_server::arg_testnet_rpc_restricted_bind_port = {
"testnet-rpc-restricted-bind-port"
, "Port for testnet restricted RPC server"
, ""
};
const command_line::arg_descriptor<bool> core_rpc_server::arg_restricted_rpc = { const command_line::arg_descriptor<bool> core_rpc_server::arg_restricted_rpc = {
"restricted-rpc" "restricted-rpc"
, "Restrict RPC to view only commands and do not return privacy sensitive data in RPC calls" , "Restrict RPC to view only commands and do not return privacy sensitive data in RPC calls"

View File

@ -53,7 +53,9 @@ namespace cryptonote
public: public:
static const command_line::arg_descriptor<std::string> arg_rpc_bind_port; static const command_line::arg_descriptor<std::string> arg_rpc_bind_port;
static const command_line::arg_descriptor<std::string> arg_rpc_restricted_bind_port;
static const command_line::arg_descriptor<std::string> arg_testnet_rpc_bind_port; static const command_line::arg_descriptor<std::string> arg_testnet_rpc_bind_port;
static const command_line::arg_descriptor<std::string> arg_testnet_rpc_restricted_bind_port;
static const command_line::arg_descriptor<bool> arg_restricted_rpc; static const command_line::arg_descriptor<bool> arg_restricted_rpc;
typedef epee::net_utils::connection_context_base connection_context; typedef epee::net_utils::connection_context_base connection_context;
@ -65,7 +67,10 @@ namespace cryptonote
static void init_options(boost::program_options::options_description& desc); static void init_options(boost::program_options::options_description& desc);
bool init( bool init(
const boost::program_options::variables_map& vm const boost::program_options::variables_map& vm,
const bool restricted,
const bool testnet,
const std::string& port
); );
bool is_testnet() const { return m_testnet; } bool is_testnet() const { return m_testnet; }