From 92866cfacd7b50014e4a59f79fdfe116fb034295 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Sun, 22 Mar 2015 18:44:09 -0400 Subject: [PATCH 1/2] models: include port in peerkey --- tracker/models/models.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tracker/models/models.go b/tracker/models/models.go index 125ac5f..89df300 100644 --- a/tracker/models/models.go +++ b/tracker/models/models.go @@ -46,8 +46,8 @@ func (e NotFoundError) Error() string { return string(e) } type PeerList []Peer type PeerKey string -func NewPeerKey(peerID string, ip net.IP) PeerKey { - return PeerKey(peerID + "//" + ip.String()) +func NewPeerKey(peerID string, ip net.IP, port string) PeerKey { + return PeerKey(peerID + "//" + ip.String() + ":" + port) } func (pk PeerKey) IP() net.IP { @@ -88,7 +88,7 @@ func (p *Peer) HasIPv6() bool { } func (p *Peer) Key() PeerKey { - return NewPeerKey(p.ID, p.IP) + return NewPeerKey(p.ID, p.IP, string(p.Port)) } // Torrent is a swarm for a given torrent file. From 6188d52de0f3e07f0ed9c20f9512a64b0bb7c4e7 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Sun, 22 Mar 2015 19:41:57 -0400 Subject: [PATCH 2/2] models: Port method for PeerKey This also adds some more docs for related stuff in the models package. --- tracker/models/models.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tracker/models/models.go b/tracker/models/models.go index 89df300..e48388c 100644 --- a/tracker/models/models.go +++ b/tracker/models/models.go @@ -43,13 +43,18 @@ type NotFoundError ClientError func (e ClientError) Error() string { return string(e) } func (e NotFoundError) Error() string { return string(e) } +// PeerList represents a list of peers: either seeders or leechers. type PeerList []Peer + +// PeerKey is the key used to uniquely identify a peer in a swarm. type PeerKey string +// NewPeerKey creates a properly formatted PeerKey. func NewPeerKey(peerID string, ip net.IP, port string) PeerKey { return PeerKey(peerID + "//" + ip.String() + ":" + port) } +// IP parses and returns the IP address for a given PeerKey. func (pk PeerKey) IP() net.IP { ip := net.ParseIP(strings.Split(string(pk), "//")[1]) if rval := ip.To4(); rval != nil { @@ -58,10 +63,16 @@ func (pk PeerKey) IP() net.IP { return ip } +// PeerID returns the PeerID section of a PeerKey. func (pk PeerKey) PeerID() string { return strings.Split(string(pk), "//")[0] } +// Port returns the port section of the PeerKey. +func (pk PeerKey) Port() string { + return strings.Split(string(pk), "//")[2] +} + // Peer is a participant in a swarm. type Peer struct { ID string `json:"id"` @@ -79,14 +90,19 @@ type Peer struct { LastAnnounce int64 `json:"last_announce"` } +// HasIPv4 determines if a peer's IP address can be represented as an IPv4 +// address. func (p *Peer) HasIPv4() bool { return !p.HasIPv6() } +// HasIPv6 determines if a peer's IP address can be represented as an IPv6 +// address. func (p *Peer) HasIPv6() bool { return len(p.IP) == net.IPv6len } +// Key returns a PeerKey for the given peer. func (p *Peer) Key() PeerKey { return NewPeerKey(p.ID, p.IP, string(p.Port)) }