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