2018-04-04 15:59:28 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2019-08-10 16:46:07 +08:00
|
|
|
"github.com/anacrolix/dht/v2/krpc"
|
2019-08-21 18:58:40 +08:00
|
|
|
|
2018-07-10 09:21:24 +08:00
|
|
|
"github.com/anacrolix/torrent/peer_protocol"
|
2018-04-04 15:59:28 +08:00
|
|
|
)
|
|
|
|
|
2019-12-18 10:52:00 +08:00
|
|
|
// Peer connection info, handed about publicly.
|
2020-05-29 17:44:48 +08:00
|
|
|
type PeerInfo struct {
|
2018-04-04 15:59:28 +08:00
|
|
|
Id [20]byte
|
2020-02-20 13:47:37 +08:00
|
|
|
Addr net.Addr
|
2020-02-21 08:07:50 +08:00
|
|
|
Source PeerSource
|
2018-04-04 15:59:28 +08:00
|
|
|
// Peer is known to support encryption.
|
|
|
|
SupportsEncryption bool
|
2018-07-10 09:21:24 +08:00
|
|
|
peer_protocol.PexPeerFlags
|
2019-12-18 10:52:00 +08:00
|
|
|
// Whether we can ignore poor or bad behaviour from the peer.
|
|
|
|
Trusted bool
|
2018-04-04 15:59:28 +08:00
|
|
|
}
|
|
|
|
|
2020-06-02 15:41:59 +08:00
|
|
|
func (me PeerInfo) equal(other PeerInfo) bool {
|
2020-04-16 10:17:18 +08:00
|
|
|
return me.Id == other.Id &&
|
|
|
|
me.Addr.String() == other.Addr.String() &&
|
|
|
|
me.Source == other.Source &&
|
|
|
|
me.SupportsEncryption == other.SupportsEncryption &&
|
|
|
|
me.PexPeerFlags == other.PexPeerFlags &&
|
|
|
|
me.Trusted == other.Trusted
|
|
|
|
}
|
|
|
|
|
2020-05-29 17:44:48 +08:00
|
|
|
// Generate PeerInfo from peer exchange
|
|
|
|
func (me *PeerInfo) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) {
|
2020-02-20 13:47:37 +08:00
|
|
|
me.Addr = ipPortAddr{append([]byte(nil), na.IP...), na.Port}
|
2020-02-21 08:07:50 +08:00
|
|
|
me.Source = PeerSourcePex
|
2018-04-04 15:59:28 +08:00
|
|
|
// If they prefer encryption, they must support it.
|
2018-07-10 09:21:24 +08:00
|
|
|
if fs.Get(peer_protocol.PexPrefersEncryption) {
|
2018-04-04 15:59:28 +08:00
|
|
|
me.SupportsEncryption = true
|
|
|
|
}
|
2018-07-10 09:21:24 +08:00
|
|
|
me.PexPeerFlags = fs
|
2018-04-04 15:59:28 +08:00
|
|
|
}
|
|
|
|
|
2020-05-29 17:44:48 +08:00
|
|
|
func (me PeerInfo) addr() IpPort {
|
2020-02-20 13:47:37 +08:00
|
|
|
return IpPort{IP: addrIpOrNil(me.Addr), Port: uint16(addrPortOrZero(me.Addr))}
|
2018-04-04 15:59:28 +08:00
|
|
|
}
|