]> Sergey Matveev's repositories - btrtrc.git/blob - config.go
Reinstate the reduce dial timeout and update some values
[btrtrc.git] / config.go
1 package torrent
2
3 import (
4         "crypto/tls"
5         "net"
6         "net/http"
7         "net/url"
8         "time"
9
10         "github.com/anacrolix/dht"
11         "github.com/anacrolix/missinggo"
12         "github.com/anacrolix/missinggo/expect"
13         "golang.org/x/time/rate"
14
15         "github.com/anacrolix/torrent/iplist"
16         "github.com/anacrolix/torrent/storage"
17 )
18
19 var DefaultHTTPUserAgent = "Go-Torrent/1.0"
20
21 // Probably not safe to modify this after it's given to a Client.
22 type ClientConfig struct {
23         // Store torrent file data in this directory unless .DefaultStorage is
24         // specified.
25         DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"`
26         // The address to listen for new uTP and TCP bittorrent protocol
27         // connections. DHT shares a UDP socket with uTP unless configured
28         // otherwise.
29         ListenHost              func(network string) string
30         ListenPort              int
31         NoDefaultPortForwarding bool
32         // Don't announce to trackers. This only leaves DHT to discover peers.
33         DisableTrackers bool `long:"disable-trackers"`
34         DisablePEX      bool `long:"disable-pex"`
35
36         // Don't create a DHT.
37         NoDHT            bool `long:"disable-dht"`
38         DhtStartingNodes dht.StartingNodesGetter
39         // Never send chunks to peers.
40         NoUpload bool `long:"no-upload"`
41         // Disable uploading even when it isn't fair.
42         DisableAggressiveUpload bool `long:"disable-aggressive-upload"`
43         // Upload even after there's nothing in it for us. By default uploading is
44         // not altruistic, we'll only upload to encourage the peer to reciprocate.
45         Seed bool `long:"seed"`
46         // Only applies to chunks uploaded to peers, to maintain responsiveness
47         // communicating local Client state to peers. Each limiter token
48         // represents one byte. The Limiter's burst must be large enough to fit a
49         // whole chunk, which is usually 16 KiB (see TorrentSpec.ChunkSize).
50         UploadRateLimiter *rate.Limiter
51         // Rate limits all reads from connections to peers. Each limiter token
52         // represents one byte. The Limiter's burst must be bigger than the
53         // largest Read performed on a the underlying rate-limiting io.Reader
54         // minus one. This is likely to be the larger of the main read loop buffer
55         // (~4096), and the requested chunk size (~16KiB, see
56         // TorrentSpec.ChunkSize).
57         DownloadRateLimiter *rate.Limiter
58
59         // User-provided Client peer ID. If not present, one is generated automatically.
60         PeerID string
61         // For the bittorrent protocol.
62         DisableUTP bool
63         // For the bittorrent protocol.
64         DisableTCP bool `long:"disable-tcp"`
65         // Called to instantiate storage for each added torrent. Builtin backends
66         // are in the storage package. If not set, the "file" implementation is
67         // used.
68         DefaultStorage storage.ClientImpl
69
70         EncryptionPolicy
71
72         // Sets usage of Socks5 Proxy. Authentication should be included in the url if needed.
73         // Example of setting: "socks5://demo:demo@192.168.99.100:1080"
74         ProxyURL string
75
76         IPBlocklist      iplist.Ranger
77         DisableIPv6      bool `long:"disable-ipv6"`
78         DisableIPv4      bool
79         DisableIPv4Peers bool
80         // Perform logging and any other behaviour that will help debug.
81         Debug bool `help:"enable debugging"`
82
83         // HTTP client used to query the tracker endpoint. Default is DefaultHTTPClient
84         HTTP *http.Client
85         // HTTPUserAgent changes default UserAgent for HTTP requests
86         HTTPUserAgent string `long:"http-user-agent"`
87         // Updated occasionally to when there's been some changes to client
88         // behaviour in case other clients are assuming anything of us. See also
89         // `bep20`.
90         ExtendedHandshakeClientVersion string // default  "go.torrent dev 20150624"
91         // Peer ID client identifier prefix. We'll update this occasionally to
92         // reflect changes to client behaviour that other clients may depend on.
93         // Also see `extendedHandshakeClientVersion`.
94         Bep20 string // default "-GT0001-"
95
96         // Peer dial timeout to use when there are limited peers.
97         NominalDialTimeout time.Duration
98         // Minimum peer dial timeout to use (even if we have lots of peers).
99         MinDialTimeout             time.Duration
100         EstablishedConnsPerTorrent int
101         HalfOpenConnsPerTorrent    int
102         TorrentPeersHighWater      int
103         TorrentPeersLowWater       int
104
105         // Limit how long handshake can take. This is to reduce the lingering
106         // impact of a few bad apples. 4s loses 1% of successful handshakes that
107         // are obtained with 60s timeout, and 5% of unsuccessful handshakes.
108         HandshakesTimeout time.Duration
109
110         PublicIp4 net.IP
111         PublicIp6 net.IP
112
113         DisableAcceptRateLimiting bool
114         dropDuplicatePeerIds      bool
115 }
116
117 func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
118         host, port, err := missinggo.ParseHostPort(addr)
119         expect.Nil(err)
120         cfg.ListenHost = func(string) string { return host }
121         cfg.ListenPort = port
122         return cfg
123 }
124
125 func NewDefaultClientConfig() *ClientConfig {
126         return &ClientConfig{
127                 HTTP: &http.Client{
128                         Timeout: time.Second * 15,
129                         Transport: &http.Transport{
130                                 Dial: (&net.Dialer{
131                                         Timeout: 15 * time.Second,
132                                 }).Dial,
133                                 TLSHandshakeTimeout: 15 * time.Second,
134                                 TLSClientConfig:     &tls.Config{InsecureSkipVerify: true},
135                         }},
136                 HTTPUserAgent:                  DefaultHTTPUserAgent,
137                 ExtendedHandshakeClientVersion: "go.torrent dev 20150624",
138                 Bep20:                          "-GT0001-",
139                 NominalDialTimeout:             20 * time.Second,
140                 MinDialTimeout:                 5 * time.Second,
141                 EstablishedConnsPerTorrent:     50,
142                 HalfOpenConnsPerTorrent:        25,
143                 TorrentPeersHighWater:          500,
144                 TorrentPeersLowWater:           50,
145                 HandshakesTimeout:              4 * time.Second,
146                 DhtStartingNodes:               dht.GlobalBootstrapAddrs,
147                 ListenHost:                     func(string) string { return "" },
148                 UploadRateLimiter:              unlimited,
149                 DownloadRateLimiter:            unlimited,
150         }
151 }
152
153 func (cfg *ClientConfig) setProxyURL() {
154         fixedURL, err := url.Parse(cfg.ProxyURL)
155         if err != nil {
156                 return
157         }
158
159         cfg.HTTP.Transport = &http.Transport{
160                 Proxy:               http.ProxyURL(fixedURL),
161                 TLSHandshakeTimeout: 15 * time.Second,
162                 TLSClientConfig:     &tls.Config{InsecureSkipVerify: true},
163         }
164 }
165
166 type EncryptionPolicy struct {
167         DisableEncryption  bool
168         ForceEncryption    bool // Don't allow unobfuscated connections.
169         PreferNoEncryption bool
170 }