Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
sambox: add the SOCKS5 sandbox boundary server
Antonio Ojea6f6d98a3082c
diff --git a/internal/sambox/socks5.go b/internal/sambox/socks5.gonew file mode 100644index 00000000..e8506c50--- /dev/null+++ b/internal/sambox/socks5.go@@ -0,0 +1,392 @@+// 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 sambox++import (+ "context"+ "encoding/binary"+ "errors"+ "fmt"+ "io"+ "log"+ "net"+ "strconv"+ "sync"+ "time"+)++// SOCKS5 (RFC 1928) is the sandbox boundary protocol. It is chosen over HTTP+// proxying for one property above all: a request carries the destination+// *name*, so egress policy is decided on "api.github.com" rather than on an+// address that says nothing about who is being talked to. It is also+// protocol-agnostic, and its reply codes give a policy denial a first-class+// representation that the guest kernel turns into a clean connection refusal.+//+// Only CONNECT is implemented. BIND is meaningless here, and UDP ASSOCIATE is+// refused deliberately: leaving it out means the only way out of a sandbox is a+// named TCP flow, which is also the only shape the policy engine can reason+// about. Inbound traffic reaches an agent over its own reverse channel, not+// over BIND.++const (+ socks5Version = 0x05+ userAuthVersion = 0x01++ methodNoAuth = 0x00+ methodUserPass = 0x02+ methodNoAcceptable = 0xFF++ cmdConnect = 0x01++ atypIPv4 = 0x01+ atypDomain = 0x03+ atypIPv6 = 0x04+)++// SOCKS5 reply codes (RFC 1928 section 6).+const (+ replySuccess = 0x00+ replyGeneralFailure = 0x01+ replyNotAllowed = 0x02+ replyHostUnreachable = 0x04+ replyConnectionRefused = 0x05+ replyCommandNotSupported = 0x07+ replyAddrTypeNotSupported = 0x08+)++// handshakeTimeout bounds the negotiation only. Once a flow is established it+// may stream for as long as it likes, so no deadline survives into the proxy.+const handshakeTimeout = 10 * time.Second++// Errors a Dialer returns to select a SOCKS5 reply code. Anything else becomes+// a general failure, which is the right default: an unrecognised failure must+// not be reported to a sandbox as a precise diagnostic.+var (+ // ErrNotAllowed is a policy denial: the destination is not permitted.+ ErrNotAllowed = errors.New("connection not allowed by ruleset")++ // ErrHostUnreachable means the destination could not be resolved or routed.+ ErrHostUnreachable = errors.New("host unreachable")++ // ErrConnectionRefused means the destination actively refused the flow.+ ErrConnectionRefused = errors.New("connection refused")+)++// Destination is a requested target exactly as it arrived on the sandbox+// boundary. Name is a domain when the client sent one, which is the case for+// every flow that came through tun2socks with virtual DNS; a literal address+// arrives when the sandbox dialled an IP directly, and IsName says which.+type Destination struct {+ Name string+ Port uint16+ IsName bool+}++// Address renders the destination as a dial target.+func (d Destination) Address() string {+ return net.JoinHostPort(d.Name, strconv.Itoa(int(d.Port)))+}++func (d Destination) String() string { return d.Address() }++// Credentials are the RFC 1929 username and password. When one sam-box+// multiplexes several agents over a single socket, this is how a flow says+// which agent it belongs to; the password is never logged.+type Credentials struct {+ Username string+ Password string+}++// Dialer decides whether a requested destination may be reached and opens it.+// It is the single policy enforcement point on the sandbox boundary.+type Dialer interface {+ DialDestination(ctx context.Context, creds *Credentials, dst Destination) (net.Conn, error)+}++// DialerFunc adapts a function to Dialer.+type DialerFunc func(ctx context.Context, creds *Credentials, dst Destination) (net.Conn, error)++func (f DialerFunc) DialDestination(ctx context.Context, creds *Credentials, dst Destination) (net.Conn, error) {+ return f(ctx, creds, dst)+}++// SOCKS5Server serves the sandbox-facing side of the boundary.+type SOCKS5Server struct {+ // Dialer is required.+ Dialer Dialer++ // Authenticate, when set, makes RFC 1929 username/password the only+ // acceptable method: a client that offers no credentials is rejected rather+ // than silently downgraded to an anonymous flow.+ Authenticate func(Credentials) error+}++// Serve accepts connections until the listener fails or ctx is cancelled.+func (s *SOCKS5Server) Serve(ctx context.Context, l net.Listener) error {+ if s.Dialer == nil {+ return errors.New("sambox: SOCKS5Server requires a Dialer")+ }++ go func() {+ <-ctx.Done()+ _ = l.Close()+ }()++ var wg sync.WaitGroup+ defer wg.Wait()++ for {+ conn, err := l.Accept()+ if err != nil {+ if ctx.Err() != nil || errors.Is(err, net.ErrClosed) {+ return nil+ }+ return err+ }+ wg.Add(1)+ go func() {+ defer wg.Done()+ s.handle(ctx, conn)+ }()+ }+}++func (s *SOCKS5Server) handle(ctx context.Context, conn net.Conn) {+ defer func() { _ = conn.Close() }()++ if err := conn.SetDeadline(time.Now().Add(handshakeTimeout)); err != nil {+ return+ }++ creds, err := s.negotiate(conn)+ if err != nil {+ return+ }++ dst, err := readRequest(conn)+ if err != nil {+ var rerr *replyError+ if errors.As(err, &rerr) {+ _ = writeReply(conn, rerr.code)+ }+ return+ }++ // Negotiation is over. Dialling a mesh destination can involve discovery, so+ // it must not inherit the handshake deadline; bounding it is the Dialer's+ // job, through the context it is given.+ if err := conn.SetDeadline(time.Time{}); err != nil {+ return+ }++ upstream, err := s.Dialer.DialDestination(ctx, creds, dst)+ if err != nil {+ _ = writeReply(conn, replyCodeFor(err))+ return+ }+ defer func() { _ = upstream.Close() }()++ if err := writeReply(conn, replySuccess); err != nil {+ return+ }++ relay(conn, upstream)+}++// negotiate performs method selection and, when required, RFC 1929 auth.+func (s *SOCKS5Server) negotiate(conn net.Conn) (*Credentials, error) {+ header := make([]byte, 2)+ if _, err := io.ReadFull(conn, header); err != nil {+ return nil, err+ }+ if header[0] != socks5Version {+ return nil, fmt.Errorf("unsupported SOCKS version %d", header[0])+ }++ methods := make([]byte, int(header[1]))+ if _, err := io.ReadFull(conn, methods); err != nil {+ return nil, err+ }++ want := byte(methodNoAuth)+ if s.Authenticate != nil {+ want = methodUserPass+ }+ if !containsByte(methods, want) {+ _, _ = conn.Write([]byte{socks5Version, methodNoAcceptable})+ return nil, fmt.Errorf("client offered no acceptable authentication method")+ }+ if _, err := conn.Write([]byte{socks5Version, want}); err != nil {+ return nil, err+ }+ if s.Authenticate == nil {+ return nil, nil+ }++ creds, err := readUserPass(conn)+ if err != nil {+ return nil, err+ }+ if err := s.Authenticate(*creds); err != nil {+ _, _ = conn.Write([]byte{userAuthVersion, 0x01})+ log.Printf("sambox: SOCKS5 authentication rejected for user %q", creds.Username)+ return nil, err+ }+ if _, err := conn.Write([]byte{userAuthVersion, 0x00}); err != nil {+ return nil, err+ }+ return creds, nil+}++func readUserPass(conn net.Conn) (*Credentials, error) {+ header := make([]byte, 2)+ if _, err := io.ReadFull(conn, header); err != nil {+ return nil, err+ }+ if header[0] != userAuthVersion {+ return nil, fmt.Errorf("unsupported username/password auth version %d", header[0])+ }++ username := make([]byte, int(header[1]))+ if _, err := io.ReadFull(conn, username); err != nil {+ return nil, err+ }++ plen := make([]byte, 1)+ if _, err := io.ReadFull(conn, plen); err != nil {+ return nil, err+ }+ password := make([]byte, int(plen[0]))+ if _, err := io.ReadFull(conn, password); err != nil {+ return nil, err+ }++ return &Credentials{Username: string(username), Password: string(password)}, nil+}++// replyError carries the SOCKS5 code a malformed or unsupported request must be+// answered with.+type replyError struct {+ code byte+ err error+}++func (e *replyError) Error() string { return e.err.Error() }++func readRequest(conn net.Conn) (Destination, error) {+ header := make([]byte, 4)+ if _, err := io.ReadFull(conn, header); err != nil {+ return Destination{}, err+ }+ if header[0] != socks5Version {+ return Destination{}, fmt.Errorf("unsupported SOCKS version %d", header[0])+ }+ if header[1] != cmdConnect {+ return Destination{}, &replyError{replyCommandNotSupported, fmt.Errorf("unsupported command %d", header[1])}+ }++ var dst Destination+ switch header[3] {+ case atypDomain:+ length := make([]byte, 1)+ if _, err := io.ReadFull(conn, length); err != nil {+ return Destination{}, err+ }+ if length[0] == 0 {+ return Destination{}, &replyError{replyGeneralFailure, errors.New("empty domain name")}+ }+ name := make([]byte, int(length[0]))+ if _, err := io.ReadFull(conn, name); err != nil {+ return Destination{}, err+ }+ dst.Name = string(name)+ dst.IsName = true+ case atypIPv4, atypIPv6:+ size := net.IPv4len+ if header[3] == atypIPv6 {+ size = net.IPv6len+ }+ addr := make([]byte, size)+ if _, err := io.ReadFull(conn, addr); err != nil {+ return Destination{}, err+ }+ dst.Name = net.IP(addr).String()+ default:+ return Destination{}, &replyError{replyAddrTypeNotSupported, fmt.Errorf("unsupported address type %d", header[3])}+ }++ port := make([]byte, 2)+ if _, err := io.ReadFull(conn, port); err != nil {+ return Destination{}, err+ }+ dst.Port = binary.BigEndian.Uint16(port)++ return dst, nil+}++// writeReply answers with a bound address of 0.0.0.0:0: the client has no use+// for the real one, and it would leak the host's addressing into the sandbox.+func writeReply(conn net.Conn, code byte) error {+ _, err := conn.Write([]byte{socks5Version, code, 0x00, atypIPv4, 0, 0, 0, 0, 0, 0})+ return err+}++func replyCodeFor(err error) byte {+ switch {+ case errors.Is(err, ErrNotAllowed):+ return replyNotAllowed+ case errors.Is(err, ErrHostUnreachable):+ return replyHostUnreachable+ case errors.Is(err, ErrConnectionRefused):+ return replyConnectionRefused+ default:+ return replyGeneralFailure+ }+}++func relay(client, upstream net.Conn) {+ var wg sync.WaitGroup+ wg.Add(2)+ go func() {+ defer wg.Done()+ _, _ = io.Copy(upstream, client)+ closeWrite(upstream)+ }()+ go func() {+ defer wg.Done()+ _, _ = io.Copy(client, upstream)+ closeWrite(client)+ }()+ wg.Wait()+}++// closeWrite propagates a half-close so a peer waiting on EOF is not left+// hanging until a timeout.+func closeWrite(conn net.Conn) {+ if cw, ok := conn.(interface{ CloseWrite() error }); ok {+ _ = cw.CloseWrite()+ return+ }+ _ = conn.Close()+}++func containsByte(haystack []byte, needle byte) bool {+ for _, b := range haystack {+ if b == needle {+ return true+ }+ }+ return false+}diff --git a/internal/sambox/socks5_test.go b/internal/sambox/socks5_test.gonew file mode 100644index 00000000..f8abc66b--- /dev/null+++ b/internal/sambox/socks5_test.go@@ -0,0 +1,385 @@+// 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 sambox++import (+ "context"+ "errors"+ "io"+ "net"+ "os"+ "path/filepath"+ "testing"+ "time"++ "golang.org/x/net/proxy"+)++// startSOCKS5 serves s on a Unix socket, the transport the sandbox boundary+// actually uses, and returns its path.+func startSOCKS5(t *testing.T, s *SOCKS5Server) string {+ t.Helper()++ // Not t.TempDir(): test names make paths long enough to hit the ~108 byte+ // sockaddr_un limit.+ dir, err := os.MkdirTemp("", "sambox")+ if err != nil {+ t.Fatalf("MkdirTemp: %v", err)+ }+ t.Cleanup(func() { _ = os.RemoveAll(dir) })++ path := filepath.Join(dir, "agent.sock")+ l, err := net.Listen("unix", path)+ if err != nil {+ t.Fatalf("listen: %v", err)+ }++ ctx, cancel := context.WithCancel(context.Background())+ done := make(chan struct{})+ go func() {+ defer close(done)+ if err := s.Serve(ctx, l); err != nil {+ t.Errorf("Serve: %v", err)+ }+ }()+ t.Cleanup(func() {+ cancel()+ <-done+ })+ return path+}++// startEcho returns the address of a server that echoes what it is sent.+func startEcho(t *testing.T) string {+ t.Helper()+ l, err := net.Listen("tcp", "127.0.0.1:0")+ if err != nil {+ t.Fatalf("listen: %v", err)+ }+ t.Cleanup(func() { _ = l.Close() })++ go func() {+ for {+ conn, err := l.Accept()+ if err != nil {+ return+ }+ go func() {+ defer func() { _ = conn.Close() }()+ _, _ = io.Copy(conn, conn)+ }()+ }+ }()+ return l.Addr().String()+}++// dialRaw speaks the protocol by hand, for the cases a well-behaved client+// library will never produce.+func dialRaw(t *testing.T, path string) net.Conn {+ t.Helper()+ conn, err := net.Dial("unix", path)+ if err != nil {+ t.Fatalf("dial: %v", err)+ }+ t.Cleanup(func() { _ = conn.Close() })+ if err := conn.SetDeadline(time.Now().Add(5 * time.Second)); err != nil {+ t.Fatalf("SetDeadline: %v", err)+ }+ return conn+}++// greetNoAuth completes method selection and returns the connection ready for a+// request.+func greetNoAuth(t *testing.T, conn net.Conn) {+ t.Helper()+ if _, err := conn.Write([]byte{socks5Version, 1, methodNoAuth}); err != nil {+ t.Fatalf("write greeting: %v", err)+ }+ reply := make([]byte, 2)+ if _, err := io.ReadFull(conn, reply); err != nil {+ t.Fatalf("read method selection: %v", err)+ }+ if reply[0] != socks5Version || reply[1] != methodNoAuth {+ t.Fatalf("method selection = %v, want [5 0]", reply)+ }+}++func readReplyCode(t *testing.T, conn net.Conn) byte {+ t.Helper()+ reply := make([]byte, 10)+ if _, err := io.ReadFull(conn, reply); err != nil {+ t.Fatalf("read reply: %v", err)+ }+ if reply[0] != socks5Version {+ t.Fatalf("reply version = %d, want %d", reply[0], socks5Version)+ }+ return reply[1]+}++// TestConnectPreservesDestinationName is the property the whole boundary rests+// on: policy must see the name the agent asked for, never a resolved address.+func TestConnectPreservesDestinationName(t *testing.T) {+ echo := startEcho(t)++ seen := make(chan Destination, 1)+ path := startSOCKS5(t, &SOCKS5Server{+ Dialer: DialerFunc(func(ctx context.Context, creds *Credentials, dst Destination) (net.Conn, error) {+ seen <- dst+ return net.Dial("tcp", echo)+ }),+ })++ dialer, err := proxy.SOCKS5("unix", path, nil, proxy.Direct)+ if err != nil {+ t.Fatalf("proxy.SOCKS5: %v", err)+ }+ conn, err := dialer.Dial("tcp", "api.github.com:443")+ if err != nil {+ t.Fatalf("Dial: %v", err)+ }+ defer func() { _ = conn.Close() }()++ if _, err := conn.Write([]byte("ping")); err != nil {+ t.Fatalf("write: %v", err)+ }+ got := make([]byte, 4)+ if _, err := io.ReadFull(conn, got); err != nil {+ t.Fatalf("read: %v", err)+ }+ if string(got) != "ping" {+ t.Errorf("echo = %q, want %q", got, "ping")+ }++ dst := <-seen+ if !dst.IsName {+ t.Errorf("destination %+v was not reported as a name", dst)+ }+ if dst.Name != "api.github.com" || dst.Port != 443 {+ t.Errorf("destination = %s, want api.github.com:443", dst)+ }+}++func TestConnectDeniedByPolicy(t *testing.T) {+ path := startSOCKS5(t, &SOCKS5Server{+ Dialer: DialerFunc(func(ctx context.Context, creds *Credentials, dst Destination) (net.Conn, error) {+ return nil, ErrNotAllowed+ }),+ })++ conn := dialRaw(t, path)+ greetNoAuth(t, conn)+ writeConnectByName(t, conn, "evil.example", 80)++ if code := readReplyCode(t, conn); code != replyNotAllowed {+ t.Errorf("reply = %#x, want %#x (connection not allowed by ruleset)", code, replyNotAllowed)+ }+}++func TestUnsupportedCommandsAreRefused(t *testing.T) {+ path := startSOCKS5(t, &SOCKS5Server{+ Dialer: DialerFunc(func(ctx context.Context, creds *Credentials, dst Destination) (net.Conn, error) {+ t.Error("dialer must not be reached for an unsupported command")+ return nil, errors.New("unreachable")+ }),+ })++ // BIND and UDP ASSOCIATE. UDP is refused on purpose: the only way out of a+ // sandbox is a named TCP flow.+ for _, cmd := range []byte{0x02, 0x03} {+ conn := dialRaw(t, path)+ greetNoAuth(t, conn)+ if _, err := conn.Write([]byte{socks5Version, cmd, 0x00, atypIPv4, 127, 0, 0, 1, 0x00, 0x50}); err != nil {+ t.Fatalf("write request: %v", err)+ }+ if code := readReplyCode(t, conn); code != replyCommandNotSupported {+ t.Errorf("cmd %#x: reply = %#x, want %#x", cmd, code, replyCommandNotSupported)+ }+ }+}++func TestUnsupportedAddressTypeIsRefused(t *testing.T) {+ path := startSOCKS5(t, &SOCKS5Server{+ Dialer: DialerFunc(func(ctx context.Context, creds *Credentials, dst Destination) (net.Conn, error) {+ t.Error("dialer must not be reached for an unsupported address type")+ return nil, errors.New("unreachable")+ }),+ })++ conn := dialRaw(t, path)+ greetNoAuth(t, conn)+ if _, err := conn.Write([]byte{socks5Version, cmdConnect, 0x00, 0x02, 0x00, 0x50}); err != nil {+ t.Fatalf("write request: %v", err)+ }+ if code := readReplyCode(t, conn); code != replyAddrTypeNotSupported {+ t.Errorf("reply = %#x, want %#x", code, replyAddrTypeNotSupported)+ }+}++func TestConnectByIPLiteralIsNotReportedAsAName(t *testing.T) {+ echo := startEcho(t)++ seen := make(chan Destination, 1)+ path := startSOCKS5(t, &SOCKS5Server{+ Dialer: DialerFunc(func(ctx context.Context, creds *Credentials, dst Destination) (net.Conn, error) {+ seen <- dst+ return net.Dial("tcp", echo)+ }),+ })++ conn := dialRaw(t, path)+ greetNoAuth(t, conn)+ if _, err := conn.Write([]byte{socks5Version, cmdConnect, 0x00, atypIPv4, 192, 0, 2, 10, 0x01, 0xBB}); err != nil {+ t.Fatalf("write request: %v", err)+ }+ if code := readReplyCode(t, conn); code != replySuccess {+ t.Fatalf("reply = %#x, want success", code)+ }++ dst := <-seen+ if dst.IsName {+ t.Errorf("destination %+v was reported as a name", dst)+ }+ if dst.Name != "192.0.2.10" || dst.Port != 443 {+ t.Errorf("destination = %s, want 192.0.2.10:443", dst)+ }+}++// TestAuthenticationIsNotDowngraded pins that a server expecting credentials+// refuses an anonymous client instead of serving it unidentified.+func TestAuthenticationIsNotDowngraded(t *testing.T) {+ path := startSOCKS5(t, &SOCKS5Server{+ Dialer: DialerFunc(func(ctx context.Context, creds *Credentials, dst Destination) (net.Conn, error) {+ t.Error("dialer must not be reached for an unauthenticated client")+ return nil, errors.New("unreachable")+ }),+ Authenticate: func(Credentials) error { return nil },+ })++ conn := dialRaw(t, path)+ if _, err := conn.Write([]byte{socks5Version, 1, methodNoAuth}); err != nil {+ t.Fatalf("write greeting: %v", err)+ }+ reply := make([]byte, 2)+ if _, err := io.ReadFull(conn, reply); err != nil {+ t.Fatalf("read method selection: %v", err)+ }+ if reply[1] != methodNoAcceptable {+ t.Errorf("method selection = %#x, want %#x (no acceptable methods)", reply[1], methodNoAcceptable)+ }+}++func TestAuthenticatedFlowCarriesCredentials(t *testing.T) {+ echo := startEcho(t)++ seen := make(chan *Credentials, 1)+ path := startSOCKS5(t, &SOCKS5Server{+ Dialer: DialerFunc(func(ctx context.Context, creds *Credentials, dst Destination) (net.Conn, error) {+ seen <- creds+ return net.Dial("tcp", echo)+ }),+ Authenticate: func(c Credentials) error {+ if c.Username != "reviewer-7.prod.acme.example" {+ return errors.New("unknown agent")+ }+ return nil+ },+ })++ auth := &proxy.Auth{User: "reviewer-7.prod.acme.example", Password: "admission-token"}+ dialer, err := proxy.SOCKS5("unix", path, auth, proxy.Direct)+ if err != nil {+ t.Fatalf("proxy.SOCKS5: %v", err)+ }+ conn, err := dialer.Dial("tcp", "code-reviewer.mcp.sam.alt:80")+ if err != nil {+ t.Fatalf("Dial: %v", err)+ }+ defer func() { _ = conn.Close() }()++ creds := <-seen+ if creds == nil {+ t.Fatal("dialer received no credentials")+ }+ if creds.Username != "reviewer-7.prod.acme.example" || creds.Password != "admission-token" {+ t.Errorf("credentials = %+v, want the agent id and its admission token", creds)+ }+}++func TestRejectedCredentialsFailTheHandshake(t *testing.T) {+ path := startSOCKS5(t, &SOCKS5Server{+ Dialer: DialerFunc(func(ctx context.Context, creds *Credentials, dst Destination) (net.Conn, error) {+ t.Error("dialer must not be reached when authentication fails")+ return nil, errors.New("unreachable")+ }),+ Authenticate: func(Credentials) error { return errors.New("unknown agent") },+ })++ dialer, err := proxy.SOCKS5("unix", path, &proxy.Auth{User: "bar", Password: "nope"}, proxy.Direct)+ if err != nil {+ t.Fatalf("proxy.SOCKS5: %v", err)+ }+ if _, err := dialer.Dial("tcp", "api.github.com:443"); err == nil {+ t.Fatal("Dial succeeded with rejected credentials, want an error")+ }+}++func TestNonSOCKS5GreetingIsDropped(t *testing.T) {+ path := startSOCKS5(t, &SOCKS5Server{+ Dialer: DialerFunc(func(ctx context.Context, creds *Credentials, dst Destination) (net.Conn, error) {+ t.Error("dialer must not be reached for a non-SOCKS5 client")+ return nil, errors.New("unreachable")+ }),+ })++ conn := dialRaw(t, path)+ if _, err := conn.Write([]byte{0x04, 0x01}); err != nil {+ t.Fatalf("write greeting: %v", err)+ }+ if _, err := io.ReadFull(conn, make([]byte, 1)); !errors.Is(err, io.EOF) {+ t.Errorf("read after bad version = %v, want EOF", err)+ }+}++func TestReplyCodeFor(t *testing.T) {+ tests := []struct {+ name string+ err error+ want byte+ }{+ {"policy denial", ErrNotAllowed, replyNotAllowed},+ {"unreachable", ErrHostUnreachable, replyHostUnreachable},+ {"refused", ErrConnectionRefused, replyConnectionRefused},+ {"wrapped denial", errors.Join(errors.New("context"), ErrNotAllowed), replyNotAllowed},+ {"anything else stays generic", errors.New("boom"), replyGeneralFailure},+ }++ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ if got := replyCodeFor(tc.err); got != tc.want {+ t.Errorf("replyCodeFor(%v) = %#x, want %#x", tc.err, got, tc.want)+ }+ })+ }+}++func writeConnectByName(t *testing.T, conn net.Conn, name string, port uint16) {+ t.Helper()+ req := []byte{socks5Version, cmdConnect, 0x00, atypDomain, byte(len(name))}+ req = append(req, name...)+ req = append(req, byte(port>>8), byte(port))+ if _, err := conn.Write(req); err != nil {+ t.Fatalf("write request: %v", err)+ }+}