From 19af5ba3293f16168a9aa02eb4278eb5a6b07555 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Fri, 20 Nov 2026 22:22:13 +0100 Subject: [PATCH 1/5] feat(traffic): add opt-in RFC 1928 SOCKS5 CONNECT listener with target gate and egress governor --- intercept/target_dial.go | 59 ++++ server/manager.go | 8 + traffic/socks5.go | 688 +++++++++++++++++++++++++++++++++++++++ traffic/traffic.go | 10 + 4 files changed, 765 insertions(+) create mode 100644 traffic/socks5.go diff --git a/intercept/target_dial.go b/intercept/target_dial.go index 2ed1d07fb..62007db4c 100644 --- a/intercept/target_dial.go +++ b/intercept/target_dial.go @@ -3,12 +3,15 @@ package intercept import ( "context" "encoding/json" + "errors" "fmt" "net" "net/netip" "strconv" "strings" "time" + + "github.com/Autumn-27/artex/db" ) // The HTTP clients the platform owns (WebFetch and the custom http tool) @@ -178,3 +181,59 @@ func (e *dialError) Error() string { } return msg + " Do not fetch the target with another tool or by its address." } + +// IsTargetBlocked reports whether err came from PinnedDialer / DialSOCKS5 +// refusing a target before dialing (rule block, unpinned target, or unavailable +// target gate), returning its platform control code. +func IsTargetBlocked(err error) (string, bool) { + var de *dialError + if errors.As(err, &de) { + return de.code, true + } + return "", false +} + +// DialSOCKS5 enforces the platform's target gate before dialing a SOCKS5 +// CONNECT destination, reusing the same policy functions as PinnedDialer: +// - When ctx carries an enabled TargetScope, delegates directly to dialTarget +// (asset block rules, exemptions, fresh DNS resolution, and pin checks). +// - Otherwise enforces global asset block rules (domain, IP, and CIDR, before +// and after fresh DNS resolution) and dials only the resolved addresses that +// passed those rules. +func (i *Interceptor) DialSOCKS5(ctx context.Context, network, addr string) (net.Conn, error) { + if i == nil { + return baseDial(ctx, network, addr) + } + if scope, ok := TargetScopeFromContext(ctx); ok && scope.Enabled != nil && scope.Enabled() { + return i.dialTarget(ctx, scope, "socks5", network, addr) + } + t, err := addrTarget(network, addr) + if err != nil { + return nil, &dialError{code: TargetGateUnavailable, target: addr, reason: "the address of the connection could not be read (" + err.Error() + ")"} + } + if i.targets == nil { + return i.baseDialer()(ctx, network, addr) + } + rules, err := i.targets.BlockRules(db.TargetScopeRef{}) + if err != nil { + return nil, &dialError{code: TargetGateUnavailable, target: t.String(), reason: "the asset rules could not be read: " + err.Error()} + } + ct := &checkedTarget{Target: t} + if t.Kind != TargetDomain { + ct.ips, ct.resolver, ct.resolvedAt = []string{t.Host}, literalResolver, time.Now().UTC() + } + if rule, hit := blockRule(rules, ct); hit { + return nil, &dialError{code: TargetRuleBlocked, target: t.String(), reason: fmt.Sprintf("%s is blocked by an asset rule. %s", t, rule.Reason())} + } + if t.Kind == TargetDomain { + c := &targetCheck{i: i, ctx: ctx, tool: "socks5", fresh: true} + c.resolve(ct, true) + if ct.err != nil { + return nil, ct.err + } + if rule, hit := blockRule(rules, ct); hit { + return nil, &dialError{code: TargetRuleBlocked, target: t.String(), reason: fmt.Sprintf("%s is blocked by an asset rule. %s", t, rule.Reason())} + } + } + return i.dialAddrs(ctx, network, ct.ips, t.Port) +} diff --git a/server/manager.go b/server/manager.go index 9e2a558c4..de1a29454 100644 --- a/server/manager.go +++ b/server/manager.go @@ -463,6 +463,14 @@ func NewManager(dir, proxyAddr string) (*Manager, error) { if tr != nil { m.traffic = tr m.reloadRewriteRules() // in place before the proxy accepts a connection + if scfg, ok := traffic.SOCKS5ConfigFromEnv(); ok { + scfg.DialContext = m.interceptor.DialSOCKS5 + if bound, serr := tr.StartSOCKS5(scfg); serr != nil { + log.Printf("[traffic/socks5] disabled: %v", serr) + } else { + log.Printf("[traffic/socks5] opt-in SOCKS5 CONNECT listener on %s", bound) + } + } go func() { log.Printf("[traffic] recording proxy on %s (set HTTP_PROXY=%s + trust _ca CA)", proxyAddr, tr.ProxyAddr()) if err := tr.Start(); err != nil { diff --git a/traffic/socks5.go b/traffic/socks5.go new file mode 100644 index 000000000..e7eab68a6 --- /dev/null +++ b/traffic/socks5.go @@ -0,0 +1,688 @@ +package traffic + +import ( + "context" + "encoding/binary" + "errors" + "fmt" + "io" + "log" + "net" + "net/url" + "os" + "strconv" + "strings" + "sync" + "syscall" + "time" + + "github.com/Autumn-27/artex/egress" + "github.com/Autumn-27/artex/intercept" +) + +// Opt-in environment flags for the SOCKS5 inbound listener (P4.1a). +// Off by default: when neither is set, no SOCKS5 listener is started and the +// HTTP recording proxy is unchanged. +const ( + // SOCKS5EnabledEnv turns on the SOCKS5 CONNECT listener on DefaultSOCKS5Addr + // (loopback) when set to 1/true/on/yes. + SOCKS5EnabledEnv = "ARTEX_SOCKS5_ENABLED" + // SOCKS5AddrEnv optionally overrides the bind address (default 127.0.0.1:1080). + // Setting a non-empty address also enables the listener unless + // ARTEX_SOCKS5_ENABLED is explicitly false/0/off/no. + SOCKS5AddrEnv = "ARTEX_SOCKS5_ADDR" + + DefaultSOCKS5Addr = "127.0.0.1:1080" + DefaultSOCKS5MaxConcurrent = 64 + DefaultSOCKS5MaxBytesPerDir = 4 * 1024 * 1024 + DefaultSOCKS5PreviewBytes = blobPreview + DefaultSOCKS5IdleTimeout = 2 * time.Minute + DefaultSOCKS5HandshakeTimeout = 10 * time.Second + DefaultSOCKS5DialTimeout = 30 * time.Second + DefaultSOCKS5PaceTimeout = paceWaitMax +) + +// RFC 1928 / RFC 1929 protocol constants. +const ( + socks5Version = 0x05 + socks5AuthNone = 0x00 + socks5AuthUserPass = 0x02 + socks5AuthNoAccept = 0xFF + socks5UserPassVer = 0x01 + + socks5CmdConnect = 0x01 + socks5CmdBind = 0x02 + socks5CmdUDPAssociate = 0x03 + + socks5AtypIPv4 = 0x01 + socks5AtypDomain = 0x03 + socks5AtypIPv6 = 0x04 + + socks5RepSuccess = 0x00 + socks5RepGeneralFailure = 0x01 + socks5RepNotAllowed = 0x02 + socks5RepNetworkUnreachable = 0x03 + socks5RepHostUnreachable = 0x04 + socks5RepConnectionRefused = 0x05 + socks5RepTTLExpired = 0x06 + socks5RepCmdNotSupported = 0x07 + socks5RepAtypNotSupported = 0x08 +) + +// SOCKS5Config configures the opt-in RFC 1928 SOCKS5 CONNECT listener. +type SOCKS5Config struct { + // Addr is the TCP bind address (default DefaultSOCKS5Addr, loopback-bound). + Addr string + // MaxConcurrent bounds simultaneous active SOCKS5 streams (default 64). + MaxConcurrent int + // MaxBytesPerDir caps bytes captured for storage per direction (default 4 MiB). + // Relay continues counting total bytes; exceeding the cap sets Truncated=true. + MaxBytesPerDir int64 + // PreviewBytes caps the inline preview per direction (default 8 KiB). + PreviewBytes int + // IdleTimeout closes a relayed stream after no bytes move in either direction. + IdleTimeout time.Duration + // HandshakeTimeout bounds the RFC 1928 greeting, auth, and CONNECT request. + HandshakeTimeout time.Duration + // DialTimeout bounds the outbound TCP dial. + DialTimeout time.Duration + // PaceTimeout bounds waiting on the egress governor before refusing the dial. + PaceTimeout time.Duration + // DialContext opens the outbound TCP connection after policy checks. + // Defaults to intercept.PinnedDialer("socks5", false). + DialContext func(ctx context.Context, network, addr string) (net.Conn, error) + // BaseContext, when non-nil, derives the context passed to the egress + // governor and DialContext (e.g. carrying an intercept.TargetScope). + BaseContext func() context.Context + // OnRecord, when non-nil, receives each completed raw TCP capture. + // When nil and attached to a Traffic store, the store's raw-TCP recorder runs. + OnRecord func(RawTCPCapture) +} + +// RawTCPCapture is the completed metadata and bounded directional payloads of +// one relayed SOCKS5 TCP connection. +type RawTCPCapture struct { + Host string + Port int + StartedAt time.Time + EndedAt time.Time + C2SBytes int64 + S2CBytes int64 + C2SData []byte + S2CData []byte + Truncated bool + Paced bool +} + +// SOCKS5ConfigFromEnv parses ARTEX_SOCKS5_ENABLED and ARTEX_SOCKS5_ADDR. +// Returns (cfg, false) when disabled (the default). +func SOCKS5ConfigFromEnv() (SOCKS5Config, bool) { + rawEnabled := strings.ToLower(strings.TrimSpace(os.Getenv(SOCKS5EnabledEnv))) + rawAddr := strings.TrimSpace(os.Getenv(SOCKS5AddrEnv)) + + switch rawEnabled { + case "0", "false", "off", "no": + return SOCKS5Config{}, false + case "1", "true", "on", "yes": + addr := rawAddr + if addr == "" { + addr = DefaultSOCKS5Addr + } + return SOCKS5Config{Addr: addr}, true + case "": + if rawAddr == "" { + return SOCKS5Config{}, false + } + return SOCKS5Config{Addr: rawAddr}, true + default: + log.Printf("[traffic] %s=%q is not a boolean; leaving SOCKS5 disabled", SOCKS5EnabledEnv, rawEnabled) + return SOCKS5Config{}, false + } +} + +func (c SOCKS5Config) withDefaults() SOCKS5Config { + if strings.TrimSpace(c.Addr) == "" { + c.Addr = DefaultSOCKS5Addr + } + if c.MaxConcurrent <= 0 { + c.MaxConcurrent = DefaultSOCKS5MaxConcurrent + } + if c.MaxBytesPerDir <= 0 { + c.MaxBytesPerDir = DefaultSOCKS5MaxBytesPerDir + } + if c.PreviewBytes <= 0 { + c.PreviewBytes = DefaultSOCKS5PreviewBytes + } + if c.IdleTimeout <= 0 { + c.IdleTimeout = DefaultSOCKS5IdleTimeout + } + if c.HandshakeTimeout <= 0 { + c.HandshakeTimeout = DefaultSOCKS5HandshakeTimeout + } + if c.DialTimeout <= 0 { + c.DialTimeout = DefaultSOCKS5DialTimeout + } + if c.PaceTimeout <= 0 { + c.PaceTimeout = DefaultSOCKS5PaceTimeout + } + if c.DialContext == nil { + c.DialContext = intercept.PinnedDialer("socks5", false) + } + return c +} + +type socks5Server struct { + t *Traffic + cfg SOCKS5Config + ln net.Listener + sem chan struct{} + stop chan struct{} + once sync.Once + wg sync.WaitGroup + + mu sync.Mutex + active map[net.Conn]struct{} +} + +// StartSOCKS5 binds and starts the opt-in RFC 1928 SOCKS5 CONNECT listener on t. +// Returns the actual bound address (useful when cfg.Addr uses port 0). +func (t *Traffic) StartSOCKS5(cfg SOCKS5Config) (string, error) { + cfg = cfg.withDefaults() + ln, err := net.Listen("tcp", cfg.Addr) + if err != nil { + return "", fmt.Errorf("socks5 listen %s: %w", cfg.Addr, err) + } + srv := &socks5Server{ + t: t, + cfg: cfg, + ln: ln, + sem: make(chan struct{}, cfg.MaxConcurrent), + stop: make(chan struct{}), + active: map[net.Conn]struct{}{}, + } + t.socksMu.Lock() + if t.socks != nil { + t.socksMu.Unlock() + _ = ln.Close() + return "", errors.New("socks5 listener already running") + } + t.socks = srv + t.socksMu.Unlock() + + srv.wg.Add(1) + go srv.serve() + return ln.Addr().String(), nil +} + +// SOCKS5Addr returns the bound SOCKS5 listener address, or "" when disabled. +func (t *Traffic) SOCKS5Addr() string { + t.socksMu.Lock() + defer t.socksMu.Unlock() + if t.socks == nil || t.socks.ln == nil { + return "" + } + return t.socks.ln.Addr().String() +} + +func (s *socks5Server) Close() error { + var closeErr error + s.once.Do(func() { + close(s.stop) + closeErr = s.ln.Close() + s.mu.Lock() + for c := range s.active { + _ = c.Close() + } + s.mu.Unlock() + }) + s.wg.Wait() + return closeErr +} + +func (s *socks5Server) stopping() bool { + select { + case <-s.stop: + return true + default: + return false + } +} + +func (s *socks5Server) trackConn(c net.Conn) bool { + s.mu.Lock() + defer s.mu.Unlock() + if s.stopping() { + return false + } + s.active[c] = struct{}{} + return true +} + +func (s *socks5Server) untrackConn(c net.Conn) { + s.mu.Lock() + delete(s.active, c) + s.mu.Unlock() +} + +func (s *socks5Server) serve() { + defer s.wg.Done() + for { + conn, err := s.ln.Accept() + if err != nil { + if s.stopping() || errors.Is(err, net.ErrClosed) { + return + } + var ne net.Error + if errors.As(err, &ne) && ne.Timeout() { + continue + } + log.Printf("[traffic/socks5] accept error: %v", err) + return + } + if !s.trackConn(conn) { + _ = conn.Close() + return + } + s.wg.Add(1) + go func(c net.Conn) { + defer s.wg.Done() + defer s.untrackConn(c) + defer c.Close() + s.handleConn(c) + }(conn) + } +} + +func (s *socks5Server) handleConn(client net.Conn) { + // 1. Handshake (greeting + optional RFC 1929 auth + CONNECT request) under HandshakeTimeout. + _ = client.SetDeadline(time.Now().Add(s.cfg.HandshakeTimeout)) + + paced, ok := s.negotiateAuth(client) + if !ok { + return + } + + host, port, rep := readSOCKS5Request(client) + if rep != socks5RepSuccess { + _ = writeSOCKS5Reply(client, rep, nil) + return + } + + // 2. Enforce max concurrent streams before touching policy or dialing. + select { + case s.sem <- struct{}{}: + defer func() { <-s.sem }() + default: + _ = writeSOCKS5Reply(client, socks5RepGeneralFailure, nil) + return + } + + // 3. Apply the egress governor and target gate before dialing. + targetAddr := net.JoinHostPort(host, strconv.Itoa(port)) + upstream, rep := s.dialTarget(host, port, targetAddr) + if rep != socks5RepSuccess { + _ = writeSOCKS5Reply(client, rep, nil) + return + } + if !s.trackConn(upstream) { + _ = upstream.Close() + _ = writeSOCKS5Reply(client, socks5RepGeneralFailure, nil) + return + } + defer s.untrackConn(upstream) + defer upstream.Close() + + // Clear handshake deadline before replying and starting the relay. + _ = client.SetDeadline(time.Time{}) + if err := writeSOCKS5Reply(client, socks5RepSuccess, upstream.LocalAddr()); err != nil { + return + } + + // 4. Bidirectional half-close-aware relay with idle timeout and bounded capture. + startedAt := time.Now().UTC() + c2sCap := &limitedCapture{max: s.cfg.MaxBytesPerDir} + s2cCap := &limitedCapture{max: s.cfg.MaxBytesPerDir} + + var relayWG sync.WaitGroup + relayWG.Add(2) + go func() { + defer relayWG.Done() + relayHalf(upstream, client, c2sCap, s.cfg.IdleTimeout) + closeWrite(upstream) + }() + go func() { + defer relayWG.Done() + relayHalf(client, upstream, s2cCap, s.cfg.IdleTimeout) + closeWrite(client) + }() + relayWG.Wait() + endedAt := time.Now().UTC() + + rec := RawTCPCapture{ + Host: host, + Port: port, + StartedAt: startedAt, + EndedAt: endedAt, + C2SBytes: c2sCap.total, + S2CBytes: s2cCap.total, + C2SData: c2sCap.buf, + S2CData: s2cCap.buf, + Truncated: c2sCap.truncated || s2cCap.truncated, + Paced: paced, + } + s.recordCapture(rec) +} + +func (s *socks5Server) recordCapture(rec RawTCPCapture) { + if s.cfg.OnRecord != nil { + s.cfg.OnRecord(rec) + } +} + +func (s *socks5Server) negotiateAuth(conn net.Conn) (paced bool, ok bool) { + var hdr [2]byte + if _, err := io.ReadFull(conn, hdr[:]); err != nil { + return false, false + } + if hdr[0] != socks5Version || hdr[1] == 0 { + return false, false + } + methods := make([]byte, int(hdr[1])) + if _, err := io.ReadFull(conn, methods); err != nil { + return false, false + } + + var hasNone, hasUserPass bool + for _, m := range methods { + switch m { + case socks5AuthNone: + hasNone = true + case socks5AuthUserPass: + hasUserPass = true + } + } + + // Prefer username/password when offered so clients carrying the pace marker + // (egress.PaceUser) can pass it through without requiring real credentials. + // Accept no-auth for loopback clients. + if hasUserPass { + if _, err := conn.Write([]byte{socks5Version, socks5AuthUserPass}); err != nil { + return false, false + } + return readSOCKS5UserPass(conn) + } + if hasNone && isLoopbackRemote(conn.RemoteAddr()) { + if _, err := conn.Write([]byte{socks5Version, socks5AuthNone}); err != nil { + return false, false + } + return false, true + } + _, _ = conn.Write([]byte{socks5Version, socks5AuthNoAccept}) + return false, false +} + +// readSOCKS5UserPass parses an RFC 1929 username/password subnegotiation. +// Credentials are never required (any username/password succeeds); a username +// equal to egress.PaceUser marks the connection as paced. +func readSOCKS5UserPass(conn net.Conn) (paced bool, ok bool) { + var head [2]byte + if _, err := io.ReadFull(conn, head[:]); err != nil { + return false, false + } + if head[0] != socks5UserPassVer { + _, _ = conn.Write([]byte{socks5UserPassVer, 0x01}) + return false, false + } + uname := make([]byte, int(head[1])) + if _, err := io.ReadFull(conn, uname); err != nil { + return false, false + } + var plen [1]byte + if _, err := io.ReadFull(conn, plen[:]); err != nil { + return false, false + } + if plen[0] > 0 { + pass := make([]byte, int(plen[0])) + if _, err := io.ReadFull(conn, pass); err != nil { + return false, false + } + } + if _, err := conn.Write([]byte{socks5UserPassVer, 0x00}); err != nil { + return false, false + } + return string(uname) == egress.PaceUser, true +} + +func readSOCKS5Request(conn net.Conn) (host string, port int, rep byte) { + var hdr [4]byte + if _, err := io.ReadFull(conn, hdr[:]); err != nil { + return "", 0, socks5RepGeneralFailure + } + if hdr[0] != socks5Version || hdr[2] != 0x00 { + return "", 0, socks5RepGeneralFailure + } + cmd := hdr[1] + atyp := hdr[3] + + // Read the address + port before rejecting unsupported commands so the stream + // stays cleanly framed if the peer reads our reply. + switch atyp { + case socks5AtypIPv4: + var ip [4]byte + if _, err := io.ReadFull(conn, ip[:]); err != nil { + return "", 0, socks5RepGeneralFailure + } + host = net.IP(ip[:]).String() + case socks5AtypDomain: + var l [1]byte + if _, err := io.ReadFull(conn, l[:]); err != nil { + return "", 0, socks5RepGeneralFailure + } + if l[0] == 0 { + return "", 0, socks5RepGeneralFailure + } + dom := make([]byte, int(l[0])) + if _, err := io.ReadFull(conn, dom); err != nil { + return "", 0, socks5RepGeneralFailure + } + host = strings.TrimSpace(string(dom)) + if host == "" { + return "", 0, socks5RepGeneralFailure + } + case socks5AtypIPv6: + var ip [16]byte + if _, err := io.ReadFull(conn, ip[:]); err != nil { + return "", 0, socks5RepGeneralFailure + } + host = net.IP(ip[:]).String() + default: + return "", 0, socks5RepAtypNotSupported + } + + var pbuf [2]byte + if _, err := io.ReadFull(conn, pbuf[:]); err != nil { + return "", 0, socks5RepGeneralFailure + } + port = int(binary.BigEndian.Uint16(pbuf[:])) + + if cmd != socks5CmdConnect { + return "", 0, socks5RepCmdNotSupported + } + if port <= 0 || port > 65535 { + return "", 0, socks5RepGeneralFailure + } + return host, port, socks5RepSuccess +} + +func writeSOCKS5Reply(conn net.Conn, rep byte, bindAddr net.Addr) error { + var bndIP net.IP + var bndPort int + if tcpAddr, ok := bindAddr.(*net.TCPAddr); ok && tcpAddr != nil { + bndIP = tcpAddr.IP + bndPort = tcpAddr.Port + } + var out []byte + if ip4 := bndIP.To4(); ip4 != nil { + out = make([]byte, 0, 10) + out = append(out, socks5Version, rep, 0x00, socks5AtypIPv4) + out = append(out, ip4...) + } else if ip16 := bndIP.To16(); ip16 != nil { + out = make([]byte, 0, 22) + out = append(out, socks5Version, rep, 0x00, socks5AtypIPv6) + out = append(out, ip16...) + } else { + out = make([]byte, 0, 10) + out = append(out, socks5Version, rep, 0x00, socks5AtypIPv4, 0, 0, 0, 0) + } + var p [2]byte + binary.BigEndian.PutUint16(p[:], uint16(bndPort)) + out = append(out, p[:]...) + _, err := conn.Write(out) + return err +} + +func (s *socks5Server) dialTarget(host string, port int, targetAddr string) (net.Conn, byte) { + baseCtx := context.Background() + if s.cfg.BaseContext != nil { + if ctx := s.cfg.BaseContext(); ctx != nil { + baseCtx = ctx + } + } + stopCtx, stopCancel := context.WithCancel(baseCtx) + defer stopCancel() + go func() { + select { + case <-s.stop: + stopCancel() + case <-stopCtx.Done(): + } + }() + + // Run through the same egress governor (egress.Do) and target gate dialer + // (intercept.PinnedDialer / Interceptor.DialSOCKS5) the HTTP path uses. + govCtx, govCancel := context.WithTimeout(stopCtx, s.cfg.PaceTimeout) + defer govCancel() + + targetURL := &url.URL{Scheme: "tcp", Host: targetAddr} + dialCalled := false + conn, err := egress.Do(egress.Current(), govCtx, targetURL, func() (net.Conn, error) { + dialCalled = true + dialCtx, dialCancel := context.WithTimeout(stopCtx, s.cfg.DialTimeout) + defer dialCancel() + return s.cfg.DialContext(dialCtx, "tcp", targetAddr) + }) + if err != nil { + if !dialCalled { + // Refused by the egress governor before dialing. + return nil, socks5RepNotAllowed + } + return nil, mapDialErrorToSOCKS5Reply(err) + } + return conn, socks5RepSuccess +} + +func mapDialErrorToSOCKS5Reply(err error) byte { + if _, blocked := intercept.IsTargetBlocked(err); blocked { + return socks5RepNotAllowed + } + var dnsErr *net.DNSError + if errors.As(err, &dnsErr) { + return socks5RepHostUnreachable + } + if errors.Is(err, syscall.ECONNREFUSED) { + return socks5RepConnectionRefused + } + if errors.Is(err, syscall.ENETUNREACH) { + return socks5RepNetworkUnreachable + } + if errors.Is(err, syscall.EHOSTUNREACH) { + return socks5RepHostUnreachable + } + if errors.Is(err, context.DeadlineExceeded) { + return socks5RepTTLExpired + } + low := strings.ToLower(err.Error()) + switch { + case strings.Contains(low, "connection refused"): + return socks5RepConnectionRefused + case strings.Contains(low, "network is unreachable"): + return socks5RepNetworkUnreachable + case strings.Contains(low, "no route to host"), strings.Contains(low, "no such host"), strings.Contains(low, "host is unreachable"): + return socks5RepHostUnreachable + } + return socks5RepGeneralFailure +} + +func isLoopbackRemote(addr net.Addr) bool { + if addr == nil { + return false + } + switch a := addr.(type) { + case *net.TCPAddr: + return a.IP != nil && a.IP.IsLoopback() + default: + h, _, err := net.SplitHostPort(addr.String()) + if err != nil { + h = addr.String() + } + ip := net.ParseIP(strings.Trim(h, "[]")) + return ip != nil && ip.IsLoopback() + } +} + +type limitedCapture struct { + max int64 + total int64 + buf []byte + truncated bool +} + +func (c *limitedCapture) record(p []byte) { + if len(p) == 0 { + return + } + c.total += int64(len(p)) + rem := c.max - int64(len(c.buf)) + if rem <= 0 { + c.truncated = true + return + } + if int64(len(p)) > rem { + c.buf = append(c.buf, p[:rem]...) + c.truncated = true + return + } + c.buf = append(c.buf, p...) +} + +func relayHalf(dst, src net.Conn, cap *limitedCapture, idle time.Duration) { + buf := make([]byte, 32*1024) + for { + if idle > 0 { + _ = src.SetReadDeadline(time.Now().Add(idle)) + } + n, rerr := src.Read(buf) + if n > 0 { + chunk := buf[:n] + cap.record(chunk) + if idle > 0 { + _ = dst.SetWriteDeadline(time.Now().Add(idle)) + } + if _, werr := dst.Write(chunk); werr != nil { + return + } + } + if rerr != nil { + return + } + } +} + +func closeWrite(c net.Conn) { + type halfCloser interface { + CloseWrite() error + } + if hc, ok := c.(halfCloser); ok { + _ = hc.CloseWrite() + } +} diff --git a/traffic/traffic.go b/traffic/traffic.go index 8444b831c..f25ced454 100644 --- a/traffic/traffic.go +++ b/traffic/traffic.go @@ -232,6 +232,9 @@ type Traffic struct { // sweeping tracks the snapshot sweeper, which runs until Close. It is kept // apart from reaping, which only ever holds work that finishes on its own. sweeping sync.WaitGroup + // socks holds the opt-in SOCKS5 CONNECT listener when started via StartSOCKS5. + socksMu sync.Mutex + socks *socks5Server } // Open initializes the traffic tree, blob store and SQLite index under dir. @@ -445,6 +448,13 @@ func (t *Traffic) Close() error { if t.closed != nil { t.closeOnce.Do(func() { close(t.closed) }) } + t.socksMu.Lock() + srv := t.socks + t.socks = nil + t.socksMu.Unlock() + if srv != nil { + _ = srv.Close() + } t.reaping.Wait() t.sweeping.Wait() return t.db.Close() From 551c14e64425261d39f04df6d8050e8a1e44e799 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Fri, 20 Nov 2026 22:23:24 +0100 Subject: [PATCH 2/5] feat(traffic): record raw TCP streams in SQLite store with previews and blob refs --- traffic/raw_tcp.go | 257 +++++++++++++++++++++++++++++++++++++++++++++ traffic/socks5.go | 5 + traffic/traffic.go | 23 ++++ 3 files changed, 285 insertions(+) create mode 100644 traffic/raw_tcp.go diff --git a/traffic/raw_tcp.go b/traffic/raw_tcp.go new file mode 100644 index 000000000..6e3854c83 --- /dev/null +++ b/traffic/raw_tcp.go @@ -0,0 +1,257 @@ +package traffic + +import ( + "crypto/sha256" + "database/sql" + "encoding/hex" + "fmt" + "log" + "os" + "path/filepath" + "strings" + "time" +) + +// RawTCPStream is one recorded raw TCP connection relayed by the opt-in SOCKS5 +// listener. It never creates an HTTP exchanges row. +type RawTCPStream struct { + ID string `json:"id"` + Protocol string `json:"protocol"` + Host string `json:"host"` + Port int `json:"port"` + StartedAt int64 `json:"started_at"` // unix milliseconds + EndedAt int64 `json:"ended_at"` // unix milliseconds + DurationMs int64 `json:"duration_ms"` // non-negative duration in milliseconds + C2SBytes int64 `json:"c2s_bytes"` + S2CBytes int64 `json:"s2c_bytes"` + Truncated bool `json:"truncated"` + C2SPreview []byte `json:"c2s_preview,omitempty"` + C2SBlob string `json:"c2s_blob,omitempty"` + S2CPreview []byte `json:"s2c_preview,omitempty"` + S2CBlob string `json:"s2c_blob,omitempty"` +} + +// RawTCPFilter narrows ListRawTCPStreams queries. +type RawTCPFilter struct { + Host string + Port int + Limit int + Offset int +} + +func (t *Traffic) newRawTCPStreamID(started time.Time) string { + n := t.seq.Add(1) + return fmt.Sprintf("tcp-%d-%04d", started.UnixMilli(), n%10000) +} + +// spillRawDir stores one direction's captured bytes: up to previewCap stays +// inline in preview with no blob; beyond previewCap, the full captured bytes +// are written to the content-addressed _blobs store and preview holds the +// leading previewCap bytes. +func (t *Traffic) spillRawDir(data []byte, previewCap int) (preview []byte, blobHash string) { + if len(data) == 0 { + return nil, "" + } + if previewCap <= 0 { + previewCap = DefaultSOCKS5PreviewBytes + } + if len(data) <= previewCap { + cp := make([]byte, len(data)) + copy(cp, data) + return cp, "" + } + sum := sha256.Sum256(data) + h := hex.EncodeToString(sum[:]) + blobDir := filepath.Join(t.dir, "_blobs", "sha256", h[:2]) + if err := os.MkdirAll(blobDir, 0o755); err != nil { + log.Printf("[traffic/socks5] failed to create blob directory: %v", err) + return append([]byte(nil), data[:previewCap]...), "" + } + blobPath := filepath.Join(blobDir, h+".bin") + if _, err := os.Stat(blobPath); os.IsNotExist(err) { + if err := os.WriteFile(blobPath, data, 0o644); err != nil { + log.Printf("[traffic/socks5] failed to write blob %s: %v", h, err) + return append([]byte(nil), data[:previewCap]...), "" + } + } + return append([]byte(nil), data[:previewCap]...), h +} + +// RecordRawTCPStream persists one raw TCP connection into raw_tcp_streams and +// registers any spilled directional blobs in blob_refs. It never writes to the +// HTTP exchanges table. +func (t *Traffic) RecordRawTCPStream(rec RawTCPCapture, previewBytes int) (string, error) { + if t == nil || t.db == nil { + return "", fmt.Errorf("traffic store unavailable") + } + started := rec.StartedAt.UTC() + if started.IsZero() { + started = time.Now().UTC() + } + ended := rec.EndedAt.UTC() + if ended.IsZero() || ended.Before(started) { + ended = started + } + durMs := ended.Sub(started).Milliseconds() + if durMs < 0 { + durMs = 0 + } + + c2sPrev, c2sBlob := t.spillRawDir(rec.C2SData, previewBytes) + s2cPrev, s2cBlob := t.spillRawDir(rec.S2CData, previewBytes) + + t.wmu.Lock() + defer t.wmu.Unlock() + + id := t.newRawTCPStreamID(started) + trunc := 0 + if rec.Truncated { + trunc = 1 + } + + tx, err := t.db.Begin() + if err != nil { + return "", fmt.Errorf("begin raw tcp tx: %w", err) + } + defer tx.Rollback() //nolint:errcheck + + if _, err := tx.Exec(` +INSERT INTO raw_tcp_streams( + id, protocol, host, port, started_at, ended_at, duration_ms, + c2s_bytes, s2c_bytes, truncated, c2s_preview, c2s_blob, s2c_preview, s2c_blob +) VALUES(?, 'tcp', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, + id, strings.ToLower(strings.TrimSpace(rec.Host)), rec.Port, + started.UnixMilli(), ended.UnixMilli(), durMs, + rec.C2SBytes, rec.S2CBytes, trunc, + c2sPrev, nullIfEmpty(c2sBlob), s2cPrev, nullIfEmpty(s2cBlob), + ); err != nil { + return "", fmt.Errorf("insert raw_tcp_streams: %w", err) + } + + for _, h := range []string{c2sBlob, s2cBlob} { + if h == "" { + continue + } + if _, err := tx.Exec(`INSERT OR IGNORE INTO blob_refs(hash, exchange_id) VALUES(?, ?)`, h, id); err != nil { + return "", fmt.Errorf("register raw tcp blob ref: %w", err) + } + } + + if err := tx.Commit(); err != nil { + return "", fmt.Errorf("commit raw tcp tx: %w", err) + } + return id, nil +} + +// GetRawTCPStream fetches a single recorded raw TCP stream by ID. Returns nil, nil +// when not found. +func (t *Traffic) GetRawTCPStream(id string) (*RawTCPStream, error) { + if t == nil || t.db == nil { + return nil, fmt.Errorf("traffic store unavailable") + } + var s RawTCPStream + var trunc int + var c2sBlob, s2cBlob sql.NullString + err := t.db.QueryRow(` +SELECT id, protocol, host, port, started_at, ended_at, duration_ms, + c2s_bytes, s2c_bytes, truncated, c2s_preview, c2s_blob, s2c_preview, s2c_blob +FROM raw_tcp_streams WHERE id = ?`, id).Scan( + &s.ID, &s.Protocol, &s.Host, &s.Port, &s.StartedAt, &s.EndedAt, &s.DurationMs, + &s.C2SBytes, &s.S2CBytes, &trunc, &s.C2SPreview, &c2sBlob, &s.S2CPreview, &s2cBlob, + ) + if err == sql.ErrNoRows { + return nil, nil + } + if err != nil { + return nil, err + } + s.Truncated = trunc != 0 + s.C2SBlob = c2sBlob.String + s.S2CBlob = s2cBlob.String + return &s, nil +} + +// ListRawTCPStreams returns raw TCP streams newest-first along with the total +// count matching the filter. +func (t *Traffic) ListRawTCPStreams(f RawTCPFilter) ([]RawTCPStream, int, error) { + if t == nil || t.db == nil { + return nil, 0, fmt.Errorf("traffic store unavailable") + } + var conds []string + var args []any + if h := strings.ToLower(strings.TrimSpace(f.Host)); h != "" { + conds = append(conds, "host = ?") + args = append(args, h) + } + if f.Port > 0 { + conds = append(conds, "port = ?") + args = append(args, f.Port) + } + where := "" + if len(conds) > 0 { + where = " WHERE " + strings.Join(conds, " AND ") + } + + var total int + if err := t.db.QueryRow(`SELECT COUNT(*) FROM raw_tcp_streams`+where, args...).Scan(&total); err != nil { + return nil, 0, err + } + + limit := f.Limit + if limit <= 0 { + limit = 50 + } + if limit > 500 { + limit = 500 + } + offset := f.Offset + if offset < 0 { + offset = 0 + } + + qArgs := append(append([]any{}, args...), limit, offset) + rows, err := t.db.Query(` +SELECT id, protocol, host, port, started_at, ended_at, duration_ms, + c2s_bytes, s2c_bytes, truncated, c2s_preview, c2s_blob, s2c_preview, s2c_blob +FROM raw_tcp_streams`+where+` +ORDER BY started_at DESC, id DESC +LIMIT ? OFFSET ?`, qArgs...) + if err != nil { + return nil, 0, err + } + defer rows.Close() + + out := []RawTCPStream{} + for rows.Next() { + var s RawTCPStream + var trunc int + var c2sBlob, s2cBlob sql.NullString + if err := rows.Scan( + &s.ID, &s.Protocol, &s.Host, &s.Port, &s.StartedAt, &s.EndedAt, &s.DurationMs, + &s.C2SBytes, &s.S2CBytes, &trunc, &s.C2SPreview, &c2sBlob, &s.S2CPreview, &s2cBlob, + ); err != nil { + return nil, 0, err + } + s.Truncated = trunc != 0 + s.C2SBlob = c2sBlob.String + s.S2CBlob = s2cBlob.String + out = append(out, s) + } + return out, total, rows.Err() +} + +// CountRawTCPStreams returns the number of recorded raw TCP streams (all hosts +// when host is empty, or filtered to one exact host). +func (t *Traffic) CountRawTCPStreams(host string) (int, error) { + if t == nil || t.db == nil { + return 0, fmt.Errorf("traffic store unavailable") + } + var n int + h := strings.ToLower(strings.TrimSpace(host)) + if h == "" { + err := t.db.QueryRow(`SELECT COUNT(*) FROM raw_tcp_streams`).Scan(&n) + return n, err + } + err := t.db.QueryRow(`SELECT COUNT(*) FROM raw_tcp_streams WHERE host = ?`, h).Scan(&n) + return n, err +} diff --git a/traffic/socks5.go b/traffic/socks5.go index e7eab68a6..5508d54cf 100644 --- a/traffic/socks5.go +++ b/traffic/socks5.go @@ -374,6 +374,11 @@ func (s *socks5Server) handleConn(client net.Conn) { } func (s *socks5Server) recordCapture(rec RawTCPCapture) { + if s.t != nil && s.t.db != nil { + if _, err := s.t.RecordRawTCPStream(rec, s.cfg.PreviewBytes); err != nil { + log.Printf("[traffic/socks5] failed to record raw tcp stream %s:%d: %v", rec.Host, rec.Port, err) + } + } if s.cfg.OnRecord != nil { s.cfg.OnRecord(rec) } diff --git a/traffic/traffic.go b/traffic/traffic.go index f25ced454..a6132b918 100644 --- a/traffic/traffic.go +++ b/traffic/traffic.go @@ -113,6 +113,29 @@ CREATE TABLE IF NOT EXISTS exchange_rewrites ( after_blob TEXT, PRIMARY KEY (exchange_id, seq) ); + +-- Raw TCP streams relayed through the opt-in SOCKS5 CONNECT listener (P4.1a). +-- Stored separately from HTTP exchanges: a raw TCP stream never creates a row +-- in exchanges. Oversized directional payloads spill to _blobs/sha256 and +-- register their hashes in blob_refs under the stream's tcp-* id. +CREATE TABLE IF NOT EXISTS raw_tcp_streams ( + id TEXT PRIMARY KEY, + protocol TEXT NOT NULL DEFAULT 'tcp', + host TEXT NOT NULL, + port INTEGER NOT NULL, + started_at INTEGER NOT NULL, -- unix milliseconds + ended_at INTEGER NOT NULL, -- unix milliseconds + duration_ms INTEGER NOT NULL, + c2s_bytes INTEGER NOT NULL DEFAULT 0, + s2c_bytes INTEGER NOT NULL DEFAULT 0, + truncated INTEGER NOT NULL DEFAULT 0, + c2s_preview BLOB, + c2s_blob TEXT, + s2c_preview BLOB, + s2c_blob TEXT +); +CREATE INDEX IF NOT EXISTS idx_raw_tcp_host ON raw_tcp_streams(host); +CREATE INDEX IF NOT EXISTS idx_raw_tcp_started ON raw_tcp_streams(started_at DESC, id DESC); ` // ftsSchema is applied separately from indexSchema: a driver build without FTS5 From 8b49362db4b88a58c47642d2b5f6a12ff6cfc758 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Fri, 20 Nov 2026 22:30:08 +0100 Subject: [PATCH 3/5] test(traffic): add SOCKS5 listener and raw TCP recording tests --- server/socks5_test.go | 108 ++++++ traffic/socks5_test.go | 843 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 951 insertions(+) create mode 100644 server/socks5_test.go create mode 100644 traffic/socks5_test.go diff --git a/server/socks5_test.go b/server/socks5_test.go new file mode 100644 index 000000000..fe9d0791a --- /dev/null +++ b/server/socks5_test.go @@ -0,0 +1,108 @@ +package server + +import ( + "encoding/binary" + "fmt" + "io" + "net" + "strconv" + "sync/atomic" + "testing" + "time" + + "github.com/Autumn-27/artex/db" + "github.com/Autumn-27/artex/traffic" +) + +func TestManagerSOCKS5OptInFlagAndAssetBlockRuleRefusal(t *testing.T) { + // 1. Flag off (default): Manager starts NO SOCKS5 listener. + t.Setenv(traffic.SOCKS5EnabledEnv, "") + t.Setenv(traffic.SOCKS5AddrEnv, "") + mOff, err := NewManager(t.TempDir(), "127.0.0.1:0") + if err != nil { + db.SkipPGUnavailable(t, err) + } + t.Cleanup(func() { _ = mOff.Close() }) + if mOff.traffic == nil { + t.Fatal("expected HTTP recording proxy to start") + } + if got := mOff.traffic.SOCKS5Addr(); got != "" { + t.Fatalf("SOCKS5Addr with flag off = %q, want empty", got) + } + + // 2. Flag on (ARTEX_SOCKS5_ADDR=127.0.0.1:0): listener starts and enforces + // global asset block rules via m.interceptor.DialSOCKS5 BEFORE dialing. + t.Setenv(traffic.SOCKS5AddrEnv, "127.0.0.1:0") + mOn, err := NewManager(t.TempDir(), "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = mOn.Close() }) + socksAddr := mOn.traffic.SOCKS5Addr() + if socksAddr == "" { + t.Fatal("expected SOCKS5 listener when ARTEX_SOCKS5_ADDR is set") + } + + // Add a global asset block rule for 127.0.0.1 and ensure cleanup. + rule, err := mOn.pg.CreateAssetInterceptRule( + "exact_ip", + "127.0.0.1", + fmt.Sprintf("p41a-block-test-%d", time.Now().UnixNano()), + true, + ) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = mOn.pg.DeleteAssetInterceptRule(rule.ID) }) + + // Target TCP server on 127.0.0.1: must NEVER receive a connection. + targetLn, err := net.Listen("tcp4", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer targetLn.Close() + var accepted atomic.Int32 + go func() { + for { + c, err := targetLn.Accept() + if err != nil { + return + } + accepted.Add(1) + _ = c.Close() + } + }() + _, portStr, _ := net.SplitHostPort(targetLn.Addr().String()) + port, _ := strconv.Atoi(portStr) + + c, err := net.DialTimeout("tcp", socksAddr, 3*time.Second) + if err != nil { + t.Fatal(err) + } + defer c.Close() + _ = c.SetDeadline(time.Now().Add(5 * time.Second)) + // Greeting: VER=5, NMETHODS=1, NO AUTH + if _, err := c.Write([]byte{0x05, 0x01, 0x00}); err != nil { + t.Fatal(err) + } + var sel [2]byte + if _, err := io.ReadFull(c, sel[:]); err != nil || sel != [2]byte{0x05, 0x00} { + t.Fatalf("method selection = %v err=%v", sel, err) + } + // CONNECT 127.0.0.1: + req := []byte{0x05, 0x01, 0x00, 0x01, 127, 0, 0, 1, 0, 0} + binary.BigEndian.PutUint16(req[8:10], uint16(port)) + if _, err := c.Write(req); err != nil { + t.Fatal(err) + } + var rep [10]byte + if _, err := io.ReadFull(c, rep[:]); err != nil { + t.Fatal(err) + } + if rep[1] != 0x02 { + t.Fatalf("blocked target SOCKS5 reply = 0x%02x, want 0x02 (not allowed by ruleset)", rep[1]) + } + if accepted.Load() != 0 { + t.Fatalf("blocked target accepted %d connections, want 0", accepted.Load()) + } +} diff --git a/traffic/socks5_test.go b/traffic/socks5_test.go new file mode 100644 index 000000000..956203a33 --- /dev/null +++ b/traffic/socks5_test.go @@ -0,0 +1,843 @@ +package traffic + +import ( + "bytes" + "context" + "encoding/binary" + "errors" + "io" + "net" + "runtime" + "strconv" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/Autumn-27/artex/egress" + "github.com/Autumn-27/artex/intercept" +) + +func startEchoServer(t *testing.T, network, addr string) (net.Listener, *atomic.Int32) { + t.Helper() + ln, err := net.Listen(network, addr) + if err != nil { + t.Fatalf("listen %s %s: %v", network, addr, err) + } + var accepted atomic.Int32 + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() + for { + c, err := ln.Accept() + if err != nil { + return + } + accepted.Add(1) + wg.Add(1) + go func(conn net.Conn) { + defer wg.Done() + defer conn.Close() + _, _ = io.Copy(conn, conn) + }(c) + } + }() + t.Cleanup(func() { + _ = ln.Close() + wg.Wait() + }) + return ln, &accepted +} + +func socks5BuildRequest(cmd byte, host string, port int) []byte { + req := []byte{socks5Version, cmd, 0x00} + if ip := net.ParseIP(host); ip != nil { + if ip4 := ip.To4(); ip4 != nil { + req = append(req, socks5AtypIPv4) + req = append(req, ip4...) + } else { + req = append(req, socks5AtypIPv6) + req = append(req, ip.To16()...) + } + } else { + req = append(req, socks5AtypDomain, byte(len(host))) + req = append(req, []byte(host)...) + } + var p [2]byte + binary.BigEndian.PutUint16(p[:], uint16(port)) + return append(req, p[:]...) +} + +func socks5ReadReply(t *testing.T, conn net.Conn) byte { + t.Helper() + var hdr [4]byte + if _, err := io.ReadFull(conn, hdr[:]); err != nil { + t.Fatalf("read socks5 reply header: %v", err) + } + if hdr[0] != socks5Version { + t.Fatalf("reply version = 0x%02x, want 0x05", hdr[0]) + } + switch hdr[3] { + case socks5AtypIPv4: + var buf [6]byte + if _, err := io.ReadFull(conn, buf[:]); err != nil { + t.Fatalf("read ipv4 reply tail: %v", err) + } + case socks5AtypIPv6: + var buf [18]byte + if _, err := io.ReadFull(conn, buf[:]); err != nil { + t.Fatalf("read ipv6 reply tail: %v", err) + } + case socks5AtypDomain: + var l [1]byte + if _, err := io.ReadFull(conn, l[:]); err != nil { + t.Fatalf("read domain len: %v", err) + } + buf := make([]byte, int(l[0])+2) + if _, err := io.ReadFull(conn, buf); err != nil { + t.Fatalf("read domain tail: %v", err) + } + default: + t.Fatalf("unexpected reply atyp 0x%02x", hdr[3]) + } + return hdr[1] +} + +func socks5DialNoAuth(t *testing.T, proxyAddr, host string, port int) (net.Conn, byte) { + t.Helper() + c, err := net.DialTimeout("tcp", proxyAddr, 3*time.Second) + if err != nil { + t.Fatalf("dial proxy %s: %v", proxyAddr, err) + } + _ = c.SetDeadline(time.Now().Add(5 * time.Second)) + if _, err := c.Write([]byte{socks5Version, 1, socks5AuthNone}); err != nil { + _ = c.Close() + t.Fatalf("write greeting: %v", err) + } + var sel [2]byte + if _, err := io.ReadFull(c, sel[:]); err != nil { + _ = c.Close() + t.Fatalf("read method selection: %v", err) + } + if sel[0] != socks5Version || sel[1] != socks5AuthNone { + _ = c.Close() + t.Fatalf("method selection = %v, want [5 0]", sel) + } + if _, err := c.Write(socks5BuildRequest(socks5CmdConnect, host, port)); err != nil { + _ = c.Close() + t.Fatalf("write connect req: %v", err) + } + rep := socks5ReadReply(t, c) + _ = c.SetDeadline(time.Time{}) + return c, rep +} + +func socks5DialUserPass(t *testing.T, proxyAddr, user, pass, host string, port int) (net.Conn, byte) { + t.Helper() + c, err := net.DialTimeout("tcp", proxyAddr, 3*time.Second) + if err != nil { + t.Fatalf("dial proxy %s: %v", proxyAddr, err) + } + _ = c.SetDeadline(time.Now().Add(5 * time.Second)) + if _, err := c.Write([]byte{socks5Version, 2, socks5AuthNone, socks5AuthUserPass}); err != nil { + _ = c.Close() + t.Fatalf("write greeting: %v", err) + } + var sel [2]byte + if _, err := io.ReadFull(c, sel[:]); err != nil { + _ = c.Close() + t.Fatalf("read method selection: %v", err) + } + if sel[0] != socks5Version || sel[1] != socks5AuthUserPass { + _ = c.Close() + t.Fatalf("method selection = %v, want [5 2]", sel) + } + auth := []byte{socks5UserPassVer, byte(len(user))} + auth = append(auth, []byte(user)...) + auth = append(auth, byte(len(pass))) + auth = append(auth, []byte(pass)...) + if _, err := c.Write(auth); err != nil { + _ = c.Close() + t.Fatalf("write user/pass: %v", err) + } + var aresp [2]byte + if _, err := io.ReadFull(c, aresp[:]); err != nil { + _ = c.Close() + t.Fatalf("read user/pass reply: %v", err) + } + if aresp[0] != socks5UserPassVer || aresp[1] != 0x00 { + _ = c.Close() + t.Fatalf("auth reply = %v, want [1 0]", aresp) + } + if _, err := c.Write(socks5BuildRequest(socks5CmdConnect, host, port)); err != nil { + _ = c.Close() + t.Fatalf("write connect req: %v", err) + } + rep := socks5ReadReply(t, c) + _ = c.SetDeadline(time.Time{}) + return c, rep +} + +func waitForRawStreams(t *testing.T, tr *Traffic, want int) []RawTCPStream { + t.Helper() + deadline := time.Now().Add(3 * time.Second) + for time.Now().Before(deadline) { + items, total, err := tr.ListRawTCPStreams(RawTCPFilter{Limit: 100}) + if err != nil { + t.Fatalf("ListRawTCPStreams: %v", err) + } + if total >= want { + return items + } + time.Sleep(15 * time.Millisecond) + } + items, total, _ := tr.ListRawTCPStreams(RawTCPFilter{Limit: 100}) + t.Fatalf("timed out waiting for %d raw tcp streams; got total=%d (%+v)", want, total, items) + return nil +} + +func TestSOCKS5FlagOffMeansNoListener(t *testing.T) { + t.Setenv(SOCKS5EnabledEnv, "") + t.Setenv(SOCKS5AddrEnv, "") + if cfg, ok := SOCKS5ConfigFromEnv(); ok || cfg.Addr != "" { + t.Fatalf("default SOCKS5ConfigFromEnv = (%+v, %v), want disabled", cfg, ok) + } + t.Setenv(SOCKS5EnabledEnv, "0") + t.Setenv(SOCKS5AddrEnv, "127.0.0.1:1080") + if _, ok := SOCKS5ConfigFromEnv(); ok { + t.Fatalf("explicit ARTEX_SOCKS5_ENABLED=0 must stay disabled even when addr is set") + } + t.Setenv(SOCKS5EnabledEnv, "1") + t.Setenv(SOCKS5AddrEnv, "") + cfg, ok := SOCKS5ConfigFromEnv() + if !ok || cfg.Addr != DefaultSOCKS5Addr { + t.Fatalf("enabled default = (%+v, %v), want (%s, true)", cfg, ok, DefaultSOCKS5Addr) + } + + tr, err := Open(t.TempDir(), "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer tr.Close() + if got := tr.SOCKS5Addr(); got != "" { + t.Fatalf("SOCKS5Addr with flag off = %q, want empty", got) + } +} + +func TestSOCKS5HandshakeSuccessAuthAndHalfCloseNoHTTPExchange(t *testing.T) { + prev := egress.CurrentProfile() + t.Cleanup(func() { egress.SetProfile(prev) }) + egress.SetProfile(egress.Profile{Name: "off"}) + + // Server that reads until client half-closes (CloseWrite), then replies. + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer ln.Close() + go func() { + for { + c, err := ln.Accept() + if err != nil { + return + } + go func(conn net.Conn) { + defer conn.Close() + req, _ := io.ReadAll(conn) + _, _ = conn.Write(append([]byte("ACK:"), req...)) + }(c) + } + }() + targetHost, targetPortStr, _ := net.SplitHostPort(ln.Addr().String()) + targetPort, _ := strconv.Atoi(targetPortStr) + + tr, err := Open(t.TempDir(), "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer tr.Close() + + var pacedSeen atomic.Bool + var unpacedSeen atomic.Bool + proxyAddr, err := tr.StartSOCKS5(SOCKS5Config{ + Addr: "127.0.0.1:0", + OnRecord: func(rec RawTCPCapture) { + if rec.Paced { + pacedSeen.Store(true) + } else { + unpacedSeen.Store(true) + } + }, + }) + if err != nil { + t.Fatal(err) + } + + // 1. Loopback no-auth + half-close relay. + c1, rep := socks5DialNoAuth(t, proxyAddr, targetHost, targetPort) + if rep != socks5RepSuccess { + t.Fatalf("no-auth connect rep = 0x%02x, want 0x00", rep) + } + if _, err := c1.Write([]byte("hello-half-close")); err != nil { + t.Fatal(err) + } + if err := c1.(*net.TCPConn).CloseWrite(); err != nil { + t.Fatal(err) + } + resp1, err := io.ReadAll(c1) + _ = c1.Close() + if err != nil { + t.Fatal(err) + } + if string(resp1) != "ACK:hello-half-close" { + t.Fatalf("half-close reply = %q, want %q", resp1, "ACK:hello-half-close") + } + + // 2. Username/password with pace marker (egress.PaceUser). + c2, rep := socks5DialUserPass(t, proxyAddr, egress.PaceUser, egress.PaceUser, targetHost, targetPort) + if rep != socks5RepSuccess { + t.Fatalf("user/pass pace connect rep = 0x%02x, want 0x00", rep) + } + _, _ = c2.Write([]byte("paced-payload")) + _ = c2.(*net.TCPConn).CloseWrite() + resp2, err := io.ReadAll(c2) + _ = c2.Close() + if err != nil || string(resp2) != "ACK:paced-payload" { + t.Fatalf("paced reply = %q err=%v", resp2, err) + } + + // 3. Username/password with arbitrary/empty credentials still accepted. + c3, rep := socks5DialUserPass(t, proxyAddr, "", "", targetHost, targetPort) + if rep != socks5RepSuccess { + t.Fatalf("empty user/pass connect rep = 0x%02x, want 0x00", rep) + } + _, _ = c3.Write([]byte("anon")) + _ = c3.(*net.TCPConn).CloseWrite() + _, _ = io.ReadAll(c3) + _ = c3.Close() + + streams := waitForRawStreams(t, tr, 3) + if !pacedSeen.Load() || !unpacedSeen.Load() { + t.Fatalf("paced=%v unpaced=%v, want both true", pacedSeen.Load(), unpacedSeen.Load()) + } + + // Verify store fields, CountRawTCPStreams, GetRawTCPStream, and ZERO HTTP exchanges. + cntAll, err := tr.CountRawTCPStreams("") + if err != nil || cntAll != 3 { + t.Fatalf("CountRawTCPStreams('') = %d err=%v, want 3", cntAll, err) + } + cntHost, err := tr.CountRawTCPStreams(targetHost) + if err != nil || cntHost != 3 { + t.Fatalf("CountRawTCPStreams(%q) = %d err=%v, want 3", targetHost, cntHost, err) + } + got, err := tr.GetRawTCPStream(streams[0].ID) + if err != nil || got == nil { + t.Fatalf("GetRawTCPStream(%q) = %+v err=%v", streams[0].ID, got, err) + } + if got.Protocol != "tcp" || got.Host != targetHost || got.Port != targetPort || got.Truncated { + t.Fatalf("unexpected stream metadata: %+v", got) + } + if got.C2SBlob != "" || got.S2CBlob != "" { + t.Fatalf("small payload unexpectedly spilled to blob: %+v", got) + } + + var httpRows int + if err := tr.DB().QueryRow(`SELECT COUNT(*) FROM exchanges`).Scan(&httpRows); err != nil { + t.Fatal(err) + } + if httpRows != 0 { + t.Fatalf("raw TCP streams created %d HTTP exchanges rows, want 0", httpRows) + } +} + +type fakeRemoteAddrConn struct { + net.Conn + remote net.Addr +} + +func (f fakeRemoteAddrConn) RemoteAddr() net.Addr { return f.remote } + +func TestSOCKS5HandshakeFailuresUnsupportedCommandsAndMalformedInput(t *testing.T) { + tr, err := Open(t.TempDir(), "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer tr.Close() + + proxyAddr, err := tr.StartSOCKS5(SOCKS5Config{ + Addr: "127.0.0.1:0", + HandshakeTimeout: 150 * time.Millisecond, + }) + if err != nil { + t.Fatal(err) + } + + // 1. BIND and UDP ASSOCIATE rejected with 0x07 (command not supported). + for _, tc := range []struct { + name string + cmd byte + }{ + {"bind", socks5CmdBind}, + {"udp_associate", socks5CmdUDPAssociate}, + {"unknown_cmd", 0x09}, + } { + t.Run(tc.name, func(t *testing.T) { + c, err := net.DialTimeout("tcp", proxyAddr, 2*time.Second) + if err != nil { + t.Fatal(err) + } + defer c.Close() + _, _ = c.Write([]byte{socks5Version, 1, socks5AuthNone}) + var sel [2]byte + if _, err := io.ReadFull(c, sel[:]); err != nil { + t.Fatal(err) + } + _, _ = c.Write(socks5BuildRequest(tc.cmd, "127.0.0.1", 8080)) + if rep := socks5ReadReply(t, c); rep != socks5RepCmdNotSupported { + t.Fatalf("%s reply = 0x%02x, want 0x%02x", tc.name, rep, socks5RepCmdNotSupported) + } + }) + } + + // 2. Unsupported auth method -> 0x05 0xFF. + t.Run("unsupported_auth_method", func(t *testing.T) { + c, err := net.DialTimeout("tcp", proxyAddr, 2*time.Second) + if err != nil { + t.Fatal(err) + } + defer c.Close() + _, _ = c.Write([]byte{socks5Version, 1, 0x03}) + var sel [2]byte + if _, err := io.ReadFull(c, sel[:]); err != nil { + t.Fatal(err) + } + if sel != [2]byte{socks5Version, socks5AuthNoAccept} { + t.Fatalf("reply = %v, want [5 255]", sel) + } + }) + + // 3. Non-loopback client offering only no-auth -> rejected with 0x05 0xFF. + t.Run("non_loopback_no_auth_rejected", func(t *testing.T) { + srvSide, cliSide := net.Pipe() + defer cliSide.Close() + done := make(chan struct{}) + go func() { + defer close(done) + defer srvSide.Close() + tr.socks.handleConn(fakeRemoteAddrConn{ + Conn: srvSide, + remote: &net.TCPAddr{IP: net.ParseIP("198.51.100.10"), Port: 54321}, + }) + }() + _, _ = cliSide.Write([]byte{socks5Version, 1, socks5AuthNone}) + var sel [2]byte + if _, err := io.ReadFull(cliSide, sel[:]); err != nil { + t.Fatal(err) + } + if sel != [2]byte{socks5Version, socks5AuthNoAccept} { + t.Fatalf("non-loopback no-auth reply = %v, want [5 255]", sel) + } + <-done + }) + + // 4. Unsupported ATYP -> 0x08. + t.Run("unsupported_atyp", func(t *testing.T) { + c, err := net.DialTimeout("tcp", proxyAddr, 2*time.Second) + if err != nil { + t.Fatal(err) + } + defer c.Close() + _, _ = c.Write([]byte{socks5Version, 1, socks5AuthNone}) + var sel [2]byte + _, _ = io.ReadFull(c, sel[:]) + _, _ = c.Write([]byte{socks5Version, socks5CmdConnect, 0x00, 0x09, 0x00, 0x50}) + if rep := socks5ReadReply(t, c); rep != socks5RepAtypNotSupported { + t.Fatalf("atyp reply = 0x%02x, want 0x%02x", rep, socks5RepAtypNotSupported) + } + }) + + // 5. Malformed inputs & handshake deadline: must close cleanly without panic. + malformed := [][]byte{ + {0x04, 0x01, 0x00}, // wrong version + {0x05, 0x00}, // 0 methods + {0x05}, // truncated greeting + {0x05, 0x01, 0x02, 0x02, 0x01, 'a', 0x01, 'b'}, // bad RFC1929 version + {0x05, 0x01, 0x00, 0x05, 0x01, 0xFF, 0x01, 127, 0, 0, 1, 0, 80}, // bad RSV + {0x05, 0x01, 0x00, 0x05, 0x01, 0x00, 0x03, 0x00, 0, 80}, // empty domain + {0x05, 0x01, 0x00, 0x05, 0x01, 0x00, 0x01, 127, 0, 0, 1, 0, 0}, // zero port + nil, // silent client -> handshake timeout + } + baseG := runtime.NumGoroutine() + for i, payload := range malformed { + c, err := net.DialTimeout("tcp", proxyAddr, 2*time.Second) + if err != nil { + t.Fatalf("malformed[%d] dial: %v", i, err) + } + if len(payload) > 0 { + _, _ = c.Write(payload) + _ = c.(*net.TCPConn).CloseWrite() + } + _ = c.SetReadDeadline(time.Now().Add(time.Second)) + _, _ = io.Copy(io.Discard, c) + _ = c.Close() + } + time.Sleep(50 * time.Millisecond) + if afterG := runtime.NumGoroutine(); afterG > baseG+4 { + t.Fatalf("possible goroutine leak after malformed handshakes: before=%d after=%d", baseG, afterG) + } +} + +func TestSOCKS5DeniedTargetRefusedBeforeDial(t *testing.T) { + prev := egress.CurrentProfile() + t.Cleanup(func() { egress.SetProfile(prev) }) + egress.SetProfile(egress.Profile{Name: "off"}) + + echoLn, accepted := startEchoServer(t, "tcp4", "127.0.0.1:0") + host, portStr, _ := net.SplitHostPort(echoLn.Addr().String()) + port, _ := strconv.Atoi(portStr) + + tr, err := Open(t.TempDir(), "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer tr.Close() + + // Use intercept.PinnedDialer with a gated TargetScope whose Interceptor is nil: + // PinnedDialer returns TargetGateUnavailable (a platform dialError) BEFORE dialing. + proxyAddr, err := tr.StartSOCKS5(SOCKS5Config{ + Addr: "127.0.0.1:0", + BaseContext: func() context.Context { + return intercept.WithTargetScope(context.Background(), intercept.TargetScope{ + TaskID: "99", + Enabled: func() bool { return true }, + }) + }, + DialContext: intercept.PinnedDialer("socks5", true), + }) + if err != nil { + t.Fatal(err) + } + + c, rep := socks5DialNoAuth(t, proxyAddr, host, port) + _ = c.Close() + if rep != socks5RepNotAllowed { + t.Fatalf("denied target reply = 0x%02x, want 0x%02x", rep, socks5RepNotAllowed) + } + if accepted.Load() != 0 { + t.Fatalf("upstream accepted %d connections despite target gate refusal", accepted.Load()) + } + if n, _ := tr.CountRawTCPStreams(""); n != 0 { + t.Fatalf("refused connection recorded %d raw streams, want 0", n) + } +} + +func TestSOCKS5GovernorRefusalBeforeDial(t *testing.T) { + prev := egress.CurrentProfile() + t.Cleanup(func() { egress.SetProfile(prev) }) + // 1 token burst, very slow refill: 2nd request must wait far longer than PaceTimeout. + egress.SetProfile(egress.Profile{Name: "tight", RPS: 0.01, Burst: 1, MaxPerHost: 1, MaxGlobal: 1}) + + echoLn, _ := startEchoServer(t, "tcp4", "127.0.0.1:0") + host, portStr, _ := net.SplitHostPort(echoLn.Addr().String()) + port, _ := strconv.Atoi(portStr) + + var dialCalls atomic.Int32 + tr, err := Open(t.TempDir(), "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer tr.Close() + + proxyAddr, err := tr.StartSOCKS5(SOCKS5Config{ + Addr: "127.0.0.1:0", + PaceTimeout: 40 * time.Millisecond, + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + dialCalls.Add(1) + return (&net.Dialer{Timeout: 2 * time.Second}).DialContext(ctx, network, addr) + }, + }) + if err != nil { + t.Fatal(err) + } + + // First call consumes the single burst token. + c1, rep1 := socks5DialUserPass(t, proxyAddr, egress.PaceUser, egress.PaceUser, host, port) + if rep1 != socks5RepSuccess { + t.Fatalf("first connect rep = 0x%02x, want 0x00", rep1) + } + _ = c1.Close() + + // Second call exceeds PaceTimeout inside egress.Do and is refused BEFORE dial. + before := dialCalls.Load() + c2, rep2 := socks5DialUserPass(t, proxyAddr, egress.PaceUser, egress.PaceUser, host, port) + _ = c2.Close() + if rep2 != socks5RepNotAllowed { + t.Fatalf("governor refusal reply = 0x%02x, want 0x%02x", rep2, socks5RepNotAllowed) + } + if dialCalls.Load() != before { + t.Fatalf("dialer was called despite egress governor refusal (before=%d after=%d)", before, dialCalls.Load()) + } +} + +func TestSOCKS5ByteCapsTruncationAndBlobSpill(t *testing.T) { + prev := egress.CurrentProfile() + t.Cleanup(func() { egress.SetProfile(prev) }) + egress.SetProfile(egress.Profile{Name: "off"}) + + echoLn, _ := startEchoServer(t, "tcp4", "127.0.0.1:0") + host, portStr, _ := net.SplitHostPort(echoLn.Addr().String()) + port, _ := strconv.Atoi(portStr) + + tr, err := Open(t.TempDir(), "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer tr.Close() + + const ( + maxPerDir = 2048 + previewCap = 256 + sendTotal = 6144 + ) + proxyAddr, err := tr.StartSOCKS5(SOCKS5Config{ + Addr: "127.0.0.1:0", + MaxBytesPerDir: maxPerDir, + PreviewBytes: previewCap, + }) + if err != nil { + t.Fatal(err) + } + + payload := make([]byte, sendTotal) + for i := range payload { + payload[i] = byte(i % 251) // includes NUL bytes (binary stream) + } + + c, rep := socks5DialNoAuth(t, proxyAddr, host, port) + if rep != socks5RepSuccess { + t.Fatalf("connect rep = 0x%02x", rep) + } + go func() { + _, _ = c.Write(payload) + _ = c.(*net.TCPConn).CloseWrite() + }() + echoed, err := io.ReadAll(c) + _ = c.Close() + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(echoed, payload) { + t.Fatalf("relayed payload len=%d differed from sent len=%d", len(echoed), len(payload)) + } + + streams := waitForRawStreams(t, tr, 1) + s := streams[0] + if !s.Truncated { + t.Fatalf("expected Truncated=true when %d > %d", sendTotal, maxPerDir) + } + if s.C2SBytes != sendTotal || s.S2CBytes != sendTotal { + t.Fatalf("byte counts = (%d, %d), want (%d, %d)", s.C2SBytes, s.S2CBytes, sendTotal, sendTotal) + } + if len(s.C2SPreview) != previewCap || len(s.S2CPreview) != previewCap { + t.Fatalf("preview lengths = (%d, %d), want (%d, %d)", len(s.C2SPreview), len(s.S2CPreview), previewCap, previewCap) + } + if !bytes.Equal(s.C2SPreview, payload[:previewCap]) || !bytes.Equal(s.S2CPreview, payload[:previewCap]) { + t.Fatalf("preview bytes mismatch") + } + if s.C2SBlob == "" || s.S2CBlob == "" { + t.Fatalf("expected spilled blob hashes, got c2s=%q s2c=%q", s.C2SBlob, s.S2CBlob) + } + + // Blob must be readable via Traffic.Blob and registered in blob_refs. + f, sz, err := tr.Blob(s.C2SBlob) + if err != nil { + t.Fatalf("tr.Blob(%q): %v", s.C2SBlob, err) + } + defer f.Close() + blobData, _ := io.ReadAll(f) + if sz != maxPerDir || !bytes.Equal(blobData, payload[:maxPerDir]) { + t.Fatalf("spilled blob size=%d len=%d, want %d matching prefix", sz, len(blobData), maxPerDir) + } + var refs int + if err := tr.DB().QueryRow(`SELECT COUNT(*) FROM blob_refs WHERE hash=? AND exchange_id=?`, s.C2SBlob, s.ID).Scan(&refs); err != nil || refs != 1 { + t.Fatalf("blob_refs count = %d err=%v, want 1", refs, err) + } +} + +func TestSOCKS5IPv4IPv6HostnameAndDNSFailure(t *testing.T) { + prev := egress.CurrentProfile() + t.Cleanup(func() { egress.SetProfile(prev) }) + egress.SetProfile(egress.Profile{Name: "off"}) + + echo4, _ := startEchoServer(t, "tcp4", "127.0.0.1:0") + _, p4s, _ := net.SplitHostPort(echo4.Addr().String()) + p4, _ := strconv.Atoi(p4s) + + tr, err := Open(t.TempDir(), "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer tr.Close() + + proxyAddr, err := tr.StartSOCKS5(SOCKS5Config{ + Addr: "127.0.0.1:0", + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + h, _, _ := net.SplitHostPort(addr) + if h == "nonexistent.invalid" { + return nil, &net.DNSError{Err: "no such host", Name: h, IsNotFound: true} + } + return (&net.Dialer{Timeout: 2 * time.Second}).DialContext(ctx, network, addr) + }, + }) + if err != nil { + t.Fatal(err) + } + + // 1. IPv4 literal + c4, rep := socks5DialNoAuth(t, proxyAddr, "127.0.0.1", p4) + if rep != socks5RepSuccess { + t.Fatalf("ipv4 rep = 0x%02x", rep) + } + _, _ = c4.Write([]byte("v4")) + _ = c4.(*net.TCPConn).CloseWrite() + got4, _ := io.ReadAll(c4) + _ = c4.Close() + if string(got4) != "v4" { + t.Fatalf("ipv4 echo = %q", got4) + } + + // 2. Hostname (localhost) + ch, rep := socks5DialNoAuth(t, proxyAddr, "localhost", p4) + if rep != socks5RepSuccess { + t.Fatalf("hostname rep = 0x%02x", rep) + } + _, _ = ch.Write([]byte("host")) + _ = ch.(*net.TCPConn).CloseWrite() + goth, _ := io.ReadAll(ch) + _ = ch.Close() + if string(goth) != "host" { + t.Fatalf("hostname echo = %q", goth) + } + + // 3. DNS failure -> 0x04 Host unreachable + cd, rep := socks5DialNoAuth(t, proxyAddr, "nonexistent.invalid", 443) + _ = cd.Close() + if rep != socks5RepHostUnreachable { + t.Fatalf("dns failure rep = 0x%02x, want 0x%02x", rep, socks5RepHostUnreachable) + } + + // 4. IPv6 literal (::1) if available in this kernel/environment. + wantStreams := 2 + if echo6, err := net.Listen("tcp6", "[::1]:0"); err == nil { + t.Cleanup(func() { _ = echo6.Close() }) + go func() { + for { + c, err := echo6.Accept() + if err != nil { + return + } + go func(conn net.Conn) { + defer conn.Close() + _, _ = io.Copy(conn, conn) + }(c) + } + }() + _, p6s, _ := net.SplitHostPort(echo6.Addr().String()) + p6, _ := strconv.Atoi(p6s) + c6, rep6 := socks5DialNoAuth(t, proxyAddr, "::1", p6) + if rep6 != socks5RepSuccess { + t.Fatalf("ipv6 rep = 0x%02x", rep6) + } + _, _ = c6.Write([]byte("v6")) + _ = c6.(*net.TCPConn).CloseWrite() + got6, _ := io.ReadAll(c6) + _ = c6.Close() + if string(got6) != "v6" { + t.Fatalf("ipv6 echo = %q", got6) + } + wantStreams++ + } + + waitForRawStreams(t, tr, wantStreams) +} + +func TestSOCKS5ConcurrentStreamsMaxLimitAndIdleTimeout(t *testing.T) { + prev := egress.CurrentProfile() + t.Cleanup(func() { egress.SetProfile(prev) }) + egress.SetProfile(egress.Profile{Name: "off"}) + + echoLn, _ := startEchoServer(t, "tcp4", "127.0.0.1:0") + host, portStr, _ := net.SplitHostPort(echoLn.Addr().String()) + port, _ := strconv.Atoi(portStr) + + tr, err := Open(t.TempDir(), "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer tr.Close() + + proxyAddr, err := tr.StartSOCKS5(SOCKS5Config{ + Addr: "127.0.0.1:0", + MaxConcurrent: 2, + IdleTimeout: 200 * time.Millisecond, + }) + if err != nil { + t.Fatal(err) + } + + // Open 2 concurrent streams simultaneously. + c1, rep1 := socks5DialNoAuth(t, proxyAddr, host, port) + if rep1 != socks5RepSuccess { + t.Fatalf("stream 1 rep = 0x%02x", rep1) + } + defer c1.Close() + c2, rep2 := socks5DialNoAuth(t, proxyAddr, host, port) + if rep2 != socks5RepSuccess { + t.Fatalf("stream 2 rep = 0x%02x", rep2) + } + defer c2.Close() + + // 3rd simultaneous stream must be rejected with 0x01 (MaxConcurrent=2). + c3, rep3 := socks5DialNoAuth(t, proxyAddr, host, port) + _ = c3.Close() + if rep3 != socks5RepGeneralFailure { + t.Fatalf("3rd concurrent stream rep = 0x%02x, want 0x%02x", rep3, socks5RepGeneralFailure) + } + + // Exchange bytes on both active streams while both are open. + var wg sync.WaitGroup + wg.Add(2) + checkEcho := func(c net.Conn, msg string) { + defer wg.Done() + if _, err := c.Write([]byte(msg)); err != nil { + t.Errorf("write %s: %v", msg, err) + return + } + buf := make([]byte, len(msg)) + if _, err := io.ReadFull(c, buf); err != nil || string(buf) != msg { + t.Errorf("echo %s: got %q err=%v", msg, buf, err) + } + } + go checkEcho(c1, "stream-one") + go checkEcho(c2, "stream-two") + wg.Wait() + + // Leave both idle past IdleTimeout (200ms): relay must close them cleanly and record both. + _ = c1.SetReadDeadline(time.Now().Add(2 * time.Second)) + buf := make([]byte, 1) + _, rerr := c1.Read(buf) + if rerr == nil || !errors.Is(rerr, io.EOF) { + // Peer close on idle may return EOF or connection reset depending on OS timing. + if rerr == nil { + t.Fatalf("expected idle stream 1 to close after IdleTimeout") + } + } + _ = c1.Close() + _ = c2.Close() + + streams := waitForRawStreams(t, tr, 2) + if len(streams) < 2 { + t.Fatalf("want at least 2 recorded concurrent streams, got %d", len(streams)) + } +} From afa5d0e1a17000b2202a3284cd3586d901d3bc33 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Fri, 20 Nov 2026 22:32:18 +0100 Subject: [PATCH 4/5] docs: document opt-in SOCKS5 raw TCP capture (P4.1a) and update CHANGELOG --- CHANGELOG.md | 1 + docs/traffic-socks5.md | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 docs/traffic-socks5.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 3265f870b..93a2a8c62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ All notable changes to this project are documented in this file. The format is b - **`ask_operator` target warning**: a request whose question names a host or URL outside the goal targets is tagged as containing target-derived text (#197). - **Opt-in update banner**: `ARTEX_UPDATE_CHECK` makes the backend check the latest GitHub release at most once per 24 hours and the UI show a banner; off by default (#202). - **Release provenance**: release archives carry GitHub build provenance attestations and a CycloneDX SBOM (`gh attestation verify --repo BaaRai/ARTEX`); release notes are the matching `CHANGELOG.md` section (#219, #201). +- **Opt-in SOCKS5 inbound proxy & raw TCP recording (P4.1a)**: setting `ARTEX_SOCKS5_ENABLED=1` (loopback `127.0.0.1:1080` by default) or `ARTEX_SOCKS5_ADDR=` starts an RFC 1928 SOCKS5 `CONNECT` listener alongside the HTTP recording proxy (off by default; HTTP proxy and agent proxy env vars are unchanged). Accepts loopback no-auth and RFC 1929 username/password (without requiring credentials, carrying `artex-pace` in the username), rejects `BIND`/`UDP ASSOCIATE` with `0x07`, enforces the existing target gate (`intercept.Interceptor.DialSOCKS5` / `intercept.PinnedDialer`) and egress governor (`egress.Do`) before dialing, relays half-close-aware TCP with concurrency/byte/idle/handshake bounds, and records one row per connection in SQLite `raw_tcp_streams` with capped directional previews and `_blobs` refs without writing HTTP `exchanges` rows. See `docs/traffic-socks5.md`. - **Opt-in worker cache-prefix split (P3.5 Phase C)**: setting `ARTEX_CACHE_PREFIX=1` (off by default) splits the `worker` system prompt at `DynamicBoundary = 1` into a sibling-shareable static prefix (`body + untrustedContentTail + workerTrafficBlock`) and a per-intent dynamic suffix (`workerArtifactSpec(runDir)` through ``) without reordering sections or altering the visible joined prompt text. Planner, mainagent, chat, and goals are unchanged; custom worker prompts referencing `{{.Now}}` remain volatile in the static segment. See `docs/context-telemetry.md`. - **Opt-in LLM context size telemetry (P3.5 Phase A)**: setting `ARTEX_CONTEXT_TELEMETRY=1` (off by default) records per-call request section byte sizes (`system_static`/`system_dynamic` split at `DynamicBoundary`, serialized `tools`, `messages` by role, `tool_result` subset, and `last_message_bytes`), tool/message counts, and token/cache usage in the new `llm_context` table (migration `0009_llm_context.sql`), with zero prompt/behavior change and zero content stored, independent of `llm_record`. `GET /api/llm/context/summary` aggregates input percentiles (`p50`/`p90`/`p99`), pro-rata section shares over real `input_tokens`, `cache_read/input` ratios by `(worker, model)`, and top tasks. See `docs/context-telemetry.md`. - **HAR 1.2 traffic import and export**: `POST /api/traffic/import/har` imports raw HAR 1.2 JSON captures (up to 32 MiB / 2,000 entries / 32 MiB decoded bodies) with deterministic `har-` IDs (and `_artexExchangeId` preservation for ARTEX exports so distinct same-second exchanges round-trip without collapsing), base64 decoding, `postData.params` and `cookies` array fallbacks, URL userinfo stripping, blob spilling above 256 KiB, and full-text indexing, without applying capture rewrite rules (`exchange_rewrites` stays empty); per-entry errors are reported alongside `imported`/`skipped`/`failed` counts. `GET /api/traffic/export/har` exports filtered traffic (reusing the `GET /api/traffic` filters) as a compliant HAR 1.2 document (with explicit unknown placeholders for unrecorded timings and header sizes) in stable `(ts, id)` order, encoding UTF-8 bodies as text and binary bodies as base64, capped at 1,000 entries / 20 MiB raw headers+bodies (31 MiB JSON backstop, guaranteed under the 32 MiB import limit) with explicit truncation reporting via `X-ARTEX-HAR-*` headers and `_truncated`/`_truncatedReason`/`_totalMatched` fields. The Traffic page adds **Import HAR** and **Export HAR** controls. See `docs/traffic-har.md`. diff --git a/docs/traffic-socks5.md b/docs/traffic-socks5.md new file mode 100644 index 000000000..52838876c --- /dev/null +++ b/docs/traffic-socks5.md @@ -0,0 +1,54 @@ +# Opt-in SOCKS5 Inbound Proxy & Raw TCP Recording (P4.1a) + +ARTEX ships an opt-in **RFC 1928 SOCKS5 `CONNECT`** inbound listener in `traffic/` that records raw TCP connections separately from HTTP traffic. + +## Opt-in Configuration (Off by Default) + +| Environment Variable | Default | Behavior | +|---|---|---| +| `ARTEX_SOCKS5_ENABLED` | unset (off) | Set to `1` / `true` / `on` / `yes` to start the SOCKS5 listener on `127.0.0.1:1080` (loopback). Explicit `0` / `false` / `off` / `no` forces it off even if `ARTEX_SOCKS5_ADDR` is set. | +| `ARTEX_SOCKS5_ADDR` | `127.0.0.1:1080` | Explicit TCP bind address; setting a non-empty value also enables the listener unless `ARTEX_SOCKS5_ENABLED` is explicitly false. | + +- **Flag off**: no SOCKS5 listener is started and the HTTP MITM recording proxy is byte-identical. +- **Agents unchanged**: workers and tools keep `HTTP_PROXY`, `HTTPS_PROXY`, and `ALL_PROXY` pointing to the existing HTTP proxy; ARTEX does not silently switch agents to SOCKS5. + +## Protocol Scope (P4.1a) + +- **Commands**: `CONNECT` (`0x01`) only. `BIND` (`0x02`), `UDP ASSOCIATE` (`0x03`, planned for P4.1c), and unknown commands return RFC 1928 reply `0x07` (*Command not supported*). +- **Addressing**: IPv4 (`0x01`), domain name (`0x03`), and IPv6 (`0x04`). Unknown address types return `0x08` (*Address type not supported*). +- **Authentication**: + - Loopback peers (`net.IP.IsLoopback()`) may connect with **No Auth** (`0x00`). Non-loopback peers offering only `0x00` are rejected with `0x05 0xFF`. + - **Username/Password** (`0x02`, RFC 1929) is accepted without requiring credentials so clients can pass the pace marker (`artex-pace`, `egress.PaceUser`) in the username. +- **Relay**: Full-duplex, half-close aware (`CloseWrite` on EOF in each direction). + +## Policy Enforcement Before Dialing + +Before opening any outbound TCP socket, the listener invokes the same controls used by the HTTP path: + +1. **Egress governor (`egress.Do(egress.Current(), ...)`)**: enforces the active noise profile (`off` is a zero-delay passthrough). If waiting for a slot/token exceeds `PaceTimeout`, the connection is refused before dialing with SOCKS5 reply `0x02` (*Connection not allowed by ruleset*). +2. **Target gate (`intercept.Interceptor.DialSOCKS5` / `intercept.PinnedDialer`)**: enforces global asset block rules (before and after fresh DNS resolution) and any enabled `intercept.TargetScope` pins/exemptions, dialing only verified addresses. Blocked targets return `0x02` before dialing; DNS lookup failures return `0x04` (*Host unreachable*). + +## Raw TCP Storage (SQLite) + +Each completed relayed connection writes one row to the `raw_tcp_streams` table in the SQLite traffic index (`traffic/traffic.go`, `indexSchema` — no PostgreSQL migration): + +- Columns: `id` (`tcp--`), `protocol` (`'tcp'`), `host`, `port`, `started_at`, `ended_at`, `duration_ms`, `c2s_bytes`, `s2c_bytes`, `truncated`, `c2s_preview`, `c2s_blob`, `s2c_preview`, `s2c_blob`. +- **No HTTP exchange row**: raw TCP streams never insert into `exchanges`. +- **Preview & blob spilling**: + - Directional payloads up to `PreviewBytes` (default `8 KiB`) stay inline in `c2s_preview` / `s2c_preview`. + - Payloads above `PreviewBytes` keep the leading `PreviewBytes` inline and spill the captured bytes (up to `MaxBytesPerDir`) into `_blobs/sha256/` with references registered in `blob_refs` under the stream's `tcp-*` ID. + - Exceeding `MaxBytesPerDir` sets `truncated = 1` while continuing to count exact total relayed `c2s_bytes` / `s2c_bytes`. + +## Operational Bounds + +| Parameter | Default | +|---|---| +| `MaxConcurrent` | `64` active streams (excess connections receive `0x01`) | +| `MaxBytesPerDir` | `4 MiB` captured per direction | +| `PreviewBytes` | `8 KiB` inline preview per direction | +| `IdleTimeout` | `2m` per-direction read/write deadline | +| `HandshakeTimeout` | `10s` for greeting, auth, and `CONNECT` request | +| `DialTimeout` | `30s` outbound TCP connect timeout | +| `PaceTimeout` | `30s` egress governor wait bound | + +Closing `Traffic` stops accepting new SOCKS5 connections, closes active sockets, and waits for all handler goroutines to exit before closing SQLite. From 792e965f264cb9b8fbe070413de5a5d6fd2d0ced Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Fri, 20 Nov 2026 22:44:20 +0100 Subject: [PATCH 5/5] fix(traffic): atomic temp+rename raw TCP blob spill and document SOCKS5 proxy caveats --- CHANGELOG.md | 2 +- docs/traffic-socks5.md | 4 +++- traffic/raw_tcp.go | 41 +++++++++++++++++++++++++++++++- traffic/socks5_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93a2a8c62..eb5c29cd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,7 @@ All notable changes to this project are documented in this file. The format is b - **`ask_operator` target warning**: a request whose question names a host or URL outside the goal targets is tagged as containing target-derived text (#197). - **Opt-in update banner**: `ARTEX_UPDATE_CHECK` makes the backend check the latest GitHub release at most once per 24 hours and the UI show a banner; off by default (#202). - **Release provenance**: release archives carry GitHub build provenance attestations and a CycloneDX SBOM (`gh attestation verify --repo BaaRai/ARTEX`); release notes are the matching `CHANGELOG.md` section (#219, #201). -- **Opt-in SOCKS5 inbound proxy & raw TCP recording (P4.1a)**: setting `ARTEX_SOCKS5_ENABLED=1` (loopback `127.0.0.1:1080` by default) or `ARTEX_SOCKS5_ADDR=` starts an RFC 1928 SOCKS5 `CONNECT` listener alongside the HTTP recording proxy (off by default; HTTP proxy and agent proxy env vars are unchanged). Accepts loopback no-auth and RFC 1929 username/password (without requiring credentials, carrying `artex-pace` in the username), rejects `BIND`/`UDP ASSOCIATE` with `0x07`, enforces the existing target gate (`intercept.Interceptor.DialSOCKS5` / `intercept.PinnedDialer`) and egress governor (`egress.Do`) before dialing, relays half-close-aware TCP with concurrency/byte/idle/handshake bounds, and records one row per connection in SQLite `raw_tcp_streams` with capped directional previews and `_blobs` refs without writing HTTP `exchanges` rows. See `docs/traffic-socks5.md`. +- **Opt-in SOCKS5 inbound proxy & raw TCP recording (P4.1a)**: when traffic capture is enabled, setting `ARTEX_SOCKS5_ENABLED=1` (loopback `127.0.0.1:1080` by default) or `ARTEX_SOCKS5_ADDR=` starts an RFC 1928 SOCKS5 `CONNECT` listener alongside the HTTP recording proxy (off by default; HTTP proxy and agent proxy env vars are unchanged; listener does not start if traffic recording is disabled). Accepts loopback no-auth and RFC 1929 username/password without verifying credentials (carrying `artex-pace` in the username; binding to a non-loopback address therefore exposes an unauthenticated proxy, though the target gate and governor still apply), rejects `BIND`/`UDP ASSOCIATE` with `0x07`, enforces the existing target gate (`intercept.Interceptor.DialSOCKS5` / `intercept.PinnedDialer`) and egress governor (`egress.Do`) before dialing, relays half-close-aware TCP with concurrency/byte/idle/handshake bounds, and records one row per connection in SQLite `raw_tcp_streams` with capped directional previews and atomic (temp+rename) `_blobs` refs without writing HTTP `exchanges` rows. See `docs/traffic-socks5.md`. - **Opt-in worker cache-prefix split (P3.5 Phase C)**: setting `ARTEX_CACHE_PREFIX=1` (off by default) splits the `worker` system prompt at `DynamicBoundary = 1` into a sibling-shareable static prefix (`body + untrustedContentTail + workerTrafficBlock`) and a per-intent dynamic suffix (`workerArtifactSpec(runDir)` through ``) without reordering sections or altering the visible joined prompt text. Planner, mainagent, chat, and goals are unchanged; custom worker prompts referencing `{{.Now}}` remain volatile in the static segment. See `docs/context-telemetry.md`. - **Opt-in LLM context size telemetry (P3.5 Phase A)**: setting `ARTEX_CONTEXT_TELEMETRY=1` (off by default) records per-call request section byte sizes (`system_static`/`system_dynamic` split at `DynamicBoundary`, serialized `tools`, `messages` by role, `tool_result` subset, and `last_message_bytes`), tool/message counts, and token/cache usage in the new `llm_context` table (migration `0009_llm_context.sql`), with zero prompt/behavior change and zero content stored, independent of `llm_record`. `GET /api/llm/context/summary` aggregates input percentiles (`p50`/`p90`/`p99`), pro-rata section shares over real `input_tokens`, `cache_read/input` ratios by `(worker, model)`, and top tasks. See `docs/context-telemetry.md`. - **HAR 1.2 traffic import and export**: `POST /api/traffic/import/har` imports raw HAR 1.2 JSON captures (up to 32 MiB / 2,000 entries / 32 MiB decoded bodies) with deterministic `har-` IDs (and `_artexExchangeId` preservation for ARTEX exports so distinct same-second exchanges round-trip without collapsing), base64 decoding, `postData.params` and `cookies` array fallbacks, URL userinfo stripping, blob spilling above 256 KiB, and full-text indexing, without applying capture rewrite rules (`exchange_rewrites` stays empty); per-entry errors are reported alongside `imported`/`skipped`/`failed` counts. `GET /api/traffic/export/har` exports filtered traffic (reusing the `GET /api/traffic` filters) as a compliant HAR 1.2 document (with explicit unknown placeholders for unrecorded timings and header sizes) in stable `(ts, id)` order, encoding UTF-8 bodies as text and binary bodies as base64, capped at 1,000 entries / 20 MiB raw headers+bodies (31 MiB JSON backstop, guaranteed under the 32 MiB import limit) with explicit truncation reporting via `X-ARTEX-HAR-*` headers and `_truncated`/`_truncatedReason`/`_totalMatched` fields. The Traffic page adds **Import HAR** and **Export HAR** controls. See `docs/traffic-har.md`. diff --git a/docs/traffic-socks5.md b/docs/traffic-socks5.md index 52838876c..8c0d2c5af 100644 --- a/docs/traffic-socks5.md +++ b/docs/traffic-socks5.md @@ -10,7 +10,9 @@ ARTEX ships an opt-in **RFC 1928 SOCKS5 `CONNECT`** inbound listener in `traffic | `ARTEX_SOCKS5_ADDR` | `127.0.0.1:1080` | Explicit TCP bind address; setting a non-empty value also enables the listener unless `ARTEX_SOCKS5_ENABLED` is explicitly false. | - **Flag off**: no SOCKS5 listener is started and the HTTP MITM recording proxy is byte-identical. +- **Requires traffic recorder**: the server only starts the SOCKS5 listener when the traffic recorder is enabled (inside the traffic proxy block in `server/manager.go`). If traffic capture is disabled, setting `ARTEX_SOCKS5_ENABLED` / `ARTEX_SOCKS5_ADDR` does not open a listener. - **Agents unchanged**: workers and tools keep `HTTP_PROXY`, `HTTPS_PROXY`, and `ALL_PROXY` pointing to the existing HTTP proxy; ARTEX does not silently switch agents to SOCKS5. +- **Non-loopback bind warning**: RFC 1929 username/password (`0x02`) accepts **any** username and password without verifying credentials (so the pace marker `artex-pace` can ride in the username). Binding `ARTEX_SOCKS5_ADDR` to a non-loopback address therefore exposes an **unauthenticated proxy** to anyone who can reach that port. The target gate and egress governor still apply to every connection, but keep the listener on loopback (`127.0.0.1`) or restrict access at the host firewall. ## Protocol Scope (P4.1a) @@ -36,7 +38,7 @@ Each completed relayed connection writes one row to the `raw_tcp_streams` table - **No HTTP exchange row**: raw TCP streams never insert into `exchanges`. - **Preview & blob spilling**: - Directional payloads up to `PreviewBytes` (default `8 KiB`) stay inline in `c2s_preview` / `s2c_preview`. - - Payloads above `PreviewBytes` keep the leading `PreviewBytes` inline and spill the captured bytes (up to `MaxBytesPerDir`) into `_blobs/sha256/` with references registered in `blob_refs` under the stream's `tcp-*` ID. + - Payloads above `PreviewBytes` keep the leading `PreviewBytes` inline and spill the captured bytes (up to `MaxBytesPerDir`) atomically (temp file + rename) into `_blobs/sha256/` with references registered in `blob_refs` under the stream's `tcp-*` ID. - Exceeding `MaxBytesPerDir` sets `truncated = 1` while continuing to count exact total relayed `c2s_bytes` / `s2c_bytes`. ## Operational Bounds diff --git a/traffic/raw_tcp.go b/traffic/raw_tcp.go index 6e3854c83..5bb7a7416 100644 --- a/traffic/raw_tcp.go +++ b/traffic/raw_tcp.go @@ -69,7 +69,7 @@ func (t *Traffic) spillRawDir(data []byte, previewCap int) (preview []byte, blob } blobPath := filepath.Join(blobDir, h+".bin") if _, err := os.Stat(blobPath); os.IsNotExist(err) { - if err := os.WriteFile(blobPath, data, 0o644); err != nil { + if err := writeBlobAtomic(blobDir, blobPath, h, data); err != nil { log.Printf("[traffic/socks5] failed to write blob %s: %v", h, err) return append([]byte(nil), data[:previewCap]...), "" } @@ -77,6 +77,45 @@ func (t *Traffic) spillRawDir(data []byte, previewCap int) (preview []byte, blob return append([]byte(nil), data[:previewCap]...), h } +// writeBlobAtomic writes data to a temporary file inside blobDir and renames it +// into place at blobPath so a crash mid-write cannot leave a partial file under +// a content-addressed SHA-256 hash. +func writeBlobAtomic(blobDir, blobPath, hash string, data []byte) error { + tmp, err := os.CreateTemp(blobDir, hash+".*.tmp") + if err != nil { + return err + } + tmpPath := tmp.Name() + cleanup := true + defer func() { + if cleanup { + _ = os.Remove(tmpPath) + } + }() + if _, err := tmp.Write(data); err != nil { + _ = tmp.Close() + return err + } + if err := tmp.Chmod(0o644); err != nil { + _ = tmp.Close() + return err + } + if err := tmp.Close(); err != nil { + return err + } + if err := os.Rename(tmpPath, blobPath); err != nil { + // Another goroutine may have won the race writing the same + // content-addressed hash (notably on Windows where Rename can fail + // if the destination already exists). + if _, statErr := os.Stat(blobPath); statErr == nil { + return nil + } + return err + } + cleanup = false + return nil +} + // RecordRawTCPStream persists one raw TCP connection into raw_tcp_streams and // registers any spilled directional blobs in blob_refs. It never writes to the // HTTP exchanges table. diff --git a/traffic/socks5_test.go b/traffic/socks5_test.go index 956203a33..a705c2cbf 100644 --- a/traffic/socks5_test.go +++ b/traffic/socks5_test.go @@ -7,8 +7,11 @@ import ( "errors" "io" "net" + "os" + "path/filepath" "runtime" "strconv" + "strings" "sync" "sync/atomic" "testing" @@ -841,3 +844,54 @@ func TestSOCKS5ConcurrentStreamsMaxLimitAndIdleTimeout(t *testing.T) { t.Fatalf("want at least 2 recorded concurrent streams, got %d", len(streams)) } } + +func TestRawTCPAtomicBlobSpillConcurrent(t *testing.T) { + tr, err := Open(t.TempDir(), "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer tr.Close() + payload := bytes.Repeat([]byte("ATOMIC-BLOB-"), 128) // 1536 bytes > 64 preview + const workers = 8 + var wg sync.WaitGroup + hashes := make([]string, workers) + for i := 0; i < workers; i++ { + wg.Add(1) + go func(idx int) { + defer wg.Done() + prev, h := tr.spillRawDir(payload, 64) + if len(prev) != 64 { + t.Errorf("worker %d preview len = %d, want 64", idx, len(prev)) + } + hashes[idx] = h + }(i) + } + wg.Wait() + + wantHash := hashes[0] + if wantHash == "" { + t.Fatal("expected non-empty blob hash") + } + for i, h := range hashes { + if h != wantHash { + t.Fatalf("worker %d hash = %q, want %q", i, h, wantHash) + } + } + blobDir := filepath.Join(tr.dir, "_blobs", "sha256", wantHash[:2]) + entries, err := os.ReadDir(blobDir) + if err != nil { + t.Fatalf("ReadDir(%s): %v", blobDir, err) + } + for _, e := range entries { + if strings.HasSuffix(e.Name(), ".tmp") { + t.Fatalf("leftover temp blob file: %s", e.Name()) + } + } + got, err := os.ReadFile(filepath.Join(blobDir, wantHash+".bin")) + if err != nil { + t.Fatalf("ReadFile blob: %v", err) + } + if !bytes.Equal(got, payload) { + t.Fatalf("blob content mismatch: got %d bytes, want %d", len(got), len(payload)) + } +}