X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=config.go;h=e2d0ea1ebec230cce1c7fa50e67ff5fc88f8006d;hb=HEAD;hp=c7b08965f44775eb76f71e567dd194804ed03954;hpb=4bb2233b46e97fa7ba27c1fb0b75c8adb7a23738;p=btrtrc.git diff --git a/config.go b/config.go index c7b08965..e2d0ea1e 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,7 @@ package torrent import ( + "context" "net" "net/http" "net/url" @@ -9,18 +10,45 @@ import ( "github.com/anacrolix/dht/v2" "github.com/anacrolix/dht/v2/krpc" "github.com/anacrolix/log" - "github.com/anacrolix/missinggo" - "github.com/anacrolix/missinggo/expect" - "github.com/anacrolix/missinggo/v2/conntrack" + "github.com/anacrolix/missinggo/v2" "golang.org/x/time/rate" "github.com/anacrolix/torrent/iplist" "github.com/anacrolix/torrent/mse" "github.com/anacrolix/torrent/storage" + "github.com/anacrolix/torrent/version" ) +// Contains config elements that are exclusive to tracker handling. There may be other fields in +// ClientConfig that are also relevant. +type ClientTrackerConfig struct { + // Don't announce to trackers. This only leaves DHT to discover peers. + DisableTrackers bool `long:"disable-trackers"` + // Defines DialContext func to use for HTTP tracker announcements + TrackerDialContext func(ctx context.Context, network, addr string) (net.Conn, error) + // Defines ListenPacket func to use for UDP tracker announcements + TrackerListenPacket func(network, addr string) (net.PacketConn, 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) +} + +type ClientDhtConfig struct { + // Don't create a DHT. + NoDHT bool `long:"disable-dht"` + DhtStartingNodes func(network string) dht.StartingNodesGetter + // Called for each anacrolix/dht Server created for the Client. + ConfigureAnacrolixDhtServer func(*dht.ServerConfig) + PeriodicallyAnnounceTorrentsToDht bool + // OnQuery hook func + DHTOnQuery func(query *krpc.Msg, source net.Addr) (propagate bool) +} + // Probably not safe to modify this after it's given to a Client. type ClientConfig struct { + ClientTrackerConfig + ClientDhtConfig + // Store torrent file data in this directory unless .DefaultStorage is // specified. DataDir string `long:"data-dir" description:"directory to store downloaded torrent data"` @@ -30,15 +58,7 @@ type ClientConfig struct { ListenPort int NoDefaultPortForwarding bool UpnpID string - // Don't announce to trackers. This only leaves DHT to discover peers. - DisableTrackers bool `long:"disable-trackers"` - DisablePEX bool `long:"disable-pex"` - - // Don't create a DHT. - NoDHT bool `long:"disable-dht"` - DhtStartingNodes func(network string) dht.StartingNodesGetter - // Called for each anacrolix/dht Server created for the Client. - ConfigureAnacrolixDhtServer func(*dht.ServerConfig) + DisablePEX bool `long:"disable-pex"` // Never send chunks to peers. NoUpload bool `long:"no-upload"` @@ -59,6 +79,8 @@ type ClientConfig struct { // (~4096), and the requested chunk size (~16KiB, see // TorrentSpec.ChunkSize). DownloadRateLimiter *rate.Limiter + // Maximum unverified bytes across all torrents. Not used if zero. + MaxUnverifiedBytes int64 // User-provided Client peer ID. If not present, one is generated automatically. PeerID string @@ -85,11 +107,21 @@ type ClientConfig struct { Debug bool `help:"enable debugging"` Logger log.Logger + // Used for torrent sources and webseeding if set. + WebTransport http.RoundTripper // 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) + // Defines DialContext func to use for HTTP requests, such as for fetching metainfo and webtorrent seeds + HTTPDialContext func(ctx context.Context, network, addr string) (net.Conn, error) // HTTPUserAgent changes default UserAgent for HTTP requests HTTPUserAgent string + // HttpRequestDirector modifies the request before it's sent. + // Useful for adding authentication headers, for example + HttpRequestDirector func(*http.Request) error + // WebsocketTrackerHttpHeader returns a custom header to be used when dialing a websocket connection + // to the tracker. Useful for adding authentication headers + WebsocketTrackerHttpHeader func() http.Header // Updated occasionally to when there's been some changes to client // behaviour in case other clients are assuming anything of us. See also // `bep20`. @@ -115,6 +147,11 @@ type ClientConfig struct { // 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 + // Maximum bytes to buffer per peer connection for peer request data before it is sent. + MaxAllocPeerRequestDataPerConn int64 // The IP addresses as our peers should see them. May differ from the // local interfaces due to NAT or other network configurations. @@ -131,25 +168,35 @@ type ClientConfig struct { // 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 - - ConnTracker *conntrack.Instance - - // OnQuery hook func - DHTOnQuery func(query *krpc.Msg, source net.Addr) (propagate bool) - - DefaultRequestStrategy requestStrategyMaker + // Whether to accept peer connections at all. + AcceptPeerConnections bool + // Whether a Client should want conns without delegating to any attached Torrents. This is + // useful when torrents might be added dynamically in callbacks for example. + AlwaysWantConns bool Extensions PeerExtensionBits + // Bits that peers must have set to proceed past handshakes. + MinPeerExtensions PeerExtensionBits DisableWebtorrent bool DisableWebseeds bool Callbacks Callbacks + + // ICEServers defines a slice describing servers available to be used by + // ICE, such as STUN and TURN servers. + ICEServers []string + + DialRateLimiter *rate.Limiter + + PieceHashersPerTorrent int // default: 2 } func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig { host, port, err := missinggo.ParseHostPort(addr) - expect.Nil(err) + if err != nil { + panic(err) + } cfg.ListenHost = func(string) string { return host } cfg.ListenPort = port return cfg @@ -157,10 +204,10 @@ func (cfg *ClientConfig) SetListenAddr(addr string) *ClientConfig { func NewDefaultClientConfig() *ClientConfig { cc := &ClientConfig{ - HTTPUserAgent: "Go-Torrent/1.0", - ExtendedHandshakeClientVersion: "go.torrent dev 20181121", - Bep20: "-GT0002-", - UpnpID: "anacrolix/torrent", + HTTPUserAgent: version.DefaultHttpUserAgent, + ExtendedHandshakeClientVersion: version.DefaultExtendedHandshakeClientVersion, + Bep20: version.DefaultBep20Prefix, + UpnpID: version.DefaultUpnpId, NominalDialTimeout: 20 * time.Second, MinDialTimeout: 3 * time.Second, EstablishedConnsPerTorrent: 50, @@ -169,30 +216,30 @@ func NewDefaultClientConfig() *ClientConfig { TorrentPeersHighWater: 500, TorrentPeersLowWater: 50, HandshakesTimeout: 4 * time.Second, - 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, - DropMutuallyCompletePeers: true, + KeepAliveTimeout: time.Minute, + MaxAllocPeerRequestDataPerConn: 1 << 20, + ListenHost: func(string) string { return "" }, + UploadRateLimiter: unlimited, + DownloadRateLimiter: unlimited, + DisableAcceptRateLimiting: true, + DropMutuallyCompletePeers: true, HeaderObfuscationPolicy: HeaderObfuscationPolicy{ Preferred: true, RequirePreferred: false, }, - CryptoSelector: mse.DefaultCryptoSelector, - CryptoProvides: mse.AllSupportedCrypto, - ListenPort: 42069, - Logger: log.Default, - - DefaultRequestStrategy: RequestStrategyDuplicateRequestTimeout(5 * time.Second), - - Extensions: defaultPeerExtensionBytes(), + CryptoSelector: mse.DefaultCryptoSelector, + CryptoProvides: mse.AllSupportedCrypto, + ListenPort: 42069, + Extensions: defaultPeerExtensionBytes(), + AcceptPeerConnections: true, + MaxUnverifiedBytes: 64 << 20, + DialRateLimiter: rate.NewLimiter(10, 10), + PieceHashersPerTorrent: 2, + } + cc.DhtStartingNodes = func(network string) dht.StartingNodesGetter { + return func() ([]dht.Addr, error) { return dht.GlobalBootstrapAddrs(network) } } - //cc.ConnTracker.SetNoMaxEntries() - //cc.ConnTracker.Timeout = func(conntrack.Entry) time.Duration { return 0 } + cc.PeriodicallyAnnounceTorrentsToDht = true return cc }