2014-08-21 16:07:06 +08:00
|
|
|
package torrent
|
|
|
|
|
2014-11-29 02:13:08 +08:00
|
|
|
import (
|
2017-12-01 15:12:29 +08:00
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2017-03-19 14:04:32 +08:00
|
|
|
"github.com/anacrolix/dht"
|
2016-10-10 11:58:29 +08:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
2015-08-03 23:07:22 +08:00
|
|
|
"github.com/anacrolix/torrent/iplist"
|
2016-03-28 17:38:30 +08:00
|
|
|
"github.com/anacrolix/torrent/storage"
|
2014-11-29 02:13:08 +08:00
|
|
|
)
|
|
|
|
|
2017-11-08 02:14:13 +08:00
|
|
|
var DefaultHTTPClient = &http.Client{
|
|
|
|
Timeout: time.Second * 15,
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 15 * time.Second,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 15 * time.Second,
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
},
|
|
|
|
}
|
2017-12-28 16:47:51 +08:00
|
|
|
var DefaultHTTPUserAgent = "Go-Torrent/1.0"
|
2017-11-08 02:14:13 +08:00
|
|
|
|
2015-03-08 14:28:14 +08:00
|
|
|
// Override Client defaults.
|
2014-08-21 16:07:06 +08:00
|
|
|
type Config struct {
|
2018-01-11 14:11:54 +08:00
|
|
|
// Store torrent file data in this directory unless .DefaultStorage is
|
2015-03-08 14:28:14 +08:00
|
|
|
// specified.
|
2015-03-25 12:41:15 +08:00
|
|
|
DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"`
|
2015-03-08 14:28:14 +08:00
|
|
|
// The address to listen for new uTP and TCP bittorrent protocol
|
|
|
|
// connections. DHT shares a UDP socket with uTP unless configured
|
|
|
|
// otherwise.
|
2018-01-25 10:14:20 +08:00
|
|
|
ListenAddr string `long:"listen-addr" value-name:"HOST:PORT"`
|
|
|
|
NoDefaultPortForwarding bool
|
2015-03-08 14:28:14 +08:00
|
|
|
// Don't announce to trackers. This only leaves DHT to discover peers.
|
2015-03-25 12:41:15 +08:00
|
|
|
DisableTrackers bool `long:"disable-trackers"`
|
2015-03-25 12:42:14 +08:00
|
|
|
DisablePEX bool `long:"disable-pex"`
|
2015-03-08 14:28:14 +08:00
|
|
|
// Don't create a DHT.
|
2015-03-25 12:41:15 +08:00
|
|
|
NoDHT bool `long:"disable-dht"`
|
2015-03-08 14:28:14 +08:00
|
|
|
// Overrides the default DHT configuration.
|
2016-01-16 21:12:53 +08:00
|
|
|
DHTConfig dht.ServerConfig
|
2016-10-10 11:57:34 +08:00
|
|
|
|
|
|
|
// Never send chunks to peers.
|
2015-03-25 12:41:15 +08:00
|
|
|
NoUpload bool `long:"no-upload"`
|
2017-11-05 11:04:33 +08:00
|
|
|
// Disable uploading even when it isn't fair.
|
|
|
|
DisableAggressiveUpload bool `long:"disable-aggressive-upload"`
|
2015-05-15 06:39:53 +08:00
|
|
|
// Upload even after there's nothing in it for us. By default uploading is
|
2016-10-10 11:57:34 +08:00
|
|
|
// not altruistic, we'll upload slightly more than we download from each
|
|
|
|
// peer.
|
2015-05-15 06:39:53 +08:00
|
|
|
Seed bool `long:"seed"`
|
2018-01-07 17:31:10 +08:00
|
|
|
// 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 14:29:39 +08:00
|
|
|
UploadRateLimiter *rate.Limiter
|
2018-01-07 17:31:10 +08:00
|
|
|
// 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 14:29:39 +08:00
|
|
|
DownloadRateLimiter *rate.Limiter
|
2016-10-10 11:58:29 +08:00
|
|
|
|
2015-03-08 14:28:14 +08: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.
|
2015-04-20 15:35:21 +08:00
|
|
|
DisableTCP bool `long:"disable-tcp"`
|
2016-09-16 10:13:06 +08: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 10:42:41 +08:00
|
|
|
DefaultStorage storage.ClientImpl
|
|
|
|
|
2017-09-13 16:20:20 +08:00
|
|
|
EncryptionPolicy
|
2015-08-03 23:07:22 +08:00
|
|
|
|
2018-02-16 07:46:11 +08:00
|
|
|
IPBlocklist iplist.Ranger
|
|
|
|
DisableIPv6 bool `long:"disable-ipv6"`
|
|
|
|
DisableIPv4 bool
|
|
|
|
DisableIPv4Peers bool
|
2015-08-23 10:59:03 +08:00
|
|
|
// Perform logging and any other behaviour that will help debug.
|
2017-11-08 16:29:01 +08:00
|
|
|
Debug bool `help:"enable debugging"`
|
2017-11-08 02:14:13 +08:00
|
|
|
|
|
|
|
// HTTP client used to query the tracker endpoint. Default is DefaultHTTPClient
|
|
|
|
HTTP *http.Client
|
2017-12-28 16:47:51 +08:00
|
|
|
// HTTPUserAgent changes default UserAgent for HTTP requests
|
|
|
|
HTTPUserAgent string `long:"http-user-agent"`
|
2017-11-08 02:14:13 +08:00
|
|
|
// Updated occasionally to when there's been some changes to client
|
|
|
|
// behaviour in case other clients are assuming anything of us. See also
|
|
|
|
// `bep20`.
|
|
|
|
ExtendedHandshakeClientVersion string // default "go.torrent dev 20150624"
|
|
|
|
// 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`.
|
|
|
|
Bep20 string // default "-GT0001-"
|
|
|
|
|
|
|
|
NominalDialTimeout time.Duration // default time.Second * 30
|
|
|
|
MinDialTimeout time.Duration // default 5 * time.Second
|
|
|
|
EstablishedConnsPerTorrent int // default 80
|
|
|
|
HalfOpenConnsPerTorrent int // default 80
|
|
|
|
TorrentPeersHighWater int // default 200
|
|
|
|
TorrentPeersLowWater int // default 50
|
|
|
|
|
|
|
|
// 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 // default 20 * time.Second
|
2018-03-29 11:29:15 +08:00
|
|
|
|
|
|
|
PublicIp4 net.IP
|
|
|
|
PublicIp6 net.IP
|
2017-11-08 02:14:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) setDefaults() {
|
|
|
|
if cfg.HTTP == nil {
|
|
|
|
cfg.HTTP = DefaultHTTPClient
|
|
|
|
}
|
2017-12-28 16:47:51 +08:00
|
|
|
if cfg.HTTPUserAgent == "" {
|
|
|
|
cfg.HTTPUserAgent = DefaultHTTPUserAgent
|
|
|
|
}
|
2017-11-08 02:14:13 +08:00
|
|
|
if cfg.ExtendedHandshakeClientVersion == "" {
|
|
|
|
cfg.ExtendedHandshakeClientVersion = "go.torrent dev 20150624"
|
|
|
|
}
|
|
|
|
if cfg.Bep20 == "" {
|
|
|
|
cfg.Bep20 = "-GT0001-"
|
|
|
|
}
|
|
|
|
if cfg.NominalDialTimeout == 0 {
|
|
|
|
cfg.NominalDialTimeout = 30 * time.Second
|
|
|
|
}
|
|
|
|
if cfg.MinDialTimeout == 0 {
|
|
|
|
cfg.MinDialTimeout = 5 * time.Second
|
|
|
|
}
|
|
|
|
if cfg.EstablishedConnsPerTorrent == 0 {
|
2018-02-16 09:15:56 +08:00
|
|
|
cfg.EstablishedConnsPerTorrent = 50
|
2017-11-08 02:14:13 +08:00
|
|
|
}
|
|
|
|
if cfg.HalfOpenConnsPerTorrent == 0 {
|
2018-02-16 09:15:56 +08:00
|
|
|
cfg.HalfOpenConnsPerTorrent = (cfg.EstablishedConnsPerTorrent + 1) / 2
|
2017-11-08 02:14:13 +08:00
|
|
|
}
|
|
|
|
if cfg.TorrentPeersHighWater == 0 {
|
2018-02-16 09:15:56 +08:00
|
|
|
// Memory and freshness are the concern here.
|
2018-02-08 20:52:54 +08:00
|
|
|
cfg.TorrentPeersHighWater = 500
|
2017-11-08 02:14:13 +08:00
|
|
|
}
|
|
|
|
if cfg.TorrentPeersLowWater == 0 {
|
2018-02-16 09:15:56 +08:00
|
|
|
cfg.TorrentPeersLowWater = 2 * cfg.HalfOpenConnsPerTorrent
|
2017-11-08 02:14:13 +08:00
|
|
|
}
|
|
|
|
if cfg.HandshakesTimeout == 0 {
|
|
|
|
cfg.HandshakesTimeout = 20 * time.Second
|
|
|
|
}
|
2014-08-21 16:07:06 +08:00
|
|
|
}
|
2017-09-13 16:20:20 +08:00
|
|
|
|
|
|
|
type EncryptionPolicy struct {
|
|
|
|
DisableEncryption bool
|
|
|
|
ForceEncryption bool // Don't allow unobfuscated connections.
|
|
|
|
PreferNoEncryption bool
|
|
|
|
}
|