9 "github.com/anacrolix/dht"
10 "github.com/anacrolix/missinggo"
11 "github.com/anacrolix/missinggo/expect"
12 "github.com/anacrolix/torrent/iplist"
13 "github.com/anacrolix/torrent/storage"
14 "golang.org/x/time/rate"
17 var DefaultHTTPUserAgent = "Go-Torrent/1.0"
19 // Probably not safe to modify this after it's given to a Client.
20 type ClientConfig struct {
21 // Store torrent file data in this directory unless .DefaultStorage is
23 DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"`
24 // The address to listen for new uTP and TCP bittorrent protocol
25 // connections. DHT shares a UDP socket with uTP unless configured
27 ListenHost func(network string) string
29 NoDefaultPortForwarding bool
30 // Don't announce to trackers. This only leaves DHT to discover peers.
31 DisableTrackers bool `long:"disable-trackers"`
32 DisablePEX bool `long:"disable-pex"`
34 // Don't create a DHT.
35 NoDHT bool `long:"disable-dht"`
36 DhtStartingNodes dht.StartingNodesGetter
37 // Never send chunks to peers.
38 NoUpload bool `long:"no-upload"`
39 // Disable uploading even when it isn't fair.
40 DisableAggressiveUpload bool `long:"disable-aggressive-upload"`
41 // Upload even after there's nothing in it for us. By default uploading is
42 // not altruistic, we'll only upload to encourage the peer to reciprocate.
43 Seed bool `long:"seed"`
44 // Only applies to chunks uploaded to peers, to maintain responsiveness
45 // communicating local Client state to peers. Each limiter token
46 // represents one byte. The Limiter's burst must be large enough to fit a
47 // whole chunk, which is usually 16 KiB (see TorrentSpec.ChunkSize).
48 UploadRateLimiter *rate.Limiter
49 // Rate limits all reads from connections to peers. Each limiter token
50 // represents one byte. The Limiter's burst must be bigger than the
51 // largest Read performed on a the underlying rate-limiting io.Reader
52 // minus one. This is likely to be the larger of the main read loop buffer
53 // (~4096), and the requested chunk size (~16KiB, see
54 // TorrentSpec.ChunkSize).
55 DownloadRateLimiter *rate.Limiter
57 // User-provided Client peer ID. If not present, one is generated automatically.
59 // For the bittorrent protocol.
61 // For the bittorrent protocol.
62 DisableTCP bool `long:"disable-tcp"`
63 // Called to instantiate storage for each added torrent. Builtin backends
64 // are in the storage package. If not set, the "file" implementation is
66 DefaultStorage storage.ClientImpl
70 // Sets usage of Socks5 Proxy. Authentication should be included in the url if needed.
71 // Examples: socks5://demo:demo@192.168.99.100:1080
72 // http://proxy.domain.com:3128
75 IPBlocklist iplist.Ranger
76 DisableIPv6 bool `long:"disable-ipv6"`
79 // Perform logging and any other behaviour that will help debug.
80 Debug bool `help:"enable debugging"`
82 // HTTPProxy defines proxy for HTTP requests.
83 // Format: func(*Request) (*url.URL, error),
84 // or result of http.ProxyURL(HTTPProxy).
85 // By default, it is composed from ClientConfig.ProxyURL,
86 // if not set explicitly in ClientConfig struct
87 HTTPProxy func(*http.Request) (*url.URL, error)
88 // HTTPUserAgent changes default UserAgent for HTTP requests
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 // Peer dial timeout to use when there are limited peers.
100 NominalDialTimeout time.Duration
101 // Minimum peer dial timeout to use (even if we have lots of peers).
102 MinDialTimeout time.Duration
103 EstablishedConnsPerTorrent int
104 HalfOpenConnsPerTorrent int
105 // Maximum number of peer addresses in reserve.
106 TorrentPeersHighWater int
107 // Minumum number of peers before effort is made to obtain more peers.
108 TorrentPeersLowWater int
110 // Limit how long handshake can take. This is to reduce the lingering
111 // impact of a few bad apples. 4s loses 1% of successful handshakes that
112 // are obtained with 60s timeout, and 5% of unsuccessful handshakes.
113 HandshakesTimeout time.Duration
115 // The IP addresses as our peers should see them. May differ from the
116 // local interfaces due to NAT or other network configurations.
120 DisableAcceptRateLimiting bool
121 // Don't add connections that have the same peer ID as an existing
122 // connection for a given Torrent.
123 dropDuplicatePeerIds bool
126 func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
127 host, port, err := missinggo.ParseHostPort(addr)
129 cfg.ListenHost = func(string) string { return host }
130 cfg.ListenPort = port
134 func NewDefaultClientConfig() *ClientConfig {
135 return &ClientConfig{
136 HTTPUserAgent: DefaultHTTPUserAgent,
137 ExtendedHandshakeClientVersion: "go.torrent dev 20150624",
139 NominalDialTimeout: 20 * time.Second,
140 MinDialTimeout: 3 * time.Second,
141 EstablishedConnsPerTorrent: 50,
142 HalfOpenConnsPerTorrent: 25,
143 TorrentPeersHighWater: 500,
144 TorrentPeersLowWater: 50,
145 HandshakesTimeout: 4 * time.Second,
146 DhtStartingNodes: dht.GlobalBootstrapAddrs,
147 ListenHost: func(string) string { return "" },
148 UploadRateLimiter: unlimited,
149 DownloadRateLimiter: unlimited,
153 type EncryptionPolicy struct {
154 DisableEncryption bool
155 ForceEncryption bool // Don't allow unobfuscated connections.
156 PreferNoEncryption bool