]> Sergey Matveev's repositories - btrtrc.git/blob - config.go
Allow disabling accept limiting and modify some constants
[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         "golang.org/x/time/rate"
11
12         "github.com/anacrolix/dht"
13         "github.com/anacrolix/missinggo"
14         "github.com/anacrolix/missinggo/expect"
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 upload slightly more than we download from each
45         // peer.
46         Seed bool `long:"seed"`
47         // Only applies to chunks uploaded to peers, to maintain responsiveness
48         // communicating local Client state to peers. Each limiter token
49         // represents one byte. The Limiter's burst must be large enough to fit a
50         // whole chunk, which is usually 16 KiB (see TorrentSpec.ChunkSize).
51         UploadRateLimiter *rate.Limiter
52         // Rate limits all reads from connections to peers. Each limiter token
53         // represents one byte. The Limiter's burst must be bigger than the
54         // largest Read performed on a the underlying rate-limiting io.Reader
55         // minus one. This is likely to be the larger of the main read loop buffer
56         // (~4096), and the requested chunk size (~16KiB, see
57         // TorrentSpec.ChunkSize).
58         DownloadRateLimiter *rate.Limiter
59
60         // User-provided Client peer ID. If not present, one is generated automatically.
61         PeerID string
62         // For the bittorrent protocol.
63         DisableUTP bool
64         // For the bittorrent protocol.
65         DisableTCP bool `long:"disable-tcp"`
66         // Called to instantiate storage for each added torrent. Builtin backends
67         // are in the storage package. If not set, the "file" implementation is
68         // used.
69         DefaultStorage storage.ClientImpl
70
71         EncryptionPolicy
72
73         // Sets usage of Socks5 Proxy. Authentication should be included in the url if needed.
74         // Example of setting: "socks5://demo:demo@192.168.99.100:1080"
75         ProxyURL string
76
77         IPBlocklist      iplist.Ranger
78         DisableIPv6      bool `long:"disable-ipv6"`
79         DisableIPv4      bool
80         DisableIPv4Peers bool
81         // Perform logging and any other behaviour that will help debug.
82         Debug bool `help:"enable debugging"`
83
84         // HTTP client used to query the tracker endpoint. Default is DefaultHTTPClient
85         HTTP *http.Client
86         // HTTPUserAgent changes default UserAgent for HTTP requests
87         HTTPUserAgent string `long:"http-user-agent"`
88         // Updated occasionally to when there's been some changes to client
89         // behaviour in case other clients are assuming anything of us. See also
90         // `bep20`.
91         ExtendedHandshakeClientVersion string // default  "go.torrent dev 20150624"
92         // Peer ID client identifier prefix. We'll update this occasionally to
93         // reflect changes to client behaviour that other clients may depend on.
94         // Also see `extendedHandshakeClientVersion`.
95         Bep20 string // default "-GT0001-"
96
97         NominalDialTimeout         time.Duration // default  time.Second * 30
98         MinDialTimeout             time.Duration // default  5 * time.Second
99         EstablishedConnsPerTorrent int           // default 80
100         HalfOpenConnsPerTorrent    int           // default  80
101         TorrentPeersHighWater      int           // default 200
102         TorrentPeersLowWater       int           // default 50
103
104         // Limit how long handshake can take. This is to reduce the lingering
105         // impact of a few bad apples. 4s loses 1% of successful handshakes that
106         // are obtained with 60s timeout, and 5% of unsuccessful handshakes.
107         HandshakesTimeout time.Duration // default  20 * time.Second
108
109         PublicIp4 net.IP
110         PublicIp6 net.IP
111
112         DisableAcceptRateLimiting bool
113 }
114
115 func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
116         host, port, err := missinggo.ParseHostPort(addr)
117         expect.Nil(err)
118         cfg.ListenHost = func(string) string { return host }
119         cfg.ListenPort = port
120         return cfg
121 }
122
123 func NewDefaultClientConfig() *ClientConfig {
124         return &ClientConfig{
125                 HTTP: &http.Client{
126                         Timeout: time.Second * 15,
127                         Transport: &http.Transport{
128                                 Dial: (&net.Dialer{
129                                         Timeout: 15 * time.Second,
130                                 }).Dial,
131                                 TLSHandshakeTimeout: 15 * time.Second,
132                                 TLSClientConfig:     &tls.Config{InsecureSkipVerify: true},
133                         }},
134                 HTTPUserAgent:                  DefaultHTTPUserAgent,
135                 ExtendedHandshakeClientVersion: "go.torrent dev 20150624",
136                 Bep20:                      "-GT0001-",
137                 NominalDialTimeout:         30 * time.Second,
138                 MinDialTimeout:             5 * time.Second,
139                 EstablishedConnsPerTorrent: 50,
140                 HalfOpenConnsPerTorrent:    25,
141                 TorrentPeersHighWater:      500,
142                 TorrentPeersLowWater:       50,
143                 HandshakesTimeout:          20 * time.Second,
144                 DhtStartingNodes:           dht.GlobalBootstrapAddrs,
145                 ListenHost:                 func(string) string { return "" },
146                 UploadRateLimiter:          unlimited,
147                 DownloadRateLimiter:        unlimited,
148         }
149 }
150
151 func (cfg *ClientConfig) setProxyURL() {
152         fixedURL, err := url.Parse(cfg.ProxyURL)
153         if err != nil {
154                 return
155         }
156
157         cfg.HTTP.Transport = &http.Transport{
158                 Proxy:               http.ProxyURL(fixedURL),
159                 TLSHandshakeTimeout: 15 * time.Second,
160                 TLSClientConfig:     &tls.Config{InsecureSkipVerify: true},
161         }
162 }
163
164 type EncryptionPolicy struct {
165         DisableEncryption  bool
166         ForceEncryption    bool // Don't allow unobfuscated connections.
167         PreferNoEncryption bool
168 }