Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
router: single-port mode via an HTTP fallback on the WebSocket listener
Antonio Ojea0b0703fbce30
diff --git a/internal/router/config.go b/internal/router/config.goindex fc3de6b4..b74fbefc 100644--- a/internal/router/config.go+++ b/internal/router/config.go@@ -16,6 +16,7 @@ package routerimport ("fmt"+ "net/http""time""github.com/google/sam/api"@@ -44,6 +45,10 @@ type Options struct {HighWaterMark int// RequiredRole restricts enrollment and startup to only accept tokens containing this role.RequiredRole string+ // HTTPFallbackHandler, when set, serves ordinary (non-WebSocket-upgrade)+ // HTTP requests arriving on the router's WebSocket listen addrs, letting a+ // single port carry both libp2p and REST traffic (single-port mode).+ HTTPFallbackHandler http.Handler}// Default sets default values for options.diff --git a/internal/router/router.go b/internal/router/router.goindex d2a53be6..6a9e6522 100644--- a/internal/router/router.go+++ b/internal/router/router.go@@ -48,6 +48,11 @@ import ("github.com/libp2p/go-libp2p/p2p/net/connmgr""github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"+ libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"+ "github.com/libp2p/go-libp2p/p2p/transport/tcp"+ libp2pwebrtc "github.com/libp2p/go-libp2p/p2p/transport/webrtc"+ ws "github.com/libp2p/go-libp2p/p2p/transport/websocket"+ "github.com/libp2p/go-libp2p/p2p/transport/webtransport""github.com/libp2p/go-msgio""github.com/multiformats/go-multiaddr"madns "github.com/multiformats/go-multiaddr-dns"@@ -136,6 +141,24 @@ func NewRouter(ctx context.Context, config Options) (*Router, error) {}, nil}+// transportOptions mirrors libp2p.DefaultTransports, attaching the HTTP+// fallback handler to the WebSocket transport in single-port mode. The+// explicit list is required because combining DefaultTransports with an+// extra Transport(websocket.New, ...) registers the WS transport twice and+// fails host construction.+func (r *Router) transportOptions() libp2p.Option {+ if r.config.HTTPFallbackHandler == nil {+ return libp2p.DefaultTransports+ }+ return libp2p.ChainOptions(+ libp2p.Transport(tcp.NewTCPTransport),+ libp2p.Transport(libp2pquic.NewTransport),+ libp2p.Transport(ws.New, ws.WithHTTPHandler(r.config.HTTPFallbackHandler)),+ libp2p.Transport(libp2pwebtransport.New),+ libp2p.Transport(libp2pwebrtc.New),+ )+}+// Start performs enrollment, syncs keys, launches libp2p host, and starts tasks.func (r *Router) Start() error {// 1. Load or Generate persistent identity key@@ -179,7 +202,7 @@ func (r *Router) Start() error {p2pOpts := []libp2p.Option{libp2p.Identity(r.privKey),- libp2p.DefaultTransports,+ r.transportOptions(),libp2p.ListenAddrStrings(r.config.ListenAddrs...),libp2p.Security(libp2ptls.ID, libp2ptls.New),libp2p.ConnectionManager(cm),diff --git a/tests/integration/singleport_test.go b/tests/integration/singleport_test.gonew file mode 100644index 00000000..57b8bad5--- /dev/null+++ b/tests/integration/singleport_test.go@@ -0,0 +1,165 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package integration_test++import (+ "context"+ "crypto/sha256"+ "fmt"+ "net"+ "net/http"+ "path/filepath"+ "strconv"+ "testing"+ "time"++ "github.com/google/sam/api"+ "github.com/google/sam/internal/controlplane"+ "github.com/google/sam/internal/router"+ "github.com/google/sam/internal/storage"+ "github.com/libp2p/go-libp2p"+ "github.com/libp2p/go-libp2p/core/network"+ "github.com/libp2p/go-libp2p/core/peer"+ "github.com/multiformats/go-multiaddr"+)++// TestSinglePortWebSocketAndHTTP pins the single-binary (sam-one) topology:+// one TCP socket, owned by the router's WebSocket transport, carries both+// libp2p traffic (WebSocket upgrades) and plain HTTP requests (fallback+// handler), including control-plane routes registered on the shared mux. The+// router itself enrolls and leases against the control plane over loopback+// with a bootstrap token and no OIDC issuer configured.+func TestSinglePortWebSocketAndHTTP(t *testing.T) {+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)+ defer cancel()++ tmp := t.TempDir()+ store, err := storage.NewSQLStore("sqlite", filepath.Join(tmp, "cp.db"))+ if err != nil {+ t.Fatalf("failed to create store: %v", err)+ }+ t.Cleanup(func() { _ = store.Close() })++ // Bootstrap-token-only control plane: no OIDC issuer at all.+ cp, err := controlplane.NewServer(controlplane.Options{+ ListenAddr: "127.0.0.1:0",+ AutoApproveEnrollment: true,+ }, store)+ if err != nil {+ t.Fatalf("failed to create control plane: %v", err)+ }+ if err := cp.Start(); err != nil {+ t.Fatalf("failed to start control plane: %v", err)+ }+ t.Cleanup(func() { _ = cp.Close() })++ // Seed the router's bootstrap token directly in the store, the way a+ // single-binary distribution provisions its embedded router on first boot.+ const routerToken = "single-port-router-token"+ tokenID := fmt.Sprintf("%x", sha256.Sum256([]byte(routerToken)))+ now := time.Now()+ if err := store.SaveBootstrapToken(ctx, &storage.BootstrapToken{+ ID: tokenID,+ TokenHash: tokenID,+ Role: api.RoleRouter,+ MaxUsages: 1,+ CreatedAt: now,+ ExpiresAt: now.Add(time.Hour),+ }); err != nil {+ t.Fatalf("failed to seed router bootstrap token: %v", err)+ }++ // Shared mux served as the WS transport's HTTP fallback: control-plane+ // routes plus a probe endpoint.+ mux := http.NewServeMux()+ cp.RegisterRoutes(mux)+ mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {+ _, _ = w.Write([]byte("single-port ok"))+ })++ rtr, err := router.NewRouter(ctx, router.Options{+ ControlPlaneURL: "http://" + cp.Addr(),+ ListenAddrs: []string{"/ip4/127.0.0.1/tcp/0/ws"},+ AllowLoopback: true,+ KeysDBPath: filepath.Join(tmp, "router.key"),+ BootstrapToken: routerToken,+ HTTPFallbackHandler: mux,+ })+ if err != nil {+ t.Fatalf("failed to create router: %v", err)+ }+ if err := rtr.Start(); err != nil {+ t.Fatalf("failed to start router: %v", err)+ }+ t.Cleanup(func() { _ = rtr.Close() })++ // Locate the bound single port from the router's WS multiaddr.+ var wsAddr multiaddr.Multiaddr+ var port string+ for _, a := range rtr.Host.Addrs() {+ if _, err := a.ValueForProtocol(multiaddr.P_WS); err == nil {+ wsAddr = a+ if port, err = a.ValueForProtocol(multiaddr.P_TCP); err != nil {+ t.Fatalf("ws multiaddr %s has no tcp port: %v", a, err)+ }+ break+ }+ }+ if wsAddr == nil {+ t.Fatalf("router advertises no /ws listen addr, got %v", rtr.Host.Addrs())+ }++ // 1. Plain HTTP through the libp2p-owned socket reaches the fallback mux,+ // including the control-plane API.+ client := &http.Client{Timeout: 5 * time.Second}+ base := "http://127.0.0.1:" + port+ for _, path := range []string{"/hello", "/healthz", "/info"} {+ resp, err := client.Get(base + path)+ if err != nil {+ t.Fatalf("GET %s over the single port failed: %v", path, err)+ }+ if resp.StatusCode != http.StatusOK {+ t.Errorf("GET %s over the single port: status %s, want 200", path, resp.Status)+ }+ _ = resp.Body.Close()+ }++ // 2. A default-transports libp2p client (same dial path as sam-node)+ // connects over the very same port.+ dialer, err := libp2p.New(libp2p.NoListenAddrs)+ if err != nil {+ t.Fatalf("failed to create dialer host: %v", err)+ }+ t.Cleanup(func() { _ = dialer.Close() })++ if err := dialer.Connect(ctx, peer.AddrInfo{ID: rtr.Host.ID(), Addrs: []multiaddr.Multiaddr{wsAddr}}); err != nil {+ t.Fatalf("libp2p dial over the single port failed: %v", err)+ }+ if got := dialer.Network().Connectedness(rtr.Host.ID()); got != network.Connected {+ t.Fatalf("connectedness to router = %s, want Connected", got)+ }++ // 3. The router self-enrolled with the bootstrap token and leased over+ // loopback: /info must advertise it.+ _, cpPortStr, err := net.SplitHostPort(cp.Addr())+ if err != nil {+ t.Fatalf("failed to parse control plane addr %q: %v", cp.Addr(), err)+ }+ cpPort, err := strconv.Atoi(cpPortStr)+ if err != nil {+ t.Fatalf("failed to parse control plane port %q: %v", cpPortStr, err)+ }+ waitForActiveRouters(t, cpPort, 1, 10*time.Second)+}