add RPC calls and commands to get/set bans
This commit is contained in:
@@ -392,5 +392,34 @@ bool t_command_parser_executor::hard_fork_info(const std::vector<std::string>& a
|
||||
return m_executor.hard_fork_info(version);
|
||||
}
|
||||
|
||||
bool t_command_parser_executor::show_bans(const std::vector<std::string>& args)
|
||||
{
|
||||
if (!args.empty()) return false;
|
||||
return m_executor.print_bans();
|
||||
}
|
||||
|
||||
bool t_command_parser_executor::ban(const std::vector<std::string>& args)
|
||||
{
|
||||
if (args.size() != 1 && args.size() != 2) return false;
|
||||
std::string ip = args[0];
|
||||
time_t seconds = P2P_IP_BLOCKTIME;
|
||||
if (args.size() > 1)
|
||||
{
|
||||
seconds = std::stoi(args[0]);
|
||||
if (seconds == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return m_executor.ban(ip, seconds);
|
||||
}
|
||||
|
||||
bool t_command_parser_executor::unban(const std::vector<std::string>& args)
|
||||
{
|
||||
if (args.size() != 1) return false;
|
||||
std::string ip = args[0];
|
||||
return m_executor.unban(ip);
|
||||
}
|
||||
|
||||
|
||||
} // namespace daemonize
|
||||
|
||||
@@ -106,6 +106,12 @@ public:
|
||||
bool stop_save_graph(const std::vector<std::string>& args);
|
||||
|
||||
bool hard_fork_info(const std::vector<std::string>& args);
|
||||
|
||||
bool show_bans(const std::vector<std::string>& args);
|
||||
|
||||
bool ban(const std::vector<std::string>& args);
|
||||
|
||||
bool unban(const std::vector<std::string>& args);
|
||||
};
|
||||
|
||||
} // namespace daemonize
|
||||
|
||||
@@ -194,6 +194,21 @@ t_command_server::t_command_server(
|
||||
, std::bind(&t_command_parser_executor::hard_fork_info, &m_parser, p::_1)
|
||||
, "Print hard fork voting information"
|
||||
);
|
||||
m_command_lookup.set_handler(
|
||||
"bans"
|
||||
, std::bind(&t_command_parser_executor::show_bans, &m_parser, p::_1)
|
||||
, "Show the currently banned IPs"
|
||||
);
|
||||
m_command_lookup.set_handler(
|
||||
"ban"
|
||||
, std::bind(&t_command_parser_executor::ban, &m_parser, p::_1)
|
||||
, "Ban a given IP for a time"
|
||||
);
|
||||
m_command_lookup.set_handler(
|
||||
"unban"
|
||||
, std::bind(&t_command_parser_executor::unban, &m_parser, p::_1)
|
||||
, "Unban a given IP"
|
||||
);
|
||||
}
|
||||
|
||||
bool t_command_server::process_command_str(const std::string& cmd)
|
||||
|
||||
@@ -1036,4 +1036,110 @@ bool t_rpc_command_executor::hard_fork_info(uint8_t version)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool t_rpc_command_executor::print_bans()
|
||||
{
|
||||
cryptonote::COMMAND_RPC_GETBANS::request req;
|
||||
cryptonote::COMMAND_RPC_GETBANS::response res;
|
||||
std::string fail_message = "Unsuccessful";
|
||||
epee::json_rpc::error error_resp;
|
||||
|
||||
if (m_is_rpc)
|
||||
{
|
||||
if (!m_rpc_client->json_rpc_request(req, res, "get_bans", fail_message.c_str()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!m_rpc_server->on_get_bans(req, res, error_resp))
|
||||
{
|
||||
tools::fail_msg_writer() << fail_message.c_str();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
time_t now = time(nullptr);
|
||||
for (auto i = res.bans.begin(); i != res.bans.end(); ++i)
|
||||
{
|
||||
time_t seconds = i->seconds - now;
|
||||
tools::msg_writer() << epee::string_tools::get_ip_string_from_int32(i->ip) << " banned for " << seconds << " seconds";
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool t_rpc_command_executor::ban(const std::string &ip, time_t seconds)
|
||||
{
|
||||
cryptonote::COMMAND_RPC_SETBANS::request req;
|
||||
cryptonote::COMMAND_RPC_SETBANS::response res;
|
||||
std::string fail_message = "Unsuccessful";
|
||||
epee::json_rpc::error error_resp;
|
||||
|
||||
cryptonote::COMMAND_RPC_SETBANS::ban ban;
|
||||
if (!epee::string_tools::get_ip_int32_from_string(ban.ip, ip))
|
||||
{
|
||||
tools::fail_msg_writer() << "Invalid IP";
|
||||
return true;
|
||||
}
|
||||
ban.ban = true;
|
||||
ban.seconds = seconds;
|
||||
req.bans.push_back(ban);
|
||||
|
||||
if (m_is_rpc)
|
||||
{
|
||||
if (!m_rpc_client->json_rpc_request(req, res, "set_bans", fail_message.c_str()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!m_rpc_server->on_set_bans(req, res, error_resp))
|
||||
{
|
||||
tools::fail_msg_writer() << fail_message.c_str();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool t_rpc_command_executor::unban(const std::string &ip)
|
||||
{
|
||||
cryptonote::COMMAND_RPC_SETBANS::request req;
|
||||
cryptonote::COMMAND_RPC_SETBANS::response res;
|
||||
std::string fail_message = "Unsuccessful";
|
||||
epee::json_rpc::error error_resp;
|
||||
|
||||
cryptonote::COMMAND_RPC_SETBANS::ban ban;
|
||||
if (!epee::string_tools::get_ip_int32_from_string(ban.ip, ip))
|
||||
{
|
||||
tools::fail_msg_writer() << "Invalid IP";
|
||||
return true;
|
||||
}
|
||||
ban.ban = false;
|
||||
ban.seconds = 0;
|
||||
req.bans.push_back(ban);
|
||||
|
||||
if (m_is_rpc)
|
||||
{
|
||||
if (!m_rpc_client->json_rpc_request(req, res, "set_bans", fail_message.c_str()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!m_rpc_server->on_set_bans(req, res, error_resp))
|
||||
{
|
||||
tools::fail_msg_writer() << fail_message.c_str();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}// namespace daemonize
|
||||
|
||||
@@ -124,6 +124,12 @@ public:
|
||||
bool stop_save_graph();
|
||||
|
||||
bool hard_fork_info(uint8_t version);
|
||||
|
||||
bool print_bans();
|
||||
|
||||
bool ban(const std::string &ip, time_t seconds);
|
||||
|
||||
bool unban(const std::string &ip);
|
||||
};
|
||||
|
||||
} // namespace daemonize
|
||||
|
||||
Reference in New Issue
Block a user