Skip to content

Agent Mesh

add reviews

Antonio Ojea35d8b1fe4235
diff --git a/cmd/sam-node/mcp.go b/cmd/sam-node/mcp.go
index 954367c4..85cac7ec 100644
--- a/cmd/sam-node/mcp.go
+++ b/cmd/sam-node/mcp.go
@@ -342,15 +342,6 @@ func (n *SamNode) fetchRemoteServiceCatalog(ctx context.Context, peerID peer.ID,
return services, nil
}
-func hasCircuit(addr multiaddr.Multiaddr) bool {
- for _, proto := range addr.Protocols() {
- if proto.Code == multiaddr.P_CIRCUIT {
- return true
- }
- }
- return false
-}
-
// preparePeerAddrs scans the target peer's addresses, filters out unroutable private IPs,
// and ensures relay circuits are available. This prevents dial backoff errors when the
// relay is behind a load-balanced DNS address or when pods advertise internal IPs.
@@ -392,12 +383,14 @@ func (n *SamNode) preparePeerAddrs(ctx context.Context, targetPeer peer.ID) {
seen := make(map[string]struct{})
changed := false
- addUnique := func(ma multiaddr.Multiaddr) {
+ addUnique := func(ma multiaddr.Multiaddr) bool {
str := ma.String()
if _, ok := seen[str]; !ok {
seen[str] = struct{}{}
validAddrs = append(validAddrs, ma)
+ return true
}
+ return false
}
for _, ma := range addrs {
@@ -438,16 +431,7 @@ func (n *SamNode) preparePeerAddrs(ctx context.Context, targetPeer peer.ID) {
circuitAddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s/p2p-circuit", relayID.String()))
if err == nil {
- // Check if we already have this address
- hasIt := false
- for _, existing := range validAddrs {
- if existing.Equal(circuitAddr) {
- hasIt = true
- break
- }
- }
- if !hasIt {
- addUnique(circuitAddr)
+ if addUnique(circuitAddr) {
changed = true
}
}
diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.go
index 8bbf43c3..0defd137 100644
--- a/cmd/sam-node/node.go
+++ b/cmd/sam-node/node.go
@@ -300,6 +300,19 @@ func NewSamNode(ctx context.Context, cfg SamNodeConfig) (*SamNode, error) {
}
node.Host = h
+ h.Network().Notify(&network.NotifyBundle{
+ ConnectedF: func(n network.Network, c network.Conn) {
+ remotePeer := c.RemotePeer()
+ remoteAddr := c.RemoteMultiaddr()
+ if hasCircuit(remoteAddr) || !isPrivateIP(remoteAddr) {
+ if err := h.Peerstore().Put(remotePeer, PeerstoreKeyPrivateIPFailed, true); err != nil {
+ logger.Errorf("[Discovery] Failed to put peerstore private IP failed key: %v", err)
+ }
+ logger.Debugf("[Discovery] Peer %s connected via relay/public IP (%s), marking private IP as failed", remotePeer, remoteAddr)
+ }
+ },
+ })
+
// Permanently add the static relay address to the peerstore so we can build relay paths later
for _, pi := range staticRelays {
h.Peerstore().AddAddrs(pi.ID, pi.Addrs, peerstore.PermanentAddrTTL)
@@ -1420,3 +1433,12 @@ func isPrivateIP(addr multiaddr.Multiaddr) bool {
}
return false
}
+
+func hasCircuit(addr multiaddr.Multiaddr) bool {
+ for _, proto := range addr.Protocols() {
+ if proto.Code == multiaddr.P_CIRCUIT {
+ return true
+ }
+ }
+ return false
+}
diff --git a/cmd/sam-node/sidecar.go b/cmd/sam-node/sidecar.go
index 832cf282..dfabc655 100644
--- a/cmd/sam-node/sidecar.go
+++ b/cmd/sam-node/sidecar.go
@@ -30,6 +30,7 @@ import (
"github.com/google/sam/api"
libp2phttp "github.com/libp2p/go-libp2p-http"
+ "github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"google.golang.org/protobuf/encoding/protojson"
)
@@ -348,6 +349,10 @@ func createEgressProxy(node *SamNode) http.Handler {
proxy := &httputil.ReverseProxy{
Director: func(req *http.Request) {
+ ctx := req.Context()
+ ctx = network.WithAllowLimitedConn(ctx, "egress-proxy")
+ *req = *req.WithContext(ctx)
+
parts := strings.SplitN(req.URL.Path, "/", 6)
if len(parts) < 5 {
return
@@ -355,7 +360,9 @@ func createEgressProxy(node *SamNode) http.Handler {
peerID := parts[2]
pid, err := peer.Decode(peerID)
if err == nil {
- node.preparePeerAddrs(req.Context(), pid)
+ if cond := node.Host.Network().Connectedness(pid); cond != network.Connected && cond != network.Limited {
+ node.preparePeerAddrs(ctx, pid)
+ }
}
serviceType := parts[3]
serviceName := parts[4]