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