-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move FilterAddrs here from go-addr-util
- Loading branch information
1 parent
eb5c75f
commit 8e0d03f
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package manet | ||
|
||
import ma "github.com/multiformats/go-multiaddr" | ||
|
||
// FilterAddrs is a filter that removes certain addresses, according to the given filters. | ||
// If all filters return true, the address is kept. | ||
func FilterAddrs(a []ma.Multiaddr, filters ...func(ma.Multiaddr) bool) []ma.Multiaddr { | ||
b := make([]ma.Multiaddr, 0, len(a)) | ||
for _, addr := range a { | ||
good := true | ||
for _, filter := range filters { | ||
good = good && filter(addr) | ||
} | ||
if good { | ||
b = append(b, addr) | ||
} | ||
} | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package manet | ||
|
||
import ( | ||
"testing" | ||
|
||
ma "github.com/multiformats/go-multiaddr" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFilterAddrs(t *testing.T) { | ||
bad := []ma.Multiaddr{ | ||
newMultiaddr(t, "/ip6/fe80::1/tcp/1234"), // link local | ||
newMultiaddr(t, "/ip6/fe80::100/tcp/1234"), // link local | ||
} | ||
|
||
good := []ma.Multiaddr{ | ||
newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234"), | ||
newMultiaddr(t, "/ip6/::1/tcp/1234"), | ||
newMultiaddr(t, "/ip4/1.2.3.4/udp/1234/utp"), | ||
} | ||
|
||
goodAndBad := append(good, bad...) | ||
|
||
// test filters | ||
filter := AddrOverNonLocalIP | ||
|
||
require.Empty(t, FilterAddrs(bad, filter)) | ||
require.ElementsMatch(t, FilterAddrs(good, filter), good) | ||
require.ElementsMatch(t, FilterAddrs(goodAndBad, filter), good) | ||
} |