]> Sergey Matveev's repositories - btrtrc.git/blob - config.go
Change the default client port
[btrtrc.git] / config.go
1 package torrent
2
3 import (
4         "net"
5         "net/http"
6         "net/url"
7         "time"
8
9         "github.com/anacrolix/dht"
10         "github.com/anacrolix/dht/krpc"
11         "github.com/anacrolix/missinggo"
12         "github.com/anacrolix/missinggo/conntrack"
13         "github.com/anacrolix/missinggo/expect"
14         "github.com/anacrolix/torrent/iplist"
15         "github.com/anacrolix/torrent/mse"
16         "github.com/anacrolix/torrent/storage"
17         "golang.org/x/time/rate"
18 )
19
20 var DefaultHTTPUserAgent = "Go-Torrent/1.0"
21
22 // Probably not safe to modify this after it's given to a Client.
23 type ClientConfig struct {
24         // Store torrent file data in this directory unless .DefaultStorage is
25         // specified.
26         DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"`
27         // The address to listen for new uTP and TCP bittorrent protocol
28         // connections. DHT shares a UDP socket with uTP unless configured
29         // otherwise.
30         ListenHost              func(network string) string
31         ListenPort              int
32         NoDefaultPortForwarding bool
33         // Don't announce to trackers. This only leaves DHT to discover peers.
34         DisableTrackers bool `long:"disable-trackers"`
35         DisablePEX      bool `long:"disable-pex"`
36
37         // Don't create a DHT.
38         NoDHT            bool `long:"disable-dht"`
39         DhtStartingNodes dht.StartingNodesGetter
40         // Never send chunks to peers.
41         NoUpload bool `long:"no-upload"`
42         // Disable uploading even when it isn't fair.
43         DisableAggressiveUpload bool `long:"disable-aggressive-upload"`
44         // Upload even after there's nothing in it for us. By default uploading is
45         // not altruistic, we'll only upload to encourage the peer to reciprocate.
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         HeaderObfuscationPolicy HeaderObfuscationPolicy
72         CryptoProvides          mse.CryptoMethod
73         CryptoSelector          mse.CryptoSelector
74
75         // Sets usage of Socks5 Proxy. Authentication should be included in the url if needed.
76         // Examples: socks5://demo:demo@192.168.99.100:1080
77         //                       http://proxy.domain.com:3128
78         ProxyURL string
79
80         IPBlocklist      iplist.Ranger
81         DisableIPv6      bool `long:"disable-ipv6"`
82         DisableIPv4      bool
83         DisableIPv4Peers bool
84         // Perform logging and any other behaviour that will help debug.
85         Debug bool `help:"enable debugging"`
86
87         // HTTPProxy defines proxy for HTTP requests.
88         // Format: func(*Request) (*url.URL, error),
89         // or result of http.ProxyURL(HTTPProxy).
90         // By default, it is composed from ClientConfig.ProxyURL,
91         // if not set explicitly in ClientConfig struct
92         HTTPProxy func(*http.Request) (*url.URL, error)
93         // HTTPUserAgent changes default UserAgent for HTTP requests
94         HTTPUserAgent string
95         // Updated occasionally to when there's been some changes to client
96         // behaviour in case other clients are assuming anything of us. See also
97         // `bep20`.
98         ExtendedHandshakeClientVersion string
99         // Peer ID client identifier prefix. We'll update this occasionally to
100         // reflect changes to client behaviour that other clients may depend on.
101         // Also see `extendedHandshakeClientVersion`.
102         Bep20 string
103
104         // Peer dial timeout to use when there are limited peers.
105         NominalDialTimeout time.Duration
106         // Minimum peer dial timeout to use (even if we have lots of peers).
107         MinDialTimeout             time.Duration
108         EstablishedConnsPerTorrent int
109         HalfOpenConnsPerTorrent    int
110         // Maximum number of peer addresses in reserve.
111         TorrentPeersHighWater int
112         // Minumum number of peers before effort is made to obtain more peers.
113         TorrentPeersLowWater int
114
115         // Limit how long handshake can take. This is to reduce the lingering
116         // impact of a few bad apples. 4s loses 1% of successful handshakes that
117         // are obtained with 60s timeout, and 5% of unsuccessful handshakes.
118         HandshakesTimeout time.Duration
119
120         // The IP addresses as our peers should see them. May differ from the
121         // local interfaces due to NAT or other network configurations.
122         PublicIp4 net.IP
123         PublicIp6 net.IP
124
125         DisableAcceptRateLimiting bool
126         // Don't add connections that have the same peer ID as an existing
127         // connection for a given Torrent.
128         dropDuplicatePeerIds bool
129
130         ConnTracker *conntrack.Instance
131
132         // OnQuery hook func
133         DHTOnQuery func(query *krpc.Msg, source net.Addr) (propagate bool)
134 }
135
136 func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
137         host, port, err := missinggo.ParseHostPort(addr)
138         expect.Nil(err)
139         cfg.ListenHost = func(string) string { return host }
140         cfg.ListenPort = port
141         return cfg
142 }
143
144 func NewDefaultClientConfig() *ClientConfig {
145         cc := &ClientConfig{
146                 HTTPUserAgent:                  DefaultHTTPUserAgent,
147                 ExtendedHandshakeClientVersion: "go.torrent dev 20181121",
148                 Bep20:                          "-GT0002-",
149                 NominalDialTimeout:             20 * time.Second,
150                 MinDialTimeout:                 3 * time.Second,
151                 EstablishedConnsPerTorrent:     50,
152                 HalfOpenConnsPerTorrent:        25,
153                 TorrentPeersHighWater:          500,
154                 TorrentPeersLowWater:           50,
155                 HandshakesTimeout:              4 * time.Second,
156                 DhtStartingNodes:               dht.GlobalBootstrapAddrs,
157                 ListenHost:                     func(string) string { return "" },
158                 UploadRateLimiter:              unlimited,
159                 DownloadRateLimiter:            unlimited,
160                 ConnTracker:                    conntrack.NewInstance(),
161                 HeaderObfuscationPolicy: HeaderObfuscationPolicy{
162                         Preferred:        true,
163                         RequirePreferred: false,
164                 },
165                 CryptoSelector: mse.DefaultCryptoSelector,
166                 CryptoProvides: mse.AllSupportedCrypto,
167                 ListenPort:     42069,
168         }
169         cc.ConnTracker.SetNoMaxEntries()
170         cc.ConnTracker.Timeout = func(conntrack.Entry) time.Duration { return 0 }
171         return cc
172 }
173
174 type HeaderObfuscationPolicy struct {
175         RequirePreferred bool // Whether the value of Preferred is a strict requirement
176         Preferred        bool // Whether header obfuscation is preferred
177 }