Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
resolve addresses
Antonio Ojeae9f619f067d3
diff --git a/cmd/sam-node/mcp.go b/cmd/sam-node/mcp.goindex 75600f91..32855e88 100644--- a/cmd/sam-node/mcp.go+++ b/cmd/sam-node/mcp.go@@ -274,25 +274,37 @@ func (n *SamNode) resolveRelayAddresses(ctx context.Context, targetPeer peer.ID)addrs := n.Host.Peerstore().Addrs(targetPeer)for _, ma := range addrs {parts := multiaddr.Split(ma)- if len(parts) >= 2 && parts[len(parts)-1].Protocol().Code == multiaddr.P_CIRCUIT {- p2pPart := parts[len(parts)-2]- if p2pPart.Protocol().Code == multiaddr.P_P2P {- relayID, err := peer.Decode(p2pPart.Value())- if err == nil {- // Found a relay. Let's find its direct IPs via DHT.- pi, err := n.DHT.FindPeer(ctx, relayID)- if err == nil && len(pi.Addrs) > 0 {- for _, relayAddr := range pi.Addrs {- // Check if it's a direct IP (not dns)- code := relayAddr.Protocols()[0].Code- if code == multiaddr.P_IP4 || code == multiaddr.P_IP6 {- circuitPart, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s/p2p-circuit", relayID.String()))- newMa := relayAddr.Encapsulate(circuitPart)- n.Host.Peerstore().AddAddr(targetPeer, newMa, peerstore.TempAddrTTL)- }- }- }- }+ if len(parts) < 2 {+ continue+ }++ if parts[len(parts)-1].Protocol().Code != multiaddr.P_CIRCUIT {+ continue+ }++ p2pPart := parts[len(parts)-2]+ if p2pPart.Protocol().Code != multiaddr.P_P2P {+ continue+ }++ relayID, err := peer.Decode(p2pPart.Value())+ if err != nil {+ continue+ }++ // Found a relay. Let's find its direct IPs via DHT.+ pi, err := n.DHT.FindPeer(ctx, relayID)+ if err != nil || len(pi.Addrs) == 0 {+ continue+ }++ for _, relayAddr := range pi.Addrs {+ // Check if it's a direct IP (not dns)+ code := relayAddr.Protocols()[0].Code+ if code == multiaddr.P_IP4 || code == multiaddr.P_IP6 {+ circuitPart, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s/p2p-circuit", relayID.String()))+ newMa := relayAddr.Encapsulate(circuitPart)+ n.Host.Peerstore().AddAddr(targetPeer, newMa, peerstore.TempAddrTTL)}}}diff --git a/cmd/sam-node/mcp_test.go b/cmd/sam-node/mcp_test.goindex 829530d6..3afd8434 100644--- a/cmd/sam-node/mcp_test.go+++ b/cmd/sam-node/mcp_test.go@@ -15,9 +15,16 @@package mainimport (+ "context"+ "fmt""net/http""net/http/httptest""testing"++ dht "github.com/libp2p/go-libp2p-kad-dht"+ "github.com/libp2p/go-libp2p/core/peerstore"+ mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"+ "github.com/multiformats/go-multiaddr")func TestMCPHandler_HTTP(t *testing.T) {@@ -62,3 +69,81 @@ func TestMCPHandler_HTTP(t *testing.T) {t.Errorf("Expected status OK or BadRequest on /mcp/events, got %d", resp2.StatusCode)}}++func TestResolveRelayAddresses(t *testing.T) {+ ctx := context.Background()+ mn := mocknet.New()++ // Create local host+ localHost, err := mn.GenPeer()+ if err != nil {+ t.Fatal(err)+ }++ // Create a real DHT bound to the mock host+ kdht, err := dht.New(ctx, localHost, dht.Mode(dht.ModeServer))+ if err != nil {+ t.Fatal(err)+ }++ node := &SamNode{+ Host: localHost,+ DHT: kdht,+ }++ // Create relay host+ relayHost, err := mn.GenPeer()+ if err != nil {+ t.Fatal(err)+ }+ relayID := relayHost.ID()++ // Add relay direct IP to DHT peerstore+ relayDirectAddr, _ := multiaddr.NewMultiaddr("/ip4/10.0.0.1/tcp/4501")+ localHost.Peerstore().AddAddr(relayID, relayDirectAddr, peerstore.PermanentAddrTTL)+ relayHost.Peerstore().AddAddr(relayID, relayDirectAddr, peerstore.PermanentAddrTTL)++ // Create DHT for relay+ relayDHT, err := dht.New(ctx, relayHost, dht.Mode(dht.ModeServer))+ if err != nil {+ t.Fatal(err)+ }++ mn.LinkAll()+ mn.ConnectAllButSelf()++ // Wait for DHT+ kdht.RoutingTable().TryAddPeer(relayID, true, true)+ relayDHT.RoutingTable().TryAddPeer(localHost.ID(), true, true)++ // Create a target node behind the relay+ targetHost, err := mn.GenPeer()+ if err != nil {+ t.Fatal(err)+ }+ targetID := targetHost.ID()++ // Add a dns-based circuit address for the target node to the local host's peerstore+ circuitAddrStr := fmt.Sprintf("/dns4/hub.com/tcp/4501/p2p/%s/p2p-circuit", relayID.String())+ circuitAddr, _ := multiaddr.NewMultiaddr(circuitAddrStr)+ localHost.Peerstore().AddAddr(targetID, circuitAddr, peerstore.PermanentAddrTTL)++ // Run the function+ node.resolveRelayAddresses(ctx, targetID)++ // Verify that the direct IP circuit address was added to the target's peerstore+ addrs := localHost.Peerstore().Addrs(targetID)+ foundDirect := false+ expectedDirectAddrStr := fmt.Sprintf("/ip4/10.0.0.1/tcp/4501/p2p/%s/p2p-circuit", relayID.String())++ for _, addr := range addrs {+ if addr.String() == expectedDirectAddrStr {+ foundDirect = true+ break+ }+ }++ if !foundDirect {+ t.Errorf("Expected address %s not found in peerstore. Got: %v", expectedDirectAddrStr, addrs)+ }+}