From 27b7fbe3f369be2d0bc8efd3f160c9a9182a1fca Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 17 Jul 2019 18:12:11 +1000 Subject: [PATCH] Fix announcing to S3 HTTP trackers --- cmd/tracker-announce/main.go | 6 +++--- torrent.go | 10 +++++----- tracker/http.go | 12 +++++++++++- tracker/tracker.go | 2 +- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/cmd/tracker-announce/main.go b/cmd/tracker-announce/main.go index f2eb02e4..4c029c39 100644 --- a/cmd/tracker-announce/main.go +++ b/cmd/tracker-announce/main.go @@ -2,16 +2,16 @@ package main import ( "log" - "math" "net/url" "strings" "sync" + "github.com/davecgh/go-spew/spew" + "github.com/anacrolix/tagflag" "github.com/anacrolix/torrent" "github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/tracker" - "github.com/davecgh/go-spew/spew" ) func argSpec(arg string) (ts *torrent.TorrentSpec, err error) { @@ -37,7 +37,7 @@ func main() { tagflag.Parse(&flags) ar := tracker.AnnounceRequest{ NumWant: -1, - Left: math.MaxUint64, + Left: -1, Port: flags.Port, } var wg sync.WaitGroup diff --git a/torrent.go b/torrent.go index 33f43b30..15029945 100644 --- a/torrent.go +++ b/torrent.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "math" "math/rand" "net/url" "os" @@ -15,6 +14,8 @@ import ( "time" "unsafe" + "github.com/davecgh/go-spew/spew" + "github.com/anacrolix/dht" "github.com/anacrolix/log" "github.com/anacrolix/missinggo" @@ -28,7 +29,6 @@ import ( pp "github.com/anacrolix/torrent/peer_protocol" "github.com/anacrolix/torrent/storage" "github.com/anacrolix/torrent/tracker" - "github.com/davecgh/go-spew/spew" ) func (t *Torrent) chunkIndexSpec(chunkIndex pp.Integer, piece pieceIndex) chunkSpec { @@ -635,11 +635,11 @@ func (t *Torrent) bytesLeft() (left int64) { } // Bytes left to give in tracker announces. -func (t *Torrent) bytesLeftAnnounce() uint64 { +func (t *Torrent) bytesLeftAnnounce() int64 { if t.haveInfo() { - return uint64(t.bytesLeft()) + return t.bytesLeft() } else { - return math.MaxUint64 + return -1 } } diff --git a/tracker/http.go b/tracker/http.go index 87927e77..65cb1989 100644 --- a/tracker/http.go +++ b/tracker/http.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "fmt" "io" + "math" "net" "net/http" "net/url" @@ -74,7 +75,16 @@ func setAnnounceParams(_url *url.URL, ar *AnnounceRequest, opts Announce) { q.Set("port", fmt.Sprintf("%d", ar.Port)) q.Set("uploaded", strconv.FormatInt(ar.Uploaded, 10)) q.Set("downloaded", strconv.FormatInt(ar.Downloaded, 10)) - q.Set("left", strconv.FormatUint(ar.Left, 10)) + + // The AWS S3 tracker returns "400 Bad Request: left(-1) was not in the valid range 0 - + // 9223372036854775807" if left is out of range, or "500 Internal Server Error: Internal Server + // Error" if omitted entirely. + left := ar.Left + if left < 0 { + left = math.MaxInt64 + } + q.Set("left", strconv.FormatInt(left, 10)) + if ar.Event != None { q.Set("event", ar.Event.String()) } diff --git a/tracker/tracker.go b/tracker/tracker.go index 446e2cb2..c3eefb98 100644 --- a/tracker/tracker.go +++ b/tracker/tracker.go @@ -14,7 +14,7 @@ type AnnounceRequest struct { InfoHash [20]byte PeerId [20]byte Downloaded int64 - Left uint64 + Left int64 // If less than 0, math.MaxInt64 will be used for HTTP trackers instead. Uploaded int64 // Apparently this is optional. None can be used for announces done at // regular intervals. -- 2.48.1