Skip to content

Commit

Permalink
Fix address filtering for /ip6, add tests
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Lars Gierth <larsg@systemli.org>
  • Loading branch information
Lars Gierth committed Apr 11, 2016
1 parent 9ce2eeb commit 5aa798c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
6 changes: 2 additions & 4 deletions p2p/net/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package filter

import (
"net"
"strings"
"sync"

manet "gx/ipfs/QmYVqhVfbK4BKvbW88Lhm26b3ud14sTBvcm1H7uWUx1Fkp/go-multiaddr-net"
Expand All @@ -27,14 +26,13 @@ func (fs *Filters) AddDialFilter(f *net.IPNet) {
}

func (f *Filters) AddrBlocked(a ma.Multiaddr) bool {
_, addr, err := manet.DialArgs(a)
ipstr, err := manet.ToNetAddr(a)
if err != nil {
// if we cant parse it, its probably not blocked
return false
}

ipstr := strings.Split(addr, ":")[0]
ip := net.ParseIP(ipstr)
ip := net.ParseIP(ipstr.String())
f.mu.RLock()
defer f.mu.RUnlock()
for _, ft := range f.filters {
Expand Down
51 changes: 51 additions & 0 deletions p2p/net/filter/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package filter

import (
"net"
"testing"

ma "gx/ipfs/QmcobAGsCjYt5DXoq9et9L8yR8er7o7Cu3DTvpaq12jYSz/go-multiaddr"
)

func TestFilter(t *testing.T) {
f := NewFilters()
for _, cidr := range []string{
"1.2.3.0/24",
"4.3.2.1/32",
"fd00::/8",
"fc00::1/128",
} {
_, ipnet, _ := net.ParseCIDR(cidr)
f.AddDialFilter(ipnet)
}

for _, blocked := range []string{
"/ip4/1.2.3.4",
"/ip4/4.3.2.1",
"/ip6/fd00::2",
"/ip6/fc00::1",
} {
maddr, err := ma.NewMultiaddr(blocked)
if err != nil {
t.Error(err)
}
if !f.AddrBlocked(maddr) {
t.Fatalf("expected %s to be blocked", blocked)
}
}

for _, notBlocked := range []string{
"/ip4/1.2.4.1",
"/ip4/4.3.2.2",
"/ip6/fe00::1",
"/ip6/fc00::2",
} {
maddr, err := ma.NewMultiaddr(notBlocked)
if err != nil {
t.Error(err)
}
if f.AddrBlocked(maddr) {
t.Fatalf("expected %s to not be blocked", notBlocked)
}
}
}

0 comments on commit 5aa798c

Please sign in to comment.