2
0
mirror of synced 2025-02-23 14:18:13 +00:00

Make trackerServer package

This commit is contained in:
Matt Joiner 2022-12-18 10:56:25 +11:00
parent 16da3c0c46
commit b06b614845
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
5 changed files with 44 additions and 21 deletions

View 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)

View 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)

View 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
}

9
tracker/server/use.go Normal file
View File

@ -0,0 +1,9 @@
package trackerServer
import "github.com/anacrolix/torrent/tracker"
type (
AnnounceRequest = tracker.AnnounceRequest
Client = tracker.Client
Peer = tracker.Peer
)

View 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)
}