torrent/config.go

184 lines
6.9 KiB
Go
Raw Normal View History

2014-08-21 08:07:06 +00:00
package torrent
import (
2017-12-01 07:12:29 +00:00
"net"
"net/http"
"net/url"
2017-12-01 07:12:29 +00:00
"time"
2019-08-10 08:46:07 +00:00
"github.com/anacrolix/dht/v2"
"github.com/anacrolix/dht/v2/krpc"
2019-08-21 10:44:12 +00:00
"github.com/anacrolix/log"
"github.com/anacrolix/missinggo"
"github.com/anacrolix/missinggo/expect"
2019-11-02 11:35:57 +00:00
"github.com/anacrolix/missinggo/v2/conntrack"
2019-08-21 10:58:40 +00:00
"golang.org/x/time/rate"
"github.com/anacrolix/torrent/iplist"
"github.com/anacrolix/torrent/mse"
2016-03-28 09:38:30 +00:00
"github.com/anacrolix/torrent/storage"
)
var DefaultHTTPUserAgent = "Go-Torrent/1.0"
// Probably not safe to modify this after it's given to a Client.
type ClientConfig struct {
2018-01-11 06:11:54 +00:00
// Store torrent file data in this directory unless .DefaultStorage is
2015-03-08 06:28:14 +00:00
// specified.
DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"`
2015-03-08 06:28:14 +00:00
// The address to listen for new uTP and TCP bittorrent protocol
// connections. DHT shares a UDP socket with uTP unless configured
// otherwise.
ListenHost func(network string) string
ListenPort int
NoDefaultPortForwarding bool
2015-03-08 06:28:14 +00:00
// Don't announce to trackers. This only leaves DHT to discover peers.
DisableTrackers bool `long:"disable-trackers"`
DisablePEX bool `long:"disable-pex"`
2016-10-10 03:57:34 +00:00
// Don't create a DHT.
NoDHT bool `long:"disable-dht"`
DhtStartingNodes dht.StartingNodesGetter
2016-10-10 03:57:34 +00:00
// Never send chunks to peers.
NoUpload bool `long:"no-upload"`
// Disable uploading even when it isn't fair.
DisableAggressiveUpload bool `long:"disable-aggressive-upload"`
2015-05-14 22:39:53 +00:00
// Upload even after there's nothing in it for us. By default uploading is
2018-07-07 01:33:48 +00:00
// not altruistic, we'll only upload to encourage the peer to reciprocate.
2015-05-14 22:39:53 +00:00
Seed bool `long:"seed"`
// Only applies to chunks uploaded to peers, to maintain responsiveness
// communicating local Client state to peers. Each limiter token
// represents one byte. The Limiter's burst must be large enough to fit a
// whole chunk, which is usually 16 KiB (see TorrentSpec.ChunkSize).
2016-10-10 06:29:39 +00:00
UploadRateLimiter *rate.Limiter
// Rate limits all reads from connections to peers. Each limiter token
// represents one byte. The Limiter's burst must be bigger than the
// largest Read performed on a the underlying rate-limiting io.Reader
// minus one. This is likely to be the larger of the main read loop buffer
// (~4096), and the requested chunk size (~16KiB, see
// TorrentSpec.ChunkSize).
2016-10-10 06:29:39 +00:00
DownloadRateLimiter *rate.Limiter
2015-03-08 06:28:14 +00:00
// User-provided Client peer ID. If not present, one is generated automatically.
PeerID string
// For the bittorrent protocol.
DisableUTP bool
// For the bittorrent protocol.
DisableTCP bool `long:"disable-tcp"`
2016-09-16 02:13:06 +00:00
// Called to instantiate storage for each added torrent. Builtin backends
// are in the storage package. If not set, the "file" implementation is
// used.
2016-09-16 02:42:41 +00:00
DefaultStorage storage.ClientImpl
HeaderObfuscationPolicy HeaderObfuscationPolicy
2019-07-23 01:30:05 +00:00
// The crypto methods to offer when initiating connections with header obfuscation.
CryptoProvides mse.CryptoMethod
// Chooses the crypto method to use when receiving connections with header obfuscation.
CryptoSelector mse.CryptoSelector
// Sets usage of Socks5 Proxy. Authentication should be included in the url if needed.
// Examples: socks5://demo:demo@192.168.99.100:1080
// http://proxy.domain.com:3128
ProxyURL string
IPBlocklist iplist.Ranger
DisableIPv6 bool `long:"disable-ipv6"`
DisableIPv4 bool
DisableIPv4Peers bool
// Perform logging and any other behaviour that will help debug.
2019-08-21 10:44:12 +00:00
Debug bool `help:"enable debugging"`
Logger log.Logger
// HTTPProxy defines proxy for HTTP requests.
// Format: func(*Request) (*url.URL, error),
// or result of http.ProxyURL(HTTPProxy).
// By default, it is composed from ClientConfig.ProxyURL,
// if not set explicitly in ClientConfig struct
HTTPProxy func(*http.Request) (*url.URL, error)
// HTTPUserAgent changes default UserAgent for HTTP requests
HTTPUserAgent string
// Updated occasionally to when there's been some changes to client
// behaviour in case other clients are assuming anything of us. See also
// `bep20`.
2018-11-21 06:06:31 +00:00
ExtendedHandshakeClientVersion string
// Peer ID client identifier prefix. We'll update this occasionally to
// reflect changes to client behaviour that other clients may depend on.
// Also see `extendedHandshakeClientVersion`.
2018-11-21 06:06:31 +00:00
Bep20 string
// Peer dial timeout to use when there are limited peers.
NominalDialTimeout time.Duration
// Minimum peer dial timeout to use (even if we have lots of peers).
MinDialTimeout time.Duration
EstablishedConnsPerTorrent int
HalfOpenConnsPerTorrent int
// Maximum number of peer addresses in reserve.
TorrentPeersHighWater int
// Minumum number of peers before effort is made to obtain more peers.
TorrentPeersLowWater int
// Limit how long handshake can take. This is to reduce the lingering
// impact of a few bad apples. 4s loses 1% of successful handshakes that
// are obtained with 60s timeout, and 5% of unsuccessful handshakes.
HandshakesTimeout time.Duration
// The IP addresses as our peers should see them. May differ from the
// local interfaces due to NAT or other network configurations.
PublicIp4 net.IP
PublicIp6 net.IP
DisableAcceptRateLimiting bool
// Don't add connections that have the same peer ID as an existing
// connection for a given Torrent.
dropDuplicatePeerIds bool
2018-11-21 06:02:22 +00:00
ConnTracker *conntrack.Instance
2019-05-23 02:13:48 +00:00
// OnQuery hook func
DHTOnQuery func(query *krpc.Msg, source net.Addr) (propagate bool)
}
func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
host, port, err := missinggo.ParseHostPort(addr)
expect.Nil(err)
cfg.ListenHost = func(string) string { return host }
cfg.ListenPort = port
return cfg
}
func NewDefaultClientConfig() *ClientConfig {
cc := &ClientConfig{
HTTPUserAgent: DefaultHTTPUserAgent,
2018-11-21 06:06:31 +00:00
ExtendedHandshakeClientVersion: "go.torrent dev 20181121",
Bep20: "-GT0002-",
NominalDialTimeout: 20 * time.Second,
MinDialTimeout: 3 * time.Second,
EstablishedConnsPerTorrent: 50,
HalfOpenConnsPerTorrent: 25,
TorrentPeersHighWater: 500,
TorrentPeersLowWater: 50,
HandshakesTimeout: 4 * time.Second,
DhtStartingNodes: dht.GlobalBootstrapAddrs,
ListenHost: func(string) string { return "" },
UploadRateLimiter: unlimited,
DownloadRateLimiter: unlimited,
ConnTracker: conntrack.NewInstance(),
HeaderObfuscationPolicy: HeaderObfuscationPolicy{
Preferred: true,
RequirePreferred: false,
},
CryptoSelector: mse.DefaultCryptoSelector,
CryptoProvides: mse.AllSupportedCrypto,
2019-07-19 06:19:21 +00:00
ListenPort: 42069,
2019-08-21 10:44:12 +00:00
Logger: log.Default,
2018-04-12 13:34:31 +00:00
}
cc.ConnTracker.SetNoMaxEntries()
cc.ConnTracker.Timeout = func(conntrack.Entry) time.Duration { return 0 }
return cc
2014-08-21 08:07:06 +00:00
}
type HeaderObfuscationPolicy struct {
2019-07-23 01:30:05 +00:00
RequirePreferred bool // Whether the value of Preferred is a strict requirement.
Preferred bool // Whether header obfuscation is preferred.
}