]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Make trackerServer package
authorMatt Joiner <anacrolix@gmail.com>
Sat, 17 Dec 2022 23:56:25 +0000 (10:56 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 17 Dec 2022 23:56:25 +0000 (10:56 +1100)
tracker/http/server/server.go
tracker/server/server.go [moved from tracker/server.go with 95% similarity]
tracker/server/upstream-announcing.go [moved from tracker/upstream-announcing.go with 61% similarity]
tracker/server/use.go [new file with mode: 0644]
tracker/udp/server/server.go

index 781c640ba4ae84cd6d9671fab2225dfba51e93be..bd2783e35c48be579ef317ee0f8dddaaf34b4b74 100644 (file)
@@ -11,6 +11,7 @@ import (
        "github.com/anacrolix/dht/v2/krpc"
        "github.com/anacrolix/generics"
        "github.com/anacrolix/log"
+       trackerServer "github.com/anacrolix/torrent/tracker/server"
 
        "github.com/anacrolix/torrent/bencode"
        "github.com/anacrolix/torrent/tracker"
@@ -18,7 +19,7 @@ import (
 )
 
 type Handler struct {
-       Announce *tracker.AnnounceHandler
+       Announce *trackerServer.AnnounceHandler
        // Called to derive an announcer's IP if non-nil. If not specified, the Request.RemoteAddr is
        // used. Necessary for instances running behind reverse proxies for example.
        RequestHost func(r *http.Request) (netip.Addr, error)
@@ -74,13 +75,20 @@ func (me Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        }
        portU64, err := strconv.ParseUint(vs.Get("port"), 0, 16)
        addrPort := netip.AddrPortFrom(addr, uint16(portU64))
-       res := me.Announce.Serve(r.Context(), tracker.AnnounceRequest{
-               InfoHash: infoHash,
-               PeerId:   peerId,
-               Event:    event,
-               Port:     addrPort.Port(),
-               NumWant:  -1,
-       }, addrPort, tracker.GetPeersOpts{MaxCount: generics.Some[uint](200)})
+       res := me.Announce.Serve(
+               r.Context(),
+               tracker.AnnounceRequest{
+                       InfoHash: infoHash,
+                       PeerId:   peerId,
+                       Event:    event,
+                       Port:     addrPort.Port(),
+                       NumWant:  -1,
+               },
+               addrPort,
+               trackerServer.GetPeersOpts{
+                       MaxCount: generics.Some[uint](200),
+               },
+       )
        err = res.Err
        if err != nil {
                log.Printf("error serving announce: %v", err)
similarity index 95%
rename from tracker/server.go
rename to tracker/server/server.go
index 22f54da512734356a196c09c30d6d3bbce075f88..ced31616b7f789dbff52eb70649dae0596f1aed3 100644 (file)
@@ -1,4 +1,4 @@
-package tracker
+package trackerServer
 
 import (
        "context"
@@ -10,6 +10,7 @@ import (
 
        "github.com/anacrolix/generics"
        "github.com/anacrolix/log"
+       "github.com/anacrolix/torrent/tracker"
        "go.opentelemetry.io/otel"
        "go.opentelemetry.io/otel/attribute"
        "go.opentelemetry.io/otel/codes"
@@ -36,7 +37,12 @@ type AnnounceAddr = netip.AddrPort
 type AnnounceTracker interface {
        TrackAnnounce(ctx context.Context, req udp.AnnounceRequest, addr AnnounceAddr) error
        Scrape(ctx context.Context, infoHashes []InfoHash) ([]udp.ScrapeInfohashResult, error)
-       GetPeers(ctx context.Context, infoHash InfoHash, opts GetPeersOpts) ServerAnnounceResult
+       GetPeers(
+               ctx context.Context,
+               infoHash InfoHash,
+               opts GetPeersOpts,
+               remote AnnounceAddr,
+       ) ServerAnnounceResult
 }
 
 type ServerAnnounceResult struct {
@@ -150,7 +156,7 @@ func (me *AnnounceHandler) Serve(
                        opts.MaxCount = generics.Some(newCount)
                }
        }
-       ret = me.AnnounceTracker.GetPeers(ctx, infoHash, opts)
+       ret = me.AnnounceTracker.GetPeers(ctx, infoHash, opts, addr)
        if ret.Err != nil {
                return
        }
@@ -198,7 +204,7 @@ func (me *AnnounceHandler) augmentPeersFromUpstream(infoHash [20]byte) augmentat
        subReq := AnnounceRequest{
                InfoHash: infoHash,
                PeerId:   me.UpstreamAnnouncePeerId,
-               Event:    None,
+               Event:    tracker.None,
                Key:      0,
                NumWant:  -1,
                Port:     0,
@@ -219,7 +225,7 @@ func (me *AnnounceHandler) augmentPeersFromUpstream(infoHash [20]byte) augmentat
                                return
                        }
                        log.Printf("announcing %x upstream to %v", infoHash, url)
-                       resp, err := client.Announce(announceCtx, subReq, AnnounceOpt{
+                       resp, err := client.Announce(announceCtx, subReq, tracker.AnnounceOpt{
                                UserAgent: "aragorn",
                        })
                        interval := resp.Interval
@@ -253,7 +259,7 @@ func (me *AnnounceHandler) augmentPeersFromUpstream(infoHash [20]byte) augmentat
                        }
                        trackReq := AnnounceRequest{
                                InfoHash: infoHash,
-                               Event:    Started,
+                               Event:    tracker.Started,
                                Port:     uint16(peer.Port),
                        }
                        copy(trackReq.PeerId[:], peer.ID)
similarity index 61%
rename from tracker/upstream-announcing.go
rename to tracker/server/upstream-announcing.go
index ab5a5fb34d133e28fbab35a6c4cefb489d1b58cf..cfbf61c85a502219264f5e58ac30ad1c3568575a 100644 (file)
@@ -1,4 +1,4 @@
-package tracker
+package trackerServer
 
 import (
        "context"
@@ -7,12 +7,12 @@ import (
 
 type UpstreamAnnounceGater interface {
        Start(ctx context.Context, tracker string, infoHash InfoHash,
-       // How long the announce block remains before discarding it.
+               // How long the announce block remains before discarding it.
                timeout time.Duration,
        ) (bool, error)
        Completed(
                ctx context.Context, tracker string, infoHash InfoHash,
-       // Num of seconds reported by tracker, or some suitable value the caller has chosen.
+               // Num of seconds reported by tracker, or some suitable value the caller has chosen.
                interval int32,
        ) error
 }
diff --git a/tracker/server/use.go b/tracker/server/use.go
new file mode 100644 (file)
index 0000000..942321c
--- /dev/null
@@ -0,0 +1,9 @@
+package trackerServer
+
+import "github.com/anacrolix/torrent/tracker"
+
+type (
+       AnnounceRequest = tracker.AnnounceRequest
+       Client          = tracker.Client
+       Peer            = tracker.Peer
+)
index d86012dcf6bb494352f7ad568aa0f75a0a3b2027..2007233f4f0c067e04ff6c755ef56631eb0ea161 100644 (file)
@@ -13,10 +13,10 @@ import (
        "github.com/anacrolix/dht/v2/krpc"
        "github.com/anacrolix/generics"
        "github.com/anacrolix/log"
+       trackerServer "github.com/anacrolix/torrent/tracker/server"
        "go.opentelemetry.io/otel"
        "go.opentelemetry.io/otel/codes"
 
-       "github.com/anacrolix/torrent/tracker"
        "github.com/anacrolix/torrent/tracker/udp"
 )
 
@@ -29,12 +29,12 @@ type ConnectionTracker interface {
 
 type InfoHash = [20]byte
 
-type AnnounceTracker = tracker.AnnounceTracker
+type AnnounceTracker = trackerServer.AnnounceTracker
 
 type Server struct {
        ConnTracker  ConnectionTracker
        SendResponse func(data []byte, addr net.Addr) (int, error)
-       Announce     *tracker.AnnounceHandler
+       Announce     *trackerServer.AnnounceHandler
 }
 
 type RequestSourceAddr = net.Addr
@@ -106,7 +106,7 @@ func (me *Server) handleAnnounce(
                err = fmt.Errorf("converting source net.Addr to AnnounceAddr: %w", err)
                return err
        }
-       opts := tracker.GetPeersOpts{MaxCount: generics.Some[uint](50)}
+       opts := trackerServer.GetPeersOpts{MaxCount: generics.Some[uint](50)}
        if addrFamily == udp.AddrFamilyIpv4 {
                opts.MaxCount = generics.Some[uint](150)
        }