]> Sergey Matveev's repositories - btrtrc.git/blob - config.go
Merge commit '6ab65a49a8a72dea1a28968b2ab42a85fd4566ec'
[btrtrc.git] / config.go
1 package torrent
2
3 import (
4         "github.com/anacrolix/dht"
5         "golang.org/x/time/rate"
6
7         "github.com/anacrolix/torrent/iplist"
8         "github.com/anacrolix/torrent/storage"
9
10         "crypto/tls"
11         "net"
12         "net/http"
13         "time"
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
27 // Override Client defaults.
28 type Config struct {
29         // Store torrent file data in this directory unless TorrentDataOpener is
30         // specified.
31         DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"`
32         // The address to listen for new uTP and TCP bittorrent protocol
33         // connections. DHT shares a UDP socket with uTP unless configured
34         // otherwise.
35         ListenAddr string `long:"listen-addr" value-name:"HOST:PORT"`
36         // Don't announce to trackers. This only leaves DHT to discover peers.
37         DisableTrackers bool `long:"disable-trackers"`
38         DisablePEX      bool `long:"disable-pex"`
39         // Don't create a DHT.
40         NoDHT bool `long:"disable-dht"`
41         // Overrides the default DHT configuration.
42         DHTConfig dht.ServerConfig
43
44         // Never send chunks to peers.
45         NoUpload bool `long:"no-upload"`
46         // Disable uploading even when it isn't fair.
47         DisableAggressiveUpload bool `long:"disable-aggressive-upload"`
48         // Upload even after there's nothing in it for us. By default uploading is
49         // not altruistic, we'll upload slightly more than we download from each
50         // peer.
51         Seed bool `long:"seed"`
52         // Events are data bytes sent in pieces. The burst must be large enough to
53         // fit a whole chunk, which is usually 16 KiB.
54         UploadRateLimiter *rate.Limiter
55         // The events are bytes read from connections. The burst must be bigger
56         // than the largest Read performed on a Conn minus one. This is likely to
57         // be the larger of the main read loop buffer (~4096), and the requested
58         // chunk size (~16KiB).
59         DownloadRateLimiter *rate.Limiter
60
61         // User-provided Client peer ID. If not present, one is generated automatically.
62         PeerID string
63         // For the bittorrent protocol.
64         DisableUTP bool
65         // For the bittorrent protocol.
66         DisableTCP bool `long:"disable-tcp"`
67         // Called to instantiate storage for each added torrent. Builtin backends
68         // are in the storage package. If not set, the "file" implementation is
69         // used.
70         DefaultStorage storage.ClientImpl
71
72         EncryptionPolicy
73
74         IPBlocklist iplist.Ranger
75         DisableIPv6 bool `long:"disable-ipv6"`
76         // Perform logging and any other behaviour that will help debug.
77         Debug bool `help:"enable debugging"`
78
79         // HTTP client used to query the tracker endpoint. Default is DefaultHTTPClient
80         HTTP *http.Client
81         // Updated occasionally to when there's been some changes to client
82         // behaviour in case other clients are assuming anything of us. See also
83         // `bep20`.
84         ExtendedHandshakeClientVersion string // default  "go.torrent dev 20150624"
85         // Peer ID client identifier prefix. We'll update this occasionally to
86         // reflect changes to client behaviour that other clients may depend on.
87         // Also see `extendedHandshakeClientVersion`.
88         Bep20 string // default "-GT0001-"
89
90         NominalDialTimeout         time.Duration // default  time.Second * 30
91         MinDialTimeout             time.Duration // default  5 * time.Second
92         EstablishedConnsPerTorrent int           // default 80
93         HalfOpenConnsPerTorrent    int           // default  80
94         TorrentPeersHighWater      int           // default 200
95         TorrentPeersLowWater       int           // default 50
96
97         // Limit how long handshake can take. This is to reduce the lingering
98         // impact of a few bad apples. 4s loses 1% of successful handshakes that
99         // are obtained with 60s timeout, and 5% of unsuccessful handshakes.
100         HandshakesTimeout time.Duration // default  20 * time.Second
101 }
102
103 func (cfg *Config) setDefaults() {
104         if cfg.HTTP == nil {
105                 cfg.HTTP = DefaultHTTPClient
106         }
107         if cfg.ExtendedHandshakeClientVersion == "" {
108                 cfg.ExtendedHandshakeClientVersion = "go.torrent dev 20150624"
109         }
110         if cfg.Bep20 == "" {
111                 cfg.Bep20 = "-GT0001-"
112         }
113         if cfg.NominalDialTimeout == 0 {
114                 cfg.NominalDialTimeout = 30 * time.Second
115         }
116         if cfg.MinDialTimeout == 0 {
117                 cfg.MinDialTimeout = 5 * time.Second
118         }
119         if cfg.EstablishedConnsPerTorrent == 0 {
120                 cfg.EstablishedConnsPerTorrent = 80
121         }
122         if cfg.HalfOpenConnsPerTorrent == 0 {
123                 cfg.HalfOpenConnsPerTorrent = 80
124         }
125         if cfg.TorrentPeersHighWater == 0 {
126                 cfg.TorrentPeersHighWater = 200
127         }
128         if cfg.TorrentPeersLowWater == 0 {
129                 cfg.TorrentPeersLowWater = 50
130         }
131         if cfg.HandshakesTimeout == 0 {
132                 cfg.HandshakesTimeout = 20 * time.Second
133         }
134 }
135
136 type EncryptionPolicy struct {
137         DisableEncryption  bool
138         ForceEncryption    bool // Don't allow unobfuscated connections.
139         PreferNoEncryption bool
140 }