diff --git a/CMakeLists.txt b/CMakeLists.txt index 12f16ec..a1dee24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required (VERSION 3.24) -set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD 23) project(zit CXX) option(USE_CLANG_TIDY "Use clang-tidy when building" ON) diff --git a/src/net.cpp b/src/net.cpp index 79eb9bd..28ced88 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -69,6 +69,28 @@ namespace ssl = asio::ssl; namespace zit { +vector get_host_ip_addresses() { + vector ip_addresses; + asio::io_context io_context; + asio::ip::tcp::resolver resolver(io_context); + asio::ip::tcp::resolver::query query(asio::ip::host_name(), ""); + asio::ip::tcp::resolver::iterator it = resolver.resolve(query); + + while (it != asio::ip::tcp::resolver::iterator()) { + asio::ip::tcp::endpoint endpoint = *it++; + ip_addresses.push_back(endpoint.address().to_string()); + } + + if (logger()->should_log(spdlog::level::debug)) { + logger()->debug("IP addresses for host '{}':", asio::ip::host_name()); + for (const auto& ip : ip_addresses) { + logger()->debug(" {}", ip); + } + } + + return ip_addresses; +} + /** * When using asio::read_until we need to match multiple markers to handle non * standard responses. diff --git a/src/net.hpp b/src/net.hpp index 573d0fb..17b08fc 100644 --- a/src/net.hpp +++ b/src/net.hpp @@ -16,6 +16,11 @@ namespace zit { using string_list = std::vector; using namespace std::string_literals; +/** + * Get a list of all known IPs representing localhost. + */ +std::vector get_host_ip_addresses(); + class HttpException : public std::runtime_error { public: HttpException(const std::string& what_arg, unsigned int status_code) diff --git a/src/torrent.cpp b/src/torrent.cpp index 0372cc3..6f77585 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -865,10 +865,14 @@ std::vector> Torrent::tracker_request( // To avoid connecting to our own listening peer const auto is_local_peer = [&](const auto& peer) { const auto& url = peer->url(); - // FIXME: Get hold of the ip automatically? - return url && - (url->host() == "localhost" || url->host() == "172.17.0.1" || - url->host() == "192.168.0.18") && + static const auto local_ips = [] { + auto resolved = get_host_ip_addresses(); + resolved.emplace_back("localhost"); + static auto docker_ip{"172.17.0.1"}; + resolved.emplace_back(docker_ip); + return resolved; + }(); + return url && ranges::contains(local_ips, url->host()) && url->port().value_or(0) == m_listening_port.get(); }; diff --git a/tests/test_integrate.cpp b/tests/test_integrate.cpp index 07c51de..9bb0b48 100644 --- a/tests/test_integrate.cpp +++ b/tests/test_integrate.cpp @@ -104,7 +104,9 @@ auto start_seeder(const fs::path& data_dir, // Needed to be able to run iptables in the container "--cap-add", "NET_ADMIN", // For verbose output - "--env", "DEBUG=*", + "--env", + fmt::format("DEBUG={}", + logger()->should_log(spdlog::level::debug) ? "*" : ""), // "webtorrent", "seed", // Torrent file diff --git a/tests/test_net.cpp b/tests/test_net.cpp index b79b514..116c47d 100644 --- a/tests/test_net.cpp +++ b/tests/test_net.cpp @@ -20,6 +20,11 @@ using ::testing::ElementsAreArray; using namespace zit; using namespace std; +TEST(net, get_host_ip_addresses) { + auto ip_addresses = get_host_ip_addresses(); + EXPECT_FALSE(ip_addresses.empty()); +} + TEST(net, url_encode) { string input = "\x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45"