Compare commits
1 Commits
master
...
interfaces
Author | SHA1 | Date | |
---|---|---|---|
ffeb6c3a57 |
@ -138,14 +138,14 @@ type Config struct {
|
||||
// that's, the "get_peers" query.
|
||||
//
|
||||
// The default callback does noting.
|
||||
OnSearch func(infohash string, ip net.IP, port uint16)
|
||||
OnSearch func(infohash string, ip net.Addr, port uint16)
|
||||
|
||||
// OnTorrent is called when someone has the torrent infohash
|
||||
// or someone has just downloaded the torrent infohash,
|
||||
// that's, the "get_peers" response or "announce_peer" query.
|
||||
//
|
||||
// The default callback does noting.
|
||||
OnTorrent func(infohash string, ip net.IP, port uint16)
|
||||
OnTorrent func(infohash string, ip net.Addr, port uint16)
|
||||
|
||||
// HandleInMessage is used to intercept the incoming DHT message.
|
||||
// For example, you can debug the message as the log.
|
||||
@ -197,10 +197,10 @@ func (c *Config) set(conf ...Config) {
|
||||
c.RespTimeout = time.Second * 10
|
||||
}
|
||||
if c.OnSearch == nil {
|
||||
c.OnSearch = func(string, net.IP, uint16) {}
|
||||
c.OnSearch = func(string, net.Addr, uint16) {}
|
||||
}
|
||||
if c.OnTorrent == nil {
|
||||
c.OnTorrent = func(string, net.IP, uint16) {}
|
||||
c.OnTorrent = func(string, net.Addr, uint16) {}
|
||||
}
|
||||
if c.HandleInMessage == nil {
|
||||
c.HandleInMessage = c.in
|
||||
@ -849,14 +849,14 @@ func (s *Server) onFindNodeResp(t *transaction, a *net.UDPAddr, m krpc.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func isIPv6(ip net.IP) bool {
|
||||
func isIPv6(ip net.Addr) bool {
|
||||
if ip.To4() == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ipIsZero(ip net.IP) bool {
|
||||
func ipIsZero(ip net.Addr) bool {
|
||||
for _, b := range ip {
|
||||
if b != 0 {
|
||||
return false
|
||||
|
@ -63,12 +63,12 @@ func (pm *testPeerManager) GetPeers(infohash metainfo.Hash, maxnum int,
|
||||
return
|
||||
}
|
||||
|
||||
func onSearch(infohash string, ip net.IP, port uint16) {
|
||||
func onSearch(infohash string, ip net.Addr, port uint16) {
|
||||
addr := net.JoinHostPort(ip.String(), strconv.FormatUint(uint64(port), 10))
|
||||
fmt.Printf("%s is searching %s\n", addr, infohash)
|
||||
}
|
||||
|
||||
func onTorrent(infohash string, ip net.IP, port uint16) {
|
||||
func onTorrent(infohash string, ip net.Addr, port uint16) {
|
||||
addr := net.JoinHostPort(ip.String(), strconv.FormatUint(uint64(port), 10))
|
||||
fmt.Printf("%s has downloaded %s\n", addr, infohash)
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ type PeerManager interface {
|
||||
|
||||
type peer struct {
|
||||
ID metainfo.Hash
|
||||
IP net.IP
|
||||
IP net.Addr
|
||||
Port uint16
|
||||
Token string
|
||||
Time time.Time
|
||||
|
2
go.mod
2
go.mod
@ -1,3 +1,5 @@
|
||||
module github.com/xgfone/bt
|
||||
|
||||
go 1.11
|
||||
|
||||
require github.com/eyedeekay/sam3 v0.32.32 // indirect
|
||||
|
@ -30,7 +30,7 @@ type Node struct {
|
||||
}
|
||||
|
||||
// NewNode returns a new Node.
|
||||
func NewNode(id metainfo.Hash, ip net.IP, port int) Node {
|
||||
func NewNode(id metainfo.Hash, ip net.Addr, port int) Node {
|
||||
return Node{ID: id, Addr: metainfo.NewAddress(ip, uint16(port))}
|
||||
}
|
||||
|
||||
|
@ -23,24 +23,39 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/xgfone/bt/bencode"
|
||||
"github.com/eyedeekay/sam3/i2pkeys"
|
||||
)
|
||||
|
||||
func to4(ip net.Addr) (net.IP, net.Addr) {
|
||||
switch v := ip.(type) {
|
||||
case *net.IPAddr:
|
||||
return v.IP.To4(), v
|
||||
case *i2pkeys.I2PAddr:
|
||||
return net.ParseIP(""), nil
|
||||
case *net.TCPAddr:
|
||||
return v.IP.To4(), v
|
||||
case *net.UDPAddr:
|
||||
return v.IP.To4(), v
|
||||
}
|
||||
return net.ParseIP(""), nil
|
||||
}
|
||||
|
||||
// ErrInvalidAddr is returned when the compact address is invalid.
|
||||
var ErrInvalidAddr = fmt.Errorf("invalid compact information of ip and port")
|
||||
|
||||
// Address represents a client/server listening on a UDP port implementing
|
||||
// the DHT protocol.
|
||||
type Address struct {
|
||||
IP net.IP // For IPv4, its length must be 4.
|
||||
Port uint16
|
||||
IP net.Addr // For IPv4, its length must be 4.
|
||||
// Port uint16
|
||||
}
|
||||
|
||||
// NewAddress returns a new Address.
|
||||
func NewAddress(ip net.IP, port uint16) Address {
|
||||
if ipv4 := ip.To4(); len(ipv4) > 0 {
|
||||
ip = ipv4
|
||||
func NewAddress(ip net.Addr, port uint16) Address {
|
||||
if ipv4, ad := to4(ip); len(ipv4) > 0 {
|
||||
ip = ad
|
||||
}
|
||||
return Address{IP: ip, Port: port}
|
||||
return Address{IP: ip}
|
||||
}
|
||||
|
||||
// NewAddressFromString returns a new Address by the address string.
|
||||
@ -72,10 +87,10 @@ func NewAddressesFromString(s string) (addrs []Address, err error) {
|
||||
|
||||
addrs = make([]Address, len(ips))
|
||||
for i, ip := range ips {
|
||||
if ipv4 := ip.To4(); len(ipv4) != 0 {
|
||||
addrs[i] = Address{IP: ipv4, Port: port}
|
||||
if ipv4, ad := to4(&net.IPAddr{IP:ip}); len(ipv4) != 0 {
|
||||
addrs[i] = Address{IP: ad}
|
||||
} else {
|
||||
addrs[i] = Address{IP: ip, Port: port}
|
||||
addrs[i] = Address{IP: ad}
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,7 +109,7 @@ func (a *Address) FromString(addr string) (err error) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid address '%s': %s", addr, err)
|
||||
}
|
||||
a.Port = uint16(v)
|
||||
// a.Port = uint16(v)
|
||||
}
|
||||
|
||||
ips, err := net.LookupIP(host)
|
||||
@ -104,9 +119,9 @@ func (a *Address) FromString(addr string) (err error) {
|
||||
return fmt.Errorf("the domain '%s' has no ips", host)
|
||||
}
|
||||
|
||||
a.IP = ips[0]
|
||||
if ip := a.IP.To4(); len(ip) > 0 {
|
||||
a.IP = ip
|
||||
a.IP = &net.IPAddr{IP:ips[0]}
|
||||
if ip, ad := to4(a.IP); len(ip) > 0 {
|
||||
a.IP = ad
|
||||
}
|
||||
|
||||
return
|
||||
@ -114,9 +129,9 @@ func (a *Address) FromString(addr string) (err error) {
|
||||
|
||||
// FromUDPAddr sets the ip from net.UDPAddr.
|
||||
func (a *Address) FromUDPAddr(ua *net.UDPAddr) {
|
||||
a.Port = uint16(ua.Port)
|
||||
a.IP = ua.IP
|
||||
if ipv4 := a.IP.To4(); len(ipv4) != 0 {
|
||||
// a.Port = uint16(ua.Port)
|
||||
a.IP = &net.IPAddr{ua.IP}
|
||||
if ipv4 := to4(&net.IPAddr{a.IP}); len(ipv4) != 0 {
|
||||
a.IP = ipv4
|
||||
}
|
||||
}
|
||||
@ -143,7 +158,7 @@ func (a Address) Equal(o Address) bool {
|
||||
}
|
||||
|
||||
// HasIPAndPort reports whether the current node has the ip and the port.
|
||||
func (a Address) HasIPAndPort(ip net.IP, port uint16) bool {
|
||||
func (a Address) HasIPAndPort(ip net.Addr, port uint16) bool {
|
||||
return port == a.Port && a.IP.Equal(ip)
|
||||
}
|
||||
|
||||
@ -162,12 +177,12 @@ func (a Address) WriteBinary(w io.Writer) (m int, err error) {
|
||||
func (a *Address) UnmarshalBinary(b []byte) (err error) {
|
||||
_len := len(b) - 2
|
||||
switch _len {
|
||||
case net.IPv4len, net.IPv6len:
|
||||
case net.Addrv4len, net.Addrv6len:
|
||||
default:
|
||||
return ErrInvalidAddr
|
||||
}
|
||||
|
||||
a.IP = make(net.IP, _len)
|
||||
a.IP = make(net.Addr, _len)
|
||||
copy(a.IP, b[:_len])
|
||||
a.Port = binary.BigEndian.Uint16(b[_len:])
|
||||
return
|
||||
@ -197,7 +212,7 @@ func (a *Address) decode(vs []interface{}) (err error) {
|
||||
host := vs[0].(string)
|
||||
if a.IP = net.ParseIP(host); len(a.IP) == 0 {
|
||||
return ErrInvalidAddr
|
||||
} else if ip := a.IP.To4(); len(ip) != 0 {
|
||||
} else if ip := to4(a.IP); len(ip) != 0 {
|
||||
a.IP = ip
|
||||
}
|
||||
|
||||
@ -246,7 +261,7 @@ type HostAddress struct {
|
||||
|
||||
// NewHostAddress returns a new host addrress.
|
||||
func NewHostAddress(host string, port uint16) HostAddress {
|
||||
return HostAddress{Host: host, Port: port}
|
||||
return HostAddress{Host: host}
|
||||
}
|
||||
|
||||
// NewHostAddressFromString returns a new host address by the string.
|
||||
|
@ -43,15 +43,15 @@ const (
|
||||
)
|
||||
|
||||
// CompactIP is used to handle the compact ipv4 or ipv6.
|
||||
type CompactIP net.IP
|
||||
type CompactIP net.Addr
|
||||
|
||||
func (ci CompactIP) String() string {
|
||||
return net.IP(ci).String()
|
||||
return net.Addr(ci).String()
|
||||
}
|
||||
|
||||
// MarshalBencode implements the interface bencode.Marshaler.
|
||||
func (ci CompactIP) MarshalBencode() ([]byte, error) {
|
||||
ip := net.IP(ci)
|
||||
ip := net.Addr(ci)
|
||||
if ipv4 := ip.To4(); len(ipv4) != 0 {
|
||||
ip = ipv4
|
||||
}
|
||||
@ -60,13 +60,13 @@ func (ci CompactIP) MarshalBencode() ([]byte, error) {
|
||||
|
||||
// UnmarshalBencode implements the interface bencode.Unmarshaler.
|
||||
func (ci *CompactIP) UnmarshalBencode(b []byte) (err error) {
|
||||
var ip net.IP
|
||||
var ip net.Addr
|
||||
if err = bencode.DecodeBytes(b, &ip); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch len(ip) {
|
||||
case net.IPv4len, net.IPv6len:
|
||||
case net.Addrv4len, net.Addrv6len:
|
||||
default:
|
||||
return errInvalidIP
|
||||
}
|
||||
@ -91,7 +91,7 @@ type ExtendedHandshakeMsg struct {
|
||||
// Port is the local client port, which is redundant and no need
|
||||
// for the receiving side of the connection to send this.
|
||||
Port uint16 `bencode:"p,omitempty"` // BEP 10
|
||||
IPv6 net.IP `bencode:"ipv6,omitempty"` // BEP 10
|
||||
IPv6 net.Addr `bencode:"ipv6,omitempty"` // BEP 10
|
||||
IPv4 CompactIP `bencode:"ipv4,omitempty"` // BEP 10
|
||||
YourIP CompactIP `bencode:"yourip,omitempty"` // BEP 10
|
||||
|
||||
|
@ -31,7 +31,7 @@ import (
|
||||
// infohash: infohash of torrent.
|
||||
//
|
||||
// BEP 6
|
||||
func GenerateAllowedFastSet(set []uint32, sz uint32, ip net.IP, infohash metainfo.Hash) {
|
||||
func GenerateAllowedFastSet(set []uint32, sz uint32, ip net.Addr, infohash metainfo.Hash) {
|
||||
if ipv4 := ip.To4(); ipv4 != nil {
|
||||
ip = ipv4
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ type AnnounceRequest struct {
|
||||
Left int64 // Required, but default: 0, which should be only used for test or last.
|
||||
Event uint32 // Required, but default: 0
|
||||
|
||||
IP net.IP // Optional
|
||||
IP net.Addr // Optional
|
||||
Key int32 // Optional
|
||||
NumWant int32 // Optional, BEP 15: -1 for default. But we use 0 as default.
|
||||
Port uint16 // Optional
|
||||
|
@ -55,7 +55,7 @@ type AnnounceRequest struct {
|
||||
Uploaded int64
|
||||
Event uint32
|
||||
|
||||
IP net.IP
|
||||
IP net.Addr
|
||||
Key int32
|
||||
NumWant int32 // -1 for default
|
||||
Port uint16
|
||||
@ -73,11 +73,11 @@ func (r *AnnounceRequest) DecodeFrom(b []byte, ipv4 bool) {
|
||||
r.Event = binary.BigEndian.Uint32(b[64:68])
|
||||
|
||||
if ipv4 {
|
||||
r.IP = make(net.IP, net.IPv4len)
|
||||
r.IP = make(net.Addr, net.Addrv4len)
|
||||
copy(r.IP, b[68:72])
|
||||
b = b[72:]
|
||||
} else {
|
||||
r.IP = make(net.IP, net.IPv6len)
|
||||
r.IP = make(net.Addr, net.Addrv6len)
|
||||
copy(r.IP, b[68:84])
|
||||
b = b[84:]
|
||||
}
|
||||
@ -154,16 +154,16 @@ func (r *AnnounceResponse) DecodeFrom(b []byte, ipv4 bool) {
|
||||
r.Seeders = binary.BigEndian.Uint32(b[8:12])
|
||||
|
||||
b = b[12:]
|
||||
iplen := net.IPv6len
|
||||
iplen := net.Addrv6len
|
||||
if ipv4 {
|
||||
iplen = net.IPv4len
|
||||
iplen = net.Addrv4len
|
||||
}
|
||||
|
||||
_len := len(b)
|
||||
step := iplen + 2
|
||||
r.Addresses = make([]metainfo.Address, 0, _len/step)
|
||||
for i := step; i <= _len; i += step {
|
||||
ip := make(net.IP, iplen)
|
||||
ip := make(net.Addr, iplen)
|
||||
copy(ip, b[i-step:i-2])
|
||||
port := binary.BigEndian.Uint16(b[i-2 : i])
|
||||
r.Addresses = append(r.Addresses, metainfo.Address{IP: ip, Port: port})
|
||||
|
Reference in New Issue
Block a user