-
Notifications
You must be signed in to change notification settings - Fork 154
feat: add routing filtering to delegated routing server IPIP-484 #671
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
43eb24b
feat: add protocol filtering
2color 30b853c
test: improve tests
2color a920f23
fix: remove negative filter tests and fix filter
2color 3dc1a58
chore: add query params conditionally
2color 496805d
fix: tests
2color a4e9def
chore: update changelog
2color da4a194
fix: ensure protocol filter is case-insensitive
2color 3a6de23
feat: add generic filter iterator and use for fitlering
2color b9958d0
feat: proto & addr filter in peer routing endpoint
2color 0a8bebd
fix: use PeerRecord in the FindPeers
2color 825d82f
chore: ignore check
2color b6ed0dc
fix: conversion from bitswap record
2color f210c0d
test: add addr and protocol tests to peer handler
2color ef3f6b6
Apply suggestions from code review
2color 9811e83
fix: return nil when a record doesnt pass filter
2color 51f200a
fix: rename to protocolsAllowed for readability
2color d8edbe6
fix: include addresses that passed negative filters
2color 3d2a8e5
docs: improve comments and add test
2color 61b5def
Merge branch 'main' into add-protocol-filtering
2color f13c862
test: add real world test case
lidel 137d34f
Apply suggestions from code review
2color File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,141 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/ipfs/boxo/routing/http/types" | ||
| "github.com/multiformats/go-multiaddr" | ||
| ) | ||
|
|
||
| // filters implements IPIP-0484 | ||
|
|
||
| func parseFilter(param string) []string { | ||
| if param == "" { | ||
| return nil | ||
| } | ||
| return strings.Split(strings.ToLower(param), ",") | ||
| } | ||
|
|
||
| func filterProviders(providers []types.Record, filterAddrs, filterProtocols []string) []types.Record { | ||
| if len(filterAddrs) == 0 && len(filterProtocols) == 0 { | ||
| return providers | ||
| } | ||
|
|
||
| filtered := make([]types.Record, 0, len(providers)) | ||
|
|
||
| for _, provider := range providers { | ||
| if schema := provider.GetSchema(); schema == types.SchemaPeer { | ||
| peer, ok := provider.(*types.PeerRecord) | ||
| if !ok { | ||
| logger.Errorw("problem casting find providers result", "Schema", provider.GetSchema(), "Type", reflect.TypeOf(provider).String()) | ||
| // if the type assertion fails, we exlude record from results | ||
| continue | ||
| } | ||
|
|
||
| record := applyFilters(peer, filterAddrs, filterProtocols) | ||
|
|
||
| if record != nil { | ||
| filtered = append(filtered, record) | ||
| } | ||
|
|
||
| } else { | ||
| // Will we ever encounter the SchemaBitswap type? Evidence seems to suggest that no longer | ||
| logger.Errorw("encountered unknown provider schema", "Schema", provider.GetSchema(), "Type", reflect.TypeOf(provider).String()) | ||
| } | ||
| } | ||
| return filtered | ||
| } | ||
|
|
||
| // Applies the filters. Returns nil if the provider does not pass the protocols filter | ||
| // The address filter is more complicated because it potentially modifies the Addrs slice. | ||
| func applyFilters(provider *types.PeerRecord, filterAddrs, filterProtocols []string) *types.PeerRecord { | ||
| if !applyProtocolFilter(provider.Protocols, filterProtocols) { | ||
| // If the provider doesn't match any of the passed protocols, the provider is omitted from the response. | ||
| return nil | ||
| } | ||
|
|
||
| // return untouched if there's no filter or filterAddrsQuery contains "unknown" and provider has no addrs | ||
| if len(filterAddrs) == 0 || (len(provider.Addrs) == 0 && slices.Contains(filterAddrs, "unknown")) { | ||
| return provider | ||
| } | ||
|
|
||
| filteredAddrs := applyAddrFilter(provider.Addrs, filterAddrs) | ||
|
|
||
| // If filtering resulted in no addrs, omit the provider | ||
| if len(filteredAddrs) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| provider.Addrs = filteredAddrs | ||
| return provider | ||
| } | ||
|
|
||
| // 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. | ||
| func applyAddrFilter(addrs []types.Multiaddr, filterAddrsQuery []string) []types.Multiaddr { | ||
| if len(filterAddrsQuery) == 0 { | ||
| return addrs | ||
| } | ||
|
|
||
| filteredAddrs := make([]types.Multiaddr, 0, len(addrs)) | ||
|
|
||
2color marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for _, addr := range addrs { | ||
2color marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| protocols := addr.Protocols() | ||
| includeAddr := true | ||
|
|
||
| // First, check all negative filters | ||
| for _, filter := range filterAddrsQuery { | ||
| if strings.HasPrefix(filter, "!") { | ||
| protocolStringFromFilter := strings.TrimPrefix(filter, "!") | ||
| protocolFromFilter := multiaddr.ProtocolWithName(protocolStringFromFilter) | ||
2color marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if containsProtocol(protocols, protocolFromFilter) { | ||
| includeAddr = false | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 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 | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return filteredAddrs | ||
| } | ||
|
|
||
| func containsProtocol(protos []multiaddr.Protocol, proto multiaddr.Protocol) bool { | ||
| for _, p := range protos { | ||
| if p.Code == proto.Code { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func applyProtocolFilter(peerProtocols []string, filterProtocols []string) bool { | ||
2color marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if len(filterProtocols) == 0 { | ||
| // If no filter is passed, do not filter | ||
| return true | ||
| } | ||
|
|
||
| for _, filterProtocol := range filterProtocols { | ||
| if filterProtocol == "unknown" && len(peerProtocols) == 0 { | ||
| return true | ||
| } | ||
|
|
||
| for _, peerProtocol := range peerProtocols { | ||
| return peerProtocol == filterProtocol | ||
| } | ||
| } | ||
| return false | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.