more fixups

This commit is contained in:
idk
2020-12-19 17:37:55 -05:00
parent 24602b3dde
commit 1b5592e0f8
6 changed files with 18 additions and 16 deletions

View File

@ -120,8 +120,8 @@ func TestDecode(t *testing.T) {
{`d1:X3:foo1:Yi10e1:h3:bare`, new(dT), dT{"foo", 10, ""}, false, false},
{`d3:fooli0ei1ee3:barli2ei3eee`, new(map[string][]int), map[string][]int{
"foo": []int{0, 1},
"bar": []int{2, 3},
"foo": {0, 1},
"bar": {2, 3},
}, false, false},
{`de`, new(map[string]string), map[string]string{}, false, false},

View File

@ -112,8 +112,8 @@ func TestEncode(t *testing.T) {
{"c": 2, "d": 3},
}, `ld1:ai0e1:bi1eed1:ci2e1:di3eee`, false},
{[][]byte{
[]byte{'0', '2', '4', '6', '8'},
[]byte{'a', 'c', 'e'},
{'0', '2', '4', '6', '8'},
{'a', 'c', 'e'},
}, `l5:024683:acee`, false},
{(*[]interface{})(nil), ``, false},

View File

@ -515,7 +515,7 @@ func (s *Server) handleQuery(raddr net.Addr, m krpc.Message) {
r.Nodes6 = s.routingTable6.Closest(m.A.InfoHash, s.conf.K)
}
if ni {
r.NodesI2P = s.routingTableI2P.Closest(m.A.InfoHash, s.conf.K)
r.Nodes = s.routingTableI2P.Closest(m.A.InfoHash, s.conf.K)
}
}
s.reply(raddr, m.T, r)

View File

@ -149,7 +149,7 @@ func ExampleServer() {
time.Sleep(time.Second * 2)
// Add the peer to let the DHT server1 has the peer.
pm.AddPeer(infohash, metainfo.NewAddress(&net.IPAddr{IP:net.ParseIP("127.0.0.1")}, 9001))
pm.AddPeer(infohash, metainfo.NewAddress(&net.IPAddr{IP: net.ParseIP("127.0.0.1")}, 9001))
// Search the torrent infohash again, but from DHT server2,
// which will search the DHT server1 recursively.

View File

@ -281,7 +281,7 @@ type ResponseResult struct {
// find_node
Nodes6 CompactIPv6Node `bencode:"nodes6,omitempty"` // BEP 32
NodesI2P CompactI2PNode `bencode:nodes,omitempty`
// NodesI2P CompactI2PNode `bencode:nodes,omitempty`
// Token is used for future "announce_peer".
//

View File

@ -19,6 +19,7 @@ import (
"encoding/binary"
"fmt"
"io"
"log"
"net"
"strconv"
@ -32,7 +33,7 @@ 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 {
Addr net.Addr // For IPv4, its length must be 4.
Addr net.Addr // For IPv4, its length must be 4.
Port uint16
}
@ -170,9 +171,9 @@ func (a Address) UDPAddr() *net.UDPAddr {
func (a Address) String() string {
if a.Addr == nil {
if a.Port == 0 {
return net.JoinHostPort("127.0.0.1", strconv.FormatUint(uint64(0), 10))
}
if a.Port == 0 {
return net.JoinHostPort("127.0.0.1", strconv.FormatUint(uint64(0), 10))
}
return net.JoinHostPort("127.0.0.1", strconv.FormatUint(uint64(a.Port), 10))
}
host, _ := SplitHostPort(a.Addr)
@ -185,10 +186,10 @@ func (a Address) String() string {
// Equal reports whether n is equal to o, which is equal to
// n.HasIPAndPort(o.IP, o.Port)
func (a Address) Equal(o Address) bool {
if a.String() == o.String(){
return a.Port == o.Port
}
return false
if a.String() == o.String() {
return a.Port == o.Port
}
return false
}
// HasIPAndPort reports whether the current node has the ip and the port.
@ -213,6 +214,7 @@ func (a *Address) UnmarshalBinary(b []byte) (err error) {
switch _len {
case net.IPv4len, net.IPv6len:
default:
log.Println("UNMARSHAL", _len)
return ErrInvalidAddr
}
@ -240,7 +242,7 @@ func (a *Address) decode(vs []interface{}) (err error) {
}
}()
host := vs[0].(string)
host, _, _ := net.SplitHostPort(vs[0].(string))
ip := net.ParseIP(host)
if len(ip) == 0 {
return ErrInvalidAddr