This commit is contained in:
Sean Darcy 2021-11-05 16:19:11 +11:00
parent d476f216cc
commit 6651fdd33a
5 changed files with 30 additions and 50 deletions

View file

@ -1154,16 +1154,16 @@ bool rpc_command_executor::in_peers(bool set, uint32_t limit)
bool rpc_command_executor::print_bans()
{
GETBANS::response res{};
if (!invoke<GETBANS>({}, res, "Failed to retrieve ban list"))
auto maybe_bans = try_running([this] { return invoke<GETBANS>(); }, "Failed to retrieve ban list");
if (!maybe_bans)
return false;
auto bans = *maybe_bans;
if (!res.bans.empty())
if (!bans.empty())
{
for (auto i = res.bans.begin(); i != res.bans.end(); ++i)
for (auto i = bans.begin(); i != bans.end(); ++i)
{
tools::msg_writer() << i->host << " banned for " << i->seconds << " seconds";
tools::msg_writer() << (*i)["host"] << " banned for " << (*i)["seconds"] << " seconds";
}
}
else

View file

@ -1896,10 +1896,8 @@ namespace cryptonote::rpc {
hfinfo.response["status"] = STATUS_OK;
}
//------------------------------------------------------------------------------------------------------------------------------
GETBANS::response core_rpc_server::invoke(GETBANS::request&& req, rpc_context context)
void core_rpc_server::invoke(GETBANS& get_bans, rpc_context context)
{
GETBANS::response res{};
PERF_TIMER(on_get_bans);
auto now = time(nullptr);
@ -1907,30 +1905,30 @@ namespace cryptonote::rpc {
for (std::map<std::string, time_t>::const_iterator i = blocked_hosts.begin(); i != blocked_hosts.end(); ++i)
{
if (i->second > now) {
GETBANS::ban b;
ban b;
b.host = i->first;
b.ip = 0;
uint32_t ip;
if (epee::string_tools::get_ip_int32_from_string(ip, b.host))
b.ip = ip;
b.seconds = i->second - now;
res.bans.push_back(b);
get_bans.response["bans"].push_back(b);
}
}
std::map<epee::net_utils::ipv4_network_subnet, time_t> blocked_subnets = m_p2p.get_blocked_subnets();
for (std::map<epee::net_utils::ipv4_network_subnet, time_t>::const_iterator i = blocked_subnets.begin(); i != blocked_subnets.end(); ++i)
{
if (i->second > now) {
GETBANS::ban b;
ban b;
b.host = i->first.host_str();
b.ip = 0;
b.seconds = i->second - now;
res.bans.push_back(b);
get_bans.response["bans"].push_back(b);
}
}
res.status = STATUS_OK;
return res;
get_bans.response["status"] = STATUS_OK;
return;
}
//------------------------------------------------------------------------------------------------------------------------------
void core_rpc_server::invoke(BANNED& banned, rpc_context context)

View file

@ -246,6 +246,7 @@ namespace cryptonote::rpc {
void invoke(FLUSH_CACHE& flush_cache, rpc_context context);
void invoke(GET_LAST_BLOCK_HEADER& get_last_block_header, rpc_context context);
void invoke(GET_BLOCK_HEADER_BY_HASH& get_block_header_by_hash, rpc_context context);
void invoke(GETBANS& get_bans, rpc_context context);
// Deprecated Monero NIH binary endpoints:
GET_ALT_BLOCKS_HASHES_BIN::response invoke(GET_ALT_BLOCKS_HASHES_BIN::request&& req, rpc_context context);
@ -265,7 +266,6 @@ namespace cryptonote::rpc {
GET_BLOCK_HEADERS_RANGE::response invoke(GET_BLOCK_HEADERS_RANGE::request&& req, rpc_context context);
GET_BLOCK::response invoke(GET_BLOCK::request&& req, rpc_context context);
SETBANS::response invoke(SETBANS::request&& req, rpc_context context);
GETBANS::response invoke(GETBANS::request&& req, rpc_context context);
GET_OUTPUT_HISTOGRAM::response invoke(GET_OUTPUT_HISTOGRAM::request&& req, rpc_context context);
GET_ALTERNATE_CHAINS::response invoke(GET_ALTERNATE_CHAINS::request&& req, rpc_context context);
RELAY_TX::response invoke(RELAY_TX::request&& req, rpc_context context);

View file

@ -156,19 +156,6 @@ KV_SERIALIZE_MAP_CODE_BEGIN(SET_BOOTSTRAP_DAEMON::request)
KV_SERIALIZE_MAP_CODE_END()
KV_SERIALIZE_MAP_CODE_BEGIN(GETBANS::ban)
KV_SERIALIZE(host)
KV_SERIALIZE(ip)
KV_SERIALIZE(seconds)
KV_SERIALIZE_MAP_CODE_END()
KV_SERIALIZE_MAP_CODE_BEGIN(GETBANS::response)
KV_SERIALIZE(status)
KV_SERIALIZE(bans)
KV_SERIALIZE_MAP_CODE_END()
KV_SERIALIZE_MAP_CODE_BEGIN(SETBANS::ban)
KV_SERIALIZE(host)
KV_SERIALIZE(ip)

View file

@ -1237,32 +1237,27 @@ namespace cryptonote::rpc {
} request;
};
OXEN_RPC_DOC_INTROSPECT
// Get list of banned IPs.
/// Get list of banned IPs.
///
/// Inputs: None
///
/// Output values available from a restricted/admin RPC endpoint:
///
/// - \p status General RPC status string. `"OK"` means everything looks good.
/// - \p bans List of banned nodes
struct GETBANS : RPC_COMMAND
{
static constexpr auto names() { return NAMES("get_bans"); }
struct request : EMPTY {};
struct ban
{
std::string host; // Banned host (IP in A.B.C.D form).
uint32_t ip; // Banned IP address, in Int format.
uint32_t seconds; // Local Unix time that IP is banned until.
KV_MAP_SERIALIZABLE
};
struct response
{
std::string status; // General RPC error code. "OK" means everything looks good.
std::vector<ban> bans; // List of banned nodes:
KV_MAP_SERIALIZABLE
};
};
struct ban
{
std::string host; // Banned host (IP in A.B.C.D form).
uint32_t ip; // Banned IP address, in Int format.
uint32_t seconds; // Local Unix time that IP is banned until.
};
inline void to_json(nlohmann::json& j, const ban& b) { j = nlohmann::json{{"host", b.host}, {"ip", b.ip}, {"seconds", b.seconds}}; };
OXEN_RPC_DOC_INTROSPECT
// Ban another node by IP.
struct SETBANS : RPC_COMMAND