]> Sergey Matveev's repositories - btrtrc.git/blobdiff - tracker/udp.go
Drop support for go 1.20
[btrtrc.git] / tracker / udp.go
index 55e9ad37d5b84736d1a11338e07c61e96d8c88e1..cf68188751d6c0bda395936a176f6cc38e17ebc9 100644 (file)
@@ -1,27 +1,51 @@
 package tracker
 
-type UDPConnectionRequest struct {
-       ConnectionId int64
-       Action       int32
-       TransctionId int32
-}
+import (
+       "context"
+       "encoding/binary"
+
+       "github.com/anacrolix/generics"
 
-type UDPAnnounceResponseHeader struct {
-       Action        int32
-       TransactionId int32
-       Interval      int32
-       Leechers      int32
-       Seeders       int32
+       trHttp "github.com/anacrolix/torrent/tracker/http"
+       "github.com/anacrolix/torrent/tracker/udp"
+       "github.com/anacrolix/torrent/types/infohash"
+)
+
+type udpClient struct {
+       cl         *udp.ConnClient
+       requestUri string
 }
 
-type UDPAnnounceResponse struct {
-       UDPAnnounceResponseHeader
-       PeerAddrSlice
+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
+               }),
+       )
 }
 
-type PeerAddr struct {
-       IP   int32
-       Port int16
+func (c *udpClient) Close() error {
+       return c.cl.Close()
 }
 
-type PeerAddrSlice []PeerAddr
+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(opts.ClientIp4.To4())
+       }
+       h, nas, err := c.cl.Announce(ctx, req, udp.Options{RequestUri: c.requestUri})
+       if err != nil {
+               return
+       }
+       res.Interval = h.Interval
+       res.Leechers = h.Leechers
+       res.Seeders = h.Seeders
+       for _, cp := range nas.NodeAddrs() {
+               res.Peers = append(res.Peers, trHttp.Peer{}.FromNodeAddr(cp))
+       }
+       return
+}