2015-03-27 12:12:15 +08:00
|
|
|
package tracker
|
2013-11-06 22:55:29 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-11-28 09:02:12 +08:00
|
|
|
"context"
|
2013-12-14 19:21:45 +08:00
|
|
|
"crypto/rand"
|
2013-11-06 22:55:29 +08:00
|
|
|
"encoding/binary"
|
2015-08-17 17:52:47 +08:00
|
|
|
"fmt"
|
2013-11-06 22:55:29 +08:00
|
|
|
"io"
|
2015-03-12 17:07:10 +08:00
|
|
|
"io/ioutil"
|
2013-11-06 22:55:29 +08:00
|
|
|
"net"
|
2015-03-12 17:07:10 +08:00
|
|
|
"net/url"
|
2014-03-16 23:30:10 +08:00
|
|
|
"sync"
|
2013-11-06 22:55:29 +08:00
|
|
|
"testing"
|
2014-04-08 17:39:34 +08:00
|
|
|
|
2019-08-10 16:46:07 +08:00
|
|
|
"github.com/anacrolix/dht/v2/krpc"
|
2016-02-07 15:06:13 +08:00
|
|
|
_ "github.com/anacrolix/envpprof"
|
2019-01-08 16:20:53 +08:00
|
|
|
"github.com/pkg/errors"
|
2015-08-17 17:52:47 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-08-02 12:57:28 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2013-11-06 22:55:29 +08:00
|
|
|
)
|
|
|
|
|
2018-02-12 20:49:05 +08:00
|
|
|
var trackers = []string{
|
|
|
|
"udp://tracker.coppersurfer.tk:6969",
|
|
|
|
"udp://tracker.leechers-paradise.org:6969",
|
|
|
|
}
|
|
|
|
|
2014-03-16 23:30:10 +08:00
|
|
|
// Ensure net.IPs are stored big-endian, to match the way they're read from
|
|
|
|
// the wire.
|
2013-11-06 22:55:29 +08:00
|
|
|
func TestNetIPv4Bytes(t *testing.T) {
|
|
|
|
ip := net.IP([]byte{127, 0, 0, 1})
|
|
|
|
if ip.String() != "127.0.0.1" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if string(ip) != "\x7f\x00\x00\x01" {
|
|
|
|
t.Fatal([]byte(ip))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalAnnounceResponse(t *testing.T) {
|
2018-02-11 12:13:00 +08:00
|
|
|
peers := krpc.CompactIPv4NodeAddrs{
|
2015-08-17 17:52:47 +08:00
|
|
|
{[]byte{127, 0, 0, 1}, 2},
|
|
|
|
{[]byte{255, 0, 0, 3}, 4},
|
2013-11-06 22:55:29 +08:00
|
|
|
}
|
2015-08-17 17:52:47 +08:00
|
|
|
b, err := peers.MarshalBinary()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t,
|
|
|
|
"\x7f\x00\x00\x01\x00\x02\xff\x00\x00\x03\x00\x04",
|
|
|
|
b)
|
|
|
|
require.EqualValues(t, 12, binary.Size(AnnounceResponseHeader{}))
|
2013-11-06 22:55:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Failure to write an entire packet to UDP is expected to given an error.
|
|
|
|
func TestLongWriteUDP(t *testing.T) {
|
2015-10-04 16:58:23 +08:00
|
|
|
t.Parallel()
|
2017-08-27 00:10:17 +08:00
|
|
|
l, err := net.ListenUDP("udp4", nil)
|
2018-01-06 13:53:40 +08:00
|
|
|
require.NoError(t, err)
|
2013-11-06 22:55:29 +08:00
|
|
|
defer l.Close()
|
|
|
|
c, err := net.DialUDP("udp", nil, l.LocalAddr().(*net.UDPAddr))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
for msgLen := 1; ; msgLen *= 2 {
|
|
|
|
n, err := c.Write(make([]byte, msgLen))
|
|
|
|
if err != nil {
|
2015-08-02 12:57:28 +08:00
|
|
|
require.Contains(t, err.Error(), "message too long")
|
2013-11-06 22:55:29 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if n < msgLen {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShortBinaryRead(t *testing.T) {
|
|
|
|
var data ResponseHeader
|
|
|
|
err := binary.Read(bytes.NewBufferString("\x00\x00\x00\x01"), binary.BigEndian, &data)
|
2014-12-04 02:51:49 +08:00
|
|
|
if err != io.ErrUnexpectedEOF {
|
|
|
|
t.FailNow()
|
2013-11-06 22:55:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertInt16ToInt(t *testing.T) {
|
|
|
|
i := 50000
|
|
|
|
if int(uint16(int16(i))) != 50000 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
2013-12-14 19:21:45 +08:00
|
|
|
|
2015-08-17 17:52:47 +08:00
|
|
|
func TestAnnounceLocalhost(t *testing.T) {
|
2015-10-04 16:58:23 +08:00
|
|
|
t.Parallel()
|
2015-08-17 17:52:47 +08:00
|
|
|
srv := server{
|
|
|
|
t: map[[20]byte]torrent{
|
2016-11-22 11:01:09 +08:00
|
|
|
{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1}: {
|
2015-08-17 17:52:47 +08:00
|
|
|
Seeders: 1,
|
|
|
|
Leechers: 2,
|
2018-02-11 12:13:00 +08:00
|
|
|
Peers: krpc.CompactIPv4NodeAddrs{
|
2015-08-17 17:52:47 +08:00
|
|
|
{[]byte{1, 2, 3, 4}, 5},
|
|
|
|
{[]byte{6, 7, 8, 9}, 10},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
srv.pc, err = net.ListenPacket("udp", ":0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer srv.pc.Close()
|
|
|
|
go func() {
|
|
|
|
require.NoError(t, srv.serveOne())
|
|
|
|
}()
|
|
|
|
req := AnnounceRequest{
|
|
|
|
NumWant: -1,
|
|
|
|
Event: Started,
|
|
|
|
}
|
|
|
|
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})
|
|
|
|
go func() {
|
|
|
|
require.NoError(t, srv.serveOne())
|
|
|
|
}()
|
2018-02-19 13:19:18 +08:00
|
|
|
ar, err := Announce{
|
|
|
|
TrackerUrl: fmt.Sprintf("udp://%s/announce", srv.pc.LocalAddr().String()),
|
|
|
|
Request: req,
|
|
|
|
}.Do()
|
2015-08-17 17:52:47 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 1, ar.Seeders)
|
|
|
|
assert.EqualValues(t, 2, len(ar.Peers))
|
|
|
|
}
|
|
|
|
|
2013-12-14 19:21:45 +08:00
|
|
|
func TestUDPTracker(t *testing.T) {
|
2015-10-04 16:58:23 +08:00
|
|
|
t.Parallel()
|
2014-12-03 04:23:01 +08:00
|
|
|
if testing.Short() {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2015-03-27 12:12:15 +08:00
|
|
|
req := AnnounceRequest{
|
2013-12-14 19:21:45 +08:00
|
|
|
NumWant: -1,
|
|
|
|
}
|
|
|
|
rand.Read(req.PeerId[:])
|
2014-03-16 23:30:10 +08:00
|
|
|
copy(req.InfoHash[:], []uint8{0xa3, 0x56, 0x41, 0x43, 0x74, 0x23, 0xe6, 0x26, 0xd9, 0x38, 0x25, 0x4a, 0x6b, 0x80, 0x49, 0x10, 0xa6, 0x67, 0xa, 0xc1})
|
2018-02-19 13:19:18 +08:00
|
|
|
ar, err := Announce{
|
|
|
|
TrackerUrl: trackers[0],
|
|
|
|
Request: req,
|
|
|
|
}.Do()
|
2016-05-23 07:29:14 +08:00
|
|
|
// Skip any net errors as we don't control the server.
|
2019-01-08 16:20:53 +08:00
|
|
|
if _, ok := errors.Cause(err).(net.Error); ok {
|
2016-05-23 07:29:14 +08:00
|
|
|
t.Skip(err)
|
2016-05-07 16:17:40 +08:00
|
|
|
}
|
2015-08-17 17:52:47 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Log(ar)
|
2013-12-14 19:21:45 +08:00
|
|
|
}
|
2014-03-16 23:30:10 +08:00
|
|
|
|
2015-08-17 17:52:47 +08:00
|
|
|
func TestAnnounceRandomInfoHashThirdParty(t *testing.T) {
|
2015-10-04 16:58:23 +08:00
|
|
|
t.Parallel()
|
2014-12-03 04:23:01 +08:00
|
|
|
if testing.Short() {
|
2015-08-17 17:52:47 +08:00
|
|
|
// This test involves contacting third party servers that may have
|
2018-11-28 09:02:25 +08:00
|
|
|
// unpredictable results.
|
2014-12-03 04:23:01 +08:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
2015-03-27 12:12:15 +08:00
|
|
|
req := AnnounceRequest{
|
|
|
|
Event: Stopped,
|
2014-12-04 02:51:49 +08:00
|
|
|
}
|
|
|
|
rand.Read(req.PeerId[:])
|
|
|
|
rand.Read(req.InfoHash[:])
|
2014-03-16 23:30:10 +08:00
|
|
|
wg := sync.WaitGroup{}
|
2018-11-28 09:02:12 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2018-02-12 20:49:05 +08:00
|
|
|
for _, url := range trackers {
|
2015-08-17 17:52:47 +08:00
|
|
|
wg.Add(1)
|
2014-03-16 23:30:10 +08:00
|
|
|
go func(url string) {
|
|
|
|
defer wg.Done()
|
2018-02-19 13:19:18 +08:00
|
|
|
resp, err := Announce{
|
|
|
|
TrackerUrl: url,
|
|
|
|
Request: req,
|
2018-11-28 09:02:12 +08:00
|
|
|
Context: ctx,
|
2018-02-19 13:19:18 +08:00
|
|
|
}.Do()
|
2014-03-16 23:30:10 +08:00
|
|
|
if err != nil {
|
2014-11-21 06:24:49 +08:00
|
|
|
t.Logf("error announcing to %s: %s", url, err)
|
|
|
|
return
|
2014-03-16 23:30:10 +08:00
|
|
|
}
|
|
|
|
if resp.Leechers != 0 || resp.Seeders != 0 || len(resp.Peers) != 0 {
|
2015-08-17 17:52:47 +08:00
|
|
|
// The info hash we generated was random in 2^160 space. If we
|
|
|
|
// get a hit, something is weird.
|
2014-03-16 23:30:10 +08:00
|
|
|
t.Fatal(resp)
|
|
|
|
}
|
2015-08-17 17:52:47 +08:00
|
|
|
t.Logf("announced to %s", url)
|
2018-11-28 09:02:12 +08:00
|
|
|
cancel()
|
2014-03-16 23:30:10 +08:00
|
|
|
}(url)
|
|
|
|
}
|
2018-11-28 09:02:12 +08:00
|
|
|
wg.Wait()
|
2021-02-03 16:42:46 +08:00
|
|
|
cancel()
|
2014-03-16 23:30:10 +08:00
|
|
|
}
|
2015-03-12 17:07:10 +08:00
|
|
|
|
|
|
|
// Check that URLPath option is done correctly.
|
|
|
|
func TestURLPathOption(t *testing.T) {
|
|
|
|
conn, err := net.ListenUDP("udp", nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
go func() {
|
2018-02-19 13:19:18 +08:00
|
|
|
_, err := Announce{
|
|
|
|
TrackerUrl: (&url.URL{
|
|
|
|
Scheme: "udp",
|
|
|
|
Host: conn.LocalAddr().String(),
|
|
|
|
Path: "/announce",
|
|
|
|
}).String(),
|
|
|
|
}.Do()
|
2015-03-12 17:07:10 +08:00
|
|
|
if err != nil {
|
2016-02-07 15:06:13 +08:00
|
|
|
defer conn.Close()
|
2015-03-12 17:07:10 +08:00
|
|
|
}
|
2016-02-07 15:06:13 +08:00
|
|
|
require.NoError(t, err)
|
2015-03-12 17:07:10 +08:00
|
|
|
}()
|
|
|
|
var b [512]byte
|
|
|
|
_, addr, _ := conn.ReadFrom(b[:])
|
|
|
|
r := bytes.NewReader(b[:])
|
|
|
|
var h RequestHeader
|
|
|
|
read(r, &h)
|
|
|
|
w := &bytes.Buffer{}
|
|
|
|
write(w, ResponseHeader{
|
|
|
|
TransactionId: h.TransactionId,
|
|
|
|
})
|
|
|
|
write(w, ConnectionResponse{42})
|
|
|
|
conn.WriteTo(w.Bytes(), addr)
|
|
|
|
n, _, _ := conn.ReadFrom(b[:])
|
|
|
|
r = bytes.NewReader(b[:n])
|
|
|
|
read(r, &h)
|
2015-03-27 12:12:15 +08:00
|
|
|
read(r, &AnnounceRequest{})
|
2015-03-12 17:07:10 +08:00
|
|
|
all, _ := ioutil.ReadAll(r)
|
|
|
|
if string(all) != "\x02\x09/announce" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
w = &bytes.Buffer{}
|
|
|
|
write(w, ResponseHeader{
|
|
|
|
TransactionId: h.TransactionId,
|
|
|
|
})
|
|
|
|
write(w, AnnounceResponseHeader{})
|
|
|
|
conn.WriteTo(w.Bytes(), addr)
|
|
|
|
}
|