Skip to content

Agent Mesh

node: reach a peer named by ID through the authenticated routers

Antonio Ojea1723054682f8
diff --git a/internal/node/mcp.go b/internal/node/mcp.go
index 61d57d58..de44fa25 100644
--- a/internal/node/mcp.go
+++ b/internal/node/mcp.go
@@ -571,6 +571,19 @@ func (n *SamNode) preparePeerAddrs(ctx context.Context, targetPeer peer.ID) {
}
}
+ // A peer nothing announced an address for is still reachable through the
+ // routers that admitted both of us: an SDK agent publishes no service and
+ // no record, and is named by peer ID alone. Every router relays for the
+ // peers it authenticated, so each is a path worth trying.
+ if len(validAddrs) == 0 {
+ for _, router := range n.authenticatedRouterIDs() {
+ circuitAddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s/p2p-circuit", router.String()))
+ if err == nil && addUnique(circuitAddr) {
+ changed = true
+ }
+ }
+ }
+
if changed && len(validAddrs) > 0 {
// Clear existing addresses to prevent libp2p from hanging on dead private IPs
n.Host.Peerstore().ClearAddrs(targetPeer)
diff --git a/internal/node/mcp_test.go b/internal/node/mcp_test.go
index b09878d0..41dfbf63 100644
--- a/internal/node/mcp_test.go
+++ b/internal/node/mcp_test.go
@@ -24,6 +24,7 @@ import (
"github.com/google/sam/api"
dht "github.com/libp2p/go-libp2p-kad-dht"
+ "github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/multiformats/go-multiaddr"
@@ -155,6 +156,56 @@ func TestResolveRelayAddresses(t *testing.T) {
}
}
+// A peer named by ID alone, with no address anywhere (an SDK agent publishes
+// no record), is given a circuit through every router that admitted this
+// node, and through nothing else.
+func TestPreparePeerAddrsFallsBackToAuthenticatedRouters(t *testing.T) {
+ ctx := context.Background()
+ mn := mocknet.New()
+ localHost, err := mn.GenPeer()
+ if err != nil {
+ t.Fatal(err)
+ }
+ kdht, err := dht.New(localHost, dht.Mode(dht.ModeClient))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer func() { _ = kdht.Close() }()
+ admitted, err := mn.GenPeer()
+ if err != nil {
+ t.Fatal(err)
+ }
+ stale, err := mn.GenPeer()
+ if err != nil {
+ t.Fatal(err)
+ }
+ _ = mn.LinkAll()
+ if _, err := mn.ConnectPeers(localHost.ID(), admitted.ID()); err != nil {
+ t.Fatal(err)
+ }
+ unknown, err := mn.GenPeer()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ node := &SamNode{
+ Host: localHost,
+ DHT: kdht,
+ BiscuitTimeout: 500 * time.Millisecond,
+ authenticatedRouters: map[peer.ID]bool{admitted.ID(): true, stale.ID(): true},
+ }
+ node.preparePeerAddrs(ctx, unknown.ID())
+
+ var got []string
+ for _, a := range localHost.Peerstore().Addrs(unknown.ID()) {
+ got = append(got, a.String())
+ }
+ want := []string{fmt.Sprintf("/p2p/%s/p2p-circuit", admitted.ID())}
+ if len(got) != 1 || got[0] != want[0] {
+ t.Fatalf("addresses for an unknown peer: got %v, want only a circuit through the connected router %v", got, want)
+ }
+}
+
func TestSplitToolName(t *testing.T) {
tests := []struct {
name string
diff --git a/internal/node/node.go b/internal/node/node.go
index af908022..07b77312 100644
--- a/internal/node/node.go
+++ b/internal/node/node.go
@@ -761,6 +761,20 @@ func (n *SamNode) isAuthenticatedAndConnected(router peer.ID) bool {
return authed && n.Host.Network().Connectedness(router) == network.Connected
}
+// authenticatedRouterIDs lists the routers this node passed the handshake
+// with and is still connected to: the relays that will open a circuit for it.
+func (n *SamNode) authenticatedRouterIDs() []peer.ID {
+ n.mu.Lock()
+ defer n.mu.Unlock()
+ ids := make([]peer.ID, 0, len(n.authenticatedRouters))
+ for pid := range n.authenticatedRouters {
+ if n.Host.Network().Connectedness(pid) == network.Connected {
+ ids = append(ids, pid)
+ }
+ }
+ return ids
+}
+
func (n *SamNode) LoadMeshConfig() ([]byte, []string, error) {
return n.Store.LoadMeshConfig()
}