From: afjoseph <7126721+afjoseph@users.noreply.github.com> Date: Mon, 25 Apr 2022 14:11:15 +0000 (+0200) Subject: [client] Handle nil IP in badPeerAddr() X-Git-Tag: v1.43.0~24 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=5bcd5d1b135aca9b43d28095bcdd1c105028bc1f;p=btrtrc.git [client] Handle nil IP in badPeerAddr() --- diff --git a/client.go b/client.go index ee8261f3..a351f049 100644 --- a/client.go +++ b/client.go @@ -1136,8 +1136,9 @@ func (cl *Client) badPeerAddr(addr PeerRemoteAddr) bool { return false } +// Returns whether the IP address and port are considered "bad". func (cl *Client) badPeerIPPort(ip net.IP, port int) bool { - if port == 0 { + if port == 0 || ip == nil { return true } if cl.dopplegangerAddr(net.JoinHostPort(ip.String(), strconv.FormatInt(int64(port), 10))) { diff --git a/client_test.go b/client_test.go index 7b7dd2fd..65182d58 100644 --- a/client_test.go +++ b/client_test.go @@ -4,6 +4,8 @@ import ( "encoding/binary" "fmt" "io" + "net" + "net/netip" "os" "path/filepath" "reflect" @@ -773,3 +775,75 @@ func TestClientDisabledImplicitNetworksButDhtEnabled(t *testing.T) { assert.Empty(t, cl.listeners) assert.NotEmpty(t, cl.DhtServers()) } + +func TestBadPeerIpPort(t *testing.T) { + for _, tc := range []struct { + title string + ip net.IP + port int + expectedOk bool + setup func(*Client) + }{ + {"empty both", nil, 0, true, func(*Client) {}}, + {"empty/nil ip", nil, 6666, true, func(*Client) {}}, + { + "empty port", + net.ParseIP("127.0.0.1/32"), + 0, true, + func(*Client) {}, + }, + { + "in doppleganger addresses", + net.ParseIP("127.0.0.1/32"), + 2322, + true, + func(cl *Client) { + cl.dopplegangerAddrs["10.0.0.1:2322"] = struct{}{} + }, + }, + { + "in IP block list", + net.ParseIP("10.0.0.1"), + 2322, + true, + func(cl *Client) { + cl.ipBlockList = iplist.New([]iplist.Range{ + iplist.Range{First: net.ParseIP("10.0.0.1"), Last: net.ParseIP("10.0.0.255")}, + }) + }, + }, + { + "in bad peer IPs", + net.ParseIP("10.0.0.1"), + 2322, + true, + func(cl *Client) { + ipAddr, ok := netip.AddrFromSlice(net.ParseIP("10.0.0.1")) + require.True(t, ok) + cl.badPeerIPs = map[netip.Addr]struct{}{} + cl.badPeerIPs[ipAddr] = struct{}{} + }, + }, + { + "good", + net.ParseIP("10.0.0.1"), + 2322, + false, + func(cl *Client) {}, + }, + } { + t.Run(tc.title, func(t *testing.T) { + cfg := TestingConfig(t) + cfg.DisableTCP = true + cfg.DisableUTP = true + cfg.NoDHT = false + cl, err := NewClient(cfg) + require.NoError(t, err) + defer cl.Close() + + tc.setup(cl) + require.Equal(t, tc.expectedOk, cl.badPeerIPPort(tc.ip, tc.port)) + }) + } + +}