diff --git a/http/announce_test.go b/http/announce_test.go index dc9649e..55edb84 100644 --- a/http/announce_test.go +++ b/http/announce_test.go @@ -31,10 +31,10 @@ func TestPublicAnnounce(t *testing.T) { peer3 := makePeerParams("peer3", false) peer1["event"] = "started" - expected := makeResponse(1, 0) + expected := makeResponse(1, 0, peer1) checkAnnounce(peer1, expected, srv, t) - expected = makeResponse(2, 0) + expected = makeResponse(2, 0, peer2) checkAnnounce(peer2, expected, srv, t) expected = makeResponse(2, 1, peer1, peer2) @@ -147,7 +147,7 @@ func TestPrivateAnnounce(t *testing.T) { peer2 := makePeerParams("-TR2820-peer2", false) peer3 := makePeerParams("-TR2820-peer3", true) - expected := makeResponse(0, 1) + expected := makeResponse(0, 1, peer1) srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1" checkAnnounce(peer1, expected, srv, t) @@ -189,7 +189,7 @@ func TestPreferredSubnet(t *testing.T) { peerD1 := makePeerParams("peerD1", false, "fc02::1") peerD2 := makePeerParams("peerD2", false, "fc02::2") - expected := makeResponse(0, 1) + expected := makeResponse(0, 1, peerA1) checkAnnounce(peerA1, expected, srv, t) expected = makeResponse(0, 2, peerA1) @@ -255,7 +255,7 @@ func TestCompactAnnounce(t *testing.T) { peer3["compact"] = "1" expected := makeResponse(0, 1) - expected["peers"] = "" + expected["peers"] = compact checkAnnounce(peer1, expected, srv, t) expected = makeResponse(0, 2) diff --git a/tracker/announce.go b/tracker/announce.go index 68b2bc5..eed040c 100644 --- a/tracker/announce.go +++ b/tracker/announce.go @@ -280,6 +280,10 @@ func newAnnounceResponse(ann *models.Announce) *models.AnnounceResponse { if ann.NumWant > 0 && ann.Event != "stopped" && ann.Event != "paused" { res.IPv4Peers, res.IPv6Peers = getPeers(ann) + + if len(res.IPv4Peers)+len(res.IPv6Peers) == 0 { + models.AppendPeer(&res.IPv4Peers, &res.IPv6Peers, ann, ann.Peer) + } } return res diff --git a/tracker/models/peermap.go b/tracker/models/peermap.go index 278a40f..824f614 100644 --- a/tracker/models/peermap.go +++ b/tracker/models/peermap.go @@ -159,7 +159,7 @@ func (pm *PeerMap) AppendPeers(ipv4s, ipv6s PeerList, ann *Announce, wanted int) } else if peersEquivalent(&peer, ann.Peer) { continue } else { - appendPeer(&ipv4s, &ipv6s, ann, &peer, &count) + count += AppendPeer(&ipv4s, &ipv6s, ann, &peer) } } @@ -174,7 +174,7 @@ func (pm *PeerMap) AppendPeers(ipv4s, ipv6s PeerList, ann *Announce, wanted int) } else if peersEquivalent(&peer, ann.Peer) { continue } else { - appendPeer(&ipv4s, &ipv6s, ann, &peer, &count) + count += AppendPeer(&ipv4s, &ipv6s, ann, &peer) } } } @@ -183,18 +183,20 @@ func (pm *PeerMap) AppendPeers(ipv4s, ipv6s PeerList, ann *Announce, wanted int) return ipv4s, ipv6s } -// appendPeer adds a peer to its corresponding peerlist. -func appendPeer(ipv4s, ipv6s *PeerList, ann *Announce, peer *Peer, count *int) { +// AppendPeer adds a peer to its corresponding peerlist. +func AppendPeer(ipv4s, ipv6s *PeerList, ann *Announce, peer *Peer) int { if ann.HasIPv6() && peer.HasIPv6() { *ipv6s = append(*ipv6s, *peer) - *count++ + return 1 } else if ann.Config.RespectAF && ann.HasIPv4() && peer.HasIPv4() { *ipv4s = append(*ipv4s, *peer) - *count++ + return 1 } else if !ann.Config.RespectAF && peer.HasIPv4() { *ipv4s = append(*ipv4s, *peer) - *count++ + return 1 } + + return 0 } // peersEquivalent checks if two peers represent the same entity.