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 upload slightly more than we download from each
46 Seed bool `long:"seed"`
47 // Only applies to chunks uploaded to peers, to maintain responsiveness
48 // communicating local Client state to peers. Each limiter token
49 // represents one byte. The Limiter's burst must be large enough to fit a
50 // whole chunk, which is usually 16 KiB (see TorrentSpec.ChunkSize).
51 UploadRateLimiter *rate.Limiter
52 // Rate limits all reads from connections to peers. Each limiter token
53 // represents one byte. The Limiter's burst must be bigger than the
54 // largest Read performed on a the underlying rate-limiting io.Reader
55 // minus one. This is likely to be the larger of the main read loop buffer
56 // (~4096), and the requested chunk size (~16KiB, see
57 // TorrentSpec.ChunkSize).
58 DownloadRateLimiter *rate.Limiter
60 // User-provided Client peer ID. If not present, one is generated automatically.
62 // For the bittorrent protocol.
64 // For the bittorrent protocol.
65 DisableTCP bool `long:"disable-tcp"`
66 // Called to instantiate storage for each added torrent. Builtin backends
67 // are in the storage package. If not set, the "file" implementation is
69 DefaultStorage storage.ClientImpl
73 // Sets usage of Socks5 Proxy. Authentication should be included in the url if needed.
74 // Example of setting: "socks5://demo:demo@192.168.99.100:1080"
77 IPBlocklist iplist.Ranger
78 DisableIPv6 bool `long:"disable-ipv6"`
81 // Perform logging and any other behaviour that will help debug.
82 Debug bool `help:"enable debugging"`
84 // HTTP client used to query the tracker endpoint. Default is DefaultHTTPClient
86 // HTTPUserAgent changes default UserAgent for HTTP requests
87 HTTPUserAgent string `long:"http-user-agent"`
88 // Updated occasionally to when there's been some changes to client
89 // behaviour in case other clients are assuming anything of us. See also
91 ExtendedHandshakeClientVersion string // default "go.torrent dev 20150624"
92 // Peer ID client identifier prefix. We'll update this occasionally to
93 // reflect changes to client behaviour that other clients may depend on.
94 // Also see `extendedHandshakeClientVersion`.
95 Bep20 string // default "-GT0001-"
97 NominalDialTimeout time.Duration // default time.Second * 30
98 MinDialTimeout time.Duration // default 5 * time.Second
99 EstablishedConnsPerTorrent int // default 80
100 HalfOpenConnsPerTorrent int // default 80
101 TorrentPeersHighWater int // default 200
102 TorrentPeersLowWater int // default 50
104 // Limit how long handshake can take. This is to reduce the lingering
105 // impact of a few bad apples. 4s loses 1% of successful handshakes that
106 // are obtained with 60s timeout, and 5% of unsuccessful handshakes.
107 HandshakesTimeout time.Duration // default 20 * time.Second
112 DisableAcceptRateLimiting bool
113 dropDuplicatePeerIds bool
116 func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
117 host, port, err := missinggo.ParseHostPort(addr)
119 cfg.ListenHost = func(string) string { return host }
120 cfg.ListenPort = port
124 func NewDefaultClientConfig() *ClientConfig {
125 return &ClientConfig{
127 Timeout: time.Second * 15,
128 Transport: &http.Transport{
130 Timeout: 15 * time.Second,
132 TLSHandshakeTimeout: 15 * time.Second,
133 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
135 HTTPUserAgent: DefaultHTTPUserAgent,
136 ExtendedHandshakeClientVersion: "go.torrent dev 20150624",
138 NominalDialTimeout: 30 * time.Second,
139 MinDialTimeout: 5 * time.Second,
140 EstablishedConnsPerTorrent: 50,
141 HalfOpenConnsPerTorrent: 25,
142 TorrentPeersHighWater: 500,
143 TorrentPeersLowWater: 50,
144 HandshakesTimeout: 20 * time.Second,
145 DhtStartingNodes: dht.GlobalBootstrapAddrs,
146 ListenHost: func(string) string { return "" },
147 UploadRateLimiter: unlimited,
148 DownloadRateLimiter: unlimited,
152 func (cfg *ClientConfig) setProxyURL() {
153 fixedURL, err := url.Parse(cfg.ProxyURL)
158 cfg.HTTP.Transport = &http.Transport{
159 Proxy: http.ProxyURL(fixedURL),
160 TLSHandshakeTimeout: 15 * time.Second,
161 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
165 type EncryptionPolicy struct {
166 DisableEncryption bool
167 ForceEncryption bool // Don't allow unobfuscated connections.
168 PreferNoEncryption bool