Skip to content

Commit 851d35e

Browse files
committed
p2p: allow comments in banlist files
In-line comments explicitly explaining banned hosts/subnets might help assuage fears of some good banlists' arbitaryiness.
1 parent 9866a0e commit 851d35e

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

src/p2p/net_node.inl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,16 @@ namespace nodetool
527527
std::istringstream iss(banned_ips);
528528
for (std::string line; std::getline(iss, line); )
529529
{
530+
// ignore comments after '#' character
531+
const size_t pound_idx = line.find('#');
532+
if (pound_idx != std::string::npos)
533+
line.resize(pound_idx);
534+
535+
// trim whitespace and ignore empty lines
536+
boost::trim_right(line);
537+
if (line.empty())
538+
continue;
539+
530540
auto subnet = net::get_ipv4_subnet_address(line);
531541
if (subnet)
532542
{

tests/data/node/banlist_1.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# magicfolk
2+
255.255.255.0 # Saruman the White
3+
128.128.128.0 # Gandalf the Gray
4+
150.75.0.0 # Radagast the Brown
5+
99.98.0.0/16 # All of Misty Mountain
6+
7+
# personal enemies
8+
1.2.3.4 # this woman used to give me swirlies
9+
6.7.8.9 # I just don't like the cut of his jib
10+
1.0.0.7#Literally James Bond, he wrecked my aston martin
11+
100.98.1.13 # Earl from HOA
12+
100.98.1.0/24 #The rest of the HOA for good measure
13+
#
14+
15+
#7.7.7.7
16+
#^^^We're chill now, she's truly an angel
17+

tests/unit_tests/node_server.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "cryptonote_core/i_core_events.h"
3636
#include "cryptonote_protocol/cryptonote_protocol_handler.h"
3737
#include "cryptonote_protocol/cryptonote_protocol_handler.inl"
38+
#include "unit_tests_utils.h"
3839
#include <condition_variable>
3940

4041
#define MAKE_IPV4_ADDRESS(a,b,c,d) epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),0}
@@ -114,6 +115,18 @@ static bool is_blocked(Server &server, const epee::net_utils::network_address &a
114115
return true;
115116
}
116117
}
118+
119+
if (address.get_type_id() != epee::net_utils::address_type::ipv4)
120+
return false;
121+
122+
const epee::net_utils::ipv4_network_address ipv4_address = address.as<epee::net_utils::ipv4_network_address>();
123+
124+
// check if in a blocked ipv4 subnet
125+
const std::map<epee::net_utils::ipv4_network_subnet, time_t> subnets = server.get_blocked_subnets();
126+
for (const auto &subnet : subnets)
127+
if (subnet.first.matches(ipv4_address))
128+
return true;
129+
117130
return false;
118131
}
119132

@@ -266,6 +279,78 @@ TEST(ban, ignores_port)
266279
ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,6)));
267280
}
268281

282+
TEST(ban, file_banlist)
283+
{
284+
test_core pr_core;
285+
cryptonote::t_cryptonote_protocol_handler<test_core> cprotocol(pr_core, NULL);
286+
Server server(cprotocol);
287+
cprotocol.set_p2p_endpoint(&server);
288+
289+
auto create_node_dir = [](){
290+
boost::system::error_code ec;
291+
auto path = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path("daemon-%%%%%%%%%%%%%%%%", ec);
292+
if (ec)
293+
return boost::filesystem::path{};
294+
auto success = boost::filesystem::create_directory(path, ec);
295+
if (!ec && success)
296+
return path;
297+
return boost::filesystem::path{};
298+
};
299+
const auto node_dir = create_node_dir();
300+
ASSERT_TRUE(!node_dir.empty());
301+
auto auto_remove_node_dir = epee::misc_utils::create_scope_leave_handler([&node_dir](){
302+
boost::filesystem::remove_all(node_dir);
303+
});
304+
305+
boost::program_options::variables_map vm;
306+
boost::program_options::store(
307+
boost::program_options::command_line_parser({
308+
"--data-dir",
309+
node_dir.string(),
310+
"--ban-list",
311+
(unit_test::data_dir / "node" / "banlist_1.txt").string()
312+
}).options([]{
313+
boost::program_options::options_description options_description{};
314+
cryptonote::core::init_options(options_description);
315+
Server::init_options(options_description);
316+
return options_description;
317+
}()).run(),
318+
vm
319+
);
320+
321+
ASSERT_TRUE(server.init(vm));
322+
323+
// Test cases (look in the banlist_1.txt file)
324+
325+
// magicfolk
326+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(255,255,255,0,9999)) );
327+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(128,128,128,0,9999)) );
328+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(150,75,0,0,9999)) );
329+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(99,98,0,0,9999)) );
330+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(99,98,0,255,9999)) );
331+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(99,98,1,0,9999)) );
332+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(99,98,1,0,9999)) );
333+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(99,98,255,255,9999)) );
334+
EXPECT_FALSE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(99,99,0,0,9999)) );
335+
336+
// personal enemies
337+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(1,2,3,4,9999)) );
338+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(6,7,8,9,9999)) );
339+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(1,0,0,7,9999)) );
340+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(1,0,0,7,9999)) );
341+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(100,98,1,13,9999)) );
342+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(100,98,1,0,9999)) );
343+
EXPECT_TRUE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(100,98,1,255,9999)) );
344+
EXPECT_FALSE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(100,98,2,0,9999)) );
345+
EXPECT_FALSE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(100,98,0,255,9999)) );
346+
347+
// angel
348+
EXPECT_FALSE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(007,007,007,007,9999)) );
349+
350+
// random IP
351+
EXPECT_FALSE( is_blocked(server, MAKE_IPV4_ADDRESS_PORT(145,036,205,235,9999)) );
352+
}
353+
269354
TEST(node_server, bind_same_p2p_port)
270355
{
271356
struct test_data_t

0 commit comments

Comments
 (0)