10 "golang.org/x/time/rate"
12 "github.com/anacrolix/dht"
13 "github.com/anacrolix/missinggo"
14 "github.com/anacrolix/missinggo/expect"
15 "github.com/anacrolix/torrent/iplist"
16 "github.com/anacrolix/torrent/storage"
19 var DefaultHTTPUserAgent = "Go-Torrent/1.0"
21 // Probably not safe to modify this after it's given to a Client.
22 type ClientConfig struct {
23 // Store torrent file data in this directory unless .DefaultStorage is
25 DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"`
26 // The address to listen for new uTP and TCP bittorrent protocol
27 // connections. DHT shares a UDP socket with uTP unless configured
29 ListenHost func(network string) string
31 NoDefaultPortForwarding bool
32 // Don't announce to trackers. This only leaves DHT to discover peers.
33 DisableTrackers bool `long:"disable-trackers"`
34 DisablePEX bool `long:"disable-pex"`
36 // Don't create a DHT.
37 NoDHT bool `long:"disable-dht"`
38 DhtStartingNodes dht.StartingNodesGetter
39 // Never send chunks to peers.
40 NoUpload bool `long:"no-upload"`
41 // Disable uploading even when it isn't fair.
42 DisableAggressiveUpload bool `long:"disable-aggressive-upload"`
43 // Upload even after there's nothing in it for us. By default uploading is
44 // not altruistic, we'll only upload to encourage the peer to reciprocate.
45 Seed bool `long:"seed"`
46 // Only applies to chunks uploaded to peers, to maintain responsiveness
47 // communicating local Client state to peers. Each limiter token
48 // represents one byte. The Limiter's burst must be large enough to fit a
49 // whole chunk, which is usually 16 KiB (see TorrentSpec.ChunkSize).
50 UploadRateLimiter *rate.Limiter
51 // Rate limits all reads from connections to peers. Each limiter token
52 // represents one byte. The Limiter's burst must be bigger than the
53 // largest Read performed on a the underlying rate-limiting io.Reader
54 // minus one. This is likely to be the larger of the main read loop buffer
55 // (~4096), and the requested chunk size (~16KiB, see
56 // TorrentSpec.ChunkSize).
57 DownloadRateLimiter *rate.Limiter
59 // User-provided Client peer ID. If not present, one is generated automatically.
61 // For the bittorrent protocol.
63 // For the bittorrent protocol.
64 DisableTCP bool `long:"disable-tcp"`
65 // Called to instantiate storage for each added torrent. Builtin backends
66 // are in the storage package. If not set, the "file" implementation is
68 DefaultStorage storage.ClientImpl
72 // Sets usage of Socks5 Proxy. Authentication should be included in the url if needed.
73 // Example of setting: "socks5://demo:demo@192.168.99.100:1080"
76 IPBlocklist iplist.Ranger
77 DisableIPv6 bool `long:"disable-ipv6"`
80 // Perform logging and any other behaviour that will help debug.
81 Debug bool `help:"enable debugging"`
83 // HTTP client used to query the tracker endpoint. Default is DefaultHTTPClient
85 // HTTPUserAgent changes default UserAgent for HTTP requests
86 HTTPUserAgent string `long:"http-user-agent"`
87 // Updated occasionally to when there's been some changes to client
88 // behaviour in case other clients are assuming anything of us. See also
90 ExtendedHandshakeClientVersion string // default "go.torrent dev 20150624"
91 // Peer ID client identifier prefix. We'll update this occasionally to
92 // reflect changes to client behaviour that other clients may depend on.
93 // Also see `extendedHandshakeClientVersion`.
94 Bep20 string // default "-GT0001-"
96 NominalDialTimeout time.Duration // default time.Second * 30
97 MinDialTimeout time.Duration // default 5 * time.Second
98 EstablishedConnsPerTorrent int // default 80
99 HalfOpenConnsPerTorrent int // default 80
100 TorrentPeersHighWater int // default 200
101 TorrentPeersLowWater int // default 50
103 // Limit how long handshake can take. This is to reduce the lingering
104 // impact of a few bad apples. 4s loses 1% of successful handshakes that
105 // are obtained with 60s timeout, and 5% of unsuccessful handshakes.
106 HandshakesTimeout time.Duration // default 20 * time.Second
111 DisableAcceptRateLimiting bool
112 dropDuplicatePeerIds bool
115 func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
116 host, port, err := missinggo.ParseHostPort(addr)
118 cfg.ListenHost = func(string) string { return host }
119 cfg.ListenPort = port
123 func NewDefaultClientConfig() *ClientConfig {
124 return &ClientConfig{
126 Timeout: time.Second * 15,
127 Transport: &http.Transport{
129 Timeout: 15 * time.Second,
131 TLSHandshakeTimeout: 15 * time.Second,
132 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
134 HTTPUserAgent: DefaultHTTPUserAgent,
135 ExtendedHandshakeClientVersion: "go.torrent dev 20150624",
137 NominalDialTimeout: 30 * time.Second,
138 MinDialTimeout: 5 * time.Second,
139 EstablishedConnsPerTorrent: 50,
140 HalfOpenConnsPerTorrent: 25,
141 TorrentPeersHighWater: 500,
142 TorrentPeersLowWater: 50,
143 HandshakesTimeout: 20 * time.Second,
144 DhtStartingNodes: dht.GlobalBootstrapAddrs,
145 ListenHost: func(string) string { return "" },
146 UploadRateLimiter: unlimited,
147 DownloadRateLimiter: unlimited,
151 func (cfg *ClientConfig) setProxyURL() {
152 fixedURL, err := url.Parse(cfg.ProxyURL)
157 cfg.HTTP.Transport = &http.Transport{
158 Proxy: http.ProxyURL(fixedURL),
159 TLSHandshakeTimeout: 15 * time.Second,
160 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
164 type EncryptionPolicy struct {
165 DisableEncryption bool
166 ForceEncryption bool // Don't allow unobfuscated connections.
167 PreferNoEncryption bool