Add ability to set DialContext/ListenPacket for tracker announcements (#760)
This is useful if you want to use a custom dialer to proxy requests via an external server since the HTTPProxy can only be used with tcp trackers and not udp.
This commit is contained in:
parent
75cc4e98d4
commit
f120b93e1c
@ -1,6 +1,7 @@
|
||||
package torrent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@ -90,6 +91,10 @@ type ClientConfig struct {
|
||||
// Defines proxy for HTTP requests, such as for trackers. It's commonly set from the result of
|
||||
// "net/http".ProxyURL(HTTPProxy).
|
||||
HTTPProxy func(*http.Request) (*url.URL, error)
|
||||
// Defines DialContext func to use for HTTP tracker announcements
|
||||
TrackerDialContext func(ctx context.Context, network, addr string) (net.Conn, error)
|
||||
// Defines ListenPacket func to use for UDP tracker announcements
|
||||
TrackerListenPacket func(network, addr string) (net.PacketConn, error)
|
||||
// Takes a tracker's hostname and requests DNS A and AAAA records.
|
||||
// Used in case DNS lookups require a special setup (i.e., dns-over-https)
|
||||
LookupTrackerIp func(*url.URL) ([]net.IP, error)
|
||||
|
@ -2,6 +2,7 @@ package tracker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/url"
|
||||
|
||||
"github.com/anacrolix/log"
|
||||
@ -21,6 +22,7 @@ type NewClientOpts struct {
|
||||
// Overrides the network in the scheme. Probably a legacy thing.
|
||||
UdpNetwork string
|
||||
Logger log.Logger
|
||||
ListenPacket func(network, addr string) (net.PacketConn, error)
|
||||
}
|
||||
|
||||
func NewClient(urlStr string, opts NewClientOpts) (Client, error) {
|
||||
@ -40,6 +42,7 @@ func NewClient(urlStr string, opts NewClientOpts) (Client, error) {
|
||||
Network: network,
|
||||
Host: _url.Host,
|
||||
Logger: opts.Logger,
|
||||
ListenPacket: opts.ListenPacket,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -1,7 +1,9 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
@ -12,9 +14,11 @@ type Client struct {
|
||||
}
|
||||
|
||||
type ProxyFunc func(*http.Request) (*url.URL, error)
|
||||
type DialContextFunc func(ctx context.Context, network, addr string) (net.Conn, error)
|
||||
|
||||
type NewClientOpts struct {
|
||||
Proxy ProxyFunc
|
||||
DialContext DialContextFunc
|
||||
ServerName string
|
||||
AllowKeepAlive bool
|
||||
}
|
||||
@ -24,6 +28,7 @@ func NewClient(url_ *url.URL, opts NewClientOpts) Client {
|
||||
url_: url_,
|
||||
hc: &http.Client{
|
||||
Transport: &http.Transport{
|
||||
DialContext: opts.DialContext,
|
||||
Proxy: opts.Proxy,
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
@ -37,6 +38,8 @@ type Announce struct {
|
||||
Request AnnounceRequest
|
||||
HostHeader string
|
||||
HTTPProxy func(*http.Request) (*url.URL, error)
|
||||
DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
|
||||
ListenPacket func(network, addr string) (net.PacketConn, error)
|
||||
ServerName string
|
||||
UserAgent string
|
||||
UdpNetwork string
|
||||
@ -55,10 +58,12 @@ func (me Announce) Do() (res AnnounceResponse, err error) {
|
||||
cl, err := NewClient(me.TrackerUrl, NewClientOpts{
|
||||
Http: trHttp.NewClientOpts{
|
||||
Proxy: me.HTTPProxy,
|
||||
DialContext: me.DialContext,
|
||||
ServerName: me.ServerName,
|
||||
},
|
||||
UdpNetwork: me.UdpNetwork,
|
||||
Logger: me.Logger.WithContextValue(fmt.Sprintf("tracker client for %q", me.TrackerUrl)),
|
||||
ListenPacket: me.ListenPacket,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -9,6 +9,8 @@ import (
|
||||
"github.com/anacrolix/missinggo/v2"
|
||||
)
|
||||
|
||||
type listenPacketFunc func(network, addr string) (net.PacketConn, error)
|
||||
|
||||
type NewConnClientOpts struct {
|
||||
// The network to operate to use, such as "udp4", "udp", "udp6".
|
||||
Network string
|
||||
@ -18,6 +20,8 @@ type NewConnClientOpts struct {
|
||||
Ipv6 *bool
|
||||
// Logger to use for internal errors.
|
||||
Logger log.Logger
|
||||
// Custom function to use as a substitute for net.ListenPacket
|
||||
ListenPacket listenPacketFunc
|
||||
}
|
||||
|
||||
// Manages a Client with a specific connection.
|
||||
@ -80,7 +84,13 @@ func (me clientWriter) Write(p []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
func NewConnClient(opts NewConnClientOpts) (cc *ConnClient, err error) {
|
||||
conn, err := net.ListenPacket(opts.Network, ":0")
|
||||
var conn net.PacketConn
|
||||
if opts.ListenPacket != nil {
|
||||
conn, err = opts.ListenPacket(opts.Network, ":0")
|
||||
} else {
|
||||
conn, err = net.ListenPacket(opts.Network, ":0")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -158,6 +158,8 @@ func (me *trackerScraper) announce(ctx context.Context, event tracker.AnnounceEv
|
||||
res, err := tracker.Announce{
|
||||
Context: ctx,
|
||||
HTTPProxy: me.t.cl.config.HTTPProxy,
|
||||
DialContext: me.t.cl.config.TrackerDialContext,
|
||||
ListenPacket: me.t.cl.config.TrackerListenPacket,
|
||||
UserAgent: me.t.cl.config.HTTPUserAgent,
|
||||
TrackerUrl: me.trackerUrl(ip),
|
||||
Request: req,
|
||||
|
Loading…
x
Reference in New Issue
Block a user