From f87b7329599a6e0b5f830de714dba7b8d72fe895 Mon Sep 17 00:00:00 2001 From: "Bora M. Alper" Date: Tue, 12 Sep 2017 15:22:53 +0100 Subject: [PATCH 1/2] added the initial version of the (t *Torrent) KnownSwarm() function --- torrent.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/torrent.go b/torrent.go index 7f04e5f3..aba33139 100644 --- a/torrent.go +++ b/torrent.go @@ -11,6 +11,7 @@ import ( "math/rand" "net" "os" + "strconv" "sync" "text/tabwriter" "time" @@ -122,6 +123,44 @@ func (t *Torrent) Closed() <-chan struct{} { return t.closed.LockedChan(&t.cl.mu) } +// KnownSwarm returns the known subset of the peers in the Torrent's swarm, including active, +// pending, and half-open peers. +func (t *Torrent) KnownSwarm() (ks []Peer) { + // Add pending peers to the list + for _, peer := range t.peers { + ks = append(ks, peer) + } + + // Add active peers to the list + for conn := range t.conns { + host, portString, err := net.SplitHostPort(conn.remoteAddr().String()) + if err != nil { + panic(err) + } + + ip := net.ParseIP(host) + port, err := strconv.Atoi(portString) + if err != nil { + panic(err) + } + + ks = append(ks, Peer{ + Id: conn.PeerID, + IP: ip, + Port: port, + Source: conn.Discovery, + // TODO: the connection can be unencrypted due to our (or the peer's) preference, + // but the remote peer might support the encryption. Find a better way to query + // that information, if possible. + SupportsEncryption: conn.encrypted, + }) + } + + // TODO: how can we add half-open peers? + + return +} + func (t *Torrent) setChunkSize(size pp.Integer) { t.chunkSize = size t.chunkPool = &sync.Pool{ From a66b0e83f3ec40cc42fae906390ea6d808af03c6 Mon Sep 17 00:00:00 2001 From: "Bora M. Alper" Date: Sat, 16 Sep 2017 11:48:16 +0100 Subject: [PATCH 2/2] added support for half-open peers in KnownSwarm() function --- torrent.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/torrent.go b/torrent.go index aba33139..d476e170 100644 --- a/torrent.go +++ b/torrent.go @@ -131,6 +131,11 @@ func (t *Torrent) KnownSwarm() (ks []Peer) { ks = append(ks, peer) } + // Add half-open peers to the list + for _, peer := range t.halfOpen { + ks = append(ks, peer) + } + // Add active peers to the list for conn := range t.conns { host, portString, err := net.SplitHostPort(conn.remoteAddr().String()) @@ -149,15 +154,16 @@ func (t *Torrent) KnownSwarm() (ks []Peer) { IP: ip, Port: port, Source: conn.Discovery, - // TODO: the connection can be unencrypted due to our (or the peer's) preference, - // but the remote peer might support the encryption. Find a better way to query - // that information, if possible. + // > If the connection is encrypted, that's certainly enough to set SupportsEncryption. + // > But if we're not connected to them with an encrypted connection, I couldn't say + // > what's appropriate. We can carry forward the SupportsEncryption value as we + // > received it from trackers/DHT/PEX, or just use the encryption state for the + // > connection. It's probably easiest to do the latter for now. + // https://github.com/anacrolix/torrent/pull/188 SupportsEncryption: conn.encrypted, }) } - // TODO: how can we add half-open peers? - return }