2014-08-21 18:07:06 +10:00
|
|
|
package torrent
|
|
|
|
|
2014-11-28 12:13:08 -06:00
|
|
|
import (
|
2022-07-07 01:51:58 -04:00
|
|
|
"context"
|
2017-12-01 18:12:29 +11:00
|
|
|
"net"
|
2018-10-30 23:32:33 +01:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-12-01 18:12:29 +11:00
|
|
|
"time"
|
|
|
|
|
2019-08-10 18:46:07 +10:00
|
|
|
"github.com/anacrolix/dht/v2"
|
|
|
|
"github.com/anacrolix/dht/v2/krpc"
|
2019-08-21 20:44:12 +10:00
|
|
|
"github.com/anacrolix/log"
|
2021-06-25 11:29:59 +10:00
|
|
|
"github.com/anacrolix/missinggo/v2"
|
2019-08-21 20:58:40 +10:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
2015-08-04 01:07:22 +10:00
|
|
|
"github.com/anacrolix/torrent/iplist"
|
2019-07-19 13:23:36 +10:00
|
|
|
"github.com/anacrolix/torrent/mse"
|
2016-03-28 20:38:30 +11:00
|
|
|
"github.com/anacrolix/torrent/storage"
|
2022-11-15 23:22:10 +11:00
|
|
|
"github.com/anacrolix/torrent/version"
|
2014-11-28 12:13:08 -06:00
|
|
|
)
|
|
|
|
|
2022-12-07 10:42:29 +11:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2018-06-16 16:30:04 +10:00
|
|
|
// Probably not safe to modify this after it's given to a Client.
|
|
|
|
type ClientConfig struct {
|
2022-12-07 10:42:29 +11:00
|
|
|
ClientTrackerConfig
|
|
|
|
ClientDhtConfig
|
|
|
|
|
2018-01-11 17:11:54 +11:00
|
|
|
// Store torrent file data in this directory unless .DefaultStorage is
|
2015-03-08 17:28:14 +11:00
|
|
|
// specified.
|
2015-03-25 15:41:15 +11:00
|
|
|
DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"`
|
2020-02-20 16:47:37 +11:00
|
|
|
// The address to listen for new uTP and TCP BitTorrent protocol connections. DHT shares a UDP
|
|
|
|
// socket with uTP unless configured otherwise.
|
2018-04-12 15:06:53 +10:00
|
|
|
ListenHost func(network string) string
|
|
|
|
ListenPort int
|
2018-01-25 13:14:20 +11:00
|
|
|
NoDefaultPortForwarding bool
|
2019-12-23 14:04:07 +11:00
|
|
|
UpnpID string
|
2022-12-07 10:42:29 +11:00
|
|
|
DisablePEX bool `long:"disable-pex"`
|
2021-02-20 22:16:54 +11:00
|
|
|
|
2016-10-10 14:57:34 +11:00
|
|
|
// Never send chunks to peers.
|
2015-03-25 15:41:15 +11:00
|
|
|
NoUpload bool `long:"no-upload"`
|
2017-11-05 03:04:33 +00:00
|
|
|
// Disable uploading even when it isn't fair.
|
|
|
|
DisableAggressiveUpload bool `long:"disable-aggressive-upload"`
|
2015-05-15 08:39:53 +10:00
|
|
|
// Upload even after there's nothing in it for us. By default uploading is
|
2018-07-07 11:33:48 +10:00
|
|
|
// not altruistic, we'll only upload to encourage the peer to reciprocate.
|
2015-05-15 08:39:53 +10:00
|
|
|
Seed bool `long:"seed"`
|
2018-01-07 20:31:10 +11:00
|
|
|
// Only applies to chunks uploaded to peers, to maintain responsiveness
|
|
|
|
// communicating local Client state to peers. Each limiter token
|
|
|
|
// represents one byte. The Limiter's burst must be large enough to fit a
|
|
|
|
// whole chunk, which is usually 16 KiB (see TorrentSpec.ChunkSize).
|
2016-10-10 17:29:39 +11:00
|
|
|
UploadRateLimiter *rate.Limiter
|
2018-01-07 20:31:10 +11:00
|
|
|
// Rate limits all reads from connections to peers. Each limiter token
|
|
|
|
// represents one byte. The Limiter's burst must be bigger than the
|
|
|
|
// largest Read performed on a the underlying rate-limiting io.Reader
|
|
|
|
// minus one. This is likely to be the larger of the main read loop buffer
|
|
|
|
// (~4096), and the requested chunk size (~16KiB, see
|
|
|
|
// TorrentSpec.ChunkSize).
|
2016-10-10 17:29:39 +11:00
|
|
|
DownloadRateLimiter *rate.Limiter
|
2021-05-14 13:40:09 +10:00
|
|
|
// Maximum unverified bytes across all torrents. Not used if zero.
|
|
|
|
MaxUnverifiedBytes int64
|
2016-10-10 14:58:29 +11:00
|
|
|
|
2015-03-08 17:28:14 +11:00
|
|
|
// User-provided Client peer ID. If not present, one is generated automatically.
|
|
|
|
PeerID string
|
|
|
|
// For the bittorrent protocol.
|
|
|
|
DisableUTP bool
|
|
|
|
// For the bittorrent protocol.
|
2015-04-20 17:35:21 +10:00
|
|
|
DisableTCP bool `long:"disable-tcp"`
|
2016-09-16 12:13:06 +10:00
|
|
|
// Called to instantiate storage for each added torrent. Builtin backends
|
|
|
|
// are in the storage package. If not set, the "file" implementation is
|
2020-02-21 14:12:44 +11:00
|
|
|
// used (and Closed when the Client is Closed).
|
2016-09-16 12:42:41 +10:00
|
|
|
DefaultStorage storage.ClientImpl
|
|
|
|
|
2019-07-19 13:23:36 +10:00
|
|
|
HeaderObfuscationPolicy HeaderObfuscationPolicy
|
2019-07-23 11:30:05 +10:00
|
|
|
// The crypto methods to offer when initiating connections with header obfuscation.
|
|
|
|
CryptoProvides mse.CryptoMethod
|
|
|
|
// Chooses the crypto method to use when receiving connections with header obfuscation.
|
|
|
|
CryptoSelector mse.CryptoSelector
|
2015-08-04 01:07:22 +10:00
|
|
|
|
2018-02-16 10:46:11 +11:00
|
|
|
IPBlocklist iplist.Ranger
|
|
|
|
DisableIPv6 bool `long:"disable-ipv6"`
|
|
|
|
DisableIPv4 bool
|
|
|
|
DisableIPv4Peers bool
|
2015-08-23 12:59:03 +10:00
|
|
|
// Perform logging and any other behaviour that will help debug.
|
2019-08-21 20:44:12 +10:00
|
|
|
Debug bool `help:"enable debugging"`
|
|
|
|
Logger log.Logger
|
2017-11-07 20:14:13 +02:00
|
|
|
|
2020-02-20 17:04:11 +11:00
|
|
|
// Defines proxy for HTTP requests, such as for trackers. It's commonly set from the result of
|
|
|
|
// "net/http".ProxyURL(HTTPProxy).
|
2018-10-30 23:32:33 +01:00
|
|
|
HTTPProxy func(*http.Request) (*url.URL, error)
|
2022-11-14 00:19:02 -08:00
|
|
|
// 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)
|
2017-12-28 10:47:51 +02:00
|
|
|
// HTTPUserAgent changes default UserAgent for HTTP requests
|
2018-07-11 15:10:58 +10:00
|
|
|
HTTPUserAgent string
|
2022-11-29 10:44:46 +11:00
|
|
|
// HttpRequestDirector modifies the request before it's sent.
|
2022-11-28 23:35:36 +00:00
|
|
|
// Useful for adding authentication headers, for example
|
2022-11-29 10:44:46 +11:00
|
|
|
HttpRequestDirector func(*http.Request) error
|
2022-12-07 22:17:33 +00:00
|
|
|
// WebsocketTrackerHttpHeader returns a custom header to be used when dialing a websocket connection
|
|
|
|
// to the tracker. Useful for adding authentication headers
|
|
|
|
WebsocketTrackerHttpHeader func() http.Header
|
2017-11-07 20:14:13 +02:00
|
|
|
// Updated occasionally to when there's been some changes to client
|
|
|
|
// behaviour in case other clients are assuming anything of us. See also
|
|
|
|
// `bep20`.
|
2018-11-21 17:06:31 +11:00
|
|
|
ExtendedHandshakeClientVersion string
|
2017-11-07 20:14:13 +02:00
|
|
|
// Peer ID client identifier prefix. We'll update this occasionally to
|
|
|
|
// reflect changes to client behaviour that other clients may depend on.
|
|
|
|
// Also see `extendedHandshakeClientVersion`.
|
2018-11-21 17:06:31 +11:00
|
|
|
Bep20 string
|
2017-11-07 20:14:13 +02:00
|
|
|
|
2018-07-10 11:19:14 +10:00
|
|
|
// Peer dial timeout to use when there are limited peers.
|
|
|
|
NominalDialTimeout time.Duration
|
|
|
|
// Minimum peer dial timeout to use (even if we have lots of peers).
|
|
|
|
MinDialTimeout time.Duration
|
|
|
|
EstablishedConnsPerTorrent int
|
|
|
|
HalfOpenConnsPerTorrent int
|
2020-09-30 16:56:27 +10:00
|
|
|
TotalHalfOpenConns int
|
2018-09-03 11:48:11 +10:00
|
|
|
// Maximum number of peer addresses in reserve.
|
|
|
|
TorrentPeersHighWater int
|
|
|
|
// Minumum number of peers before effort is made to obtain more peers.
|
|
|
|
TorrentPeersLowWater int
|
2017-11-07 20:14:13 +02:00
|
|
|
|
|
|
|
// Limit how long handshake can take. This is to reduce the lingering
|
|
|
|
// impact of a few bad apples. 4s loses 1% of successful handshakes that
|
|
|
|
// are obtained with 60s timeout, and 5% of unsuccessful handshakes.
|
2018-07-10 11:19:14 +10:00
|
|
|
HandshakesTimeout time.Duration
|
2021-08-18 11:27:21 +10:00
|
|
|
// How long between writes before sending a keep alive message on a peer connection that we want
|
|
|
|
// to maintain.
|
|
|
|
KeepAliveTimeout time.Duration
|
2023-02-14 12:46:23 +11:00
|
|
|
// Maximum bytes to buffer per peer connection for peer request data before it is sent.
|
|
|
|
MaxAllocPeerRequestDataPerConn int64
|
2018-03-29 14:29:15 +11:00
|
|
|
|
2018-09-03 11:48:11 +10:00
|
|
|
// The IP addresses as our peers should see them. May differ from the
|
|
|
|
// local interfaces due to NAT or other network configurations.
|
2018-03-29 14:29:15 +11:00
|
|
|
PublicIp4 net.IP
|
|
|
|
PublicIp6 net.IP
|
2018-06-16 16:37:13 +10:00
|
|
|
|
2020-12-21 13:57:59 +11:00
|
|
|
// Accept rate limiting affects excessive connection attempts from IPs that fail during
|
|
|
|
// handshakes or request torrents that we don't have.
|
2018-06-16 16:37:13 +10:00
|
|
|
DisableAcceptRateLimiting bool
|
2018-09-03 11:48:11 +10:00
|
|
|
// Don't add connections that have the same peer ID as an existing
|
|
|
|
// connection for a given Torrent.
|
2020-04-16 14:12:17 +10:00
|
|
|
DropDuplicatePeerIds bool
|
2021-01-05 16:58:45 +11:00
|
|
|
// Drop peers that are complete if we are also complete and have no use for the peer. This is a
|
|
|
|
// bit of a special case, since a peer could also be useless if they're just not interested, or
|
|
|
|
// we don't intend to obtain all of a torrent's data.
|
|
|
|
DropMutuallyCompletePeers bool
|
2021-06-18 14:59:20 +10:00
|
|
|
// Whether to accept peer connections at all.
|
|
|
|
AcceptPeerConnections bool
|
2021-09-21 13:04:51 +10:00
|
|
|
// Whether a Client should want conns without delegating to any attached Torrents. This is
|
2022-12-07 10:42:29 +11:00
|
|
|
// useful when torrents might be added dynamically in callbacks for example.
|
2021-09-21 13:04:51 +10:00
|
|
|
AlwaysWantConns bool
|
2018-11-21 17:02:22 +11:00
|
|
|
|
2020-04-23 13:03:35 +10:00
|
|
|
Extensions PeerExtensionBits
|
2021-10-25 20:52:44 +11:00
|
|
|
// Bits that peers must have set to proceed past handshakes.
|
|
|
|
MinPeerExtensions PeerExtensionBits
|
2020-06-01 18:24:46 +10:00
|
|
|
|
|
|
|
DisableWebtorrent bool
|
2020-06-02 16:17:32 +10:00
|
|
|
DisableWebseeds bool
|
2020-07-15 14:00:47 +10:00
|
|
|
|
|
|
|
Callbacks Callbacks
|
2017-11-07 20:14:13 +02:00
|
|
|
}
|
|
|
|
|
2018-06-16 16:30:04 +10:00
|
|
|
func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
|
2018-04-12 15:06:53 +10:00
|
|
|
host, port, err := missinggo.ParseHostPort(addr)
|
2021-09-23 09:10:30 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-04-12 15:06:53 +10:00
|
|
|
cfg.ListenHost = func(string) string { return host }
|
|
|
|
cfg.ListenPort = port
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2018-06-16 16:30:04 +10:00
|
|
|
func NewDefaultClientConfig() *ClientConfig {
|
2019-04-09 12:54:41 +10:00
|
|
|
cc := &ClientConfig{
|
2021-06-25 11:29:59 +10:00
|
|
|
HTTPUserAgent: version.DefaultHttpUserAgent,
|
|
|
|
ExtendedHandshakeClientVersion: version.DefaultExtendedHandshakeClientVersion,
|
|
|
|
Bep20: version.DefaultBep20Prefix,
|
|
|
|
UpnpID: version.DefaultUpnpId,
|
2019-04-09 12:54:41 +10:00
|
|
|
NominalDialTimeout: 20 * time.Second,
|
|
|
|
MinDialTimeout: 3 * time.Second,
|
|
|
|
EstablishedConnsPerTorrent: 50,
|
|
|
|
HalfOpenConnsPerTorrent: 25,
|
2020-09-30 16:56:27 +10:00
|
|
|
TotalHalfOpenConns: 100,
|
2019-04-09 12:54:41 +10:00
|
|
|
TorrentPeersHighWater: 500,
|
|
|
|
TorrentPeersLowWater: 50,
|
|
|
|
HandshakesTimeout: 4 * time.Second,
|
2021-08-18 11:27:21 +10:00
|
|
|
KeepAliveTimeout: time.Minute,
|
2023-02-14 12:46:23 +11:00
|
|
|
MaxAllocPeerRequestDataPerConn: 1 << 20,
|
2022-12-07 10:42:29 +11:00
|
|
|
ListenHost: func(string) string { return "" },
|
|
|
|
UploadRateLimiter: unlimited,
|
|
|
|
DownloadRateLimiter: unlimited,
|
|
|
|
DisableAcceptRateLimiting: true,
|
|
|
|
DropMutuallyCompletePeers: true,
|
2019-07-19 13:23:36 +10:00
|
|
|
HeaderObfuscationPolicy: HeaderObfuscationPolicy{
|
|
|
|
Preferred: true,
|
|
|
|
RequirePreferred: false,
|
|
|
|
},
|
2021-06-18 14:59:20 +10:00
|
|
|
CryptoSelector: mse.DefaultCryptoSelector,
|
|
|
|
CryptoProvides: mse.AllSupportedCrypto,
|
|
|
|
ListenPort: 42069,
|
|
|
|
Extensions: defaultPeerExtensionBytes(),
|
|
|
|
AcceptPeerConnections: true,
|
2022-05-09 19:37:08 +10:00
|
|
|
MaxUnverifiedBytes: 64 << 20,
|
2018-04-12 23:34:31 +10:00
|
|
|
}
|
2022-12-07 10:42:29 +11:00
|
|
|
cc.DhtStartingNodes = func(network string) dht.StartingNodesGetter {
|
|
|
|
return func() ([]dht.Addr, error) { return dht.GlobalBootstrapAddrs(network) }
|
|
|
|
}
|
|
|
|
cc.PeriodicallyAnnounceTorrentsToDht = true
|
2019-04-09 12:54:41 +10:00
|
|
|
return cc
|
2014-08-21 18:07:06 +10:00
|
|
|
}
|
2017-09-13 18:20:20 +10:00
|
|
|
|
2019-07-19 13:23:36 +10:00
|
|
|
type HeaderObfuscationPolicy struct {
|
2019-07-23 11:30:05 +10:00
|
|
|
RequirePreferred bool // Whether the value of Preferred is a strict requirement.
|
|
|
|
Preferred bool // Whether header obfuscation is preferred.
|
2017-09-13 18:20:20 +10:00
|
|
|
}
|