]> Sergey Matveev's repositories - btrtrc.git/blob - config.go
Add comments to some ClientConfig fields
[btrtrc.git] / config.go
1 package torrent
2
3 import (
4         "crypto/tls"
5         "net"
6         "net/http"
7         "time"
8
9         "github.com/anacrolix/dht"
10         "github.com/anacrolix/missinggo"
11         "github.com/anacrolix/missinggo/expect"
12         "golang.org/x/time/rate"
13
14         "github.com/anacrolix/torrent/iplist"
15         "github.com/anacrolix/torrent/storage"
16 )
17
18 var DefaultHTTPUserAgent = "Go-Torrent/1.0"
19
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
23         // specified.
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
27         // otherwise.
28         ListenHost              func(network string) string
29         ListenPort              int
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"`
34
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
57
58         // User-provided Client peer ID. If not present, one is generated automatically.
59         PeerID string
60         // For the bittorrent protocol.
61         DisableUTP bool
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
66         // used.
67         DefaultStorage storage.ClientImpl
68
69         EncryptionPolicy
70
71         // Sets usage of Socks5 Proxy. Authentication should be included in the url if needed.
72         // Example of setting: "socks5://demo:demo@192.168.99.100:1080"
73         ProxyURL string
74
75         IPBlocklist      iplist.Ranger
76         DisableIPv6      bool `long:"disable-ipv6"`
77         DisableIPv4      bool
78         DisableIPv4Peers bool
79         // Perform logging and any other behaviour that will help debug.
80         Debug bool `help:"enable debugging"`
81
82         // For querying HTTP trackers.
83         TrackerHttpClient *http.Client
84         // HTTPUserAgent changes default UserAgent for HTTP requests
85         HTTPUserAgent string
86         // Updated occasionally to when there's been some changes to client
87         // behaviour in case other clients are assuming anything of us. See also
88         // `bep20`.
89         ExtendedHandshakeClientVersion string // default  "go.torrent dev 20150624"
90         // Peer ID client identifier prefix. We'll update this occasionally to
91         // reflect changes to client behaviour that other clients may depend on.
92         // Also see `extendedHandshakeClientVersion`.
93         Bep20 string // default "-GT0001-"
94
95         // Peer dial timeout to use when there are limited peers.
96         NominalDialTimeout time.Duration
97         // Minimum peer dial timeout to use (even if we have lots of peers).
98         MinDialTimeout             time.Duration
99         EstablishedConnsPerTorrent int
100         HalfOpenConnsPerTorrent    int
101         // Maximum number of peer addresses in reserve.
102         TorrentPeersHighWater int
103         // Minumum number of peers before effort is made to obtain more peers.
104         TorrentPeersLowWater int
105
106         // Limit how long handshake can take. This is to reduce the lingering
107         // impact of a few bad apples. 4s loses 1% of successful handshakes that
108         // are obtained with 60s timeout, and 5% of unsuccessful handshakes.
109         HandshakesTimeout time.Duration
110
111         // The IP addresses as our peers should see them. May differ from the
112         // local interfaces due to NAT or other network configurations.
113         PublicIp4 net.IP
114         PublicIp6 net.IP
115
116         DisableAcceptRateLimiting bool
117         // Don't add connections that have the same peer ID as an existing
118         // connection for a given Torrent.
119         dropDuplicatePeerIds bool
120 }
121
122 func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
123         host, port, err := missinggo.ParseHostPort(addr)
124         expect.Nil(err)
125         cfg.ListenHost = func(string) string { return host }
126         cfg.ListenPort = port
127         return cfg
128 }
129
130 func NewDefaultClientConfig() *ClientConfig {
131         return &ClientConfig{
132                 TrackerHttpClient: &http.Client{
133                         Timeout: time.Second * 15,
134                         Transport: &http.Transport{
135                                 Dial: (&net.Dialer{
136                                         Timeout: 15 * time.Second,
137                                 }).Dial,
138                                 TLSHandshakeTimeout: 15 * time.Second,
139                                 TLSClientConfig:     &tls.Config{InsecureSkipVerify: true},
140                         }},
141                 HTTPUserAgent:                  DefaultHTTPUserAgent,
142                 ExtendedHandshakeClientVersion: "go.torrent dev 20150624",
143                 Bep20:                          "-GT0001-",
144                 NominalDialTimeout:             20 * time.Second,
145                 MinDialTimeout:                 3 * time.Second,
146                 EstablishedConnsPerTorrent:     50,
147                 HalfOpenConnsPerTorrent:        25,
148                 TorrentPeersHighWater:          500,
149                 TorrentPeersLowWater:           50,
150                 HandshakesTimeout:              4 * time.Second,
151                 DhtStartingNodes:               dht.GlobalBootstrapAddrs,
152                 ListenHost:                     func(string) string { return "" },
153                 UploadRateLimiter:              unlimited,
154                 DownloadRateLimiter:            unlimited,
155         }
156 }
157
158 type EncryptionPolicy struct {
159         DisableEncryption  bool
160         ForceEncryption    bool // Don't allow unobfuscated connections.
161         PreferNoEncryption bool
162 }