Skip to content

Agent Mesh

add proxy command

Antonio Ojea100bf77090f2
diff --git a/cmd/sam/proxy.go b/cmd/sam/proxy.go
new file mode 100644
index 00000000..c3f1fdd2
--- /dev/null
+++ b/cmd/sam/proxy.go
@@ -0,0 +1,232 @@
+package main
+
+import (
+ "context"
+ "fmt"
+ "io"
+ "log/slog"
+ "math/big"
+ "net/http"
+ "strconv"
+ "strings"
+ "time"
+
+ "github.com/libp2p/go-libp2p/core/peer"
+ "github.com/spf13/cobra"
+
+ samnet "sam/pkg/net"
+ "sam/pkg/protocol"
+)
+
+func newProxyCmd(cfg *runConfig) *cobra.Command {
+ cmd := &cobra.Command{
+ Use: "proxy",
+ Short: "Start a local HTTP proxy tunnel over libp2p",
+ Long: `Listen on a local HTTP port and forward requests through SAM.
+
+The destination must be set on each request via X-SAM-Target:
+ - PeerID: routes directly to a specific peer
+ - Capability: discovers the closest provider via DHT and routes there`,
+ RunE: func(cmd *cobra.Command, _ []string) error {
+ return runProxy(cmd.Context(), cfg)
+ },
+ }
+
+ f := cmd.Flags()
+ f.IntVar(&cfg.proxyPort, "port", 0, "local HTTP listen port")
+ f.StringVar(&cfg.proxyTargetHdr, "target-header", "X-SAM-Target", "request header used to select peer-id or capability")
+ f.StringVar(&cfg.proxyBiscuit, "biscuit", "dev-biscuit", "biscuit token forwarded in tunnel metadata")
+ f.DurationVar(&cfg.proxyTimeout, "timeout", 30*time.Second, "per-request tunnel timeout")
+ _ = cmd.MarkFlagRequired("port")
+
+ return cmd
+}
+
+func runProxy(parent context.Context, cfg *runConfig) error {
+ if cfg.proxyPort <= 0 {
+ return fmt.Errorf("--port must be a positive integer")
+ }
+ if strings.TrimSpace(cfg.proxyTargetHdr) == "" {
+ cfg.proxyTargetHdr = "X-SAM-Target"
+ }
+ if cfg.proxyTimeout <= 0 {
+ cfg.proxyTimeout = 30 * time.Second
+ }
+
+ node, err := buildNode(cfg)
+ if err != nil {
+ return err
+ }
+ if err := node.Start(parent); err != nil {
+ return fmt.Errorf("starting node: %w", err)
+ }
+ defer func() { _ = node.Stop(context.Background()) }()
+
+ observer, err := protocol.NewBoltObserverForFederation(cfg.federation)
+ if err != nil {
+ return fmt.Errorf("creating reputation observer: %w", err)
+ }
+ defer func() { _ = observer.Close() }()
+
+ handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ start := time.Now()
+
+ vouch, vErr := loadLocalVouch()
+ if vErr != nil {
+ http.Error(w, "unauthorized: local identity login required", http.StatusUnauthorized)
+ return
+ }
+
+ targetArg := strings.TrimSpace(r.Header.Get(cfg.proxyTargetHdr))
+ if targetArg == "" {
+ http.Error(w, fmt.Sprintf("missing %s header", cfg.proxyTargetHdr), http.StatusBadRequest)
+ return
+ }
+
+ ctx, cancel := context.WithTimeout(r.Context(), cfg.proxyTimeout)
+ defer cancel()
+
+ target, capability, err := resolveProxyTarget(ctx, node, targetArg)
+ if err != nil {
+ http.Error(w, fmt.Sprintf("target resolution failed: %v", err), http.StatusBadGateway)
+ return
+ }
+
+ body, err := io.ReadAll(r.Body)
+ if err != nil {
+ observer.OnFailure(target.ID.String(), protocol.FailureTypeProtocol)
+ http.Error(w, fmt.Sprintf("reading request body: %v", err), http.StatusBadRequest)
+ return
+ }
+
+ requestHeaders := r.Header.Clone()
+ requestHeaders.Del(cfg.proxyTargetHdr)
+
+ tunnelReq := protocol.HTTPTunnelRequest{
+ Method: r.Method,
+ Path: r.URL.RequestURI(),
+ Headers: requestHeaders,
+ Body: body,
+ }
+
+ if cfg.debug {
+ slog.Default().Info("proxy hop", "path", "[Local HTTP] -> ["+target.ID.String()+"] -> [Remote Service]")
+ }
+
+ if len(target.Addrs) > 0 {
+ _ = node.Connect(ctx, target)
+ }
+
+ resp, err := protocol.TunnelHTTP(ctx, node.Host(), target.ID, protocol.HTTPTunnelOpenRequest{
+ Vouch: vouch,
+ Biscuit: strings.TrimSpace(cfg.proxyBiscuit),
+ Capability: capability,
+ Request: tunnelReq,
+ })
+ if err != nil {
+ observer.OnFailure(target.ID.String(), protocol.FailureTypeLiveness)
+ http.Error(w, fmt.Sprintf("tunnel request failed: %v", err), http.StatusBadGateway)
+ return
+ }
+ if resp.Error != "" {
+ observer.OnFailure(target.ID.String(), protocol.FailureTypeRemote)
+ status := resp.StatusCode
+ if status == 0 {
+ status = http.StatusBadGateway
+ }
+ http.Error(w, resp.Error, status)
+ return
+ }
+
+ for key, vals := range resp.Headers {
+ for _, val := range vals {
+ w.Header().Add(key, val)
+ }
+ }
+ status := resp.StatusCode
+ if status == 0 {
+ status = http.StatusOK
+ }
+ w.WriteHeader(status)
+ _, _ = w.Write(resp.Body)
+
+ observer.OnSuccess(target.ID.String(), time.Since(start))
+ })
+
+ addr := ":" + strconv.Itoa(cfg.proxyPort)
+ srv := &http.Server{Addr: addr, Handler: handler}
+ go func() {
+ <-parent.Done()
+ shutdownCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+ _ = srv.Shutdown(shutdownCtx)
+ }()
+
+ slog.Default().Info("SAM HTTP proxy is up", "peer_id", node.PeerID(), "listen", addr, "target_header", cfg.proxyTargetHdr)
+ if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
+ return fmt.Errorf("starting local proxy HTTP server: %w", err)
+ }
+ return nil
+}
+
+func resolveProxyTarget(ctx context.Context, node samnet.Node, targetArg string) (peer.AddrInfo, string, error) {
+ targetArg = strings.TrimSpace(targetArg)
+ if targetArg == "" {
+ return peer.AddrInfo{}, "", fmt.Errorf("target peer ID or capability is required")
+ }
+
+ if pid, err := peer.Decode(targetArg); err == nil {
+ return peer.AddrInfo{ID: pid, Addrs: node.Host().Peerstore().Addrs(pid)}, "", nil
+ }
+
+ svc, err := protocol.NewDiscoveryService(node)
+ if err != nil {
+ return peer.AddrInfo{}, "", fmt.Errorf("creating discovery service: %w", err)
+ }
+ peers, err := svc.DiscoverPeers(ctx, targetArg)
+ if err != nil {
+ return peer.AddrInfo{}, "", fmt.Errorf("discovering capability %q: %w", targetArg, err)
+ }
+ if len(peers) == 0 {
+ return peer.AddrInfo{}, "", fmt.Errorf("no peers found for capability %q", targetArg)
+ }
+
+ closest := closestPeer(node.PeerID(), peers)
+ return closest, targetArg, nil
+}
+
+func closestPeer(local peer.ID, peers []peer.AddrInfo) peer.AddrInfo {
+ if len(peers) == 1 {
+ return peers[0]
+ }
+ localBytes := []byte(local)
+ best := peers[0]
+ bestDistance := xorDistance(localBytes, []byte(best.ID))
+ for i := 1; i < len(peers); i++ {
+ d := xorDistance(localBytes, []byte(peers[i].ID))
+ if d.Cmp(bestDistance) < 0 {
+ best = peers[i]
+ bestDistance = d
+ }
+ }
+ return best
+}
+
+func xorDistance(a, b []byte) *big.Int {
+ max := len(a)
+ if len(b) > max {
+ max = len(b)
+ }
+ buf := make([]byte, max)
+ for i := 0; i < max; i++ {
+ var av, bv byte
+ if i < len(a) {
+ av = a[i]
+ }
+ if i < len(b) {
+ bv = b[i]
+ }
+ buf[i] = av ^ bv
+ }
+ return new(big.Int).SetBytes(buf)
+}
diff --git a/cmd/sam/root.go b/cmd/sam/root.go
index 1b2c2753..d2e89b79 100644
--- a/cmd/sam/root.go
+++ b/cmd/sam/root.go
@@ -23,7 +23,8 @@ type runConfig struct {
debug bool
// sam up
- issuerName string
+ issuerName string
+ tunnelHTTPEndpoint string
// sam publish (shared by card and mcp subcommands)
capabilities []string
@@ -51,6 +52,12 @@ type runConfig struct {
callNonce string
callTimeout time.Duration
+ // sam proxy
+ proxyPort int
+ proxyTargetHdr string
+ proxyBiscuit string
+ proxyTimeout time.Duration
+
// federation storage context
federation string
@@ -85,6 +92,7 @@ func newRootCmd(cfg *runConfig) *cobra.Command {
cmd.AddCommand(newPublishCmd(cfg))
cmd.AddCommand(newMeshCmd(cfg))
cmd.AddCommand(newCallCmd(cfg))
+ cmd.AddCommand(newProxyCmd(cfg))
cmd.AddCommand(newIdentityCmd(cfg))
cmd.AddCommand(newInspectCmd(cfg))
return cmd
diff --git a/cmd/sam/up.go b/cmd/sam/up.go
index b03a8abd..de460161 100644
--- a/cmd/sam/up.go
+++ b/cmd/sam/up.go
@@ -11,7 +11,9 @@ import (
"github.com/spf13/cobra"
+ "sam/pkg/economy"
"sam/pkg/identity"
+ "sam/pkg/protocol"
)
func newUpCmd(cfg *runConfig) *cobra.Command {
@@ -23,6 +25,7 @@ func newUpCmd(cfg *runConfig) *cobra.Command {
},
}
cmd.Flags().StringVar(&cfg.issuerName, "issuer", "sam.local", "identity issuer name for voucher bootstrap")
+ cmd.Flags().StringVar(&cfg.tunnelHTTPEndpoint, "tunnel-http-endpoint", "", "optional local HTTP endpoint exposed via /sam/tunnel/http/1.0 (for example http://127.0.0.1:11434)")
return cmd
}
@@ -53,6 +56,20 @@ func runUp(parent context.Context, cfg *runConfig) error {
return fmt.Errorf("initializing verifier: %w", err)
}
+ var tunnel *protocol.HTTPTunnelService
+ if cfg.tunnelHTTPEndpoint != "" {
+ tunnel, err = protocol.NewHTTPTunnelService(
+ node.Host(),
+ cfg.tunnelHTTPEndpoint,
+ protocol.WithHTTPTunnelSkillGate(economy.NewBiscuitSkillGate(nil)),
+ )
+ if err != nil {
+ return fmt.Errorf("initializing HTTP tunnel listener: %w", err)
+ }
+ defer tunnel.Close()
+ log.Info("HTTP tunnel listener enabled", "protocol", protocol.HTTPTunnelProtocolID, "endpoint", cfg.tunnelHTTPEndpoint)
+ }
+
addrs := make([]string, 0, len(node.Addrs()))
for _, a := range node.Addrs() {
addrs = append(addrs, a.String())
diff --git a/pkg/protocol/http_tunnel.go b/pkg/protocol/http_tunnel.go
new file mode 100644
index 00000000..70851b5c
--- /dev/null
+++ b/pkg/protocol/http_tunnel.go
@@ -0,0 +1,221 @@
+package protocol
+
+import (
+ "bufio"
+ "context"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "net/url"
+ "strings"
+ "time"
+
+ "github.com/libp2p/go-libp2p/core/host"
+ "github.com/libp2p/go-libp2p/core/network"
+ "github.com/libp2p/go-libp2p/core/peer"
+ coreprotocol "github.com/libp2p/go-libp2p/core/protocol"
+
+ "sam/pkg/economy"
+ "sam/pkg/identity"
+)
+
+const HTTPTunnelProtocolID coreprotocol.ID = "/sam/tunnel/http/1.0"
+
+type HTTPTunnelOpenRequest struct {
+ Vouch *identity.Vouch `json:"vouch,omitempty"`
+ Biscuit string `json:"biscuit"`
+ Capability string `json:"capability,omitempty"`
+ Request HTTPTunnelRequest `json:"request"`
+}
+
+type HTTPTunnelRequest struct {
+ Method string `json:"method"`
+ Path string `json:"path"`
+ Headers map[string][]string `json:"headers,omitempty"`
+ Body []byte `json:"body,omitempty"`
+}
+
+type HTTPTunnelResponse struct {
+ StatusCode int `json:"status_code"`
+ Headers map[string][]string `json:"headers,omitempty"`
+ Body []byte `json:"body,omitempty"`
+ Error string `json:"error,omitempty"`
+}
+
+type HTTPTunnelService struct {
+ host host.Host
+ endpoint *url.URL
+ client *http.Client
+ skillGate *economy.BiscuitSkillGate
+}
+
+type HTTPTunnelServiceOption func(*HTTPTunnelService)
+
+func WithHTTPTunnelSkillGate(g *economy.BiscuitSkillGate) HTTPTunnelServiceOption {
+ return func(s *HTTPTunnelService) { s.skillGate = g }
+}
+
+func NewHTTPTunnelService(h host.Host, endpoint string, opts ...HTTPTunnelServiceOption) (*HTTPTunnelService, error) {
+ if h == nil {
+ return nil, fmt.Errorf("host is nil")
+ }
+ e := strings.TrimSpace(endpoint)
+ if e == "" {
+ return nil, fmt.Errorf("endpoint is required")
+ }
+ u, err := url.Parse(e)
+ if err != nil {
+ return nil, fmt.Errorf("parsing endpoint: %w", err)
+ }
+ if u.Scheme == "" || u.Host == "" {
+ return nil, fmt.Errorf("endpoint must include scheme and host")
+ }
+
+ s := &HTTPTunnelService{
+ host: h,
+ endpoint: u,
+ client: &http.Client{Timeout: 60 * time.Second},
+ skillGate: economy.NewBiscuitSkillGate(nil),
+ }
+ for _, opt := range opts {
+ opt(s)
+ }
+
+ h.SetStreamHandler(HTTPTunnelProtocolID, s.handleStream)
+ return s, nil
+}
+
+func (s *HTTPTunnelService) Close() {
+ if s == nil || s.host == nil {
+ return
+ }
+ s.host.RemoveStreamHandler(HTTPTunnelProtocolID)
+}
+
+func TunnelHTTP(ctx context.Context, h host.Host, target peer.ID, req HTTPTunnelOpenRequest) (*HTTPTunnelResponse, error) {
+ if h == nil {
+ return nil, fmt.Errorf("host is nil")
+ }
+ if target == "" {
+ return nil, fmt.Errorf("target peer id is required")
+ }
+ if strings.TrimSpace(req.Biscuit) == "" {
+ return nil, fmt.Errorf("biscuit is required")
+ }
+ if req.Vouch == nil {
+ return nil, fmt.Errorf("vouch is required")
+ }
+ if strings.TrimSpace(req.Request.Method) == "" {
+ return nil, fmt.Errorf("request method is required")
+ }
+
+ stream, err := h.NewStream(ctx, target, HTTPTunnelProtocolID)
+ if err != nil {
+ return nil, fmt.Errorf("opening HTTP tunnel stream: %w", err)
+ }
+ defer func() { _ = stream.Close() }()
+
+ if deadline, ok := ctx.Deadline(); ok {
+ _ = stream.SetDeadline(deadline)
+ }
+
+ w := bufio.NewWriter(stream)
+ if err := json.NewEncoder(w).Encode(req); err != nil {
+ return nil, fmt.Errorf("writing tunnel preface: %w", err)
+ }
+ if err := w.Flush(); err != nil {
+ return nil, fmt.Errorf("flushing tunnel preface: %w", err)
+ }
+
+ var resp HTTPTunnelResponse
+ if err := json.NewDecoder(stream).Decode(&resp); err != nil {
+ return nil, fmt.Errorf("reading tunnel response: %w", err)
+ }
+ return &resp, nil
+}
+
+func (s *HTTPTunnelService) handleStream(stream network.Stream) {
+ defer func() { _ = stream.Close() }()
+
+ var openReq HTTPTunnelOpenRequest
+ if err := json.NewDecoder(stream).Decode(&openReq); err != nil {
+ _ = json.NewEncoder(stream).Encode(HTTPTunnelResponse{StatusCode: http.StatusBadRequest, Error: fmt.Sprintf("invalid preface: %v", err)})
+ return
+ }
+
+ remotePeer := stream.Conn().RemotePeer().String()
+ if err := s.verifyMetadata(context.Background(), remotePeer, &openReq); err != nil {
+ _ = json.NewEncoder(stream).Encode(HTTPTunnelResponse{StatusCode: http.StatusForbidden, Error: err.Error()})
+ return
+ }
+
+ resp := s.forwardLocal(context.Background(), openReq.Request)
+ _ = json.NewEncoder(stream).Encode(resp)
+}
+
+func (s *HTTPTunnelService) verifyMetadata(ctx context.Context, remotePeer string, req *HTTPTunnelOpenRequest) error {
+ if req.Vouch == nil {
+ return fmt.Errorf("missing vouch metadata")
+ }
+ if req.Vouch.IsExpired() {
+ return fmt.Errorf("vouch is expired")
+ }
+ if strings.TrimSpace(req.Vouch.PeerID) != remotePeer {
+ return fmt.Errorf("vouch peer mismatch")
+ }
+ if strings.TrimSpace(req.Biscuit) == "" {
+ return fmt.Errorf("missing biscuit metadata")
+ }
+ if strings.TrimSpace(req.Capability) != "" {
+ if err := s.skillGate.CheckSkill(ctx, req.Biscuit, strings.TrimSpace(req.Capability)); err != nil {
+ return fmt.Errorf("biscuit denied capability: %w", err)
+ }
+ }
+ return nil
+}
+
+func (s *HTTPTunnelService) forwardLocal(ctx context.Context, treq HTTPTunnelRequest) HTTPTunnelResponse {
+ method := strings.TrimSpace(treq.Method)
+ if method == "" {
+ method = http.MethodGet
+ }
+
+ targetURL := *s.endpoint
+ if treq.Path != "" {
+ rawPath := treq.Path
+ if !strings.HasPrefix(rawPath, "/") {
+ rawPath = "/" + rawPath
+ }
+ if ref, err := url.Parse(rawPath); err == nil {
+ targetURL = *s.endpoint.ResolveReference(ref)
+ }
+ }
+
+ req, err := http.NewRequestWithContext(ctx, method, targetURL.String(), strings.NewReader(string(treq.Body)))
+ if err != nil {
+ return HTTPTunnelResponse{StatusCode: http.StatusBadRequest, Error: fmt.Sprintf("building local request: %v", err)}
+ }
+ for key, vals := range treq.Headers {
+ for _, val := range vals {
+ req.Header.Add(key, val)
+ }
+ }
+
+ resp, err := s.client.Do(req)
+ if err != nil {
+ return HTTPTunnelResponse{StatusCode: http.StatusBadGateway, Error: fmt.Sprintf("calling local endpoint: %v", err)}
+ }
+ defer func() { _ = resp.Body.Close() }()
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return HTTPTunnelResponse{StatusCode: http.StatusBadGateway, Error: fmt.Sprintf("reading local response: %v", err)}
+ }
+
+ return HTTPTunnelResponse{
+ StatusCode: resp.StatusCode,
+ Headers: resp.Header.Clone(),
+ Body: body,
+ }
+}
diff --git a/tests/e2e/sam.bats b/tests/e2e/sam.bats
index 675a9247..7c3a5ef5 100644
--- a/tests/e2e/sam.bats
+++ b/tests/e2e/sam.bats
@@ -94,3 +94,33 @@ teardown() {
[[ "$status" -eq 0 ]]
[[ "$output" == *"test-peer"* ]] || [[ "$output" == *"Peer ID"* ]]
}
+
+@test "sam proxy --help returns success" {
+ run "$SAM_BINARY" proxy --help
+ [[ "$status" -eq 0 ]]
+ [[ "$output" == *"X-SAM-Target"* ]] || [[ "$output" == *"--port"* ]]
+}
+
+@test "sam proxy rejects requests without local identity login" {
+ proxy_port=18080
+ proxy_log="$TEST_TMPDIR/proxy.log"
+
+ "$SAM_BINARY" proxy --port "$proxy_port" --listen /ip4/127.0.0.1/udp/4212/quic-v1 --run-for 10s >"$proxy_log" 2>&1 &
+ proxy_pid=$!
+
+ status_code="000"
+ for _ in {1..40}; do
+ status_code="$(curl -s -o "$TEST_TMPDIR/proxy_body.txt" -w "%{http_code}" -H "X-SAM-Target: 12D3KooWJ5x6k6U4k6M8QfR5fE3iZ7W2V2QX2c9Lr4R8D8vH" "http://127.0.0.1:${proxy_port}/health" || true)"
+ if [[ "$status_code" != "000" ]]; then
+ break
+ fi
+ sleep 0.1
+ done
+
+ kill "$proxy_pid" >/dev/null 2>&1 || true
+ wait "$proxy_pid" >/dev/null 2>&1 || true
+
+ [[ "$status_code" == "401" ]]
+ run cat "$TEST_TMPDIR/proxy_body.txt"
+ [[ "$output" == *"unauthorized"* ]]
+}
diff --git a/tests/integration/proxy_test.go b/tests/integration/proxy_test.go
new file mode 100644
index 00000000..ca0fb904
--- /dev/null
+++ b/tests/integration/proxy_test.go
@@ -0,0 +1,404 @@
+package integration_test
+
+import (
+ "bytes"
+ "context"
+ "fmt"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "strings"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/libp2p/go-libp2p/core/peer"
+ "github.com/multiformats/go-multiaddr"
+ "go.etcd.io/bbolt"
+
+ internaldb "sam/internal/db"
+ "sam/internal/testutils"
+ "sam/pkg/economy"
+ "sam/pkg/identity"
+ samnet "sam/pkg/net"
+ "sam/pkg/protocol"
+)
+
+const proxyRecordVersion = 1
+
+func TestProxyTunnelLegacyFlowIntegration(t *testing.T) {
+ testutils.Run(t, func(t *testing.T) {
+ runProxyTunnelLegacyFlowIntegration(t)
+ })
+}
+
+func TestProxyTunnelUnauthorizedBeforeDial(t *testing.T) {
+ testutils.Run(t, func(t *testing.T) {
+ runProxyTunnelUnauthorizedBeforeDial(t)
+ })
+}
+
+func runProxyTunnelLegacyFlowIntegration(t *testing.T) {
+ t.Helper()
+
+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ defer cancel()
+
+ tmpHome := t.TempDir()
+ t.Setenv("HOME", tmpHome)
+ t.Setenv("XDG_CONFIG_HOME", filepath.Join(tmpHome, ".config"))
+
+ federation := "proxy-legacy"
+ observer, err := protocol.NewBoltObserverForFederation(federation)
+ if err != nil {
+ t.Fatalf("creating observer: %v", err)
+ }
+ defer func() { _ = observer.Close() }()
+
+ requestSeen := make(chan *http.Request, 1)
+ bodySeen := make(chan []byte, 1)
+ backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ body, _ := io.ReadAll(r.Body)
+ requestSeen <- r.Clone(r.Context())
+ bodySeen <- append([]byte(nil), body...)
+ w.Header().Set("Content-Type", "application/json")
+ w.Header().Set("X-Backend", "sam-provider")
+ w.WriteHeader(http.StatusCreated)
+ _, _ = w.Write([]byte(`{"status":"ok"}`))
+ }))
+ defer backend.Close()
+
+ providerNode, err := newProxyNode(0, nil)
+ if err != nil {
+ t.Fatalf("creating provider node: %v", err)
+ }
+ if err := providerNode.Start(ctx); err != nil {
+ t.Fatalf("starting provider node: %v", err)
+ }
+ defer func() { _ = providerNode.Stop(context.Background()) }()
+
+ tunnelSvc, err := protocol.NewHTTPTunnelService(
+ providerNode.Host(),
+ backend.URL,
+ protocol.WithHTTPTunnelSkillGate(economy.NewBiscuitSkillGate(nil)),
+ )
+ if err != nil {
+ t.Fatalf("creating tunnel service: %v", err)
+ }
+ defer tunnelSvc.Close()
+
+ consumerNode, err := newProxyNode(0, providerBootstrap(providerNode))
+ if err != nil {
+ t.Fatalf("creating consumer node: %v", err)
+ }
+ if err := consumerNode.Start(ctx); err != nil {
+ t.Fatalf("starting consumer node: %v", err)
+ }
+ defer func() { _ = consumerNode.Stop(context.Background()) }()
+
+ providerInfo := peer.AddrInfo{ID: providerNode.PeerID(), Addrs: providerNode.Host().Addrs()}
+ if err := connectProxyWithRetry(ctx, consumerNode, providerInfo); err != nil {
+ t.Fatalf("connecting consumer->provider: %v", err)
+ }
+
+ vouch := identity.NewVouch(consumerNode.PeerID().String(), "test-issuer", "test-subject", map[string]string{"email": "[email protected]"}, 5*time.Minute)
+ proxySrv := httptest.NewServer(proxyTunnelHandler(consumerNode, observer, "X-SAM-Target", "alice;allow_skill=risk-audit", vouch))
+ defer proxySrv.Close()
+
+ payload := []byte(`{"request":"through-proxy"}`)
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, proxySrv.URL+"/data.json?via=proxy", bytes.NewReader(payload))
+ if err != nil {
+ t.Fatalf("creating request: %v", err)
+ }
+ req.Header.Set("X-SAM-Target", providerNode.PeerID().String())
+ req.Header.Set("Authorization", "Bearer test-token")
+ req.Header.Set("User-Agent", "curl/8.5.0")
+ req.Header.Set("Content-Type", "application/json")
+ req.Header.Set("X-Trace-ID", "trace-123")
+
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ t.Fatalf("sending proxy request: %v", err)
+ }
+ defer func() { _ = resp.Body.Close() }()
+
+ respBody, err := io.ReadAll(resp.Body)
+ if err != nil {
+ t.Fatalf("reading proxy response: %v", err)
+ }
+ if resp.StatusCode != http.StatusCreated {
+ t.Fatalf("unexpected status code: got=%d body=%s", resp.StatusCode, string(respBody))
+ }
+ if got := resp.Header.Get("X-Backend"); got != "sam-provider" {
+ t.Fatalf("expected X-Backend header to be preserved, got=%q", got)
+ }
+ if !strings.Contains(string(respBody), `"status":"ok"`) {
+ t.Fatalf("unexpected response body: %s", string(respBody))
+ }
+
+ select {
+ case backendReq := <-requestSeen:
+ if backendReq.Method != http.MethodPost {
+ t.Fatalf("backend method mismatch: got=%s", backendReq.Method)
+ }
+ if backendReq.URL.RequestURI() != "/data.json?via=proxy" {
+ t.Fatalf("backend path mismatch: got=%s", backendReq.URL.RequestURI())
+ }
+ if got := backendReq.Header.Get("Authorization"); got != "Bearer test-token" {
+ t.Fatalf("Authorization header not preserved: got=%q", got)
+ }
+ if got := backendReq.Header.Get("User-Agent"); got != "curl/8.5.0" {
+ t.Fatalf("User-Agent header not preserved: got=%q", got)
+ }
+ if got := backendReq.Header.Get("X-Trace-ID"); got != "trace-123" {
+ t.Fatalf("X-Trace-ID header not preserved: got=%q", got)
+ }
+ case <-time.After(2 * time.Second):
+ t.Fatalf("backend did not receive request")
+ }
+
+ select {
+ case gotBody := <-bodySeen:
+ if string(gotBody) != string(payload) {
+ t.Fatalf("backend payload mismatch: got=%s", string(gotBody))
+ }
+ case <-time.After(2 * time.Second):
+ t.Fatalf("backend body not captured")
+ }
+
+ repDBPath := filepath.Join(tmpHome, ".config", "sam", "federations", federation+".db")
+ if err := observer.Close(); err != nil {
+ t.Fatalf("closing observer: %v", err)
+ }
+ successes, failures, err := readProxyReputationCounts(repDBPath)
+ if err != nil {
+ t.Fatalf("reading reputation db: %v", err)
+ }
+ if successes == 0 {
+ t.Fatalf("expected at least one success reputation record")
+ }
+ if failures != 0 {
+ t.Fatalf("unexpected failure reputation records: %d", failures)
+ }
+}
+
+func runProxyTunnelUnauthorizedBeforeDial(t *testing.T) {
+ t.Helper()
+
+ ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
+ defer cancel()
+
+ var backendHits atomic.Int64
+ backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ backendHits.Add(1)
+ w.WriteHeader(http.StatusOK)
+ _, _ = w.Write([]byte("unexpected"))
+ }))
+ defer backend.Close()
+
+ providerNode, err := newProxyNode(0, nil)
+ if err != nil {
+ t.Fatalf("creating provider node: %v", err)
+ }
+ if err := providerNode.Start(ctx); err != nil {
+ t.Fatalf("starting provider node: %v", err)
+ }
+ defer func() { _ = providerNode.Stop(context.Background()) }()
+
+ tunnelSvc, err := protocol.NewHTTPTunnelService(providerNode.Host(), backend.URL)
+ if err != nil {
+ t.Fatalf("creating tunnel service: %v", err)
+ }
+ defer tunnelSvc.Close()
+
+ consumerNode, err := newProxyNode(0, providerBootstrap(providerNode))
+ if err != nil {
+ t.Fatalf("creating consumer node: %v", err)
+ }
+ if err := consumerNode.Start(ctx); err != nil {
+ t.Fatalf("starting consumer node: %v", err)
+ }
+ defer func() { _ = consumerNode.Stop(context.Background()) }()
+
+ proxySrv := httptest.NewServer(proxyTunnelHandler(consumerNode, protocol.NopObserver{}, "X-SAM-Target", "", nil))
+ defer proxySrv.Close()
+
+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, proxySrv.URL+"/blocked", nil)
+ if err != nil {
+ t.Fatalf("creating request: %v", err)
+ }
+ req.Header.Set("X-SAM-Target", providerNode.PeerID().String())
+
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ t.Fatalf("sending request: %v", err)
+ }
+ defer func() { _ = resp.Body.Close() }()
+
+ if resp.StatusCode != http.StatusUnauthorized {
+ body, _ := io.ReadAll(resp.Body)
+ t.Fatalf("expected unauthorized, got=%d body=%s", resp.StatusCode, string(body))
+ }
+ if backendHits.Load() != 0 {
+ t.Fatalf("expected no backend hits before authorization, got=%d", backendHits.Load())
+ }
+}
+
+func proxyTunnelHandler(node samnet.Node, observer protocol.Observer, targetHeader, biscuit string, vouch *identity.Vouch) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if vouch == nil {
+ http.Error(w, "unauthorized: local identity login required", http.StatusUnauthorized)
+ return
+ }
+
+ targetArg := strings.TrimSpace(r.Header.Get(targetHeader))
+ if targetArg == "" {
+ http.Error(w, "missing target header", http.StatusBadRequest)
+ return
+ }
+ targetID, err := peer.Decode(targetArg)
+ if err != nil {
+ http.Error(w, fmt.Sprintf("invalid target: %v", err), http.StatusBadRequest)
+ return
+ }
+
+ body, err := io.ReadAll(r.Body)
+ if err != nil {
+ http.Error(w, fmt.Sprintf("reading body: %v", err), http.StatusBadRequest)
+ return
+ }
+
+ headers := r.Header.Clone()
+ headers.Del(targetHeader)
+
+ start := time.Now()
+ resp, err := protocol.TunnelHTTP(r.Context(), node.Host(), targetID, protocol.HTTPTunnelOpenRequest{
+ Vouch: vouch,
+ Biscuit: biscuit,
+ Capability: "risk-audit",
+ Request: protocol.HTTPTunnelRequest{
+ Method: r.Method,
+ Path: r.URL.RequestURI(),
+ Headers: headers,
+ Body: body,
+ },
+ })
+ if err != nil {
+ observer.OnFailure(targetID.String(), protocol.FailureTypeLiveness)
+ http.Error(w, fmt.Sprintf("tunnel request failed: %v", err), http.StatusBadGateway)
+ return
+ }
+ if resp.Error != "" {
+ observer.OnFailure(targetID.String(), protocol.FailureTypeRemote)
+ status := resp.StatusCode
+ if status == 0 {
+ status = http.StatusBadGateway
+ }
+ http.Error(w, resp.Error, status)
+ return
+ }
+
+ for key, vals := range resp.Headers {
+ for _, val := range vals {
+ w.Header().Add(key, val)
+ }
+ }
+ status := resp.StatusCode
+ if status == 0 {
+ status = http.StatusOK
+ }
+ w.WriteHeader(status)
+ _, _ = w.Write(resp.Body)
+
+ observer.OnSuccess(targetID.String(), time.Since(start))
+ })
+}
+
+func providerBootstrap(node samnet.Node) []multiaddr.Multiaddr {
+ addrs := node.Host().Addrs()
+ if len(addrs) == 0 {
+ return nil
+ }
+ return []multiaddr.Multiaddr{addrs[0].Encapsulate(multiaddr.StringCast("/p2p/" + node.PeerID().String()))}
+}
+
+func newProxyNode(port int, bootstrap []multiaddr.Multiaddr) (samnet.Node, error) {
+ listen, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/udp/%d/quic-v1", port))
+ if err != nil {
+ return nil, fmt.Errorf("building listen address: %w", err)
+ }
+ key, err := samnet.GenerateKey()
+ if err != nil {
+ return nil, fmt.Errorf("generating node key: %w", err)
+ }
+ return samnet.New(
+ samnet.WithPrivateKey(key),
+ samnet.WithListenAddrs(listen),
+ samnet.WithBootstrapPeers(bootstrap...),
+ samnet.WithDHTMode(samnet.DHTModeServer),
+ )
+}
+
+func connectProxyWithRetry(ctx context.Context, node samnet.Node, pi peer.AddrInfo) error {
+ ticker := time.NewTicker(250 * time.Millisecond)
+ defer ticker.Stop()
+ var last error
+ for {
+ if err := node.Connect(ctx, pi); err == nil {
+ return nil
+ } else {
+ last = err
+ }
+ select {
+ case <-ctx.Done():
+ return last
+ case <-ticker.C:
+ }
+ }
+}
+
+func readProxyReputationCounts(path string) (int, int, error) {
+ if _, err := os.Stat(path); err != nil {
+ return 0, 0, err
+ }
+ db, err := bbolt.Open(path, 0o600, &bbolt.Options{Timeout: 2 * time.Second})
+ if err != nil {
+ return 0, 0, err
+ }
+ defer func() { _ = db.Close() }()
+
+ type reputationRecord struct {
+ PeerID string `json:"peer_id"`
+ OK bool `json:"ok"`
+ LatencyMS int64 `json:"latency_ms,omitempty"`
+ ErrorType string `json:"error_type,omitempty"`
+ }
+
+ codec := internaldb.JSONCodec{}
+ successes := 0
+ failures := 0
+ err = db.View(func(tx *bbolt.Tx) error {
+ bucket := tx.Bucket([]byte(internaldb.BucketReputation))
+ if bucket == nil {
+ return fmt.Errorf("missing reputation bucket")
+ }
+ return bucket.ForEach(func(_ []byte, value []byte) error {
+ var rec reputationRecord
+ if err := codec.Unmarshal(value, proxyRecordVersion, &rec, nil); err != nil {
+ return err
+ }
+ if rec.OK {
+ successes++
+ } else {
+ failures++
+ }
+ return nil
+ })
+ })
+ if err != nil {
+ return 0, 0, err
+ }
+ return successes, failures, nil
+}