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
|
|
|
"net"
|
2018-10-31 06:32:33 +08:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-12-01 15:12:29 +08:00
|
|
|
"time"
|
|
|
|
|
2019-08-10 16:46:07 +08:00
|
|
|
"github.com/anacrolix/dht/v2"
|
|
|
|
"github.com/anacrolix/dht/v2/krpc"
|
2019-08-21 18:44:12 +08:00
|
|
|
"github.com/anacrolix/log"
|
2018-04-12 13:06:53 +08:00
|
|
|
"github.com/anacrolix/missinggo"
|
|
|
|
"github.com/anacrolix/missinggo/expect"
|
2019-11-02 19:35:57 +08:00
|
|
|
"github.com/anacrolix/missinggo/v2/conntrack"
|
2019-08-21 18:58:40 +08:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
2015-08-03 23:07:22 +08:00
|
|
|
"github.com/anacrolix/torrent/iplist"
|
2019-07-19 11:23:36 +08:00
|
|
|
"github.com/anacrolix/torrent/mse"
|
2016-03-28 17:38:30 +08:00
|
|
|
"github.com/anacrolix/torrent/storage"
|
2014-11-29 02:13:08 +08:00
|
|
|
)
|
|
|
|
|
2018-06-16 14:30:04 +08:00
|
|
|
// Probably not safe to modify this after it's given to a Client.
|
|
|
|
type ClientConfig 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"`
|
2020-02-20 13:47:37 +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-04-12 13:06:53 +08:00
|
|
|
ListenHost func(network string) string
|
|
|
|
ListenPort int
|
2018-01-25 10:14:20 +08:00
|
|
|
NoDefaultPortForwarding bool
|
2019-12-23 11:04:07 +08:00
|
|
|
UpnpID string
|
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"`
|
2016-10-10 11:57:34 +08:00
|
|
|
|
2018-04-12 09:41:07 +08:00
|
|
|
// Don't create a DHT.
|
|
|
|
NoDHT bool `long:"disable-dht"`
|
2020-03-17 10:47:53 +08:00
|
|
|
DhtStartingNodes func(network string) dht.StartingNodesGetter
|
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
|
2018-07-07 09:33:48 +08:00
|
|
|
// not altruistic, we'll only upload to encourage the peer to reciprocate.
|
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
|
2020-02-21 11:12:44 +08:00
|
|
|
// used (and Closed when the Client is Closed).
|
2016-09-16 10:42:41 +08:00
|
|
|
DefaultStorage storage.ClientImpl
|
|
|
|
|
2019-07-19 11:23:36 +08:00
|
|
|
HeaderObfuscationPolicy HeaderObfuscationPolicy
|
2019-07-23 09:30:05 +08: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
|
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.
|
2019-08-21 18:44:12 +08:00
|
|
|
Debug bool `help:"enable debugging"`
|
|
|
|
Logger log.Logger
|
2017-11-08 02:14:13 +08:00
|
|
|
|
2020-02-20 14:04:11 +08:00
|
|
|
// Defines proxy for HTTP requests, such as for trackers. It's commonly set from the result of
|
|
|
|
// "net/http".ProxyURL(HTTPProxy).
|
2018-10-31 06:32:33 +08:00
|
|
|
HTTPProxy func(*http.Request) (*url.URL, error)
|
2017-12-28 16:47:51 +08:00
|
|
|
// HTTPUserAgent changes default UserAgent for HTTP requests
|
2018-07-11 13:10:58 +08:00
|
|
|
HTTPUserAgent string
|
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`.
|
2018-11-21 14:06:31 +08:00
|
|
|
ExtendedHandshakeClientVersion string
|
2017-11-08 02:14:13 +08:00
|
|
|
// 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 14:06:31 +08:00
|
|
|
Bep20 string
|
2017-11-08 02:14:13 +08:00
|
|
|
|
2018-07-10 09:19:14 +08:00
|
|
|
// 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
|
2018-09-03 09:48:11 +08:00
|
|
|
// Maximum number of peer addresses in reserve.
|
|
|
|
TorrentPeersHighWater int
|
|
|
|
// Minumum number of peers before effort is made to obtain more peers.
|
|
|
|
TorrentPeersLowWater int
|
2017-11-08 02:14:13 +08:00
|
|
|
|
|
|
|
// 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.
|
2018-07-10 09:19:14 +08:00
|
|
|
HandshakesTimeout time.Duration
|
2018-03-29 11:29:15 +08:00
|
|
|
|
2018-09-03 09:48:11 +08:00
|
|
|
// The IP addresses as our peers should see them. May differ from the
|
|
|
|
// local interfaces due to NAT or other network configurations.
|
2018-03-29 11:29:15 +08:00
|
|
|
PublicIp4 net.IP
|
|
|
|
PublicIp6 net.IP
|
2018-06-16 14:37:13 +08:00
|
|
|
|
|
|
|
DisableAcceptRateLimiting bool
|
2018-09-03 09:48:11 +08:00
|
|
|
// Don't add connections that have the same peer ID as an existing
|
|
|
|
// connection for a given Torrent.
|
2020-04-16 12:12:17 +08:00
|
|
|
DropDuplicatePeerIds bool
|
2018-11-21 14:02:22 +08:00
|
|
|
|
|
|
|
ConnTracker *conntrack.Instance
|
2019-05-23 10:13:48 +08:00
|
|
|
|
|
|
|
// OnQuery hook func
|
|
|
|
DHTOnQuery func(query *krpc.Msg, source net.Addr) (propagate bool)
|
2020-01-10 13:18:55 +08:00
|
|
|
|
2020-01-24 14:30:57 +08:00
|
|
|
DefaultRequestStrategy RequestStrategyMaker
|
2020-04-23 11:03:35 +08:00
|
|
|
|
|
|
|
Extensions PeerExtensionBits
|
2020-06-01 16:24:46 +08:00
|
|
|
|
|
|
|
DisableWebtorrent bool
|
2020-06-02 14:17:32 +08:00
|
|
|
DisableWebseeds bool
|
2020-07-15 12:00:47 +08:00
|
|
|
|
|
|
|
Callbacks Callbacks
|
2017-11-08 02:14:13 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 14:30:04 +08:00
|
|
|
func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
|
2018-04-12 13:06:53 +08:00
|
|
|
host, port, err := missinggo.ParseHostPort(addr)
|
|
|
|
expect.Nil(err)
|
|
|
|
cfg.ListenHost = func(string) string { return host }
|
|
|
|
cfg.ListenPort = port
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2018-06-16 14:30:04 +08:00
|
|
|
func NewDefaultClientConfig() *ClientConfig {
|
2019-04-09 10:54:41 +08:00
|
|
|
cc := &ClientConfig{
|
2020-02-20 14:04:11 +08:00
|
|
|
HTTPUserAgent: "Go-Torrent/1.0",
|
2018-11-21 14:06:31 +08:00
|
|
|
ExtendedHandshakeClientVersion: "go.torrent dev 20181121",
|
2019-04-09 10:54:41 +08:00
|
|
|
Bep20: "-GT0002-",
|
2019-12-23 11:04:07 +08:00
|
|
|
UpnpID: "anacrolix/torrent",
|
2019-04-09 10:54:41 +08:00
|
|
|
NominalDialTimeout: 20 * time.Second,
|
|
|
|
MinDialTimeout: 3 * time.Second,
|
|
|
|
EstablishedConnsPerTorrent: 50,
|
|
|
|
HalfOpenConnsPerTorrent: 25,
|
|
|
|
TorrentPeersHighWater: 500,
|
|
|
|
TorrentPeersLowWater: 50,
|
|
|
|
HandshakesTimeout: 4 * time.Second,
|
2020-03-17 10:47:53 +08:00
|
|
|
DhtStartingNodes: func(network string) dht.StartingNodesGetter {
|
|
|
|
return func() ([]dht.Addr, error) { return dht.GlobalBootstrapAddrs(network) }
|
|
|
|
},
|
|
|
|
ListenHost: func(string) string { return "" },
|
|
|
|
UploadRateLimiter: unlimited,
|
|
|
|
DownloadRateLimiter: unlimited,
|
|
|
|
ConnTracker: conntrack.NewInstance(),
|
|
|
|
DisableAcceptRateLimiting: true,
|
2019-07-19 11:23:36 +08:00
|
|
|
HeaderObfuscationPolicy: HeaderObfuscationPolicy{
|
|
|
|
Preferred: true,
|
|
|
|
RequirePreferred: false,
|
|
|
|
},
|
|
|
|
CryptoSelector: mse.DefaultCryptoSelector,
|
|
|
|
CryptoProvides: mse.AllSupportedCrypto,
|
2019-07-19 14:19:21 +08:00
|
|
|
ListenPort: 42069,
|
2020-01-04 13:43:15 +08:00
|
|
|
Logger: log.Default,
|
2020-01-10 13:18:55 +08:00
|
|
|
|
2020-01-24 14:34:07 +08:00
|
|
|
DefaultRequestStrategy: RequestStrategyDuplicateRequestTimeout(5 * time.Second),
|
2020-04-23 11:03:35 +08:00
|
|
|
|
|
|
|
Extensions: defaultPeerExtensionBytes(),
|
2018-04-12 21:34:31 +08:00
|
|
|
}
|
2019-11-04 09:11:26 +08:00
|
|
|
//cc.ConnTracker.SetNoMaxEntries()
|
|
|
|
//cc.ConnTracker.Timeout = func(conntrack.Entry) time.Duration { return 0 }
|
2019-04-09 10:54:41 +08:00
|
|
|
return cc
|
2014-08-21 16:07:06 +08:00
|
|
|
}
|
2017-09-13 16:20:20 +08:00
|
|
|
|
2019-07-19 11:23:36 +08:00
|
|
|
type HeaderObfuscationPolicy struct {
|
2019-07-23 09:30:05 +08:00
|
|
|
RequirePreferred bool // Whether the value of Preferred is a strict requirement.
|
|
|
|
Preferred bool // Whether header obfuscation is preferred.
|
2017-09-13 16:20:20 +08:00
|
|
|
}
|