2016-11-22 12:40:46 +08:00
|
|
|
package tracker
|
|
|
|
|
|
|
|
import (
|
2020-05-20 10:04:53 +08:00
|
|
|
"fmt"
|
2016-11-22 12:40:46 +08:00
|
|
|
"net"
|
2018-02-19 13:19:18 +08:00
|
|
|
|
2019-08-10 16:46:07 +08:00
|
|
|
"github.com/anacrolix/dht/v2/krpc"
|
2016-11-22 12:40:46 +08:00
|
|
|
)
|
|
|
|
|
2018-02-12 20:49:33 +08:00
|
|
|
type Peer struct {
|
|
|
|
IP net.IP
|
|
|
|
Port int
|
|
|
|
ID []byte
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:04:53 +08:00
|
|
|
func (p Peer) String() string {
|
|
|
|
loc := net.JoinHostPort(p.IP.String(), fmt.Sprintf("%d", p.Port))
|
|
|
|
if len(p.ID) != 0 {
|
|
|
|
return fmt.Sprintf("%x at %s", p.ID, loc)
|
|
|
|
} else {
|
|
|
|
return loc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 20:49:33 +08:00
|
|
|
// Set from the non-compact form in BEP 3.
|
2019-11-15 07:22:59 +08:00
|
|
|
func (p *Peer) FromDictInterface(d map[string]interface{}) {
|
2016-11-22 12:40:46 +08:00
|
|
|
p.IP = net.ParseIP(d["ip"].(string))
|
2019-04-13 21:25:19 +08:00
|
|
|
if _, ok := d["peer id"]; ok {
|
|
|
|
p.ID = []byte(d["peer id"].(string))
|
|
|
|
}
|
2016-11-22 12:40:46 +08:00
|
|
|
p.Port = int(d["port"].(int64))
|
|
|
|
}
|
2018-02-19 13:19:18 +08:00
|
|
|
|
|
|
|
func (p Peer) FromNodeAddr(na krpc.NodeAddr) Peer {
|
|
|
|
p.IP = na.IP
|
|
|
|
p.Port = na.Port
|
|
|
|
return p
|
|
|
|
}
|