9 "golang.org/x/time/rate"
11 "github.com/anacrolix/dht"
12 "github.com/anacrolix/torrent/iplist"
13 "github.com/anacrolix/torrent/storage"
16 var DefaultHTTPClient = &http.Client{
17 Timeout: time.Second * 15,
18 Transport: &http.Transport{
20 Timeout: 15 * time.Second,
22 TLSHandshakeTimeout: 15 * time.Second,
23 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
26 var DefaultHTTPUserAgent = "Go-Torrent/1.0"
28 // Override Client defaults.
30 // Store torrent file data in this directory unless .DefaultStorage is
32 DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"`
33 // The address to listen for new uTP and TCP bittorrent protocol
34 // connections. DHT shares a UDP socket with uTP unless configured
36 ListenAddr string `long:"listen-addr" value-name:"HOST:PORT"`
37 NoDefaultPortForwarding bool
38 // Don't announce to trackers. This only leaves DHT to discover peers.
39 DisableTrackers bool `long:"disable-trackers"`
40 DisablePEX bool `long:"disable-pex"`
42 // Don't create a DHT.
43 NoDHT bool `long:"disable-dht"`
44 DhtStartingNodes dht.StartingNodesGetter
45 // Never send chunks to peers.
46 NoUpload bool `long:"no-upload"`
47 // Disable uploading even when it isn't fair.
48 DisableAggressiveUpload bool `long:"disable-aggressive-upload"`
49 // Upload even after there's nothing in it for us. By default uploading is
50 // not altruistic, we'll upload slightly more than we download from each
52 Seed bool `long:"seed"`
53 // Only applies to chunks uploaded to peers, to maintain responsiveness
54 // communicating local Client state to peers. Each limiter token
55 // represents one byte. The Limiter's burst must be large enough to fit a
56 // whole chunk, which is usually 16 KiB (see TorrentSpec.ChunkSize).
57 UploadRateLimiter *rate.Limiter
58 // Rate limits all reads from connections to peers. Each limiter token
59 // represents one byte. The Limiter's burst must be bigger than the
60 // largest Read performed on a the underlying rate-limiting io.Reader
61 // minus one. This is likely to be the larger of the main read loop buffer
62 // (~4096), and the requested chunk size (~16KiB, see
63 // TorrentSpec.ChunkSize).
64 DownloadRateLimiter *rate.Limiter
66 // User-provided Client peer ID. If not present, one is generated automatically.
68 // For the bittorrent protocol.
70 // For the bittorrent protocol.
71 DisableTCP bool `long:"disable-tcp"`
72 // Called to instantiate storage for each added torrent. Builtin backends
73 // are in the storage package. If not set, the "file" implementation is
75 DefaultStorage storage.ClientImpl
79 IPBlocklist iplist.Ranger
80 DisableIPv6 bool `long:"disable-ipv6"`
83 // Perform logging and any other behaviour that will help debug.
84 Debug bool `help:"enable debugging"`
86 // HTTP client used to query the tracker endpoint. Default is DefaultHTTPClient
88 // HTTPUserAgent changes default UserAgent for HTTP requests
89 HTTPUserAgent string `long:"http-user-agent"`
90 // Updated occasionally to when there's been some changes to client
91 // behaviour in case other clients are assuming anything of us. See also
93 ExtendedHandshakeClientVersion string // default "go.torrent dev 20150624"
94 // Peer ID client identifier prefix. We'll update this occasionally to
95 // reflect changes to client behaviour that other clients may depend on.
96 // Also see `extendedHandshakeClientVersion`.
97 Bep20 string // default "-GT0001-"
99 NominalDialTimeout time.Duration // default time.Second * 30
100 MinDialTimeout time.Duration // default 5 * time.Second
101 EstablishedConnsPerTorrent int // default 80
102 HalfOpenConnsPerTorrent int // default 80
103 TorrentPeersHighWater int // default 200
104 TorrentPeersLowWater int // default 50
106 // Limit how long handshake can take. This is to reduce the lingering
107 // impact of a few bad apples. 4s loses 1% of successful handshakes that
108 // are obtained with 60s timeout, and 5% of unsuccessful handshakes.
109 HandshakesTimeout time.Duration // default 20 * time.Second
115 func (cfg *Config) setDefaults() {
117 cfg.HTTP = DefaultHTTPClient
119 if cfg.HTTPUserAgent == "" {
120 cfg.HTTPUserAgent = DefaultHTTPUserAgent
122 if cfg.ExtendedHandshakeClientVersion == "" {
123 cfg.ExtendedHandshakeClientVersion = "go.torrent dev 20150624"
126 cfg.Bep20 = "-GT0001-"
128 if cfg.NominalDialTimeout == 0 {
129 cfg.NominalDialTimeout = 30 * time.Second
131 if cfg.MinDialTimeout == 0 {
132 cfg.MinDialTimeout = 5 * time.Second
134 if cfg.EstablishedConnsPerTorrent == 0 {
135 cfg.EstablishedConnsPerTorrent = 50
137 if cfg.HalfOpenConnsPerTorrent == 0 {
138 cfg.HalfOpenConnsPerTorrent = (cfg.EstablishedConnsPerTorrent + 1) / 2
140 if cfg.TorrentPeersHighWater == 0 {
141 // Memory and freshness are the concern here.
142 cfg.TorrentPeersHighWater = 500
144 if cfg.TorrentPeersLowWater == 0 {
145 cfg.TorrentPeersLowWater = 2 * cfg.HalfOpenConnsPerTorrent
147 if cfg.HandshakesTimeout == 0 {
148 cfg.HandshakesTimeout = 20 * time.Second
150 if cfg.DhtStartingNodes == nil {
151 cfg.DhtStartingNodes = dht.GlobalBootstrapAddrs
155 type EncryptionPolicy struct {
156 DisableEncryption bool
157 ForceEncryption bool // Don't allow unobfuscated connections.
158 PreferNoEncryption bool