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.
|
2018-04-04 15:59:28 +08:00
|
|
|
type Peer struct {
|
|
|
|
Id [20]byte
|
|
|
|
IP net.IP
|
|
|
|
Port int
|
|
|
|
Source peerSource
|
|
|
|
// 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-01-13 03:26:29 +08:00
|
|
|
// FromPex generate Peer from peer exchange
|
2018-07-10 09:21:24 +08:00
|
|
|
func (me *Peer) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) {
|
2018-04-04 15:59:28 +08:00
|
|
|
me.IP = append([]byte(nil), na.IP...)
|
|
|
|
me.Port = na.Port
|
2019-10-17 14:46:35 +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
|
|
|
}
|
|
|
|
|
2018-11-16 07:35:30 +08:00
|
|
|
func (me Peer) addr() IpPort {
|
2020-01-13 03:26:29 +08:00
|
|
|
return IpPort{IP: me.IP, Port: uint16(me.Port)}
|
2018-04-04 15:59:28 +08:00
|
|
|
}
|