Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
add dns resolver
Antonio Ojeabd74ed2007af
diff --git a/cmd/sam-hub/main.go b/cmd/sam-hub/main.goindex 6689def7..e7eb49b1 100644--- a/cmd/sam-hub/main.go+++ b/cmd/sam-hub/main.go@@ -619,14 +619,15 @@ func main() {_ = os.Setenv("GOLOG_LOG_FMT", "json")}golog.SetAllLoggers(golog.LevelInfo)- _ = golog.SetLogLevel("dht", "fatal")- _ = golog.SetLogLevel("dht/RtRefreshManager", "fatal")if logLevel != "" {lvl, err := golog.LevelFromString(logLevel)if err == nil {golog.SetAllLoggers(lvl)}}+ // Suppress noisy DHT logs+ _ = golog.SetLogLevel("dht", "fatal")+ _ = golog.SetLogLevel("dht/RtRefreshManager", "fatal")policyConfig, err := LoadPolicyConfig(policyFile)if err != nil {diff --git a/cmd/sam-node/main.go b/cmd/sam-node/main.goindex 8bcf4d3d..48e672d5 100644--- a/cmd/sam-node/main.go+++ b/cmd/sam-node/main.go@@ -95,14 +95,15 @@ func main() {ctx := cmd.Context()// Initialize logginggolog.SetAllLoggers(golog.LevelInfo)- _ = golog.SetLogLevel("dht", "fatal")- _ = golog.SetLogLevel("dht/RtRefreshManager", "fatal")if logLevelFlag != "" {lvl, err := golog.LevelFromString(logLevelFlag)if err == nil {golog.SetAllLoggers(lvl)}}+ // Suppress noisy DHT logs+ _ = golog.SetLogLevel("dht", "fatal")+ _ = golog.SetLogLevel("dht/RtRefreshManager", "fatal")store, err := NewStore(resolveDataDir())if err != nil {diff --git a/cmd/sam-node/mcp.go b/cmd/sam-node/mcp.goindex 04ac130c..75600f91 100644--- a/cmd/sam-node/mcp.go+++ b/cmd/sam-node/mcp.go@@ -23,8 +23,10 @@ import ("github.com/google/sam/api""github.com/libp2p/go-libp2p/core/peer"+ "github.com/libp2p/go-libp2p/core/peerstore""github.com/libp2p/go-msgio""github.com/modelcontextprotocol/go-sdk/mcp"+ "github.com/multiformats/go-multiaddr""google.golang.org/protobuf/proto")@@ -140,6 +142,10 @@ func (n *SamNode) CallMCPTool(ctx context.Context, targetPeer peer.ID, toolNamemaxRetries := 3backoff := 1 * time.Second+ // Attempt to resolve direct IPs for any relay nodes in the target's addresses.+ // This helps avoid dial backoff issues when using shared DNS addresses for hubs.+ n.resolveRelayAddresses(ctx, targetPeer)+for i := 0; i < maxRetries; i++ {res, err = n.callMCPToolOnce(ctx, targetPeer, toolName, params)if err == nil {@@ -184,6 +190,9 @@ func (n *SamNode) callMCPToolOnce(ctx context.Context, targetPeer peer.ID, toolN}authBytes, _ := proto.Marshal(&authFrame)+ // Handshake should be fast; set a hard deadline to prevent hanging+ s.SetDeadline(time.Now().Add(10 * time.Second))+// Write AuthFramelogger.Debugf("Writing auth frame to %s...\n", targetPeer)writer := msgio.NewVarintWriter(s)@@ -200,6 +209,9 @@ func (n *SamNode) callMCPToolOnce(ctx context.Context, targetPeer peer.ID, toolN}defer reader.ReleaseMsg(msg)+ // Clear deadline so the MCP SDK can use the stream normally without timing out+ s.SetDeadline(time.Time{})+var resp api.AuthResponseif err := proto.Unmarshal(msg, &resp); err != nil {return nil, fmt.Errorf("invalid auth response from %s: %w", targetPeer, err)@@ -254,3 +266,34 @@ func (n *SamNode) fetchRemoteServiceCatalog(ctx context.Context, peerID peer.ID,}return services, nil}++// resolveRelayAddresses scans the target peer's addresses for any p2p-circuit relays,+// looks up the relay's direct IPs via the DHT, and adds them to the target's peerstore.+// This prevents dial backoff errors when the relay is behind a load-balanced DNS address.+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)+ }+ }+ }+ }+ }+ }+ }+}