9 "github.com/anacrolix/generics"
10 "github.com/anacrolix/log"
12 "github.com/anacrolix/torrent/tracker/udp"
15 // This is reserved for stuff like filtering by IP version, avoiding an announcer's IP or key,
16 // limiting return count, etc.
17 type GetPeersOpts struct{}
19 type InfoHash = [20]byte
21 type PeerInfo struct {
25 type AnnounceAddr = netip.AddrPort
27 type AnnounceTracker interface {
28 TrackAnnounce(ctx context.Context, req udp.AnnounceRequest, addr AnnounceAddr) error
29 Scrape(ctx context.Context, infoHashes []InfoHash) ([]udp.ScrapeInfohashResult, error)
30 GetPeers(ctx context.Context, infoHash InfoHash, opts GetPeersOpts) ([]PeerInfo, error)
33 type AnnounceHandler struct {
34 AnnounceTracker AnnounceTracker
35 UpstreamTrackers []Client
36 UpstreamTrackerUrls []string
37 UpstreamAnnouncePeerId [20]byte
40 // Operations are only removed when all the upstream peers have been tracked.
41 ongoingUpstreamAugmentations map[InfoHash]augmentationOperation
44 type peerSet = map[PeerInfo]struct{}
46 type augmentationOperation struct {
47 // Closed when no more announce responses are pending. finalPeers will contain all the peers
49 doneAnnouncing chan struct{}
50 // This receives the latest peerSet until doneAnnouncing is closed.
52 // This contains the final peerSet after doneAnnouncing is closed.
56 func (me augmentationOperation) getCurPeers() (ret peerSet) {
57 ret, _ = me.getCurPeersAndDone()
61 func (me augmentationOperation) getCurPeersAndDone() (ret peerSet, done bool) {
63 case ret = <-me.curPeers:
64 case <-me.doneAnnouncing:
71 // Adds peers from new that aren't in orig. Modifies both arguments.
72 func addMissing(orig []PeerInfo, new peerSet) {
73 for _, peer := range orig {
76 for peer := range new {
77 orig = append(orig, peer)
81 func (me *AnnounceHandler) Serve(
82 ctx context.Context, req AnnounceRequest, addr AnnounceAddr,
83 ) (peers []PeerInfo, err error) {
84 err = me.AnnounceTracker.TrackAnnounce(ctx, req, addr)
88 infoHash := req.InfoHash
89 var op generics.Option[augmentationOperation]
90 // Grab a handle to any augmentations that are already running.
92 op.Value, op.Ok = me.ongoingUpstreamAugmentations[infoHash]
94 peers, err = me.AnnounceTracker.GetPeers(ctx, infoHash, GetPeersOpts{})
98 // Take whatever peers it has ready. If it's finished, it doesn't matter if we do this inside
101 curPeers, done := op.Value.getCurPeersAndDone()
102 addMissing(peers, curPeers)
104 // It doesn't get any better with this operation. Forget it.
109 // If we didn't have an operation, and don't have enough peers, start one.
110 if !op.Ok && len(peers) <= 1 {
111 op.Value, op.Ok = me.ongoingUpstreamAugmentations[infoHash]
113 op.Set(me.augmentPeersFromUpstream(req.InfoHash))
114 generics.MakeMapIfNilAndSet(&me.ongoingUpstreamAugmentations, infoHash, op.Value)
118 // Wait a while for the current operation.
120 // Force the augmentation to return with whatever it has if it hasn't completed in a
122 ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
125 case <-op.Value.doneAnnouncing:
128 addMissing(peers, op.Value.getCurPeers())
133 func (me *AnnounceHandler) augmentPeersFromUpstream(infoHash [20]byte) augmentationOperation {
134 announceCtx, cancel := context.WithTimeout(context.Background(), time.Minute)
135 subReq := AnnounceRequest{
137 PeerId: me.UpstreamAnnouncePeerId,
143 peersChan := make(chan []Peer)
144 var pendingUpstreams sync.WaitGroup
145 for i := range me.UpstreamTrackers {
146 client := me.UpstreamTrackers[i]
147 url := me.UpstreamTrackerUrls[i]
148 pendingUpstreams.Add(1)
150 resp, err := client.Announce(announceCtx, subReq, AnnounceOpt{
151 UserAgent: "aragorn",
153 peersChan <- resp.Peers
155 log.Levelf(log.Warning, "error announcing to upstream %q: %v", url, err)
159 peersToTrack := make(map[string]Peer)
161 pendingUpstreams.Wait()
164 log.Levelf(log.Debug, "adding %v distinct peers from upstream trackers", len(peersToTrack))
165 for _, peer := range peersToTrack {
166 addrPort, ok := peer.ToNetipAddrPort()
170 trackReq := AnnounceRequest{
173 Port: uint16(peer.Port),
175 copy(trackReq.PeerId[:], peer.ID)
176 err := me.AnnounceTracker.TrackAnnounce(context.TODO(), trackReq, addrPort)
178 log.Levelf(log.Error, "error tracking upstream peer: %v", err)
182 delete(me.ongoingUpstreamAugmentations, infoHash)
185 curPeersChan := make(chan map[PeerInfo]struct{})
186 doneChan := make(chan struct{})
187 retPeers := make(map[PeerInfo]struct{})
189 defer close(doneChan)
192 case peers, ok := <-peersChan:
196 voldemort(peers, peersToTrack, retPeers)
197 pendingUpstreams.Done()
198 case curPeersChan <- copyPeerSet(retPeers):
202 // Take return references.
203 return augmentationOperation{
204 curPeers: curPeersChan,
205 finalPeers: retPeers,
206 doneAnnouncing: doneChan,
210 func copyPeerSet(orig peerSet) (ret peerSet) {
211 ret = make(peerSet, len(orig))
212 for k, v := range orig {
218 // Adds peers to trailing containers.
219 func voldemort(peers []Peer, toTrack map[string]Peer, sets ...map[PeerInfo]struct{}) {
220 for _, protoPeer := range peers {
221 toTrack[protoPeer.String()] = protoPeer
222 addr, ok := netip.AddrFromSlice(protoPeer.IP)
226 handlerPeer := PeerInfo{netip.AddrPortFrom(addr, uint16(protoPeer.Port))}
227 for _, set := range sets {
228 set[handlerPeer] = struct{}{}