Skip to content

Commit

Permalink
Fix errors with connection gating
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 7, 2024
1 parent 45a683a commit c7021f4
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 64 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ default: clean tidy $(NAME)

all: tidy releases $(PLATFORMS)

local: clean tidy install

$(BINDIR):
test -d $(BINDIR)

Expand Down
12 changes: 7 additions & 5 deletions entity/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/bahner/go-ma-actor/p2p/peer"
"github.com/libp2p/go-libp2p/core/network"
p2peer "github.com/libp2p/go-libp2p/core/peer"
log "github.com/sirupsen/logrus"
)

// This function connects to a peer using the DHT.
Expand All @@ -16,21 +17,22 @@ func (e *Entity) ConnectPeer() (pi p2peer.AddrInfo, err error) {

pid, err := e.DID.PeerID()
if err != nil {
log.Debugf("Failed to get peer ID: %v", err)
return p2peer.AddrInfo{}, err
}

// If we're already connected, return
if p.DHT.Host().Network().Connectedness(pid) == network.Connected {
return p2peer.AddrInfo{}, peer.ErrAlreadyConnected
log.Debugf("Already connected to peer: %s", pid.String())
return pi, peer.ErrAlreadyConnected
}

// Look for the peer in the DHT
pi, err = p.DHT.FindPeer(e.Ctx, pid)
if err != nil {
return pi, err
}
pai := p.DHT.Host().Peerstore().PeerInfo(pid)
log.Debugf("PeerInfo: %v", pai.Addrs)

// Connect to the peer
log.Debugf("Connecting to peer with addrs: %v", pi.Addrs)
err = p.DHT.Host().Connect(e.Ctx, pi)

return pi, err
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ go 1.22

require (
github.com/adrg/xdg v0.4.0
github.com/bahner/go-ma v0.7.4-0.20240307170127-633db23a877a
github.com/bahner/go-ma v0.7.4-0.20240307210945-9437e049e6d7
github.com/gdamore/tcell/v2 v2.7.2
github.com/ipfs/go-cid v0.4.1
github.com/libp2p/go-libp2p v0.32.2
github.com/libp2p/go-libp2p-kad-dht v0.24.4
github.com/libp2p/go-libp2p-pubsub v0.10.0
Expand Down Expand Up @@ -59,7 +60,6 @@ require (
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/boxo v0.18.0 // indirect
github.com/ipfs/go-block-format v0.2.0 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
github.com/ipfs/go-ds-measure v0.2.0 // indirect
github.com/ipfs/go-fs-lock v0.0.7 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ github.com/alibabacloud-go/tea-utils v1.3.5/go.mod h1:EI/o33aBfj3hETm4RLiAxF/ThQ
github.com/aliyun/credentials-go v1.1.0/go.mod h1:ZXrrxv386Mj6z8NpihLKpexQE550m7j3LlyCvYub9aE=
github.com/aliyun/credentials-go v1.1.2/go.mod h1:ozcZaMR5kLM7pwtCMEpVmQ242suV6qTJya2bDq4X1Tw=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/bahner/go-ma v0.7.4-0.20240307170127-633db23a877a h1:6zf8BmpzLgrHl3QMqgttkryonmbSiN24m1zedUR8Gs8=
github.com/bahner/go-ma v0.7.4-0.20240307170127-633db23a877a/go.mod h1:IgMPcdPzH1GkP5mMz2PhJh8831hGi4TzhYbOV8UFzZ0=
github.com/bahner/go-ma v0.7.4-0.20240307210945-9437e049e6d7 h1:kvLkXI/fQFvzn5rHZ8RYpvtO7Lue9XrPemf4TpUxnX8=
github.com/bahner/go-ma v0.7.4-0.20240307210945-9437e049e6d7/go.mod h1:IgMPcdPzH1GkP5mMz2PhJh8831hGi4TzhYbOV8UFzZ0=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
Expand Down
12 changes: 2 additions & 10 deletions p2p/connmgr/gater.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package connmgr

import (
"github.com/bahner/go-ma"
"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/p2p/peer"
"github.com/libp2p/go-libp2p/core/control"
Expand Down Expand Up @@ -67,16 +66,9 @@ func (cg *ConnectionGater) InterceptUpgraded(_ network.Conn) (allow bool, reason

func (cg *ConnectionGater) isAllowed(p p2peer.ID) bool {

if config.P2PDiscoveryAllow() {
if config.P2PDiscoveryAllow() || cg.AllowAll {
return true
}

// NB! Check peer.IsAllowed first. Because it might be explicitly denied and we want to adhere to that.
// So if it's explicitly denied, we don't need to check the other conditions.
if !peer.IsAllowed(p.String()) {
log.Warnf("Peer %s is explicitly denied", p)
return false
}

return cg.AllowAll || cg.ConnMgr.IsProtected(p, ma.RENDEZVOUS)
return peer.IsAllowed(p.String())
}
42 changes: 0 additions & 42 deletions p2p/dht/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,45 +84,3 @@ func (d *DHT) DiscoverPeers(ctx context.Context, discoveryOpts ...discovery.Opti

return nil
}

// // Protects a discovered peer by connecting to it and protecting it.
// // Make sure to only called this function after a peer has been discovered
// // and that it's allowed to connect to it.
// func (d *DHT) handleDiscoveredPeer(ctx context.Context, pai p2peer.AddrInfo) error {
// log.Debugf("Discovered peer: %s", pai.ID.String())

// id := pai.ID.String()

// // If peer is explicitly denied, close any connections
// if !peer.IsAllowed(id) {
// // For this to happen the peer must be known AND have been disallowed
// // Or it is unknown and allowAll is false
// log.Warnf("Discovered peer %s has been denied access.", id)
// log.Warnf("Closing connections to peer %s", id)
// d.h.Network().ClosePeer(pai.ID)
// return nil
// }

// // If the peer is already connected, skip
// if d.h.Network().Connectedness(pai.ID) == network.Connected {
// log.Debugf("Peer %s is already connected", id)
// return nil
// }

// p, err := peer.GetOrCreateFromAddrInfo(&pai)
// if err != nil {
// return err
// }

// if len(pai.Addrs) > 0 {
// log.Debugf("Peer %s discovered with addresses, attempting to connect", id)
// err = d.PeerConnectAndUpdateIfSuccessful(ctx, p)
// if err != nil {
// log.Debugf("Failed to connect to newly discovered peer %s: %v", id, err)
// return err
// }
// }

// log.Infof("Successfully discovered peer %s", id)
// return nil
// }
10 changes: 9 additions & 1 deletion p2p/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/bahner/go-ma"
"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/p2p/dht"
"github.com/bahner/go-ma-actor/p2p/mdns"
log "github.com/sirupsen/logrus"
)
Expand All @@ -27,6 +28,10 @@ func (p *P2P) DiscoverPeers() error {
// Start MDNS discovery in a new goroutine
go func() {
m, err := mdns.New(p.DHT.Host(), ma.RENDEZVOUS)
if err == mdns.ErrNoProtectedPeersFound {

Check failure on line 31 in p2p/discovery.go

View workflow job for this annotation

GitHub Actions / build

undefined: mdns.ErrNoProtectedPeersFound
log.Warnf("No protected peers found")
return
}
if err != nil {
log.Errorf("Failed to start MDNS discovery: %s", err)
return
Expand All @@ -36,8 +41,11 @@ func (p *P2P) DiscoverPeers() error {

// Wait for a discovery process to complete
err := p.DHT.DiscoverPeers(ctx)
if err != dht.ErrNoProtectedPeersFound {
return fmt.Errorf("no new peers found %w", err)
}
if err != nil {
return fmt.Errorf("failed to initialise DHT. Peer discovery unsuccessful: %w", err)
return fmt.Errorf("peer discovery unsuccessful: %w", err)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion ui/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,10 @@ func (ui *ChatUI) handleEntityConnectCommand(args []string) {
ui.displaySystemMessage("Error: " + err.Error())
return
}
log.Debugf("Connecting to peer for entity: %v", e.DID.Id)
pai, err := e.ConnectPeer()
if err != nil {
ui.displaySystemMessage("Error connecting to enityty peer: " + err.Error())
ui.displaySystemMessage("Error connecting to entity peer: " + err.Error())
return
}
ui.displaySystemMessage("Connected to " + e.DID.Id + ": " + pai.ID.String())
Expand Down
2 changes: 1 addition & 1 deletion ui/me.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (ui *ChatUI) handleMeWhereCommand(args []string) {
func (ui *ChatUI) handleMeNickCommand(args []string) {

if len(args) == 2 {
ui.displaySystemMessage(ui.e.Nick)
ui.displaySystemMessage(ui.a.Entity.Nick)
} else {
ui.handleHelpCommand(meUsage, meHelp)
}
Expand Down

0 comments on commit c7021f4

Please sign in to comment.