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