2
0
mirror of synced 2025-02-24 06:38:14 +00:00

Merge all the tracker packages, why would anyone want them separately?

This commit is contained in:
Matt Joiner 2015-03-27 15:12:15 +11:00
parent 9f62098e85
commit 5f7ad4c769
6 changed files with 44 additions and 49 deletions

View File

@ -53,7 +53,6 @@ import (
"github.com/anacrolix/torrent/mse" "github.com/anacrolix/torrent/mse"
pp "github.com/anacrolix/torrent/peer_protocol" pp "github.com/anacrolix/torrent/peer_protocol"
"github.com/anacrolix/torrent/tracker" "github.com/anacrolix/torrent/tracker"
_ "github.com/anacrolix/torrent/tracker/udp"
. "github.com/anacrolix/torrent/util" . "github.com/anacrolix/torrent/util"
) )

View File

@ -9,8 +9,6 @@ import (
"github.com/anacrolix/torrent" "github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/tracker" "github.com/anacrolix/torrent/tracker"
_ "github.com/anacrolix/torrent/tracker/http"
_ "github.com/anacrolix/torrent/tracker/udp"
) )
func argSpec(arg string) (ts *torrent.TorrentSpec, err error) { func argSpec(arg string) (ts *torrent.TorrentSpec, err error) {

View File

@ -1,4 +1,4 @@
package http package tracker
import ( import (
"bytes" "bytes"
@ -11,22 +11,20 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"github.com/anacrolix/torrent/util"
"github.com/anacrolix/libtorgo/bencode" "github.com/anacrolix/libtorgo/bencode"
"github.com/anacrolix/torrent/tracker" "github.com/anacrolix/torrent/util"
) )
func init() { func init() {
tracker.RegisterClientScheme("http", NewClient) RegisterClientScheme("http", NewClient)
} }
type client struct { type client struct {
url url.URL url url.URL
} }
func NewClient(url *url.URL) tracker.Client { func NewClient(url *url.URL) Client {
return &client{ return &client{
url: *url, url: *url,
} }
@ -41,7 +39,7 @@ type response struct {
Peers interface{} `bencode:"peers"` Peers interface{} `bencode:"peers"`
} }
func (r *response) UnmarshalPeers() (ret []tracker.Peer, err error) { func (r *response) UnmarshalPeers() (ret []Peer, err error) {
s, ok := r.Peers.(string) s, ok := r.Peers.(string)
if !ok { if !ok {
err = fmt.Errorf("unsupported peers value type: %T", r.Peers) err = fmt.Errorf("unsupported peers value type: %T", r.Peers)
@ -52,14 +50,14 @@ func (r *response) UnmarshalPeers() (ret []tracker.Peer, err error) {
if err != nil { if err != nil {
return return
} }
ret = make([]tracker.Peer, 0, len(cp)) ret = make([]Peer, 0, len(cp))
for _, p := range cp { for _, p := range cp {
ret = append(ret, tracker.Peer{net.IP(p.IP[:]), int(p.Port)}) ret = append(ret, Peer{net.IP(p.IP[:]), int(p.Port)})
} }
return return
} }
func (me *client) Announce(ar *tracker.AnnounceRequest) (ret tracker.AnnounceResponse, err error) { func (me *client) Announce(ar *AnnounceRequest) (ret AnnounceResponse, err error) {
q := make(url.Values) q := make(url.Values)
q.Set("info_hash", string(ar.InfoHash[:])) q.Set("info_hash", string(ar.InfoHash[:]))
q.Set("peer_id", string(ar.PeerId[:])) q.Set("peer_id", string(ar.PeerId[:]))
@ -67,7 +65,7 @@ func (me *client) Announce(ar *tracker.AnnounceRequest) (ret tracker.AnnounceRes
q.Set("uploaded", strconv.FormatInt(ar.Uploaded, 10)) q.Set("uploaded", strconv.FormatInt(ar.Uploaded, 10))
q.Set("downloaded", strconv.FormatInt(ar.Downloaded, 10)) q.Set("downloaded", strconv.FormatInt(ar.Downloaded, 10))
q.Set("left", strconv.FormatInt(ar.Left, 10)) q.Set("left", strconv.FormatInt(ar.Left, 10))
if ar.Event != tracker.None { if ar.Event != None {
q.Set("event", ar.Event.String()) q.Set("event", ar.Event.String())
} }
// http://stackoverflow.com/questions/17418004/why-does-tracker-server-not-understand-my-request-bittorrent-protocol // http://stackoverflow.com/questions/17418004/why-does-tracker-server-not-understand-my-request-bittorrent-protocol

View File

@ -1,4 +1,4 @@
package udp_tracker package tracker
import ( import (
"bytes" "bytes"
@ -10,8 +10,6 @@ import (
"net" "net"
"net/url" "net/url"
"time" "time"
"github.com/anacrolix/torrent/tracker"
) )
type Action int32 type Action int32
@ -55,18 +53,13 @@ type AnnounceResponseHeader struct {
Seeders int32 Seeders int32
} }
type Peer struct {
IP [4]byte
Port uint16
}
func init() { func init() {
tracker.RegisterClientScheme("udp", newClient) RegisterClientScheme("udp", newClient)
} }
func newClient(url *url.URL) tracker.Client { func newClient(url *url.URL) Client {
return &client{ return &udpClient{
url: url, url: *url,
} }
} }
@ -85,25 +78,25 @@ func timeout(contiguousTimeouts int) (d time.Duration) {
return return
} }
type client struct { type udpClient struct {
contiguousTimeouts int contiguousTimeouts int
connectionIdReceived time.Time connectionIdReceived time.Time
connectionId int64 connectionId int64
socket net.Conn socket net.Conn
url *url.URL url url.URL
} }
func (c *client) URL() string { func (c *udpClient) URL() string {
return c.url.String() return c.url.String()
} }
func (c *client) String() string { func (c *udpClient) String() string {
return c.URL() return c.URL()
} }
func (c *client) Announce(req *tracker.AnnounceRequest) (res tracker.AnnounceResponse, err error) { func (c *udpClient) Announce(req *AnnounceRequest) (res AnnounceResponse, err error) {
if !c.connected() { if !c.connected() {
err = tracker.ErrNotConnected err = ErrNotConnected
return return
} }
reqURI := c.url.RequestURI() reqURI := c.url.RequestURI()
@ -137,7 +130,7 @@ func (c *client) Announce(req *tracker.AnnounceRequest) (res tracker.AnnounceRes
default: default:
return return
} }
res.Peers = append(res.Peers, tracker.Peer{ res.Peers = append(res.Peers, Peer{
IP: p.IP[:], IP: p.IP[:],
Port: int(p.Port), Port: int(p.Port),
}) })
@ -146,7 +139,7 @@ func (c *client) Announce(req *tracker.AnnounceRequest) (res tracker.AnnounceRes
// body is the binary serializable request body. trailer is optional data // body is the binary serializable request body. trailer is optional data
// following it, such as for BEP 41. // following it, such as for BEP 41.
func (c *client) write(h *RequestHeader, body interface{}, trailer []byte) (err error) { func (c *udpClient) write(h *RequestHeader, body interface{}, trailer []byte) (err error) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err = binary.Write(buf, binary.BigEndian, h) err = binary.Write(buf, binary.BigEndian, h)
if err != nil { if err != nil {
@ -182,7 +175,7 @@ func write(w io.Writer, data interface{}) error {
// args is the binary serializable request body. trailer is optional data // args is the binary serializable request body. trailer is optional data
// following it, such as for BEP 41. // following it, such as for BEP 41.
func (c *client) request(action Action, args interface{}, options []byte) (responseBody *bytes.Reader, err error) { func (c *udpClient) request(action Action, args interface{}, options []byte) (responseBody *bytes.Reader, err error) {
tid := newTransactionId() tid := newTransactionId()
err = c.write(&RequestHeader{ err = c.write(&RequestHeader{
ConnectionId: c.connectionId, ConnectionId: c.connectionId,
@ -238,11 +231,11 @@ func readBody(r *bytes.Reader, data ...interface{}) (err error) {
return return
} }
func (c *client) connected() bool { func (c *udpClient) connected() bool {
return !c.connectionIdReceived.IsZero() && time.Now().Before(c.connectionIdReceived.Add(time.Minute)) return !c.connectionIdReceived.IsZero() && time.Now().Before(c.connectionIdReceived.Add(time.Minute))
} }
func (c *client) Connect() (err error) { func (c *udpClient) Connect() (err error) {
if c.connected() { if c.connected() {
return nil return nil
} }

View File

@ -1,4 +1,4 @@
package udp_tracker package tracker
import ( import (
"bytes" "bytes"
@ -13,7 +13,7 @@ import (
"syscall" "syscall"
"testing" "testing"
"github.com/anacrolix/torrent/tracker" "github.com/anacrolix/torrent/util"
) )
// Ensure net.IPs are stored big-endian, to match the way they're read from // Ensure net.IPs are stored big-endian, to match the way they're read from
@ -29,8 +29,10 @@ func TestNetIPv4Bytes(t *testing.T) {
} }
func TestMarshalAnnounceResponse(t *testing.T) { func TestMarshalAnnounceResponse(t *testing.T) {
w := bytes.NewBuffer(nil) w := bytes.Buffer{}
if err := binary.Write(w, binary.BigEndian, []Peer{{[4]byte{127, 0, 0, 1}, 2}, {[4]byte{255, 0, 0, 3}, 4}}); err != nil { peers := util.CompactPeers{{[4]byte{127, 0, 0, 1}, 2}, {[4]byte{255, 0, 0, 3}, 4}}
err := peers.WriteBinary(&w)
if err != nil {
t.Fatalf("error writing udp announce response addrs: %s", err) t.Fatalf("error writing udp announce response addrs: %s", err)
} }
if w.String() != "\x7f\x00\x00\x01\x00\x02\xff\x00\x00\x03\x00\x04" { if w.String() != "\x7f\x00\x00\x01\x00\x02\xff\x00\x00\x03\x00\x04" {
@ -87,16 +89,16 @@ func TestUDPTracker(t *testing.T) {
if testing.Short() { if testing.Short() {
t.SkipNow() t.SkipNow()
} }
tr, err := tracker.New("udp://tracker.openbittorrent.com:80/announce") tr, err := New("udp://tracker.openbittorrent.com:80/announce")
if err != nil { if err != nil {
t.Skip(err) t.Skip(err)
} }
if err := tr.Connect(); err != nil { if err := tr.Connect(); err != nil {
t.Skip(err) t.Skip(err)
} }
req := tracker.AnnounceRequest{ req := AnnounceRequest{
NumWant: -1, NumWant: -1,
Event: tracker.Started, Event: Started,
} }
rand.Read(req.PeerId[:]) rand.Read(req.PeerId[:])
copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1}) copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
@ -111,8 +113,8 @@ func TestAnnounceRandomInfoHash(t *testing.T) {
if testing.Short() { if testing.Short() {
t.SkipNow() t.SkipNow()
} }
req := tracker.AnnounceRequest{ req := AnnounceRequest{
Event: tracker.Stopped, Event: Stopped,
} }
rand.Read(req.PeerId[:]) rand.Read(req.PeerId[:])
rand.Read(req.InfoHash[:]) rand.Read(req.InfoHash[:])
@ -126,7 +128,7 @@ func TestAnnounceRandomInfoHash(t *testing.T) {
} { } {
go func(url string) { go func(url string) {
defer wg.Done() defer wg.Done()
tr, err := tracker.New(url) tr, err := New(url)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -165,7 +167,7 @@ func TestURLPathOption(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
log.Print("connected") log.Print("connected")
_, err = cl.Announce(&tracker.AnnounceRequest{}) _, err = cl.Announce(&AnnounceRequest{})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -184,7 +186,7 @@ func TestURLPathOption(t *testing.T) {
n, _, _ := conn.ReadFrom(b[:]) n, _, _ := conn.ReadFrom(b[:])
r = bytes.NewReader(b[:n]) r = bytes.NewReader(b[:n])
read(r, &h) read(r, &h)
read(r, &tracker.AnnounceRequest{}) read(r, &AnnounceRequest{})
all, _ := ioutil.ReadAll(r) all, _ := ioutil.ReadAll(r)
if string(all) != "\x02\x09/announce" { if string(all) != "\x02\x09/announce" {
t.FailNow() t.FailNow()

View File

@ -5,6 +5,7 @@ import (
"encoding" "encoding"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io"
"github.com/anacrolix/libtorgo/bencode" "github.com/anacrolix/libtorgo/bencode"
) )
@ -33,6 +34,10 @@ func (me *CompactPeers) UnmarshalBinary(b []byte) (err error) {
return return
} }
func (me CompactPeers) WriteBinary(w io.Writer) error {
return binary.Write(w, binary.BigEndian, me)
}
type CompactPeer struct { type CompactPeer struct {
IP [4]byte IP [4]byte
Port uint16 Port uint16