Skip to content

Commit

Permalink
add found peers to allowlist
Browse files Browse the repository at this point in the history
  • Loading branch information
nickjfree committed Dec 14, 2022
1 parent cb7d7f1 commit dbb94b8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/routing/discovery/peerfinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (pf *PeerFinder) findPeers() error {
if p.ID == pf.ID() {
continue
}
pf.AllowPeer(p.ID.String())
pf.peers <- fmt.Sprintf("ipfs/%s", p.ID)
logger.Printf("found peer %s in %s", p.ID, pf.ns)
count += 1
Expand Down
19 changes: 19 additions & 0 deletions pkg/wire/ipfs/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
dht "github.com/libp2p/go-libp2p-kad-dht"
dis_routing "github.com/libp2p/go-libp2p/p2p/discovery/routing"
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
"github.com/lucas-clemente/quic-go"
ma "github.com/multiformats/go-multiaddr"

Expand Down Expand Up @@ -271,6 +272,8 @@ type P2PHost struct {
cancel context.CancelFunc
// advertise namespace
namespace string
// allowedlist of peers
allowedPeers map[string]ma.Multiaddr
}

func NewP2PHost() (*P2PHost, error) {
Expand Down Expand Up @@ -308,6 +311,7 @@ func NewP2PHost() (*P2PHost, error) {
ctx: ctx,
peerChan: peerChan,
cancel: cancel,
allowedPeers: make(map[string]ma.Multiaddr),
}
if h.Bootstrap(bootstraps); err != nil {
return nil, err
Expand Down Expand Up @@ -409,6 +413,21 @@ func (h *P2PHost) Background() error {
}
}

// add peer to allowlist of resource manager
func (h *P2PHost) AllowPeer(peer string) error {
addr := fmt.Sprintf("/ip4/0.0.0.0/ipcidr/0/p2p/%s", peer)
if _, ok := h.allowedPeers[addr]; ok {
return nil
} else {
al := rcmgr.GetAllowlist(h.Host.Network().ResourceManager())
maAddr := ma.StringCast(addr)
if err := al.Add(maAddr); err != nil {
return errors.WithStack(err)
}
}
return nil
}

// get privkey, save it to local path
func getPrivKey(path string) (crypto.PrivKey, error) {
if _, err := os.Stat(path); err != nil {
Expand Down

0 comments on commit dbb94b8

Please sign in to comment.