From de5e56efde8f5ae0fc043fe4fd1717b44af80313 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 27 Jun 2019 16:28:09 +0200 Subject: [PATCH] fix: check for blocked addrs without allocating --- filter.go | 31 +++++++++++++++++++------------ go.mod | 5 +---- go.sum | 4 ---- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/filter.go b/filter.go index a555df5..4eb0c56 100644 --- a/filter.go +++ b/filter.go @@ -5,7 +5,6 @@ import ( "sync" ma "github.com/multiformats/go-multiaddr" - manet "github.com/multiformats/go-multiaddr-net" ) // Action is an enum modelling all possible filter actions. @@ -112,17 +111,25 @@ func (fs *Filters) RemoveLiteral(ipnet net.IPNet) (removed bool) { // Instead, the highest-specific last filter should win; that way more specific filters // override more general ones. func (fs *Filters) AddrBlocked(a ma.Multiaddr) (deny bool) { - maddr := ma.Split(a) - if len(maddr) == 0 { - return fs.DefaultAction == ActionDeny - } - netaddr, err := manet.ToNetAddr(maddr[0]) - if err != nil { - // if we can't parse it, it's probably not blocked. - return fs.DefaultAction == ActionDeny - } - netip := net.ParseIP(netaddr.String()) - if netip == nil { + var ( + netip net.IP + found bool + ) + + ma.ForEach(a, func(c ma.Component) bool { + switch c.Protocol().Code { + case ma.P_IP6ZONE: + return true + case ma.P_IP6, ma.P_IP4: + found = true + netip = net.IP(c.RawValue()) + return false + default: + return false + } + }) + + if !found { return fs.DefaultAction == ActionDeny } diff --git a/go.mod b/go.mod index 4adb057..4e46b65 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,3 @@ module github.com/libp2p/go-maddr-filter -require ( - github.com/multiformats/go-multiaddr v0.0.1 - github.com/multiformats/go-multiaddr-net v0.0.1 -) +require github.com/multiformats/go-multiaddr v0.0.1 diff --git a/go.sum b/go.sum index 11cfae5..cbf49eb 100644 --- a/go.sum +++ b/go.sum @@ -10,10 +10,6 @@ github.com/mr-tron/base58 v1.1.0 h1:Y51FGVJ91WBqCEabAi5OPUz38eAx8DakuAm5svLcsfQ= github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8= github.com/multiformats/go-multiaddr v0.0.1 h1:/QUV3VBMDI6pi6xfiw7lr6xhDWWvQKn9udPn68kLSdY= github.com/multiformats/go-multiaddr v0.0.1/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44= -github.com/multiformats/go-multiaddr-dns v0.0.1 h1:jQt9c6tDSdQLIlBo4tXYx7QUHCPjxsB1zXcag/2S7zc= -github.com/multiformats/go-multiaddr-dns v0.0.1/go.mod h1:9kWcqw/Pj6FwxAwW38n/9403szc57zJPs45fmnznu3Q= -github.com/multiformats/go-multiaddr-net v0.0.1 h1:76O59E3FavvHqNg7jvzWzsPSW5JSi/ek0E4eiDVbg9g= -github.com/multiformats/go-multiaddr-net v0.0.1/go.mod h1:nw6HSxNmCIQH27XPGBuX+d1tnvM7ihcFwHMSstNAVUU= github.com/multiformats/go-multihash v0.0.1 h1:HHwN1K12I+XllBCrqKnhX949Orn4oawPkegHMu2vDqQ= github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U= golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 h1:ng3VDlRp5/DHpSWl02R4rM9I+8M2rhmsuLwAMmkLQWE=