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 DefaultHTTPClient = &http.Client{
20 Timeout: time.Second * 15,
21 Transport: &http.Transport{
23 Timeout: 15 * time.Second,
25 TLSHandshakeTimeout: 15 * time.Second,
26 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
29 var DefaultHTTPUserAgent = "Go-Torrent/1.0"
31 // Override Client defaults.
33 // Store torrent file data in this directory unless .DefaultStorage is
35 DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"`
36 // The address to listen for new uTP and TCP bittorrent protocol
37 // connections. DHT shares a UDP socket with uTP unless configured
39 ListenHost func(network string) string
41 NoDefaultPortForwarding bool
42 // Don't announce to trackers. This only leaves DHT to discover peers.
43 DisableTrackers bool `long:"disable-trackers"`
44 DisablePEX bool `long:"disable-pex"`
46 // Don't create a DHT.
47 NoDHT bool `long:"disable-dht"`
48 DhtStartingNodes dht.StartingNodesGetter
49 // Never send chunks to peers.
50 NoUpload bool `long:"no-upload"`
51 // Disable uploading even when it isn't fair.
52 DisableAggressiveUpload bool `long:"disable-aggressive-upload"`
53 // Upload even after there's nothing in it for us. By default uploading is
54 // not altruistic, we'll upload slightly more than we download from each
56 Seed bool `long:"seed"`
57 // Only applies to chunks uploaded to peers, to maintain responsiveness
58 // communicating local Client state to peers. Each limiter token
59 // represents one byte. The Limiter's burst must be large enough to fit a
60 // whole chunk, which is usually 16 KiB (see TorrentSpec.ChunkSize).
61 UploadRateLimiter *rate.Limiter
62 // Rate limits all reads from connections to peers. Each limiter token
63 // represents one byte. The Limiter's burst must be bigger than the
64 // largest Read performed on a the underlying rate-limiting io.Reader
65 // minus one. This is likely to be the larger of the main read loop buffer
66 // (~4096), and the requested chunk size (~16KiB, see
67 // TorrentSpec.ChunkSize).
68 DownloadRateLimiter *rate.Limiter
70 // User-provided Client peer ID. If not present, one is generated automatically.
72 // For the bittorrent protocol.
74 // For the bittorrent protocol.
75 DisableTCP bool `long:"disable-tcp"`
76 // Called to instantiate storage for each added torrent. Builtin backends
77 // are in the storage package. If not set, the "file" implementation is
79 DefaultStorage storage.ClientImpl
83 // Sets usage of Socks5 Proxy. Authentication should be included in the url if needed.
84 // Example of setting: "socks5://demo:demo@192.168.99.100:1080"
87 IPBlocklist iplist.Ranger
88 DisableIPv6 bool `long:"disable-ipv6"`
91 // Perform logging and any other behaviour that will help debug.
92 Debug bool `help:"enable debugging"`
94 // HTTP client used to query the tracker endpoint. Default is DefaultHTTPClient
96 // HTTPUserAgent changes default UserAgent for HTTP requests
97 HTTPUserAgent string `long:"http-user-agent"`
98 // Updated occasionally to when there's been some changes to client
99 // behaviour in case other clients are assuming anything of us. See also
101 ExtendedHandshakeClientVersion string // default "go.torrent dev 20150624"
102 // Peer ID client identifier prefix. We'll update this occasionally to
103 // reflect changes to client behaviour that other clients may depend on.
104 // Also see `extendedHandshakeClientVersion`.
105 Bep20 string // default "-GT0001-"
107 NominalDialTimeout time.Duration // default time.Second * 30
108 MinDialTimeout time.Duration // default 5 * time.Second
109 EstablishedConnsPerTorrent int // default 80
110 HalfOpenConnsPerTorrent int // default 80
111 TorrentPeersHighWater int // default 200
112 TorrentPeersLowWater int // default 50
114 // Limit how long handshake can take. This is to reduce the lingering
115 // impact of a few bad apples. 4s loses 1% of successful handshakes that
116 // are obtained with 60s timeout, and 5% of unsuccessful handshakes.
117 HandshakesTimeout time.Duration // default 20 * time.Second
123 func (cfg *Config) SetListenAddr(addr string) *Config {
124 host, port, err := missinggo.ParseHostPort(addr)
126 cfg.ListenHost = func(string) string { return host }
127 cfg.ListenPort = port
131 func (cfg *Config) setDefaults() {
133 cfg.HTTP = DefaultHTTPClient
134 if cfg.ProxyURL != "" {
138 if cfg.HTTPUserAgent == "" {
139 cfg.HTTPUserAgent = DefaultHTTPUserAgent
141 if cfg.ExtendedHandshakeClientVersion == "" {
142 cfg.ExtendedHandshakeClientVersion = "go.torrent dev 20150624"
145 cfg.Bep20 = "-GT0001-"
147 if cfg.NominalDialTimeout == 0 {
148 cfg.NominalDialTimeout = 30 * time.Second
150 if cfg.MinDialTimeout == 0 {
151 cfg.MinDialTimeout = 5 * time.Second
153 if cfg.EstablishedConnsPerTorrent == 0 {
154 cfg.EstablishedConnsPerTorrent = 50
156 if cfg.HalfOpenConnsPerTorrent == 0 {
157 cfg.HalfOpenConnsPerTorrent = (cfg.EstablishedConnsPerTorrent + 1) / 2
159 if cfg.TorrentPeersHighWater == 0 {
160 // Memory and freshness are the concern here.
161 cfg.TorrentPeersHighWater = 500
163 if cfg.TorrentPeersLowWater == 0 {
164 cfg.TorrentPeersLowWater = 2 * cfg.HalfOpenConnsPerTorrent
166 if cfg.HandshakesTimeout == 0 {
167 cfg.HandshakesTimeout = 20 * time.Second
169 if cfg.DhtStartingNodes == nil {
170 cfg.DhtStartingNodes = dht.GlobalBootstrapAddrs
172 if cfg.ListenHost == nil {
173 cfg.ListenHost = func(string) string { return "" }
177 func (cfg *Config) setProxyURL() {
178 fixedURL, err := url.Parse(cfg.ProxyURL)
183 cfg.HTTP.Transport = &http.Transport{
184 Proxy: http.ProxyURL(fixedURL),
185 TLSHandshakeTimeout: 15 * time.Second,
186 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
190 type EncryptionPolicy struct {
191 DisableEncryption bool
192 ForceEncryption bool // Don't allow unobfuscated connections.
193 PreferNoEncryption bool