]> Sergey Matveev's repositories - btrtrc.git/blob - config.go
Adjust some config defaults
[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         "golang.org/x/time/rate"
11
12         "github.com/anacrolix/torrent/iplist"
13         "github.com/anacrolix/torrent/storage"
14 )
15
16 var DefaultHTTPClient = &http.Client{
17         Timeout: time.Second * 15,
18         Transport: &http.Transport{
19                 Dial: (&net.Dialer{
20                         Timeout: 15 * time.Second,
21                 }).Dial,
22                 TLSHandshakeTimeout: 15 * time.Second,
23                 TLSClientConfig:     &tls.Config{InsecureSkipVerify: true},
24         },
25 }
26 var DefaultHTTPUserAgent = "Go-Torrent/1.0"
27
28 // Override Client defaults.
29 type Config struct {
30         // Store torrent file data in this directory unless .DefaultStorage is
31         // specified.
32         DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"`
33         // The address to listen for new uTP and TCP bittorrent protocol
34         // connections. DHT shares a UDP socket with uTP unless configured
35         // otherwise.
36         ListenAddr              string `long:"listen-addr" value-name:"HOST:PORT"`
37         NoDefaultPortForwarding bool
38         // Don't announce to trackers. This only leaves DHT to discover peers.
39         DisableTrackers bool `long:"disable-trackers"`
40         DisablePEX      bool `long:"disable-pex"`
41         // Don't create a DHT.
42         NoDHT bool `long:"disable-dht"`
43         // Overrides the default DHT configuration.
44         DHTConfig dht.ServerConfig
45
46         // Never send chunks to peers.
47         NoUpload bool `long:"no-upload"`
48         // Disable uploading even when it isn't fair.
49         DisableAggressiveUpload bool `long:"disable-aggressive-upload"`
50         // Upload even after there's nothing in it for us. By default uploading is
51         // not altruistic, we'll upload slightly more than we download from each
52         // peer.
53         Seed bool `long:"seed"`
54         // Only applies to chunks uploaded to peers, to maintain responsiveness
55         // communicating local Client state to peers. Each limiter token
56         // represents one byte. The Limiter's burst must be large enough to fit a
57         // whole chunk, which is usually 16 KiB (see TorrentSpec.ChunkSize).
58         UploadRateLimiter *rate.Limiter
59         // Rate limits all reads from connections to peers. Each limiter token
60         // represents one byte. The Limiter's burst must be bigger than the
61         // largest Read performed on a the underlying rate-limiting io.Reader
62         // minus one. This is likely to be the larger of the main read loop buffer
63         // (~4096), and the requested chunk size (~16KiB, see
64         // TorrentSpec.ChunkSize).
65         DownloadRateLimiter *rate.Limiter
66
67         // User-provided Client peer ID. If not present, one is generated automatically.
68         PeerID string
69         // For the bittorrent protocol.
70         DisableUTP bool
71         // For the bittorrent protocol.
72         DisableTCP bool `long:"disable-tcp"`
73         // Called to instantiate storage for each added torrent. Builtin backends
74         // are in the storage package. If not set, the "file" implementation is
75         // used.
76         DefaultStorage storage.ClientImpl
77
78         EncryptionPolicy
79
80         IPBlocklist      iplist.Ranger
81         DisableIPv6      bool `long:"disable-ipv6"`
82         DisableIPv4      bool
83         DisableIPv4Peers bool
84         // Perform logging and any other behaviour that will help debug.
85         Debug bool `help:"enable debugging"`
86
87         // HTTP client used to query the tracker endpoint. Default is DefaultHTTPClient
88         HTTP *http.Client
89         // HTTPUserAgent changes default UserAgent for HTTP requests
90         HTTPUserAgent string `long:"http-user-agent"`
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
93         // `bep20`.
94         ExtendedHandshakeClientVersion string // default  "go.torrent dev 20150624"
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`.
98         Bep20 string // default "-GT0001-"
99
100         NominalDialTimeout         time.Duration // default  time.Second * 30
101         MinDialTimeout             time.Duration // default  5 * time.Second
102         EstablishedConnsPerTorrent int           // default 80
103         HalfOpenConnsPerTorrent    int           // default  80
104         TorrentPeersHighWater      int           // default 200
105         TorrentPeersLowWater       int           // default 50
106
107         // Limit how long handshake can take. This is to reduce the lingering
108         // impact of a few bad apples. 4s loses 1% of successful handshakes that
109         // are obtained with 60s timeout, and 5% of unsuccessful handshakes.
110         HandshakesTimeout time.Duration // default  20 * time.Second
111 }
112
113 func (cfg *Config) setDefaults() {
114         if cfg.HTTP == nil {
115                 cfg.HTTP = DefaultHTTPClient
116         }
117         if cfg.HTTPUserAgent == "" {
118                 cfg.HTTPUserAgent = DefaultHTTPUserAgent
119         }
120         if cfg.ExtendedHandshakeClientVersion == "" {
121                 cfg.ExtendedHandshakeClientVersion = "go.torrent dev 20150624"
122         }
123         if cfg.Bep20 == "" {
124                 cfg.Bep20 = "-GT0001-"
125         }
126         if cfg.NominalDialTimeout == 0 {
127                 cfg.NominalDialTimeout = 30 * time.Second
128         }
129         if cfg.MinDialTimeout == 0 {
130                 cfg.MinDialTimeout = 5 * time.Second
131         }
132         if cfg.EstablishedConnsPerTorrent == 0 {
133                 cfg.EstablishedConnsPerTorrent = 50
134         }
135         if cfg.HalfOpenConnsPerTorrent == 0 {
136                 cfg.HalfOpenConnsPerTorrent = (cfg.EstablishedConnsPerTorrent + 1) / 2
137         }
138         if cfg.TorrentPeersHighWater == 0 {
139                 // Memory and freshness are the concern here.
140                 cfg.TorrentPeersHighWater = 500
141         }
142         if cfg.TorrentPeersLowWater == 0 {
143                 cfg.TorrentPeersLowWater = 2 * cfg.HalfOpenConnsPerTorrent
144         }
145         if cfg.HandshakesTimeout == 0 {
146                 cfg.HandshakesTimeout = 20 * time.Second
147         }
148 }
149
150 type EncryptionPolicy struct {
151         DisableEncryption  bool
152         ForceEncryption    bool // Don't allow unobfuscated connections.
153         PreferNoEncryption bool
154 }