2021-06-24 09:53:18 +10:00
|
|
|
package udp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/anacrolix/dht/v2/krpc"
|
|
|
|
"github.com/anacrolix/missinggo/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NewConnClientOpts struct {
|
2021-11-22 18:06:27 +11:00
|
|
|
// The network to operate to use, such as "udp4", "udp", "udp6".
|
2021-06-24 09:53:18 +10:00
|
|
|
Network string
|
2021-11-22 18:06:27 +11:00
|
|
|
// Tracker address
|
|
|
|
Host string
|
|
|
|
// If non-nil, forces either IPv4 or IPv6 in the UDP tracker wire protocol.
|
|
|
|
Ipv6 *bool
|
2021-06-24 09:53:18 +10:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:06:27 +11:00
|
|
|
// Manages a Client with a specific connection.
|
2021-06-24 09:53:18 +10:00
|
|
|
type ConnClient struct {
|
2021-06-24 14:38:16 +10:00
|
|
|
Client Client
|
2021-06-24 09:53:18 +10:00
|
|
|
conn net.Conn
|
|
|
|
d Dispatcher
|
|
|
|
readErr error
|
|
|
|
ipv6 bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *ConnClient) reader() {
|
2021-07-26 10:18:24 +10:00
|
|
|
b := make([]byte, 0x800)
|
2021-06-24 09:53:18 +10:00
|
|
|
for {
|
|
|
|
n, err := cc.conn.Read(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
|
|
|
|
break
|
|
|
|
}
|
2021-08-16 04:11:31 +03:00
|
|
|
_ = cc.d.Dispatch(b[:n])
|
|
|
|
// if err != nil {
|
|
|
|
// log.Printf("dispatching packet received on %v (%q): %v", cc.conn, string(b[:n]), err)
|
|
|
|
// }
|
2021-06-24 09:53:18 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ipv6(opt *bool, network string, conn net.Conn) bool {
|
|
|
|
if opt != nil {
|
|
|
|
return *opt
|
|
|
|
}
|
|
|
|
switch network {
|
|
|
|
case "udp4":
|
|
|
|
return false
|
|
|
|
case "udp6":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
rip := missinggo.AddrIP(conn.RemoteAddr())
|
|
|
|
return rip.To16() != nil && rip.To4() == nil
|
|
|
|
}
|
|
|
|
|
2021-06-24 10:39:56 +10:00
|
|
|
func NewConnClient(opts NewConnClientOpts) (cc *ConnClient, err error) {
|
|
|
|
conn, err := net.Dial(opts.Network, opts.Host)
|
2021-06-24 09:53:18 +10:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-06-24 10:39:56 +10:00
|
|
|
cc = &ConnClient{
|
2021-06-24 14:38:16 +10:00
|
|
|
Client: Client{
|
2021-06-24 10:39:56 +10:00
|
|
|
Writer: conn,
|
|
|
|
},
|
|
|
|
conn: conn,
|
|
|
|
ipv6: ipv6(opts.Ipv6, opts.Network, conn),
|
2021-06-24 09:53:18 +10:00
|
|
|
}
|
2021-06-24 14:38:16 +10:00
|
|
|
cc.Client.Dispatcher = &cc.d
|
2021-06-24 10:39:56 +10:00
|
|
|
go cc.reader()
|
2021-06-24 09:53:18 +10:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConnClient) Close() error {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConnClient) Announce(
|
|
|
|
ctx context.Context, req AnnounceRequest, opts Options,
|
|
|
|
) (
|
|
|
|
h AnnounceResponseHeader, nas AnnounceResponsePeers, err error,
|
|
|
|
) {
|
|
|
|
nas = func() AnnounceResponsePeers {
|
|
|
|
if c.ipv6 {
|
|
|
|
return &krpc.CompactIPv6NodeAddrs{}
|
|
|
|
} else {
|
|
|
|
return &krpc.CompactIPv4NodeAddrs{}
|
|
|
|
}
|
|
|
|
}()
|
2021-06-24 14:38:16 +10:00
|
|
|
h, err = c.Client.Announce(ctx, req, nas, opts)
|
2021-06-24 09:53:18 +10:00
|
|
|
return
|
|
|
|
}
|