From 4449d4948c31e2585adcaa6da41fa27e17534f5f Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Tue, 19 May 2020 23:22:17 -0700 Subject: [PATCH] feat: move filters into a sub-package These really don't belong in the root package. --- filter.go => filter/filter.go | 12 +++++++----- filter_test.go => filter/filter_test.go | 18 ++++++++++-------- 2 files changed, 17 insertions(+), 13 deletions(-) rename filter.go => filter/filter.go (95%) rename filter_test.go => filter/filter_test.go (92%) diff --git a/filter.go b/filter/filter.go similarity index 95% rename from filter.go rename to filter/filter.go index 6751202..4eb0c56 100644 --- a/filter.go +++ b/filter/filter.go @@ -1,8 +1,10 @@ -package multiaddr +package filter import ( "net" "sync" + + ma "github.com/multiformats/go-multiaddr" ) // Action is an enum modelling all possible filter actions. @@ -108,17 +110,17 @@ func (fs *Filters) RemoveLiteral(ipnet net.IPNet) (removed bool) { // TODO: currently, the last filter to match wins always, but it shouldn't be that way. // Instead, the highest-specific last filter should win; that way more specific filters // override more general ones. -func (fs *Filters) AddrBlocked(a Multiaddr) (deny bool) { +func (fs *Filters) AddrBlocked(a ma.Multiaddr) (deny bool) { var ( netip net.IP found bool ) - ForEach(a, func(c Component) bool { + ma.ForEach(a, func(c ma.Component) bool { switch c.Protocol().Code { - case P_IP6ZONE: + case ma.P_IP6ZONE: return true - case P_IP6, P_IP4: + case ma.P_IP6, ma.P_IP4: found = true netip = net.IP(c.RawValue()) return false diff --git a/filter_test.go b/filter/filter_test.go similarity index 92% rename from filter_test.go rename to filter/filter_test.go index 741ee0a..3df0812 100644 --- a/filter_test.go +++ b/filter/filter_test.go @@ -1,8 +1,10 @@ -package multiaddr +package filter import ( "net" "testing" + + ma "github.com/multiformats/go-multiaddr" ) func TestFilterListing(t *testing.T) { @@ -66,7 +68,7 @@ func TestFilterBlocking(t *testing.T) { "/ip6/fd00::2/tcp/321", "/ip6/fc00::1/udp/321", } { - maddr, err := NewMultiaddr(blocked) + maddr, err := ma.NewMultiaddr(blocked) if err != nil { t.Error(err) } @@ -82,7 +84,7 @@ func TestFilterBlocking(t *testing.T) { "/ip6/fe00::1/tcp/321", "/ip6/fc00::2/udp/321", } { - maddr, err := NewMultiaddr(addr) + maddr, err := ma.NewMultiaddr(addr) if err != nil { t.Error(err) } @@ -112,7 +114,7 @@ func TestFilterWhitelisting(t *testing.T) { "/ip4/1.2.3.254/tcp/123", "/ip4/1.2.3.254/udp/321", } { - maddr, err := NewMultiaddr(addr) + maddr, err := ma.NewMultiaddr(addr) if err != nil { t.Error(err) } @@ -128,7 +130,7 @@ func TestFilterWhitelisting(t *testing.T) { "/ip6/fd00::2/tcp/321", "/ip6/fc00::1/udp/321", } { - maddr, err := NewMultiaddr(blocked) + maddr, err := ma.NewMultiaddr(blocked) if err != nil { t.Error(err) } @@ -159,7 +161,7 @@ func TestFiltersRemoveRules(t *testing.T) { // these are all whitelisted, should be OK for _, addr := range ips { - maddr, err := NewMultiaddr(addr) + maddr, err := ma.NewMultiaddr(addr) if err != nil { t.Error(err) } @@ -174,7 +176,7 @@ func TestFiltersRemoveRules(t *testing.T) { // Show that they all apply, these are now blacklisted & should fail for _, addr := range ips { - maddr, err := NewMultiaddr(addr) + maddr, err := ma.NewMultiaddr(addr) if err != nil { t.Error(err) } @@ -191,7 +193,7 @@ func TestFiltersRemoveRules(t *testing.T) { // our default is reject, so the 1.2.3.0/24 should be rejected now, // along with everything else for _, addr := range ips { - maddr, err := NewMultiaddr(addr) + maddr, err := ma.NewMultiaddr(addr) if err != nil { t.Error(err) }