Group Client DHT and tracker config

This commit is contained in:
Matt Joiner 2022-12-07 10:42:29 +11:00
parent 98d5a19d50
commit 682c77fcb9
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
1 changed files with 39 additions and 30 deletions

View File

@ -19,8 +19,36 @@ import (
"github.com/anacrolix/torrent/version"
)
// Contains config elements that are exclusive to tracker handling. There may be other fields in
// ClientConfig that are also relevant.
type ClientTrackerConfig struct {
// Don't announce to trackers. This only leaves DHT to discover peers.
DisableTrackers bool `long:"disable-trackers"`
// 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)
}
type ClientDhtConfig struct {
// Don't create a DHT.
NoDHT bool `long:"disable-dht"`
DhtStartingNodes func(network string) dht.StartingNodesGetter
// Called for each anacrolix/dht Server created for the Client.
ConfigureAnacrolixDhtServer func(*dht.ServerConfig)
PeriodicallyAnnounceTorrentsToDht bool
// OnQuery hook func
DHTOnQuery func(query *krpc.Msg, source net.Addr) (propagate bool)
}
// Probably not safe to modify this after it's given to a Client.
type ClientConfig struct {
ClientTrackerConfig
ClientDhtConfig
// Store torrent file data in this directory unless .DefaultStorage is
// specified.
DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"`
@ -30,16 +58,7 @@ type ClientConfig struct {
ListenPort int
NoDefaultPortForwarding bool
UpnpID string
// Don't announce to trackers. This only leaves DHT to discover peers.
DisableTrackers bool `long:"disable-trackers"`
DisablePEX bool `long:"disable-pex"`
// Don't create a DHT.
NoDHT bool `long:"disable-dht"`
DhtStartingNodes func(network string) dht.StartingNodesGetter
// Called for each anacrolix/dht Server created for the Client.
ConfigureAnacrolixDhtServer func(*dht.ServerConfig)
PeriodicallyAnnounceTorrentsToDht bool
DisablePEX bool `long:"disable-pex"`
// Never send chunks to peers.
NoUpload bool `long:"no-upload"`
@ -93,13 +112,6 @@ type ClientConfig struct {
HTTPProxy func(*http.Request) (*url.URL, error)
// Defines DialContext func to use for HTTP requests, such as for fetching metainfo and webtorrent seeds
HTTPDialContext func(ctx context.Context, network, addr string) (net.Conn, 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)
// HTTPUserAgent changes default UserAgent for HTTP requests
HTTPUserAgent string
// HttpRequestDirector modifies the request before it's sent.
@ -152,12 +164,9 @@ type ClientConfig struct {
// Whether to accept peer connections at all.
AcceptPeerConnections bool
// Whether a Client should want conns without delegating to any attached Torrents. This is
// useful when torrents might be added dynmically in callbacks for example.
// useful when torrents might be added dynamically in callbacks for example.
AlwaysWantConns bool
// OnQuery hook func
DHTOnQuery func(query *krpc.Msg, source net.Addr) (propagate bool)
Extensions PeerExtensionBits
// Bits that peers must have set to proceed past handshakes.
MinPeerExtensions PeerExtensionBits
@ -193,15 +202,11 @@ func NewDefaultClientConfig() *ClientConfig {
TorrentPeersLowWater: 50,
HandshakesTimeout: 4 * time.Second,
KeepAliveTimeout: time.Minute,
DhtStartingNodes: func(network string) dht.StartingNodesGetter {
return func() ([]dht.Addr, error) { return dht.GlobalBootstrapAddrs(network) }
},
PeriodicallyAnnounceTorrentsToDht: true,
ListenHost: func(string) string { return "" },
UploadRateLimiter: unlimited,
DownloadRateLimiter: unlimited,
DisableAcceptRateLimiting: true,
DropMutuallyCompletePeers: true,
ListenHost: func(string) string { return "" },
UploadRateLimiter: unlimited,
DownloadRateLimiter: unlimited,
DisableAcceptRateLimiting: true,
DropMutuallyCompletePeers: true,
HeaderObfuscationPolicy: HeaderObfuscationPolicy{
Preferred: true,
RequirePreferred: false,
@ -213,6 +218,10 @@ func NewDefaultClientConfig() *ClientConfig {
AcceptPeerConnections: true,
MaxUnverifiedBytes: 64 << 20,
}
cc.DhtStartingNodes = func(network string) dht.StartingNodesGetter {
return func() ([]dht.Addr, error) { return dht.GlobalBootstrapAddrs(network) }
}
cc.PeriodicallyAnnounceTorrentsToDht = true
return cc
}