From 905cc07c8b6bf8d30dab5eeb56ac384da308d37a Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Sun, 13 Dec 2020 16:59:08 +0000 Subject: daemon: the ban command can now load IPs from a file (ban @filename) --- src/daemon/command_parser_executor.cpp | 42 ++++++++++++++++++++++++++++++++-- src/daemon/command_server.cpp | 4 ++-- 2 files changed, 42 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp index 504b104b0..6695c3ecd 100644 --- a/src/daemon/command_parser_executor.cpp +++ b/src/daemon/command_parser_executor.cpp @@ -28,6 +28,7 @@ #include "common/dns_utils.h" #include "common/command_line.h" +#include "net/parse.h" #include "daemon/command_parser_executor.h" #undef MONERO_DEFAULT_LOG_CATEGORY @@ -585,7 +586,6 @@ bool t_command_parser_executor::show_bans(const std::vector& args) bool t_command_parser_executor::ban(const std::vector& 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) { @@ -602,7 +602,45 @@ bool t_command_parser_executor::ban(const std::vector& args) return false; } } - return m_executor.ban(ip, seconds); + if (boost::starts_with(args[0], "@")) + { + const std::string ban_list = args[0].substr(1); + + try + { + const boost::filesystem::path ban_list_path(ban_list); + boost::system::error_code ec; + if (!boost::filesystem::exists(ban_list_path, ec)) + { + std::cout << "Can't find ban list file " + ban_list + " - " + ec.message() << std::endl; + return true; + } + + bool ret = true; + std::ifstream ifs(ban_list_path.string()); + for (std::string line; std::getline(ifs, line); ) + { + const expect parsed_addr = net::get_network_address(line, 0); + if (!parsed_addr) + { + std::cout << "Invalid IP address: " << line << " - " << parsed_addr.error() << std::endl; + continue; + } + ret &= m_executor.ban(parsed_addr->host_str(), seconds); + } + return ret; + } + catch (const std::exception &e) + { + std::cout << "Error loading ban list: " << e.what() << std::endl; + return false; + } + } + else + { + const std::string ip = args[0]; + return m_executor.ban(ip, seconds); + } } bool t_command_parser_executor::unban(const std::vector& args) diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp index ac4c30726..46d96bda4 100644 --- a/src/daemon/command_server.cpp +++ b/src/daemon/command_server.cpp @@ -233,8 +233,8 @@ t_command_server::t_command_server( m_command_lookup.set_handler( "ban" , std::bind(&t_command_parser_executor::ban, &m_parser, p::_1) - , "ban []" - , "Ban a given for a given amount of ." + , "ban [|@] []" + , "Ban a given or list of IPs from a file for a given amount of ." ); m_command_lookup.set_handler( "unban" -- cgit v1.2.3