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