2014-08-21 08:07:06 +00:00
|
|
|
package torrent
|
|
|
|
|
2014-11-28 18:13:08 +00:00
|
|
|
import (
|
2017-12-01 07:12:29 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2018-04-12 01:41:07 +00:00
|
|
|
"github.com/anacrolix/dht"
|
2018-04-12 05:06:53 +00:00
|
|
|
"github.com/anacrolix/missinggo"
|
|
|
|
"github.com/anacrolix/missinggo/expect"
|
2018-07-07 01:36:58 +00:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
2015-08-03 15:07:22 +00:00
|
|
|
"github.com/anacrolix/torrent/iplist"
|
2016-03-28 09:38:30 +00:00
|
|
|
"github.com/anacrolix/torrent/storage"
|
2014-11-28 18:13:08 +00:00
|
|
|
)
|
|
|
|
|
2017-12-28 08:47:51 +00:00
|
|
|
var DefaultHTTPUserAgent = "Go-Torrent/1.0"
|
2017-11-07 18:14:13 +00:00
|
|
|
|
2018-06-16 06:30:04 +00:00
|
|
|
// 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.
|
2015-03-25 04:41:15 +00:00
|
|
|
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.
|
2018-04-12 05:06:53 +00:00
|
|
|
ListenHost func(network string) string
|
|
|
|
ListenPort int
|
2018-01-25 02:14:20 +00:00
|
|
|
NoDefaultPortForwarding bool
|
2015-03-08 06:28:14 +00:00
|
|
|
// Don't announce to trackers. This only leaves DHT to discover peers.
|
2015-03-25 04:41:15 +00:00
|
|
|
DisableTrackers bool `long:"disable-trackers"`
|
2015-03-25 04:42:14 +00:00
|
|
|
DisablePEX bool `long:"disable-pex"`
|
2016-10-10 03:57:34 +00:00
|
|
|
|
2018-04-12 01:41:07 +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.
|
2015-03-25 04:41:15 +00:00
|
|
|
NoUpload bool `long:"no-upload"`
|
2017-11-05 03:04:33 +00:00
|
|
|
// 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"`
|
2018-01-07 09:31:10 +00: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 06:29:39 +00:00
|
|
|
UploadRateLimiter *rate.Limiter
|
2018-01-07 09:31:10 +00: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 06:29:39 +00:00
|
|
|
DownloadRateLimiter *rate.Limiter
|
2016-10-10 03:58:29 +00:00
|
|
|
|
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.
|
2015-04-20 07:35:21 +00:00
|
|
|
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
|
|
|
|
|
2017-09-13 08:20:20 +00:00
|
|
|
EncryptionPolicy
|
2015-08-03 15:07:22 +00:00
|
|
|
|
2018-06-08 10:52:36 +00:00
|
|
|
// Sets usage of Socks5 Proxy. Authentication should be included in the url if needed.
|
|
|
|
// Example of setting: "socks5://demo:demo@192.168.99.100:1080"
|
|
|
|
ProxyURL string
|
|
|
|
|
2018-02-15 23:46:11 +00:00
|
|
|
IPBlocklist iplist.Ranger
|
|
|
|
DisableIPv6 bool `long:"disable-ipv6"`
|
|
|
|
DisableIPv4 bool
|
|
|
|
DisableIPv4Peers bool
|
2015-08-23 02:59:03 +00:00
|
|
|
// Perform logging and any other behaviour that will help debug.
|
2017-11-08 08:29:01 +00:00
|
|
|
Debug bool `help:"enable debugging"`
|
2017-11-07 18:14:13 +00:00
|
|
|
|
2018-07-11 05:10:58 +00:00
|
|
|
// For querying HTTP trackers.
|
|
|
|
TrackerHttpClient *http.Client
|
2017-12-28 08:47:51 +00:00
|
|
|
// HTTPUserAgent changes default UserAgent for HTTP requests
|
2018-07-11 05:10:58 +00:00
|
|
|
HTTPUserAgent string
|
2017-11-07 18:14:13 +00: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-"
|
|
|
|
|
2018-07-10 01:19:14 +00: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
|
|
|
|
TorrentPeersHighWater int
|
|
|
|
TorrentPeersLowWater int
|
2017-11-07 18:14:13 +00: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 01:19:14 +00:00
|
|
|
HandshakesTimeout time.Duration
|
2018-03-29 03:29:15 +00:00
|
|
|
|
|
|
|
PublicIp4 net.IP
|
|
|
|
PublicIp6 net.IP
|
2018-06-16 06:37:13 +00:00
|
|
|
|
|
|
|
DisableAcceptRateLimiting bool
|
2018-06-16 06:40:37 +00:00
|
|
|
dropDuplicatePeerIds bool
|
2017-11-07 18:14:13 +00:00
|
|
|
}
|
|
|
|
|
2018-06-16 06:30:04 +00:00
|
|
|
func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
|
2018-04-12 05:06:53 +00: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 06:30:04 +00:00
|
|
|
func NewDefaultClientConfig() *ClientConfig {
|
|
|
|
return &ClientConfig{
|
2018-07-11 05:10:58 +00:00
|
|
|
TrackerHttpClient: &http.Client{
|
2018-06-16 06:30:04 +00:00
|
|
|
Timeout: time.Second * 15,
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 15 * time.Second,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 15 * time.Second,
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}},
|
|
|
|
HTTPUserAgent: DefaultHTTPUserAgent,
|
|
|
|
ExtendedHandshakeClientVersion: "go.torrent dev 20150624",
|
2018-07-10 01:19:14 +00:00
|
|
|
Bep20: "-GT0001-",
|
|
|
|
NominalDialTimeout: 20 * time.Second,
|
2018-07-15 03:01:22 +00:00
|
|
|
MinDialTimeout: 3 * time.Second,
|
2018-07-10 01:19:14 +00:00
|
|
|
EstablishedConnsPerTorrent: 50,
|
|
|
|
HalfOpenConnsPerTorrent: 25,
|
|
|
|
TorrentPeersHighWater: 500,
|
|
|
|
TorrentPeersLowWater: 50,
|
|
|
|
HandshakesTimeout: 4 * time.Second,
|
|
|
|
DhtStartingNodes: dht.GlobalBootstrapAddrs,
|
|
|
|
ListenHost: func(string) string { return "" },
|
|
|
|
UploadRateLimiter: unlimited,
|
|
|
|
DownloadRateLimiter: unlimited,
|
2018-04-12 13:34:31 +00:00
|
|
|
}
|
2014-08-21 08:07:06 +00:00
|
|
|
}
|
2017-09-13 08:20:20 +00:00
|
|
|
|
|
|
|
type EncryptionPolicy struct {
|
|
|
|
DisableEncryption bool
|
|
|
|
ForceEncryption bool // Don't allow unobfuscated connections.
|
|
|
|
PreferNoEncryption bool
|
|
|
|
}
|