2015-03-27 12:12:15 +08:00
|
|
|
package tracker
|
2013-11-06 22:55:29 +08:00
|
|
|
|
|
|
|
import (
|
2021-06-24 08:39:56 +08:00
|
|
|
"context"
|
2013-11-06 22:55:29 +08:00
|
|
|
"encoding/binary"
|
2015-03-30 20:05:39 +08:00
|
|
|
|
2021-06-22 21:28:26 +08:00
|
|
|
trHttp "github.com/anacrolix/torrent/tracker/http"
|
2021-06-22 20:36:43 +08:00
|
|
|
"github.com/anacrolix/torrent/tracker/udp"
|
2013-11-06 22:55:29 +08:00
|
|
|
)
|
|
|
|
|
2021-06-24 08:39:56 +08:00
|
|
|
type udpClient struct {
|
|
|
|
cl *udp.ConnClient
|
|
|
|
requestUri string
|
2013-11-06 22:55:29 +08:00
|
|
|
}
|
|
|
|
|
2021-06-24 08:39:56 +08:00
|
|
|
func (c *udpClient) Close() error {
|
|
|
|
return c.cl.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpClient) Announce(ctx context.Context, req AnnounceRequest, opts trHttp.AnnounceOpt) (res AnnounceResponse, err error) {
|
|
|
|
if req.IPAddress == 0 && opts.ClientIp4 != nil {
|
2021-06-24 07:53:18 +08:00
|
|
|
// I think we're taking bytes in big-endian order (all IPs), and writing it to a natively
|
|
|
|
// ordered uint32. This will be correctly ordered when written back out by the UDP client
|
|
|
|
// later. I'm ignoring the fact that IPv6 announces shouldn't have an IP address, we have a
|
|
|
|
// perfectly good IPv4 address.
|
2021-06-24 08:39:56 +08:00
|
|
|
req.IPAddress = binary.BigEndian.Uint32(opts.ClientIp4.To4())
|
2018-02-21 10:33:12 +08:00
|
|
|
}
|
2021-06-24 08:39:56 +08:00
|
|
|
h, nas, err := c.cl.Announce(ctx, req, udp.Options{RequestUri: c.requestUri})
|
2015-08-17 17:52:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-06-22 20:36:43 +08:00
|
|
|
res.Interval = h.Interval
|
|
|
|
res.Leechers = h.Leechers
|
|
|
|
res.Seeders = h.Seeders
|
2018-02-19 13:19:18 +08:00
|
|
|
for _, cp := range nas.NodeAddrs() {
|
2021-06-22 21:28:26 +08:00
|
|
|
res.Peers = append(res.Peers, trHttp.Peer{}.FromNodeAddr(cp))
|
2013-11-06 22:55:29 +08:00
|
|
|
}
|
2015-08-17 17:52:47 +08:00
|
|
|
return
|
2013-11-06 22:55:29 +08:00
|
|
|
}
|