]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix announcing to S3 HTTP trackers
authorMatt Joiner <anacrolix@gmail.com>
Wed, 17 Jul 2019 08:12:11 +0000 (18:12 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 17 Jul 2019 08:12:11 +0000 (18:12 +1000)
cmd/tracker-announce/main.go
torrent.go
tracker/http.go
tracker/tracker.go

index f2eb02e419708bf8029af9924e96554aa49f957a..4c029c39d18fba2722c6dfbda54e9d57d77d23c8 100644 (file)
@@ -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
index 33f43b30bcef09240250573eeaae219d5830a873..15029945d1af9db6e5f190e8562349145c45d0f1 100644 (file)
@@ -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
        }
 }
 
index 87927e773214ba348cadb1b09cf98e6c956f766b..65cb198956b06608a1b5465cfa05b11cf0544d54 100644 (file)
@@ -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())
        }
index 446e2cb295ba9b8b283a477919c6ade4c5674072..c3eefb98d929957f569fda9280275ac6aee576e7 100644 (file)
@@ -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.