Skip to content

Commit

Permalink
Merge pull request #63 from libp2p/marco/docs-and-expose-allowlist
Browse files Browse the repository at this point in the history
Add docs around WithAllowlistedMultiaddrs. Expose allowlist
  • Loading branch information
MarcoPolo authored Jul 3, 2022
2 parents f1e381d + 0a1e1ea commit 9176f63
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
23 changes: 23 additions & 0 deletions p2p/host/resource-manager/allowlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@ import (
"github.com/multiformats/go-multiaddr"
)

func ExampleWithAllowlistedMultiaddrs() {
somePeer, err := test.RandPeerID()
if err != nil {
panic("Failed to generate somePeer")
}

limits := DefaultLimits.AutoScale()
rcmgr, err := NewResourceManager(NewFixedLimiter(limits), WithAllowlistedMultiaddrs([]multiaddr.Multiaddr{
// Any peer connecting from this IP address
multiaddr.StringCast("/ip4/1.2.3.4"),
// Only the specified peer from this address
multiaddr.StringCast("/ip4/2.2.3.4/p2p/" + peer.Encode(somePeer)),
// Only peers from this 1.2.3.0/24 IP address range
multiaddr.StringCast("/ip4/1.2.3.0/ipcidr/24"),
}))
if err != nil {
panic("Failed to start resource manager")
}

// Use rcmgr as before
_ = rcmgr
}

func TestAllowedSimple(t *testing.T) {
allowlist := newAllowlist()
ma := multiaddr.StringCast("/ip4/1.2.3.4/tcp/1234")
Expand Down
12 changes: 12 additions & 0 deletions p2p/host/resource-manager/rcmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ func (r *resourceManager) GetAllowlist() *Allowlist {
return r.allowlist
}

// GetAllowlist tries to get the allowlist from the given resourcemanager
// interface by checking to see if its concrete type is a resourceManager.
// Returns nil if it fails to get the allowlist.
func GetAllowlist(rcmgr network.ResourceManager) *Allowlist {
r, ok := rcmgr.(*resourceManager)
if !ok {
return nil
}

return r.allowlist
}

func (r *resourceManager) ViewSystem(f func(network.ResourceScope) error) error {
return f(r.system)
}
Expand Down
5 changes: 5 additions & 0 deletions p2p/host/resource-manager/rcmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,11 @@ func TestResourceManagerWithAllowlist(t *testing.T) {
t.Fatal(err)
}

ableToGetAllowlist := GetAllowlist(rcmgr)
if ableToGetAllowlist == nil {
t.Fatal("Expected to be able to get the allowlist")
}

// A connection comes in from a non-allowlisted ip address
_, err = rcmgr.OpenConnection(network.DirInbound, true, multiaddr.StringCast("/ip4/1.2.3.5"))
if err == nil {
Expand Down

0 comments on commit 9176f63

Please sign in to comment.