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