]> Sergey Matveev's repositories - btrtrc.git/blob - config.go
sortimports
[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         NominalDialTimeout         time.Duration // default  time.Second * 30
97         MinDialTimeout             time.Duration // default  5 * time.Second
98         EstablishedConnsPerTorrent int           // default 80
99         HalfOpenConnsPerTorrent    int           // default  80
100         TorrentPeersHighWater      int           // default 200
101         TorrentPeersLowWater       int           // default 50
102
103         // Limit how long handshake can take. This is to reduce the lingering
104         // impact of a few bad apples. 4s loses 1% of successful handshakes that
105         // are obtained with 60s timeout, and 5% of unsuccessful handshakes.
106         HandshakesTimeout time.Duration // default  20 * time.Second
107
108         PublicIp4 net.IP
109         PublicIp6 net.IP
110
111         DisableAcceptRateLimiting bool
112         dropDuplicatePeerIds      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 }