Skip to content

Commit

Permalink
Support minimum peer extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Oct 25, 2021
1 parent e8067f7 commit 3d49a29
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,11 @@ func (cl *Client) runReceivedConn(c *PeerConn) {
// Client lock must be held before entering this.
func (cl *Client) runHandshookConn(c *PeerConn, t *Torrent) error {
c.setTorrent(t)
for i, b := range cl.config.MinPeerExtensions {
if c.PeerExtensionBytes[i]&b != b {
return fmt.Errorf("peer did not meet minimum peer extensions: %x", c.PeerExtensionBytes)
}
}
if c.PeerID == cl.peerID {
if c.outgoing {
connsToSelf.Add(1)
Expand Down
7 changes: 7 additions & 0 deletions cmd/torrent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/anacrolix/missinggo/v2"
"github.com/anacrolix/tagflag"
"github.com/anacrolix/torrent/bencode"
pp "github.com/anacrolix/torrent/peer_protocol"
"github.com/anacrolix/torrent/version"
"github.com/davecgh/go-spew/spew"
"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -186,6 +187,9 @@ type DownloadCmd struct {
UtpPeers bool `default:"true"`
Webtorrent bool `default:"true"`
DisableWebseeds bool
// Don't progress past handshake for peer connections where the peer doesn't offer the fast
// extension.
RequireFastExtension bool

Ipv4 bool `default:"true"`
Ipv6 bool `default:"true"`
Expand Down Expand Up @@ -316,6 +320,9 @@ func downloadErr(flags downloadFlags) error {
if flags.Quiet {
clientConfig.Logger = log.Discard
}
if flags.RequireFastExtension {
clientConfig.MinPeerExtensions.SetBit(pp.ExtensionBitFast, true)
}
clientConfig.MaxUnverifiedBytes = flags.MaxUnverifiedBytes.Int64()

var stop missinggo.SynchronizedEvent
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ type ClientConfig struct {
DHTOnQuery func(query *krpc.Msg, source net.Addr) (propagate bool)

Extensions PeerExtensionBits
// Bits that peers must have set to proceed past handshakes.
MinPeerExtensions PeerExtensionBits

DisableWebtorrent bool
DisableWebseeds bool
Expand Down
3 changes: 3 additions & 0 deletions test/issue377_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func testReceiveChunkStorageFailure(t *testing.T, seederFast bool) {
defer testutil.ExportStatusWriter(seederClient, "s", t)()
leecherClientConfig := torrent.TestingConfig(t)
leecherClientConfig.Debug = true
// Don't require fast extension, whether the seeder will provide it or not (so we can test mixed
// cases).
leecherClientConfig.MinPeerExtensions.SetBit(pp.ExtensionBitFast, false)
justOneNetwork(leecherClientConfig)
leecherClient, err := torrent.NewClient(leecherClientConfig)
require.NoError(t, err)
Expand Down
3 changes: 3 additions & 0 deletions testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package torrent
import (
"testing"
"time"

pp "github.com/anacrolix/torrent/peer_protocol"
)

func TestingConfig(t testing.TB) *ClientConfig {
Expand All @@ -15,6 +17,7 @@ func TestingConfig(t testing.TB) *ClientConfig {
cfg.DisableAcceptRateLimiting = true
cfg.ListenPort = 0
cfg.KeepAliveTimeout = time.Millisecond
cfg.MinPeerExtensions.SetBit(pp.ExtensionBitFast, true)
//cfg.Debug = true
//cfg.Logger = cfg.Logger.WithText(func(m log.Msg) string {
// t := m.Text()
Expand Down
2 changes: 2 additions & 0 deletions torrent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ func TestPieceHashFailed(t *testing.T) {
func TestTorrentMetainfoIncompleteMetadata(t *testing.T) {
cfg := TestingConfig(t)
cfg.Debug = true
// Disable this just because we manually initiate a connection without it.
cfg.MinPeerExtensions.SetBit(pp.ExtensionBitFast, false)
cl, err := NewClient(cfg)
require.NoError(t, err)
defer cl.Close()
Expand Down

0 comments on commit 3d49a29

Please sign in to comment.