+35
@@ -0,0 +1,35 @@
|
||||
package udp
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
|
||||
"github.com/anacrolix/dht/v2/krpc"
|
||||
)
|
||||
|
||||
// Marshalled as binary by the UDP client, so be careful making changes.
|
||||
type AnnounceRequest struct {
|
||||
InfoHash [20]byte
|
||||
PeerId [20]byte
|
||||
Downloaded int64
|
||||
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.
|
||||
Event AnnounceEvent
|
||||
IPAddress uint32
|
||||
Key int32
|
||||
NumWant int32 // How many peer addresses are desired. -1 for default.
|
||||
Port uint16
|
||||
} // 82 bytes
|
||||
|
||||
type AnnounceEvent int32
|
||||
|
||||
func (e AnnounceEvent) String() string {
|
||||
// See BEP 3, "event", and https://github.com/anacrolix/torrent/issues/416#issuecomment-751427001.
|
||||
return []string{"", "completed", "started", "stopped"}[e]
|
||||
}
|
||||
|
||||
type AnnounceResponsePeers interface {
|
||||
encoding.BinaryUnmarshaler
|
||||
NodeAddrs() []krpc.NodeAddr
|
||||
}
|
||||
+179
@@ -0,0 +1,179 @@
|
||||
package udp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/anacrolix/dht/v2/krpc"
|
||||
)
|
||||
|
||||
// Client interacts with UDP trackers via its Writer and Dispatcher. It has no knowledge of
|
||||
// connection specifics.
|
||||
type Client struct {
|
||||
mu sync.Mutex
|
||||
connId ConnectionId
|
||||
connIdIssued time.Time
|
||||
Dispatcher *Dispatcher
|
||||
Writer io.Writer
|
||||
}
|
||||
|
||||
func (cl *Client) Announce(
|
||||
ctx context.Context, req AnnounceRequest, opts Options,
|
||||
// Decides whether the response body is IPv6 or IPv4, see BEP 15.
|
||||
ipv6 func(net.Addr) bool,
|
||||
) (
|
||||
respHdr AnnounceResponseHeader,
|
||||
// A slice of krpc.NodeAddr, likely wrapped in an appropriate unmarshalling wrapper.
|
||||
peers AnnounceResponsePeers,
|
||||
err error,
|
||||
) {
|
||||
respBody, addr, err := cl.request(ctx, ActionAnnounce, append(mustMarshal(req), opts.Encode()...))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
r := bytes.NewBuffer(respBody)
|
||||
err = Read(r, &respHdr)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("reading response header: %w", err)
|
||||
return
|
||||
}
|
||||
if ipv6(addr) {
|
||||
peers = &krpc.CompactIPv6NodeAddrs{}
|
||||
} else {
|
||||
peers = &krpc.CompactIPv4NodeAddrs{}
|
||||
}
|
||||
err = peers.UnmarshalBinary(r.Bytes())
|
||||
if err != nil {
|
||||
err = fmt.Errorf("reading response peers: %w", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// There's no way to pass options in a scrape, since we don't when the request body ends.
|
||||
func (cl *Client) Scrape(
|
||||
ctx context.Context, ihs []InfoHash,
|
||||
) (
|
||||
out ScrapeResponse, err error,
|
||||
) {
|
||||
respBody, _, err := cl.request(ctx, ActionScrape, mustMarshal(ScrapeRequest(ihs)))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
r := bytes.NewBuffer(respBody)
|
||||
for r.Len() != 0 {
|
||||
var item ScrapeInfohashResult
|
||||
err = Read(r, &item)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
if len(out) > len(ihs) {
|
||||
err = fmt.Errorf("got %v results but expected %v", len(out), len(ihs))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (cl *Client) connect(ctx context.Context) (err error) {
|
||||
// We could get fancier here and use RWMutex, and even fire off the connection asynchronously
|
||||
// and provide a grace period while it resolves.
|
||||
cl.mu.Lock()
|
||||
defer cl.mu.Unlock()
|
||||
if !cl.connIdIssued.IsZero() && time.Since(cl.connIdIssued) < time.Minute {
|
||||
return nil
|
||||
}
|
||||
respBody, _, err := cl.request(ctx, ActionConnect, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var connResp ConnectionResponse
|
||||
err = binary.Read(bytes.NewReader(respBody), binary.BigEndian, &connResp)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cl.connId = connResp.ConnectionId
|
||||
cl.connIdIssued = time.Now()
|
||||
return
|
||||
}
|
||||
|
||||
func (cl *Client) connIdForRequest(ctx context.Context, action Action) (id ConnectionId, err error) {
|
||||
if action == ActionConnect {
|
||||
id = ConnectRequestConnectionId
|
||||
return
|
||||
}
|
||||
err = cl.connect(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
id = cl.connId
|
||||
return
|
||||
}
|
||||
|
||||
func (cl *Client) requestWriter(ctx context.Context, action Action, body []byte, tId TransactionId) (err error) {
|
||||
var buf bytes.Buffer
|
||||
for n := 0; ; n++ {
|
||||
var connId ConnectionId
|
||||
connId, err = cl.connIdForRequest(ctx, action)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
buf.Reset()
|
||||
err = binary.Write(&buf, binary.BigEndian, RequestHeader{
|
||||
ConnectionId: connId,
|
||||
Action: action,
|
||||
TransactionId: tId,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
buf.Write(body)
|
||||
_, err = cl.Writer.Write(buf.Bytes())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-time.After(timeout(n)):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (cl *Client) request(ctx context.Context, action Action, body []byte) (respBody []byte, addr net.Addr, err error) {
|
||||
respChan := make(chan DispatchedResponse, 1)
|
||||
t := cl.Dispatcher.NewTransaction(func(dr DispatchedResponse) {
|
||||
respChan <- dr
|
||||
})
|
||||
defer t.End()
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
writeErr := make(chan error, 1)
|
||||
go func() {
|
||||
writeErr <- cl.requestWriter(ctx, action, body, t.Id())
|
||||
}()
|
||||
select {
|
||||
case dr := <-respChan:
|
||||
if dr.Header.Action == action {
|
||||
respBody = dr.Body
|
||||
addr = dr.Addr
|
||||
} else if dr.Header.Action == ActionError {
|
||||
// I've seen "Connection ID mismatch.^@" in less and other tools, I think they're just
|
||||
// not handling a trailing \x00 nicely.
|
||||
err = fmt.Errorf("error response: %#q", dr.Body)
|
||||
} else {
|
||||
err = fmt.Errorf("unexpected response action %v", dr.Header.Action)
|
||||
}
|
||||
case err = <-writeErr:
|
||||
err = fmt.Errorf("write error: %w", err)
|
||||
case <-ctx.Done():
|
||||
err = ctx.Err()
|
||||
}
|
||||
return
|
||||
}
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
package udp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/anacrolix/log"
|
||||
|
||||
"github.com/anacrolix/missinggo/v2"
|
||||
)
|
||||
|
||||
type NewConnClientOpts struct {
|
||||
// The network to operate to use, such as "udp4", "udp", "udp6".
|
||||
Network string
|
||||
// Tracker address
|
||||
Host string
|
||||
// If non-nil, forces either IPv4 or IPv6 in the UDP tracker wire protocol.
|
||||
Ipv6 *bool
|
||||
// Logger to use for internal errors.
|
||||
Logger log.Logger
|
||||
}
|
||||
|
||||
// Manages a Client with a specific connection.
|
||||
type ConnClient struct {
|
||||
Client Client
|
||||
conn net.PacketConn
|
||||
d Dispatcher
|
||||
readErr error
|
||||
closed bool
|
||||
newOpts NewConnClientOpts
|
||||
}
|
||||
|
||||
func (cc *ConnClient) reader() {
|
||||
b := make([]byte, 0x800)
|
||||
for {
|
||||
n, addr, err := cc.conn.ReadFrom(b)
|
||||
if err != nil {
|
||||
// TODO: Do bad things to the dispatcher, and incoming calls to the client if we have a
|
||||
// read error.
|
||||
cc.readErr = err
|
||||
if !cc.closed {
|
||||
panic(err)
|
||||
}
|
||||
break
|
||||
}
|
||||
err = cc.d.Dispatch(b[:n], addr)
|
||||
if err != nil {
|
||||
cc.newOpts.Logger.Levelf(log.Debug, "dispatching packet received on %v: %v", cc.conn.LocalAddr(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ipv6(opt *bool, network string, remoteAddr net.Addr) bool {
|
||||
if opt != nil {
|
||||
return *opt
|
||||
}
|
||||
switch network {
|
||||
case "udp4":
|
||||
return false
|
||||
case "udp6":
|
||||
return true
|
||||
}
|
||||
rip := missinggo.AddrIP(remoteAddr)
|
||||
return rip.To16() != nil && rip.To4() == nil
|
||||
}
|
||||
|
||||
// Allows a UDP Client to write packets to an endpoint without knowing about the network specifics.
|
||||
type clientWriter struct {
|
||||
pc net.PacketConn
|
||||
network string
|
||||
address string
|
||||
}
|
||||
|
||||
func (me clientWriter) Write(p []byte) (n int, err error) {
|
||||
addr, err := net.ResolveUDPAddr(me.network, me.address)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return me.pc.WriteTo(p, addr)
|
||||
}
|
||||
|
||||
func NewConnClient(opts NewConnClientOpts) (cc *ConnClient, err error) {
|
||||
conn, err := net.ListenPacket(opts.Network, ":0")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if opts.Logger.IsZero() {
|
||||
opts.Logger = log.Default
|
||||
}
|
||||
cc = &ConnClient{
|
||||
Client: Client{
|
||||
Writer: clientWriter{
|
||||
pc: conn,
|
||||
network: opts.Network,
|
||||
address: opts.Host,
|
||||
},
|
||||
},
|
||||
conn: conn,
|
||||
newOpts: opts,
|
||||
}
|
||||
cc.Client.Dispatcher = &cc.d
|
||||
go cc.reader()
|
||||
return
|
||||
}
|
||||
|
||||
func (cc *ConnClient) Close() error {
|
||||
cc.closed = true
|
||||
return cc.conn.Close()
|
||||
}
|
||||
|
||||
func (cc *ConnClient) Announce(
|
||||
ctx context.Context, req AnnounceRequest, opts Options,
|
||||
) (
|
||||
h AnnounceResponseHeader, nas AnnounceResponsePeers, err error,
|
||||
) {
|
||||
return cc.Client.Announce(ctx, req, opts, func(addr net.Addr) bool {
|
||||
return ipv6(cc.newOpts.Ipv6, cc.newOpts.Network, addr)
|
||||
})
|
||||
}
|
||||
|
||||
func (cc *ConnClient) LocalAddr() net.Addr {
|
||||
return cc.conn.LocalAddr()
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package udp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Maintains a mapping of transaction IDs to handlers.
|
||||
type Dispatcher struct {
|
||||
mu sync.RWMutex
|
||||
transactions map[TransactionId]Transaction
|
||||
}
|
||||
|
||||
// The caller owns b.
|
||||
func (me *Dispatcher) Dispatch(b []byte, addr net.Addr) error {
|
||||
buf := bytes.NewBuffer(b)
|
||||
var rh ResponseHeader
|
||||
err := Read(buf, &rh)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
me.mu.RLock()
|
||||
defer me.mu.RUnlock()
|
||||
if t, ok := me.transactions[rh.TransactionId]; ok {
|
||||
t.h(DispatchedResponse{
|
||||
Header: rh,
|
||||
Body: append([]byte(nil), buf.Bytes()...),
|
||||
Addr: addr,
|
||||
})
|
||||
return nil
|
||||
} else {
|
||||
return fmt.Errorf("unknown transaction id %v", rh.TransactionId)
|
||||
}
|
||||
}
|
||||
|
||||
func (me *Dispatcher) forgetTransaction(id TransactionId) {
|
||||
me.mu.Lock()
|
||||
defer me.mu.Unlock()
|
||||
delete(me.transactions, id)
|
||||
}
|
||||
|
||||
func (me *Dispatcher) NewTransaction(h TransactionResponseHandler) Transaction {
|
||||
me.mu.Lock()
|
||||
defer me.mu.Unlock()
|
||||
for {
|
||||
id := RandomTransactionId()
|
||||
if _, ok := me.transactions[id]; ok {
|
||||
continue
|
||||
}
|
||||
t := Transaction{
|
||||
d: me,
|
||||
h: h,
|
||||
id: id,
|
||||
}
|
||||
if me.transactions == nil {
|
||||
me.transactions = make(map[TransactionId]Transaction)
|
||||
}
|
||||
me.transactions[id] = t
|
||||
return t
|
||||
}
|
||||
}
|
||||
|
||||
type DispatchedResponse struct {
|
||||
Header ResponseHeader
|
||||
// Response payload, after the header.
|
||||
Body []byte
|
||||
// Response source address
|
||||
Addr net.Addr
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package udp
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
RequestUri string
|
||||
}
|
||||
|
||||
func (opts Options) Encode() (ret []byte) {
|
||||
for {
|
||||
l := len(opts.RequestUri)
|
||||
if l == 0 {
|
||||
break
|
||||
}
|
||||
if l > math.MaxUint8 {
|
||||
l = math.MaxUint8
|
||||
}
|
||||
ret = append(append(ret, optionTypeURLData, byte(l)), opts.RequestUri[:l]...)
|
||||
opts.RequestUri = opts.RequestUri[l:]
|
||||
}
|
||||
return
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package udp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Action int32
|
||||
|
||||
const (
|
||||
ActionConnect Action = iota
|
||||
ActionAnnounce
|
||||
ActionScrape
|
||||
ActionError
|
||||
|
||||
ConnectRequestConnectionId = 0x41727101980
|
||||
|
||||
// BEP 41
|
||||
optionTypeEndOfOptions = 0
|
||||
optionTypeNOP = 1
|
||||
optionTypeURLData = 2
|
||||
)
|
||||
|
||||
type TransactionId = int32
|
||||
|
||||
type ConnectionId = int64
|
||||
|
||||
type ConnectionRequest struct {
|
||||
ConnectionId ConnectionId
|
||||
Action Action
|
||||
TransactionId TransactionId
|
||||
}
|
||||
|
||||
type ConnectionResponse struct {
|
||||
ConnectionId ConnectionId
|
||||
}
|
||||
|
||||
type ResponseHeader struct {
|
||||
Action Action
|
||||
TransactionId TransactionId
|
||||
}
|
||||
|
||||
type RequestHeader struct {
|
||||
ConnectionId ConnectionId
|
||||
Action Action
|
||||
TransactionId TransactionId
|
||||
} // 16 bytes
|
||||
|
||||
type AnnounceResponseHeader struct {
|
||||
Interval int32
|
||||
Leechers int32
|
||||
Seeders int32
|
||||
}
|
||||
|
||||
type InfoHash = [20]byte
|
||||
|
||||
func marshal(data interface{}) (b []byte, err error) {
|
||||
var buf bytes.Buffer
|
||||
err = binary.Write(&buf, binary.BigEndian, data)
|
||||
b = buf.Bytes()
|
||||
return
|
||||
}
|
||||
|
||||
func mustMarshal(data interface{}) []byte {
|
||||
b, err := marshal(data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func Write(w io.Writer, data interface{}) error {
|
||||
return binary.Write(w, binary.BigEndian, data)
|
||||
}
|
||||
|
||||
func Read(r io.Reader, data interface{}) error {
|
||||
return binary.Read(r, binary.BigEndian, data)
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package udp
|
||||
|
||||
type ScrapeRequest []InfoHash
|
||||
|
||||
type ScrapeResponse []ScrapeInfohashResult
|
||||
|
||||
type ScrapeInfohashResult struct {
|
||||
Seeders int32
|
||||
Completed int32
|
||||
Leechers int32
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package udp
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const maxTimeout = 3840 * time.Second
|
||||
|
||||
func timeout(contiguousTimeouts int) (d time.Duration) {
|
||||
if contiguousTimeouts > 8 {
|
||||
contiguousTimeouts = 8
|
||||
}
|
||||
d = 15 * time.Second
|
||||
for ; contiguousTimeouts > 0; contiguousTimeouts-- {
|
||||
d *= 2
|
||||
}
|
||||
return
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package udp
|
||||
|
||||
import "math/rand"
|
||||
|
||||
func RandomTransactionId() TransactionId {
|
||||
return TransactionId(rand.Uint32())
|
||||
}
|
||||
|
||||
type TransactionResponseHandler func(dr DispatchedResponse)
|
||||
|
||||
type Transaction struct {
|
||||
id int32
|
||||
d *Dispatcher
|
||||
h TransactionResponseHandler
|
||||
}
|
||||
|
||||
func (t *Transaction) Id() TransactionId {
|
||||
return t.id
|
||||
}
|
||||
|
||||
func (t *Transaction) End() {
|
||||
t.d.forgetTransaction(t.id)
|
||||
}
|
||||
Reference in New Issue
Block a user