Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move filter into net package #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions filter.go → net/filter.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package multiaddr
package manet

import (
"net"
"sync"

ma "github.com/multiformats/go-multiaddr"
)

// Action is an enum modelling all possible filter actions.
Expand Down Expand Up @@ -98,7 +100,7 @@ func (fs *Filters) RemoveLiteral(ipnet net.IPNet) (removed bool) {
return false
}

// AddrBlocked parses a ma.Multiaddr and, if a valid netip is found, it applies the
// AddrBlocked parses a ma.ma.Multiaddr and, if a valid netip is found, it applies the
// Filter set rules, returning true if the given address should be denied, and false if
// the given address is accepted.
//
Expand All @@ -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
Expand Down
18 changes: 10 additions & 8 deletions filter_test.go → net/filter_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package multiaddr
package manet

import (
"net"
"testing"

ma "github.com/multiformats/go-multiaddr"
)

func TestFilterListing(t *testing.T) {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down