7 "github.com/anacrolix/dht"
8 "github.com/anacrolix/missinggo"
9 "github.com/anacrolix/missinggo/expect"
10 "golang.org/x/time/rate"
12 "github.com/anacrolix/torrent/iplist"
13 "github.com/anacrolix/torrent/storage"
16 var DefaultHTTPUserAgent = "Go-Torrent/1.0"
18 // Probably not safe to modify this after it's given to a Client.
19 type ClientConfig struct {
20 // Store torrent file data in this directory unless .DefaultStorage is
22 DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"`
23 // The address to listen for new uTP and TCP bittorrent protocol
24 // connections. DHT shares a UDP socket with uTP unless configured
26 ListenHost func(network string) string
28 NoDefaultPortForwarding bool
29 // Don't announce to trackers. This only leaves DHT to discover peers.
30 DisableTrackers bool `long:"disable-trackers"`
31 DisablePEX bool `long:"disable-pex"`
33 // Don't create a DHT.
34 NoDHT bool `long:"disable-dht"`
35 DhtStartingNodes dht.StartingNodesGetter
36 // Never send chunks to peers.
37 NoUpload bool `long:"no-upload"`
38 // Disable uploading even when it isn't fair.
39 DisableAggressiveUpload bool `long:"disable-aggressive-upload"`
40 // Upload even after there's nothing in it for us. By default uploading is
41 // not altruistic, we'll only upload to encourage the peer to reciprocate.
42 Seed bool `long:"seed"`
43 // Only applies to chunks uploaded to peers, to maintain responsiveness
44 // communicating local Client state to peers. Each limiter token
45 // represents one byte. The Limiter's burst must be large enough to fit a
46 // whole chunk, which is usually 16 KiB (see TorrentSpec.ChunkSize).
47 UploadRateLimiter *rate.Limiter
48 // Rate limits all reads from connections to peers. Each limiter token
49 // represents one byte. The Limiter's burst must be bigger than the
50 // largest Read performed on a the underlying rate-limiting io.Reader
51 // minus one. This is likely to be the larger of the main read loop buffer
52 // (~4096), and the requested chunk size (~16KiB, see
53 // TorrentSpec.ChunkSize).
54 DownloadRateLimiter *rate.Limiter
56 // User-provided Client peer ID. If not present, one is generated automatically.
58 // For the bittorrent protocol.
60 // For the bittorrent protocol.
61 DisableTCP bool `long:"disable-tcp"`
62 // Called to instantiate storage for each added torrent. Builtin backends
63 // are in the storage package. If not set, the "file" implementation is
65 DefaultStorage storage.ClientImpl
69 // Sets usage of Socks5 Proxy. Authentication should be included in the url if needed.
70 // Example of setting: "socks5://demo:demo@192.168.99.100:1080"
73 IPBlocklist iplist.Ranger
74 DisableIPv6 bool `long:"disable-ipv6"`
77 // Perform logging and any other behaviour that will help debug.
78 Debug bool `help:"enable debugging"`
80 // HTTPUserAgent changes default UserAgent for HTTP requests
82 // Updated occasionally to when there's been some changes to client
83 // behaviour in case other clients are assuming anything of us. See also
85 ExtendedHandshakeClientVersion string // default "go.torrent dev 20150624"
86 // Peer ID client identifier prefix. We'll update this occasionally to
87 // reflect changes to client behaviour that other clients may depend on.
88 // Also see `extendedHandshakeClientVersion`.
89 Bep20 string // default "-GT0001-"
91 // Peer dial timeout to use when there are limited peers.
92 NominalDialTimeout time.Duration
93 // Minimum peer dial timeout to use (even if we have lots of peers).
94 MinDialTimeout time.Duration
95 EstablishedConnsPerTorrent int
96 HalfOpenConnsPerTorrent int
97 // Maximum number of peer addresses in reserve.
98 TorrentPeersHighWater int
99 // Minumum number of peers before effort is made to obtain more peers.
100 TorrentPeersLowWater int
102 // Limit how long handshake can take. This is to reduce the lingering
103 // impact of a few bad apples. 4s loses 1% of successful handshakes that
104 // are obtained with 60s timeout, and 5% of unsuccessful handshakes.
105 HandshakesTimeout time.Duration
107 // The IP addresses as our peers should see them. May differ from the
108 // local interfaces due to NAT or other network configurations.
112 DisableAcceptRateLimiting bool
113 // Don't add connections that have the same peer ID as an existing
114 // connection for a given Torrent.
115 dropDuplicatePeerIds bool
118 func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
119 host, port, err := missinggo.ParseHostPort(addr)
121 cfg.ListenHost = func(string) string { return host }
122 cfg.ListenPort = port
126 func NewDefaultClientConfig() *ClientConfig {
127 return &ClientConfig{
128 HTTPUserAgent: DefaultHTTPUserAgent,
129 ExtendedHandshakeClientVersion: "go.torrent dev 20150624",
131 NominalDialTimeout: 20 * time.Second,
132 MinDialTimeout: 3 * time.Second,
133 EstablishedConnsPerTorrent: 50,
134 HalfOpenConnsPerTorrent: 25,
135 TorrentPeersHighWater: 500,
136 TorrentPeersLowWater: 50,
137 HandshakesTimeout: 4 * time.Second,
138 DhtStartingNodes: dht.GlobalBootstrapAddrs,
139 ListenHost: func(string) string { return "" },
140 UploadRateLimiter: unlimited,
141 DownloadRateLimiter: unlimited,
145 type EncryptionPolicy struct {
146 DisableEncryption bool
147 ForceEncryption bool // Don't allow unobfuscated connections.
148 PreferNoEncryption bool