tracker/udp.ConnClient: Make logging configurable
Should fix https://github.com/anacrolix/torrent/issues/701#issue-1088130173
This commit is contained in:
parent
2e9ecd5a32
commit
50fcd908ca
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
|
"github.com/anacrolix/log"
|
||||||
trHttp "github.com/anacrolix/torrent/tracker/http"
|
trHttp "github.com/anacrolix/torrent/tracker/http"
|
||||||
"github.com/anacrolix/torrent/tracker/udp"
|
"github.com/anacrolix/torrent/tracker/udp"
|
||||||
)
|
)
|
||||||
|
@ -19,6 +20,7 @@ type NewClientOpts struct {
|
||||||
Http trHttp.NewClientOpts
|
Http trHttp.NewClientOpts
|
||||||
// Overrides the network in the scheme. Probably a legacy thing.
|
// Overrides the network in the scheme. Probably a legacy thing.
|
||||||
UdpNetwork string
|
UdpNetwork string
|
||||||
|
Logger log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(urlStr string, opts NewClientOpts) (Client, error) {
|
func NewClient(urlStr string, opts NewClientOpts) (Client, error) {
|
||||||
|
@ -37,6 +39,7 @@ func NewClient(urlStr string, opts NewClientOpts) (Client, error) {
|
||||||
cc, err := udp.NewConnClient(udp.NewConnClientOpts{
|
cc, err := udp.NewConnClient(udp.NewConnClientOpts{
|
||||||
Network: network,
|
Network: network,
|
||||||
Host: _url.Host,
|
Host: _url.Host,
|
||||||
|
Logger: opts.Logger,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -3,11 +3,13 @@ package tracker
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/anacrolix/dht/v2/krpc"
|
"github.com/anacrolix/dht/v2/krpc"
|
||||||
|
"github.com/anacrolix/log"
|
||||||
trHttp "github.com/anacrolix/torrent/tracker/http"
|
trHttp "github.com/anacrolix/torrent/tracker/http"
|
||||||
"github.com/anacrolix/torrent/tracker/shared"
|
"github.com/anacrolix/torrent/tracker/shared"
|
||||||
"github.com/anacrolix/torrent/tracker/udp"
|
"github.com/anacrolix/torrent/tracker/udp"
|
||||||
|
@ -43,6 +45,7 @@ type Announce struct {
|
||||||
// If the port is zero, it's assumed to be the same as the Request.Port.
|
// If the port is zero, it's assumed to be the same as the Request.Port.
|
||||||
ClientIp6 krpc.NodeAddr
|
ClientIp6 krpc.NodeAddr
|
||||||
Context context.Context
|
Context context.Context
|
||||||
|
Logger log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// The code *is* the documentation.
|
// The code *is* the documentation.
|
||||||
|
@ -55,6 +58,7 @@ func (me Announce) Do() (res AnnounceResponse, err error) {
|
||||||
ServerName: me.ServerName,
|
ServerName: me.ServerName,
|
||||||
},
|
},
|
||||||
UdpNetwork: me.UdpNetwork,
|
UdpNetwork: me.UdpNetwork,
|
||||||
|
Logger: me.Logger.WithContextValue(fmt.Sprintf("tracker client for %q", me.TrackerUrl)),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|
|
@ -2,9 +2,10 @@ package udp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
"github.com/anacrolix/log"
|
||||||
|
|
||||||
"github.com/anacrolix/missinggo/v2"
|
"github.com/anacrolix/missinggo/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,6 +16,8 @@ type NewConnClientOpts struct {
|
||||||
Host string
|
Host string
|
||||||
// If non-nil, forces either IPv4 or IPv6 in the UDP tracker wire protocol.
|
// If non-nil, forces either IPv4 or IPv6 in the UDP tracker wire protocol.
|
||||||
Ipv6 *bool
|
Ipv6 *bool
|
||||||
|
// Logger to use for internal errors.
|
||||||
|
Logger log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// Manages a Client with a specific connection.
|
// Manages a Client with a specific connection.
|
||||||
|
@ -42,7 +45,7 @@ func (cc *ConnClient) reader() {
|
||||||
}
|
}
|
||||||
err = cc.d.Dispatch(b[:n], addr)
|
err = cc.d.Dispatch(b[:n], addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("dispatching packet received on %v: %v", cc.conn.LocalAddr(), err)
|
cc.newOpts.Logger.WithLevel(log.Debug).Printf("dispatching packet received on %v: %v", cc.conn.LocalAddr(), err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,6 +84,9 @@ func NewConnClient(opts NewConnClientOpts) (cc *ConnClient, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if opts.Logger.IsZero() {
|
||||||
|
opts.Logger = log.Default
|
||||||
|
}
|
||||||
cc = &ConnClient{
|
cc = &ConnClient{
|
||||||
Client: Client{
|
Client: Client{
|
||||||
Writer: clientWriter{
|
Writer: clientWriter{
|
||||||
|
@ -97,17 +103,21 @@ func NewConnClient(opts NewConnClientOpts) (cc *ConnClient, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConnClient) Close() error {
|
func (cc *ConnClient) Close() error {
|
||||||
c.closed = true
|
cc.closed = true
|
||||||
return c.conn.Close()
|
return cc.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConnClient) Announce(
|
func (cc *ConnClient) Announce(
|
||||||
ctx context.Context, req AnnounceRequest, opts Options,
|
ctx context.Context, req AnnounceRequest, opts Options,
|
||||||
) (
|
) (
|
||||||
h AnnounceResponseHeader, nas AnnounceResponsePeers, err error,
|
h AnnounceResponseHeader, nas AnnounceResponsePeers, err error,
|
||||||
) {
|
) {
|
||||||
return c.Client.Announce(ctx, req, opts, func(addr net.Addr) bool {
|
return cc.Client.Announce(ctx, req, opts, func(addr net.Addr) bool {
|
||||||
return ipv6(c.newOpts.Ipv6, c.newOpts.Network, addr)
|
return ipv6(cc.newOpts.Ipv6, cc.newOpts.Network, addr)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cc *ConnClient) LocalAddr() net.Addr {
|
||||||
|
return cc.conn.LocalAddr()
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/anacrolix/dht/v2/krpc"
|
"github.com/anacrolix/dht/v2/krpc"
|
||||||
|
qt "github.com/frankban/quicktest"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -73,3 +74,20 @@ func TestConvertInt16ToInt(t *testing.T) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConnClientLogDispatchUnknownTransactionId(t *testing.T) {
|
||||||
|
const network = "udp"
|
||||||
|
cc, err := NewConnClient(NewConnClientOpts{
|
||||||
|
Network: network,
|
||||||
|
})
|
||||||
|
c := qt.New(t)
|
||||||
|
c.Assert(err, qt.IsNil)
|
||||||
|
defer cc.Close()
|
||||||
|
pc, err := net.ListenPacket(network, ":0")
|
||||||
|
c.Assert(err, qt.IsNil)
|
||||||
|
defer pc.Close()
|
||||||
|
ccAddr := *cc.LocalAddr().(*net.UDPAddr)
|
||||||
|
ccAddr.IP = net.IPv6loopback
|
||||||
|
_, err = pc.WriteTo(make([]byte, 30), &ccAddr)
|
||||||
|
c.Assert(err, qt.IsNil)
|
||||||
|
}
|
||||||
|
|
|
@ -166,6 +166,7 @@ func (me *trackerScraper) announce(ctx context.Context, event tracker.AnnounceEv
|
||||||
UdpNetwork: me.u.Scheme,
|
UdpNetwork: me.u.Scheme,
|
||||||
ClientIp4: krpc.NodeAddr{IP: me.t.cl.config.PublicIp4},
|
ClientIp4: krpc.NodeAddr{IP: me.t.cl.config.PublicIp4},
|
||||||
ClientIp6: krpc.NodeAddr{IP: me.t.cl.config.PublicIp6},
|
ClientIp6: krpc.NodeAddr{IP: me.t.cl.config.PublicIp6},
|
||||||
|
Logger: me.t.logger,
|
||||||
}.Do()
|
}.Do()
|
||||||
me.t.logger.WithDefaultLevel(log.Debug).Printf("announce to %q returned %#v: %v", me.u.String(), res, err)
|
me.t.logger.WithDefaultLevel(log.Debug).Printf("announce to %q returned %#v: %v", me.u.String(), res, err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue