]> Sergey Matveev's repositories - btrtrc.git/blobdiff - tracker/udp.go
Drop support for go 1.20
[btrtrc.git] / tracker / udp.go
index f7afc9ea5906aa8eb898eab2ce849796216fd653..cf68188751d6c0bda395936a176f6cc38e17ebc9 100644 (file)
@@ -1,36 +1,43 @@
 package tracker
 
 import (
+       "context"
        "encoding/binary"
-       "net/url"
+
+       "github.com/anacrolix/generics"
 
        trHttp "github.com/anacrolix/torrent/tracker/http"
        "github.com/anacrolix/torrent/tracker/udp"
+       "github.com/anacrolix/torrent/types/infohash"
 )
 
-type udpAnnounce struct {
-       url url.URL
-       a   *Announce
+type udpClient struct {
+       cl         *udp.ConnClient
+       requestUri string
 }
 
-func (c *udpAnnounce) Do(req AnnounceRequest) (res AnnounceResponse, err error) {
-       cl, err := udp.NewConnClient(udp.NewConnClientOpts{
-               Network: c.dialNetwork(),
-               Host:    c.url.Host,
-               Ipv6:    nil,
-       })
-       if err != nil {
-               return
-       }
-       defer cl.Close()
-       if req.IPAddress == 0 && c.a.ClientIp4.IP != nil {
+func (c *udpClient) Scrape(ctx context.Context, ihs []infohash.T) (out udp.ScrapeResponse, err error) {
+       return c.cl.Client.Scrape(
+               ctx,
+               generics.SliceMap(ihs, func(from infohash.T) udp.InfoHash {
+                       return from
+               }),
+       )
+}
+
+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 {
                // 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.
-               req.IPAddress = binary.BigEndian.Uint32(c.a.ClientIp4.IP.To4())
+               req.IPAddress = binary.BigEndian.Uint32(opts.ClientIp4.To4())
        }
-       h, nas, err := cl.Announce(c.a.Context, req, udp.Options{RequestUri: c.url.RequestURI()})
+       h, nas, err := c.cl.Announce(ctx, req, udp.Options{RequestUri: c.requestUri})
        if err != nil {
                return
        }
@@ -42,19 +49,3 @@ func (c *udpAnnounce) Do(req AnnounceRequest) (res AnnounceResponse, err error)
        }
        return
 }
-
-func (c *udpAnnounce) dialNetwork() string {
-       if c.a.UdpNetwork != "" {
-               return c.a.UdpNetwork
-       }
-       return "udp"
-}
-
-// TODO: Split on IPv6, as BEP 15 says response peer decoding depends on network in use.
-func announceUDP(opt Announce, _url *url.URL) (AnnounceResponse, error) {
-       ua := udpAnnounce{
-               url: *_url,
-               a:   &opt,
-       }
-       return ua.Do(opt.Request)
-}