FedP2P/config.go

213 lines
8.4 KiB
Go
Raw Normal View History

2014-08-21 16:07:06 +08:00
package torrent
import (
2017-12-01 15:12:29 +08:00
"net"
"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"
"github.com/anacrolix/missinggo/v2"
"github.com/anacrolix/torrent/version"
2019-08-21 18:58:40 +08:00
"golang.org/x/time/rate"
"github.com/anacrolix/torrent/iplist"
"github.com/anacrolix/torrent/mse"
2016-03-28 17:38:30 +08:00
"github.com/anacrolix/torrent/storage"
)
// 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.
DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"`
// 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
UpnpID string
2015-03-08 14:28:14 +08: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 11:57:34 +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
// Called for each anacrolix/dht Server created for the Client.
ConfigureAnacrolixDhtServer func(*dht.ServerConfig)
PeriodicallyAnnounceTorrentsToDht bool
2016-10-10 11:57:34 +08: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-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"`
// 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
// 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
2021-05-14 11:40:09 +08:00
// Maximum unverified bytes across all torrents. Not used if zero.
MaxUnverifiedBytes int64
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.
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 (and Closed when the Client is Closed).
2016-09-16 10:42:41 +08:00
DefaultStorage storage.ClientImpl
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
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 18:44:12 +08:00
Debug bool `help:"enable debugging"`
Logger log.Logger
// Defines proxy for HTTP requests, such as for trackers. It's commonly set from the result of
// "net/http".ProxyURL(HTTPProxy).
HTTPProxy func(*http.Request) (*url.URL, error)
// Takes a tracker's hostname and requests DNS A and AAAA records.
// Used in case DNS lookups require a special setup (i.e., dns-over-https)
LookupTrackerIp func(*url.URL) ([]net.IP, 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 14:06:31 +08: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 14:06:31 +08: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
TotalHalfOpenConns 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
// How long between writes before sending a keep alive message on a peer connection that we want
// to maintain.
KeepAliveTimeout 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
// Accept rate limiting affects excessive connection attempts from IPs that fail during
// handshakes or request torrents that we don't have.
DisableAcceptRateLimiting bool
// Don't add connections that have the same peer ID as an existing
// connection for a given Torrent.
DropDuplicatePeerIds bool
// Drop peers that are complete if we are also complete and have no use for the peer. This is a
// bit of a special case, since a peer could also be useless if they're just not interested, or
// we don't intend to obtain all of a torrent's data.
DropMutuallyCompletePeers bool
2021-06-18 12:59:20 +08:00
// Whether to accept peer connections at all.
AcceptPeerConnections bool
2021-09-21 11:04:51 +08:00
// Whether a Client should want conns without delegating to any attached Torrents. This is
// useful when torrents might be added dynmically in callbacks for example.
AlwaysWantConns bool
2018-11-21 14:02:22 +08:00
2019-05-23 10:13:48 +08:00
// OnQuery hook func
DHTOnQuery func(query *krpc.Msg, source net.Addr) (propagate bool)
Extensions PeerExtensionBits
2021-10-25 17:52:44 +08:00
// Bits that peers must have set to proceed past handshakes.
MinPeerExtensions 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
}
func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig {
host, port, err := missinggo.ParseHostPort(addr)
2021-09-23 22:10:30 +08:00
if err != nil {
panic(err)
}
cfg.ListenHost = func(string) string { return host }
cfg.ListenPort = port
return cfg
}
func NewDefaultClientConfig() *ClientConfig {
cc := &ClientConfig{
HTTPUserAgent: version.DefaultHttpUserAgent,
ExtendedHandshakeClientVersion: version.DefaultExtendedHandshakeClientVersion,
Bep20: version.DefaultBep20Prefix,
UpnpID: version.DefaultUpnpId,
NominalDialTimeout: 20 * time.Second,
MinDialTimeout: 3 * time.Second,
EstablishedConnsPerTorrent: 50,
HalfOpenConnsPerTorrent: 25,
TotalHalfOpenConns: 100,
TorrentPeersHighWater: 500,
TorrentPeersLowWater: 50,
HandshakesTimeout: 4 * time.Second,
KeepAliveTimeout: time.Minute,
2020-03-17 10:47:53 +08:00
DhtStartingNodes: func(network string) dht.StartingNodesGetter {
return func() ([]dht.Addr, error) { return dht.GlobalBootstrapAddrs(network) }
},
PeriodicallyAnnounceTorrentsToDht: true,
ListenHost: func(string) string { return "" },
UploadRateLimiter: unlimited,
DownloadRateLimiter: unlimited,
DisableAcceptRateLimiting: true,
DropMutuallyCompletePeers: true,
HeaderObfuscationPolicy: HeaderObfuscationPolicy{
Preferred: true,
RequirePreferred: false,
},
2021-06-18 12:59:20 +08:00
CryptoSelector: mse.DefaultCryptoSelector,
CryptoProvides: mse.AllSupportedCrypto,
ListenPort: 42069,
Extensions: defaultPeerExtensionBytes(),
AcceptPeerConnections: true,
2022-05-09 17:37:08 +08:00
MaxUnverifiedBytes: 64 << 20,
2018-04-12 21:34:31 +08:00
}
return cc
2014-08-21 16:07:06 +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.
}