Skip to content

Commit

Permalink
fix: include addresses that passed negative filters
Browse files Browse the repository at this point in the history
  • Loading branch information
2color authored and wenyue committed Oct 17, 2024
1 parent 01a56fc commit c9df436
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
52 changes: 29 additions & 23 deletions routing/http/server/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,45 +129,51 @@ func applyFilters(provider *types.PeerRecord, filterAddrs, filterProtocols []str
}

// If there are only negative filters, no addresses will be included in the result. The function will return an empty list.
// For an address to be included, it must pass all negative filters AND match at least one positive filter.
// For an address to be included, it must pass all negative filters
func applyAddrFilter(addrs []types.Multiaddr, filterAddrsQuery []string) []types.Multiaddr {
if len(filterAddrsQuery) == 0 {
return addrs
}

var filteredAddrs []types.Multiaddr
var positiveFilters, negativeFilters []multiaddr.Protocol

// Separate positive and negative filters
for _, filter := range filterAddrsQuery {
if strings.HasPrefix(filter, "!") {
negativeFilters = append(negativeFilters, multiaddr.ProtocolWithName(filter[1:]))
} else {
positiveFilters = append(positiveFilters, multiaddr.ProtocolWithName(filter))
}
}

for _, addr := range addrs {
protocols := addr.Protocols()
includeAddr := true

// First, check all negative filters
for _, filter := range filterAddrsQuery {
if strings.HasPrefix(filter, "!") {
protocolFromFilter := multiaddr.ProtocolWithName(filter[1:])
if containsProtocol(protocols, protocolFromFilter) {
includeAddr = false
break
}
}

// Check negative filters
if containsAny(protocols, negativeFilters) {
continue
}

// If the address passed all negative filters, check positive filters
if includeAddr {
for _, filter := range filterAddrsQuery {
if !strings.HasPrefix(filter, "!") {
protocolFromFilter := multiaddr.ProtocolWithName(filter)
if containsProtocol(protocols, protocolFromFilter) {
filteredAddrs = append(filteredAddrs, addr)
break
}
}
}
// If no positive filters or matches a positive filter, include the address
if len(positiveFilters) == 0 || containsAny(protocols, positiveFilters) {
filteredAddrs = append(filteredAddrs, addr)
}
}

return filteredAddrs
}

// Helper function to check if protocols contain any of the filters
func containsAny(protocols []multiaddr.Protocol, filters []multiaddr.Protocol) bool {
for _, filter := range filters {
if containsProtocol(protocols, filter) {
return true
}
}
return false
}

func containsProtocol(protos []multiaddr.Protocol, proto multiaddr.Protocol) bool {
for _, p := range protos {
if p.Code == proto.Code {
Expand Down
4 changes: 2 additions & 2 deletions routing/http/server/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestApplyAddrFilter(t *testing.T) {
{
name: "Exclude TCP",
filterAddrs: []string{"!tcp"},
expectedAddrs: []types.Multiaddr{},
expectedAddrs: []types.Multiaddr{{Multiaddr: addr2}, {Multiaddr: addr5}, {Multiaddr: addr6}, {Multiaddr: addr8}},
},
{
name: "Include WebTransport and exclude p2p-circuit",
Expand All @@ -85,7 +85,7 @@ func TestApplyAddrFilter(t *testing.T) {
{
name: "Multiple negative filters",
filterAddrs: []string{"!tcp", "!ws"},
expectedAddrs: []types.Multiaddr{},
expectedAddrs: []types.Multiaddr{{Multiaddr: addr2}, {Multiaddr: addr5}, {Multiaddr: addr6}, {Multiaddr: addr8}},
},
}

Expand Down

0 comments on commit c9df436

Please sign in to comment.