Skip to content

Commit

Permalink
Remove local ip hardcode
Browse files Browse the repository at this point in the history
  • Loading branch information
Zitrax committed Dec 13, 2024
1 parent b5e2212 commit 2759713
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
22 changes: 22 additions & 0 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ namespace ssl = asio::ssl;

namespace zit {

vector<string> get_host_ip_addresses() {
vector<string> 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.
Expand Down
5 changes: 5 additions & 0 deletions src/net.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ namespace zit {
using string_list = std::vector<std::string>;
using namespace std::string_literals;

/**
* Get a list of all known IPs representing localhost.
*/
std::vector<std::string> get_host_ip_addresses();

class HttpException : public std::runtime_error {
public:
HttpException(const std::string& what_arg, unsigned int status_code)
Expand Down
12 changes: 8 additions & 4 deletions src/torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,10 +865,14 @@ std::vector<std::shared_ptr<Peer>> 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();
};

Expand Down
4 changes: 3 additions & 1 deletion tests/test_integrate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/test_net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 2759713

Please sign in to comment.