From d4d2b5c79a04858cd47ceae58401dacf8fb59836 Mon Sep 17 00:00:00 2001 From: xiphon Date: Sun, 13 Oct 2019 13:27:46 +0000 Subject: p2p+rpc: don't skip p2p or rpc port bind failure by default --- src/p2p/net_node.cpp | 2 +- src/p2p/net_node.h | 2 +- src/p2p/net_node.inl | 4 +- src/rpc/rpc_args.cpp | 6 +- src/rpc/rpc_args.h | 2 +- tests/unit_tests/CMakeLists.txt | 2 +- tests/unit_tests/ban.cpp | 264 ---------------------------------- tests/unit_tests/node_server.cpp | 299 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 308 insertions(+), 273 deletions(-) delete mode 100644 tests/unit_tests/ban.cpp create mode 100644 tests/unit_tests/node_server.cpp diff --git a/src/p2p/net_node.cpp b/src/p2p/net_node.cpp index c7fc058ca..ae23bb7fd 100644 --- a/src/p2p/net_node.cpp +++ b/src/p2p/net_node.cpp @@ -152,7 +152,7 @@ namespace nodetool const command_line::arg_descriptor arg_no_igd = {"no-igd", "Disable UPnP port mapping"}; const command_line::arg_descriptor arg_igd = {"igd", "UPnP port mapping (disabled, enabled, delayed)", "delayed"}; const command_line::arg_descriptor arg_p2p_use_ipv6 = {"p2p-use-ipv6", "Enable IPv6 for p2p", false}; - const command_line::arg_descriptor arg_p2p_require_ipv4 = {"p2p-require-ipv4", "Require successful IPv4 bind for p2p", true}; + const command_line::arg_descriptor arg_p2p_ignore_ipv4 = {"p2p-ignore-ipv4", "Ignore unsuccessful IPv4 bind for p2p", false}; const command_line::arg_descriptor arg_out_peers = {"out-peers", "set max number of out peers", -1}; const command_line::arg_descriptor arg_in_peers = {"in-peers", "set max number of in peers", -1}; const command_line::arg_descriptor arg_tos_flag = {"tos-flag", "set TOS flag", -1}; diff --git a/src/p2p/net_node.h b/src/p2p/net_node.h index d7e2e91f5..fa8ec59ac 100644 --- a/src/p2p/net_node.h +++ b/src/p2p/net_node.h @@ -510,7 +510,7 @@ namespace nodetool extern const command_line::arg_descriptor arg_p2p_bind_port; extern const command_line::arg_descriptor arg_p2p_bind_port_ipv6; extern const command_line::arg_descriptor arg_p2p_use_ipv6; - extern const command_line::arg_descriptor arg_p2p_require_ipv4; + extern const command_line::arg_descriptor arg_p2p_ignore_ipv4; extern const command_line::arg_descriptor arg_p2p_external_port; extern const command_line::arg_descriptor arg_p2p_allow_local_ip; extern const command_line::arg_descriptor > arg_p2p_add_peer; diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index bb77ea658..774a59cad 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -97,7 +97,7 @@ namespace nodetool command_line::add_arg(desc, arg_p2p_bind_port, false); command_line::add_arg(desc, arg_p2p_bind_port_ipv6, false); command_line::add_arg(desc, arg_p2p_use_ipv6); - command_line::add_arg(desc, arg_p2p_require_ipv4); + command_line::add_arg(desc, arg_p2p_ignore_ipv4); command_line::add_arg(desc, arg_p2p_external_port); command_line::add_arg(desc, arg_p2p_allow_local_ip); command_line::add_arg(desc, arg_p2p_add_peer); @@ -382,7 +382,7 @@ namespace nodetool } m_offline = command_line::get_arg(vm, cryptonote::arg_offline); m_use_ipv6 = command_line::get_arg(vm, arg_p2p_use_ipv6); - m_require_ipv4 = command_line::get_arg(vm, arg_p2p_require_ipv4); + m_require_ipv4 = !command_line::get_arg(vm, arg_p2p_ignore_ipv4); public_zone.m_notifier = cryptonote::levin::notify{ public_zone.m_net_server.get_io_service(), public_zone.m_net_server.get_config_shared(), nullptr, true }; diff --git a/src/rpc/rpc_args.cpp b/src/rpc/rpc_args.cpp index 68b33cb8c..0eaa0ef0e 100644 --- a/src/rpc/rpc_args.cpp +++ b/src/rpc/rpc_args.cpp @@ -92,7 +92,7 @@ namespace cryptonote : rpc_bind_ip({"rpc-bind-ip", rpc_args::tr("Specify IP to bind RPC server"), "127.0.0.1"}) , rpc_bind_ipv6_address({"rpc-bind-ipv6-address", rpc_args::tr("Specify IPv6 address to bind RPC server"), "::1"}) , rpc_use_ipv6({"rpc-use-ipv6", rpc_args::tr("Allow IPv6 for RPC"), false}) - , rpc_require_ipv4({"rpc-require-ipv4", rpc_args::tr("Require successful IPv4 bind for RPC"), true}) + , rpc_ignore_ipv4({"rpc-ignore-ipv4", rpc_args::tr("Ignore unsuccessful IPv4 bind for RPC"), false}) , rpc_login({"rpc-login", rpc_args::tr("Specify username[:password] required for RPC server"), "", true}) , confirm_external_bind({"confirm-external-bind", rpc_args::tr("Confirm rpc-bind-ip value is NOT a loopback (local) IP")}) , rpc_access_control_origins({"rpc-access-control-origins", rpc_args::tr("Specify a comma separated list of origins to allow cross origin resource sharing"), ""}) @@ -113,7 +113,7 @@ namespace cryptonote command_line::add_arg(desc, arg.rpc_bind_ip); command_line::add_arg(desc, arg.rpc_bind_ipv6_address); command_line::add_arg(desc, arg.rpc_use_ipv6); - command_line::add_arg(desc, arg.rpc_require_ipv4); + command_line::add_arg(desc, arg.rpc_ignore_ipv4); command_line::add_arg(desc, arg.rpc_login); command_line::add_arg(desc, arg.confirm_external_bind); command_line::add_arg(desc, arg.rpc_access_control_origins); @@ -135,7 +135,7 @@ namespace cryptonote config.bind_ip = command_line::get_arg(vm, arg.rpc_bind_ip); config.bind_ipv6_address = command_line::get_arg(vm, arg.rpc_bind_ipv6_address); config.use_ipv6 = command_line::get_arg(vm, arg.rpc_use_ipv6); - config.require_ipv4 = command_line::get_arg(vm, arg.rpc_require_ipv4); + config.require_ipv4 = !command_line::get_arg(vm, arg.rpc_ignore_ipv4); if (!config.bind_ip.empty()) { // always parse IP here for error consistency diff --git a/src/rpc/rpc_args.h b/src/rpc/rpc_args.h index cd154a4d0..bdb9c70d5 100644 --- a/src/rpc/rpc_args.h +++ b/src/rpc/rpc_args.h @@ -54,7 +54,7 @@ namespace cryptonote const command_line::arg_descriptor rpc_bind_ip; const command_line::arg_descriptor rpc_bind_ipv6_address; const command_line::arg_descriptor rpc_use_ipv6; - const command_line::arg_descriptor rpc_require_ipv4; + const command_line::arg_descriptor rpc_ignore_ipv4; const command_line::arg_descriptor rpc_login; const command_line::arg_descriptor confirm_external_bind; const command_line::arg_descriptor rpc_access_control_origins; diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index cac1fa943..96825f54f 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -30,7 +30,6 @@ set(unit_tests_sources account.cpp apply_permutation.cpp address_from_url.cpp - ban.cpp base58.cpp blockchain_db.cpp block_queue.cpp @@ -68,6 +67,7 @@ set(unit_tests_sources multiexp.cpp multisig.cpp net.cpp + node_server.cpp notify.cpp output_distribution.cpp parse_amount.cpp diff --git a/tests/unit_tests/ban.cpp b/tests/unit_tests/ban.cpp deleted file mode 100644 index 86d546387..000000000 --- a/tests/unit_tests/ban.cpp +++ /dev/null @@ -1,264 +0,0 @@ -// Copyright (c) 2014-2019, The Monero Project -// -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without modification, are -// permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, this list of -// conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright notice, this list -// of conditions and the following disclaimer in the documentation and/or other -// materials provided with the distribution. -// -// 3. Neither the name of the copyright holder nor the names of its contributors may be -// used to endorse or promote products derived from this software without specific -// prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY -// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers - -#include "gtest/gtest.h" -#include "cryptonote_core/cryptonote_core.h" -#include "p2p/net_node.h" -#include "p2p/net_node.inl" -#include "cryptonote_protocol/cryptonote_protocol_handler.h" -#include "cryptonote_protocol/cryptonote_protocol_handler.inl" - -#define MAKE_IPV4_ADDRESS(a,b,c,d) epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),0} -#define MAKE_IPV4_ADDRESS_PORT(a,b,c,d,e) epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),e} -#define MAKE_IPV4_SUBNET(a,b,c,d,e) epee::net_utils::ipv4_network_subnet{MAKE_IP(a,b,c,d),e} - -namespace cryptonote { - class blockchain_storage; -} - -class test_core -{ -public: - void on_synchronized(){} - void safesyncmode(const bool){} - uint64_t get_current_blockchain_height() const {return 1;} - void set_target_blockchain_height(uint64_t) {} - bool init(const boost::program_options::variables_map& vm) {return true ;} - bool deinit(){return true;} - bool get_short_chain_history(std::list& ids) const { return true; } - bool get_stat_info(cryptonote::core_stat_info& st_inf) const {return true;} - bool have_block(const crypto::hash& id) const {return true;} - void get_blockchain_top(uint64_t& height, crypto::hash& top_id)const{height=0;top_id=crypto::null_hash;} - bool handle_incoming_tx(const cryptonote::tx_blob_entry& tx_blob, cryptonote::tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay) { return true; } - bool handle_incoming_txs(const std::vector& tx_blob, std::vector& tvc, bool keeped_by_block, bool relayed, bool do_not_relay) { return true; } - bool handle_incoming_block(const cryptonote::blobdata& block_blob, const cryptonote::block *block, cryptonote::block_verification_context& bvc, bool update_miner_blocktemplate = true) { return true; } - void pause_mine(){} - void resume_mine(){} - bool on_idle(){return true;} - bool find_blockchain_supplement(const std::list& qblock_ids, bool clip_pruned, cryptonote::NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp){return true;} - bool handle_get_objects(cryptonote::NOTIFY_REQUEST_GET_OBJECTS::request& arg, cryptonote::NOTIFY_RESPONSE_GET_OBJECTS::request& rsp, cryptonote::cryptonote_connection_context& context){return true;} - cryptonote::blockchain_storage &get_blockchain_storage() { throw std::runtime_error("Called invalid member function: please never call get_blockchain_storage on the TESTING class test_core."); } - bool get_test_drop_download() const {return true;} - bool get_test_drop_download_height() const {return true;} - bool prepare_handle_incoming_blocks(const std::vector &blocks_entry, std::vector &blocks) { return true; } - bool cleanup_handle_incoming_blocks(bool force_sync = false) { return true; } - uint64_t get_target_blockchain_height() const { return 1; } - size_t get_block_sync_size(uint64_t height) const { return BLOCKS_SYNCHRONIZING_DEFAULT_COUNT; } - virtual void on_transaction_relayed(const cryptonote::blobdata& tx) {} - cryptonote::network_type get_nettype() const { return cryptonote::MAINNET; } - bool get_pool_transaction(const crypto::hash& id, cryptonote::blobdata& tx_blob) const { return false; } - bool pool_has_tx(const crypto::hash &txid) const { return false; } - bool get_blocks(uint64_t start_offset, size_t count, std::vector>& blocks, std::vector& txs) const { return false; } - bool get_transactions(const std::vector& txs_ids, std::vector& txs, std::vector& missed_txs) const { return false; } - bool get_block_by_hash(const crypto::hash &h, cryptonote::block &blk, bool *orphan = NULL) const { return false; } - uint8_t get_ideal_hard_fork_version() const { return 0; } - uint8_t get_ideal_hard_fork_version(uint64_t height) const { return 0; } - uint8_t get_hard_fork_version(uint64_t height) const { return 0; } - uint64_t get_earliest_ideal_height_for_version(uint8_t version) const { return 0; } - cryptonote::difficulty_type get_block_cumulative_difficulty(uint64_t height) const { return 0; } - bool fluffy_blocks_enabled() const { return false; } - uint64_t prevalidate_block_hashes(uint64_t height, const std::vector &hashes, const std::vector &weights) { return 0; } - bool pad_transactions() { return false; } - uint32_t get_blockchain_pruning_seed() const { return 0; } - bool prune_blockchain(uint32_t pruning_seed = 0) { return true; } - bool is_within_compiled_block_hash_area(uint64_t height) const { return false; } - bool has_block_weights(uint64_t height, uint64_t nblocks) const { return false; } - void stop() {} -}; - -typedef nodetool::node_server> Server; - -static bool is_blocked(Server &server, const epee::net_utils::network_address &address, time_t *t = NULL) -{ - std::map hosts = server.get_blocked_hosts(); - for (auto rec: hosts) - { - if (rec.first == address.host_str()) - { - if (t) - *t = rec.second; - return true; - } - } - return false; -} - -TEST(ban, add) -{ - test_core pr_core; - cryptonote::t_cryptonote_protocol_handler cprotocol(pr_core, NULL); - Server server(cprotocol); - cprotocol.set_p2p_endpoint(&server); - - // starts empty - ASSERT_TRUE(server.get_blocked_hosts().empty()); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); - - // add an IP - ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_TRUE(server.get_blocked_hosts().size() == 1); - ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); - - // add the same, should not change - ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_TRUE(server.get_blocked_hosts().size() == 1); - ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); - - // remove an unblocked IP, should not change - ASSERT_FALSE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,5))); - ASSERT_TRUE(server.get_blocked_hosts().size() == 1); - ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); - - // remove the IP, ends up empty - ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_TRUE(server.get_blocked_hosts().size() == 0); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); - - // remove the IP from an empty list, still empty - ASSERT_FALSE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_TRUE(server.get_blocked_hosts().size() == 0); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); - - // add two for known amounts of time, they're both blocked - ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 1)); - ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,5), 3)); - ASSERT_TRUE(server.get_blocked_hosts().size() == 2); - ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); - ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,5))); - - // these tests would need to call is_remote_ip_allowed, which is private -#if 0 - // after two seconds, the first IP is unblocked, but not the second yet - sleep(2); - ASSERT_TRUE(server.get_blocked_hosts().size() == 1); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); - - // after two more seconds, the second IP is also unblocked - sleep(2); - ASSERT_TRUE(server.get_blocked_hosts().size() == 0); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); -#endif - - // add an IP again, then re-ban for longer, then shorter - time_t t; - ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 2)); - ASSERT_TRUE(server.get_blocked_hosts().size() == 1); - ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t)); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); - ASSERT_TRUE(t >= 1); - ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 9)); - ASSERT_TRUE(server.get_blocked_hosts().size() == 1); - ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t)); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); - ASSERT_TRUE(t >= 8); - ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 5)); - ASSERT_TRUE(server.get_blocked_hosts().size() == 1); - ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t)); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); - ASSERT_TRUE(t >= 4); -} - -TEST(ban, limit) -{ - test_core pr_core; - cryptonote::t_cryptonote_protocol_handler cprotocol(pr_core, NULL); - Server server(cprotocol); - cprotocol.set_p2p_endpoint(&server); - - // starts empty - ASSERT_TRUE(server.get_blocked_hosts().empty()); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), std::numeric_limits::max() - 1)); - ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); - ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 1)); - ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); -} - -TEST(ban, subnet) -{ - time_t seconds; - test_core pr_core; - cryptonote::t_cryptonote_protocol_handler cprotocol(pr_core, NULL); - Server server(cprotocol); - cprotocol.set_p2p_endpoint(&server); - - ASSERT_TRUE(server.block_subnet(MAKE_IPV4_SUBNET(1,2,3,4,24), 10)); - ASSERT_TRUE(server.get_blocked_subnets().size() == 1); - ASSERT_TRUE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,2,3,4), &seconds)); - ASSERT_TRUE(seconds >= 9); - ASSERT_TRUE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,2,3,255), &seconds)); - ASSERT_TRUE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,2,3,0), &seconds)); - ASSERT_FALSE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,2,4,0), &seconds)); - ASSERT_FALSE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,2,2,0), &seconds)); - ASSERT_TRUE(server.unblock_subnet(MAKE_IPV4_SUBNET(1,2,3,8,24))); - ASSERT_TRUE(server.get_blocked_subnets().size() == 0); - ASSERT_FALSE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,2,3,255), &seconds)); - ASSERT_FALSE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,2,3,0), &seconds)); - ASSERT_TRUE(server.block_subnet(MAKE_IPV4_SUBNET(1,2,3,4,8), 10)); - ASSERT_TRUE(server.get_blocked_subnets().size() == 1); - ASSERT_TRUE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,255,3,255), &seconds)); - ASSERT_TRUE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,0,3,255), &seconds)); - ASSERT_FALSE(server.unblock_subnet(MAKE_IPV4_SUBNET(1,2,3,8,24))); - ASSERT_TRUE(server.get_blocked_subnets().size() == 1); - ASSERT_TRUE(server.block_subnet(MAKE_IPV4_SUBNET(1,2,3,4,8), 10)); - ASSERT_TRUE(server.get_blocked_subnets().size() == 1); - ASSERT_TRUE(server.unblock_subnet(MAKE_IPV4_SUBNET(1,255,0,0,8))); - ASSERT_TRUE(server.get_blocked_subnets().size() == 0); -} - -TEST(ban, ignores_port) -{ - time_t seconds; - test_core pr_core; - cryptonote::t_cryptonote_protocol_handler cprotocol(pr_core, NULL); - Server server(cprotocol); - cprotocol.set_p2p_endpoint(&server); - - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5))); - ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5), std::numeric_limits::max() - 1)); - ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5))); - ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,6))); - ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5))); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5))); - ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,6))); -} - -namespace nodetool { template class node_server>; } -namespace cryptonote { template class t_cryptonote_protocol_handler; } diff --git a/tests/unit_tests/node_server.cpp b/tests/unit_tests/node_server.cpp new file mode 100644 index 000000000..2cf66ccec --- /dev/null +++ b/tests/unit_tests/node_server.cpp @@ -0,0 +1,299 @@ +// Copyright (c) 2014-2019, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + +#include "gtest/gtest.h" +#include "cryptonote_core/cryptonote_core.h" +#include "p2p/net_node.h" +#include "p2p/net_node.inl" +#include "cryptonote_protocol/cryptonote_protocol_handler.h" +#include "cryptonote_protocol/cryptonote_protocol_handler.inl" + +#define MAKE_IPV4_ADDRESS(a,b,c,d) epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),0} +#define MAKE_IPV4_ADDRESS_PORT(a,b,c,d,e) epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),e} +#define MAKE_IPV4_SUBNET(a,b,c,d,e) epee::net_utils::ipv4_network_subnet{MAKE_IP(a,b,c,d),e} + +namespace cryptonote { + class blockchain_storage; +} + +class test_core +{ +public: + void on_synchronized(){} + void safesyncmode(const bool){} + uint64_t get_current_blockchain_height() const {return 1;} + void set_target_blockchain_height(uint64_t) {} + bool init(const boost::program_options::variables_map& vm) {return true ;} + bool deinit(){return true;} + bool get_short_chain_history(std::list& ids) const { return true; } + bool get_stat_info(cryptonote::core_stat_info& st_inf) const {return true;} + bool have_block(const crypto::hash& id) const {return true;} + void get_blockchain_top(uint64_t& height, crypto::hash& top_id)const{height=0;top_id=crypto::null_hash;} + bool handle_incoming_tx(const cryptonote::blobdata& tx_blob, cryptonote::tx_verification_context& tvc, bool keeped_by_block, bool relayed, bool do_not_relay) { return true; } + bool handle_incoming_txs(const std::vector& tx_blob, std::vector& tvc, bool keeped_by_block, bool relayed, bool do_not_relay) { return true; } + bool handle_incoming_block(const cryptonote::blobdata& block_blob, const cryptonote::block *block, cryptonote::block_verification_context& bvc, bool update_miner_blocktemplate = true) { return true; } + void pause_mine(){} + void resume_mine(){} + bool on_idle(){return true;} + bool find_blockchain_supplement(const std::list& qblock_ids, cryptonote::NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp){return true;} + bool handle_get_objects(cryptonote::NOTIFY_REQUEST_GET_OBJECTS::request& arg, cryptonote::NOTIFY_RESPONSE_GET_OBJECTS::request& rsp, cryptonote::cryptonote_connection_context& context){return true;} + cryptonote::blockchain_storage &get_blockchain_storage() { throw std::runtime_error("Called invalid member function: please never call get_blockchain_storage on the TESTING class test_core."); } + bool get_test_drop_download() const {return true;} + bool get_test_drop_download_height() const {return true;} + bool prepare_handle_incoming_blocks(const std::vector &blocks_entry, std::vector &blocks) { return true; } + bool cleanup_handle_incoming_blocks(bool force_sync = false) { return true; } + uint64_t get_target_blockchain_height() const { return 1; } + size_t get_block_sync_size(uint64_t height) const { return BLOCKS_SYNCHRONIZING_DEFAULT_COUNT; } + virtual void on_transaction_relayed(const cryptonote::blobdata& tx) {} + cryptonote::network_type get_nettype() const { return cryptonote::MAINNET; } + bool get_pool_transaction(const crypto::hash& id, cryptonote::blobdata& tx_blob) const { return false; } + bool pool_has_tx(const crypto::hash &txid) const { return false; } + bool get_blocks(uint64_t start_offset, size_t count, std::vector>& blocks, std::vector& txs) const { return false; } + bool get_transactions(const std::vector& txs_ids, std::vector& txs, std::vector& missed_txs) const { return false; } + bool get_block_by_hash(const crypto::hash &h, cryptonote::block &blk, bool *orphan = NULL) const { return false; } + uint8_t get_ideal_hard_fork_version() const { return 0; } + uint8_t get_ideal_hard_fork_version(uint64_t height) const { return 0; } + uint8_t get_hard_fork_version(uint64_t height) const { return 0; } + uint64_t get_earliest_ideal_height_for_version(uint8_t version) const { return 0; } + cryptonote::difficulty_type get_block_cumulative_difficulty(uint64_t height) const { return 0; } + bool fluffy_blocks_enabled() const { return false; } + uint64_t prevalidate_block_hashes(uint64_t height, const std::vector &hashes) { return 0; } + bool pad_transactions() { return false; } + uint32_t get_blockchain_pruning_seed() const { return 0; } + bool prune_blockchain(uint32_t pruning_seed = 0) { return true; } + void stop() {} +}; + +typedef nodetool::node_server> Server; + +static bool is_blocked(Server &server, const epee::net_utils::network_address &address, time_t *t = NULL) +{ + std::map hosts = server.get_blocked_hosts(); + for (auto rec: hosts) + { + if (rec.first == address.host_str()) + { + if (t) + *t = rec.second; + return true; + } + } + return false; +} + +TEST(ban, add) +{ + test_core pr_core; + cryptonote::t_cryptonote_protocol_handler cprotocol(pr_core, NULL); + Server server(cprotocol); + cprotocol.set_p2p_endpoint(&server); + + // starts empty + ASSERT_TRUE(server.get_blocked_hosts().empty()); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); + + // add an IP + ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_TRUE(server.get_blocked_hosts().size() == 1); + ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); + + // add the same, should not change + ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_TRUE(server.get_blocked_hosts().size() == 1); + ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); + + // remove an unblocked IP, should not change + ASSERT_FALSE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,5))); + ASSERT_TRUE(server.get_blocked_hosts().size() == 1); + ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); + + // remove the IP, ends up empty + ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_TRUE(server.get_blocked_hosts().size() == 0); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); + + // remove the IP from an empty list, still empty + ASSERT_FALSE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_TRUE(server.get_blocked_hosts().size() == 0); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); + + // add two for known amounts of time, they're both blocked + ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 1)); + ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,5), 3)); + ASSERT_TRUE(server.get_blocked_hosts().size() == 2); + ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); + ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS(1,2,3,5))); + + // these tests would need to call is_remote_ip_allowed, which is private +#if 0 + // after two seconds, the first IP is unblocked, but not the second yet + sleep(2); + ASSERT_TRUE(server.get_blocked_hosts().size() == 1); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); + + // after two more seconds, the second IP is also unblocked + sleep(2); + ASSERT_TRUE(server.get_blocked_hosts().size() == 0); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); +#endif + + // add an IP again, then re-ban for longer, then shorter + time_t t; + ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 2)); + ASSERT_TRUE(server.get_blocked_hosts().size() == 1); + ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t)); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); + ASSERT_TRUE(t >= 1); + ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 9)); + ASSERT_TRUE(server.get_blocked_hosts().size() == 1); + ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t)); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); + ASSERT_TRUE(t >= 8); + ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 5)); + ASSERT_TRUE(server.get_blocked_hosts().size() == 1); + ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4), &t)); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,5))); + ASSERT_TRUE(t >= 4); +} + +TEST(ban, limit) +{ + test_core pr_core; + cryptonote::t_cryptonote_protocol_handler cprotocol(pr_core, NULL); + Server server(cprotocol); + cprotocol.set_p2p_endpoint(&server); + + // starts empty + ASSERT_TRUE(server.get_blocked_hosts().empty()); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), std::numeric_limits::max() - 1)); + ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); + ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS(1,2,3,4), 1)); + ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS(1,2,3,4))); +} + +TEST(ban, subnet) +{ + time_t seconds; + test_core pr_core; + cryptonote::t_cryptonote_protocol_handler cprotocol(pr_core, NULL); + Server server(cprotocol); + cprotocol.set_p2p_endpoint(&server); + + ASSERT_TRUE(server.block_subnet(MAKE_IPV4_SUBNET(1,2,3,4,24), 10)); + ASSERT_TRUE(server.get_blocked_subnets().size() == 1); + ASSERT_TRUE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,2,3,4), &seconds)); + ASSERT_TRUE(seconds >= 9); + ASSERT_TRUE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,2,3,255), &seconds)); + ASSERT_TRUE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,2,3,0), &seconds)); + ASSERT_FALSE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,2,4,0), &seconds)); + ASSERT_FALSE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,2,2,0), &seconds)); + ASSERT_TRUE(server.unblock_subnet(MAKE_IPV4_SUBNET(1,2,3,8,24))); + ASSERT_TRUE(server.get_blocked_subnets().size() == 0); + ASSERT_FALSE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,2,3,255), &seconds)); + ASSERT_FALSE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,2,3,0), &seconds)); + ASSERT_TRUE(server.block_subnet(MAKE_IPV4_SUBNET(1,2,3,4,8), 10)); + ASSERT_TRUE(server.get_blocked_subnets().size() == 1); + ASSERT_TRUE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,255,3,255), &seconds)); + ASSERT_TRUE(server.is_host_blocked(MAKE_IPV4_ADDRESS(1,0,3,255), &seconds)); + ASSERT_FALSE(server.unblock_subnet(MAKE_IPV4_SUBNET(1,2,3,8,24))); + ASSERT_TRUE(server.get_blocked_subnets().size() == 1); + ASSERT_TRUE(server.block_subnet(MAKE_IPV4_SUBNET(1,2,3,4,8), 10)); + ASSERT_TRUE(server.get_blocked_subnets().size() == 1); + ASSERT_TRUE(server.unblock_subnet(MAKE_IPV4_SUBNET(1,255,0,0,8))); + ASSERT_TRUE(server.get_blocked_subnets().size() == 0); +} + +TEST(ban, ignores_port) +{ + time_t seconds; + test_core pr_core; + cryptonote::t_cryptonote_protocol_handler cprotocol(pr_core, NULL); + Server server(cprotocol); + cprotocol.set_p2p_endpoint(&server); + + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5))); + ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5), std::numeric_limits::max() - 1)); + ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5))); + ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,6))); + ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5))); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5))); + ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,6))); +} + +TEST(node_server, bind_same_p2p_port) +{ + const auto new_node = []() -> std::unique_ptr { + test_core pr_core; + cryptonote::t_cryptonote_protocol_handler cprotocol(pr_core, NULL); + std::unique_ptr server(new Server(cprotocol)); + cprotocol.set_p2p_endpoint(server.get()); + + return server; + }; + + const auto init = [](const std::unique_ptr& server, const char* port) -> bool { + boost::program_options::options_description desc_options("Command line options"); + cryptonote::core::init_options(desc_options); + Server::init_options(desc_options); + + const char *argv[2] = {nullptr, nullptr}; + boost::program_options::variables_map vm; + boost::program_options::store(boost::program_options::parse_command_line(1, argv, desc_options), vm); + + vm.find(nodetool::arg_p2p_bind_port.name)->second = boost::program_options::variable_value(std::string(port), false); + + boost::program_options::notify(vm); + + return server->init(vm); + }; + + constexpr char port[] = "48080"; + constexpr char port_another[] = "58080"; + + const auto node = new_node(); + EXPECT_TRUE(init(node, port)); + + EXPECT_FALSE(init(new_node(), port)); + EXPECT_TRUE(init(new_node(), port_another)); +} + +namespace nodetool { template class node_server>; } +namespace cryptonote { template class t_cryptonote_protocol_handler; } -- cgit v1.2.3