torrust-tracker icon indicating copy to clipboard operation
torrust-tracker copied to clipboard

Review IP assigned to localhost peers

Open josecelano opened this issue 7 months ago • 0 comments

If you run the tracker locally with the IP 127.0.0.1 and you leave the external IP config option (core.net.external_ip) with the default value 0.0.0.0, the peers in the announce response will have the wildcard IP 0.0.0.0.

Tracker congi (only relevant configuration for this issue):

{
  "core": {
    "net": {
      "external_ip": "0.0.0.0",
      "on_reverse_proxy": false
    }
  }
}   

If the ' external_ip ' is the wildcard IP, I think it would be better to return the original remote peer IP (loopback IP). Alternatively, we could set the 127.0.0.1 IP as the default for the external_ip.

I would do the second option because:

  1. It makes no sense to use 0.0.0.0 as a default IP.
  2. By using the 127.0.0.1 at least you can contact the tracker locally.

Example:

cargo run -p torrust-tracker-client --bin http_tracker_client announce http://127.0.0.1:7070 443c7602b4fde83d1154d6d9da48808418b181b6 | jq
{
  "complete": 2,
  "incomplete": 0,
  "interval": 120,
  "min interval": 120,
  "peers": [
    {
      "ip": "0.0.0.0",
      "peer id": [
        45,
        113,
        66,
        48,
        48,
        48,
        48,
        48,
        48,
        48,
        48,
        48,
        48,
        48,
        48,
        48,
        48,
        48,
        48,
        49
      ],
      "port": 48273
    }
  ]
}
cargo run -p torrust-tracker-client --bin udp_tracker_client announce udp://127.0.0.1:6969 443c7602b4fde83d1154d6d9da48808418b181b6 | jq
{
  "AnnounceIpv4": {
    "transaction_id": -888840697,
    "announce_interval": 120,
    "leechers": 0,
    "seeders": 3,
    "peers": [
      "0.0.0.0:17548",
      "0.0.0.0:48273"
    ]
  }
}

This is the code:

/// Assigns the correct IP address to a peer based on tracker settings.
///
/// If the client IP is a loopback address and the tracker has an external IP
/// configured, the external IP will be assigned to the peer.
#[must_use]
fn assign_ip_address_to_peer(remote_client_ip: &IpAddr, tracker_external_ip: Option<IpAddr>) -> IpAddr {
    if let Some(host_ip) = tracker_external_ip.filter(|_| remote_client_ip.is_loopback()) {
        host_ip
    } else {
        *remote_client_ip
    }
}

cc @da2ce7

josecelano avatar May 07 '25 17:05 josecelano