mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-17 13:40:53 +08:00
Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
03919e7dd5 | ||
|
|
1263cdc185 | ||
|
|
e770caa28e | ||
|
|
2edf93a0cd | ||
|
|
701a1c92ba | ||
|
|
c4eb079a95 | ||
|
|
212e29cd15 | ||
|
|
ec98975bf3 |
@@ -32,6 +32,7 @@ All notable changes to this project are documented in this file. The format is b
|
||||
|
||||
### Fixed
|
||||
|
||||
- **bbot on arm64**: the image now builds on arm64 hosts such as Apple Silicon. bbot's `gowitness` downloaded an x86-64 Chrome and `retirejs` an x86-64 Node, which can't run there, so the build failed. On arm64 the image uses Debian's `chromium` for gowitness and an arm64 Node for retirejs, and the build check now covers `retirejs` too. amd64 builds are unchanged.
|
||||
- **Auto-scope hygiene**: auto `task_scope` rows count for coverage and untested assets only under a declared root, and direct coverage counts anchored assets through fact anchors only; the scope is described as "coverage scope, not enforced" (#196).
|
||||
- **Enrich HTTP probe**: it now dials through `intercept.PinnedDialer`, so the target gate applies and a probe without a target scope is refused (#198).
|
||||
- **Agent hook data races**: the eight package-level hooks are synchronised, so a second wire-up no longer races tasks that already read them (#227).
|
||||
|
||||
+13
-2
@@ -145,14 +145,25 @@ RUN set -eux; \
|
||||
# preinstalled (--install-all-deps) so scans never need pip at runtime, which
|
||||
# would fail under a program's egress allowlist.
|
||||
ARG BBOT_VERSION=3.0.2
|
||||
# arm64: bbot's Chrome and retirejs's Node are x86-64; use native builds.
|
||||
RUN set -eux; \
|
||||
python -m venv /opt/bbot; \
|
||||
/opt/bbot/bin/pip install --no-cache-dir "bbot==${BBOT_VERSION}"; \
|
||||
ln -s /opt/bbot/bin/bbot /usr/local/bin/bbot; \
|
||||
if [ "${TARGETARCH:-amd64}" = "arm64" ]; then \
|
||||
apt-get update; \
|
||||
apt-get install -y --no-install-recommends chromium; \
|
||||
rm -rf /var/lib/apt/lists/*; \
|
||||
bbot_dir="$(/opt/bbot/bin/python -c 'import bbot, os; print(os.path.dirname(bbot.__file__))')"; \
|
||||
sed -i 's/linux-x64/linux-arm64/g' "$bbot_dir/modules/retirejs.py"; \
|
||||
fi; \
|
||||
bbot -y --install-all-deps; \
|
||||
if [ "${TARGETARCH:-amd64}" = "arm64" ]; then \
|
||||
printf '\nmodules:\n gowitness:\n chrome_path: /usr/bin/chromium\n' >> /root/.config/bbot/bbot.yml; \
|
||||
fi; \
|
||||
bbot --version >/dev/null; \
|
||||
bbot --dry-run -t example.com -m wafw00f,paramminer_getparams,lightfuzz,sslcert,gowitness >/dev/null 2>&1 || true; \
|
||||
bbot --dry-run -t example.com -m wafw00f,paramminer_getparams,lightfuzz,sslcert,gowitness --no-color >/tmp/bbot-dry.log 2>&1 || true; \
|
||||
bbot --dry-run -t example.com -m wafw00f,paramminer_getparams,lightfuzz,sslcert,gowitness,retirejs >/dev/null 2>&1 || true; \
|
||||
bbot --dry-run -t example.com -m wafw00f,paramminer_getparams,lightfuzz,sslcert,gowitness,retirejs --no-color >/tmp/bbot-dry.log 2>&1 || true; \
|
||||
! grep -qE "Installing|ERRR|CRITICAL" /tmp/bbot-dry.log || { cat /tmp/bbot-dry.log; exit 1; }
|
||||
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -826,6 +827,7 @@ func (s *Server) Handler() http.Handler {
|
||||
mux.HandleFunc("GET /api/traffic/stream", s.getTrafficStream)
|
||||
mux.HandleFunc("GET /api/traffic/blob", s.getTrafficBlob)
|
||||
mux.HandleFunc("POST /api/traffic/ingest", s.ingestTraffic)
|
||||
mux.HandleFunc("POST /api/traffic/replay", s.replayTraffic)
|
||||
mux.HandleFunc("GET /api/commands", s.pgListCommands)
|
||||
mux.HandleFunc("GET /api/commands/stats", s.pgToolStats) // aggregate call counts per tool
|
||||
mux.HandleFunc("GET /api/llm/records", s.pgListLLMRecords)
|
||||
@@ -3485,6 +3487,72 @@ func (s *Server) getTrafficStream(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, 200, map[string]any{"items": msgs, "total": total})
|
||||
}
|
||||
|
||||
// maxReplayRequestBytes bounds the replay request body (JSON, with the payload
|
||||
// possibly base64-encoded); traffic.Replay applies its own body limit after
|
||||
// decoding.
|
||||
const maxReplayRequestBytes = 16 << 20
|
||||
|
||||
// replayTraffic sends an edited request through the recording proxy and returns
|
||||
// the response; the proxy records the exchange like any other.
|
||||
//
|
||||
// Operator-only: this is the equivalent of pointing a browser at the proxy, and
|
||||
// no agent tool exposes it. If an agent tool is ever added, it must go through
|
||||
// the target gate and the task's scope like every other outbound request.
|
||||
func (s *Server) replayTraffic(w http.ResponseWriter, r *http.Request) {
|
||||
tr := s.m.Traffic()
|
||||
if tr == nil {
|
||||
writeErr(w, 503, "traffic disabled")
|
||||
return
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxReplayRequestBytes)
|
||||
var body struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Headers []traffic.ReplayHeader `json:"headers"`
|
||||
Body string `json:"body"`
|
||||
BodyBase64 bool `json:"body_base64"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
var tooLarge *http.MaxBytesError
|
||||
if errors.As(err, &tooLarge) {
|
||||
writeErr(w, 413, "replay request too large")
|
||||
return
|
||||
}
|
||||
writeErr(w, 400, "bad json: "+err.Error())
|
||||
return
|
||||
}
|
||||
payload := []byte(body.Body)
|
||||
if body.BodyBase64 {
|
||||
decoded, err := base64.StdEncoding.DecodeString(body.Body)
|
||||
if err != nil {
|
||||
writeErr(w, 400, "body is not valid base64: "+err.Error())
|
||||
return
|
||||
}
|
||||
payload = decoded
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
|
||||
defer cancel()
|
||||
res, err := tr.Replay(ctx, traffic.ReplayRequest{Method: body.Method, URL: body.URL, Headers: body.Headers, Body: payload})
|
||||
switch {
|
||||
case errors.Is(err, traffic.ErrReplayTooLarge):
|
||||
writeErr(w, 413, err.Error())
|
||||
return
|
||||
case errors.Is(err, traffic.ErrReplayInvalid):
|
||||
writeErr(w, 400, err.Error())
|
||||
return
|
||||
case errors.Is(err, context.DeadlineExceeded):
|
||||
writeErr(w, 504, "replay timed out")
|
||||
return
|
||||
case err != nil:
|
||||
writeErr(w, 502, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{
|
||||
"status": res.Status, "headers": res.Headers, "raw": res.Raw, "duration_ms": res.DurationMS,
|
||||
"truncated": res.Truncated, "recorded": res.Recorded,
|
||||
})
|
||||
}
|
||||
|
||||
// getTrafficBlob streams one oversized body by its sha256. Bodies past the inline
|
||||
// threshold are not carried by the exchange endpoint — it returns a preview plus
|
||||
// an "@blob sha256:<hash>" pointer — so this is how the UI fetches them whole.
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Autumn-27/artex/traffic"
|
||||
)
|
||||
|
||||
// replayServer wires a Server like ingestServer. With traffic off,
|
||||
// s.m.Traffic() is nil. With trafficOn but not live, the store is open and the
|
||||
// proxy is not listening. With live, NewManager opens the store and starts the
|
||||
// recording proxy on a free loopback port, as in production; the proxy has no
|
||||
// exported stop, so it keeps listening until the test binary exits.
|
||||
func replayServer(t *testing.T, trafficOn, live bool) (*Server, func(body string) *httptest.ResponseRecorder) {
|
||||
t.Helper()
|
||||
skipWithoutDSN(t)
|
||||
proxyAddr := ""
|
||||
if live {
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
proxyAddr = ln.Addr().String()
|
||||
ln.Close()
|
||||
}
|
||||
m, err := NewManager(t.TempDir(), proxyAddr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { m.Close() })
|
||||
if live {
|
||||
if m.traffic == nil {
|
||||
t.Fatal("NewManager did not open the traffic store")
|
||||
}
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
c, err := net.Dial("tcp", proxyAddr)
|
||||
if err == nil {
|
||||
c.Close()
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("proxy did not listen on %s: %v", proxyAddr, err)
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
} else if trafficOn {
|
||||
if m.traffic, err = traffic.Open(filepath.Join(m.dir, "traffic"), ":0"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
s := New(ctx, m, t.TempDir(), t.TempDir(), t.TempDir())
|
||||
s.archiveWG.Wait()
|
||||
<-s.side.done
|
||||
token, err := signJWT(s.jwtKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return s, func(body string) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest("POST", "/api/traffic/replay", strings.NewReader(body))
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
s.Handler().ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrafficReplayRouteTrafficOff(t *testing.T) {
|
||||
_, replay := replayServer(t, false, false)
|
||||
if w := replay(`{"method":"GET","url":"http://example.com/"}`); w.Code != 503 {
|
||||
t.Fatalf("traffic off = %d %s, want 503", w.Code, w.Body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrafficReplayRouteValidation(t *testing.T) {
|
||||
_, replay := replayServer(t, true, false)
|
||||
for name, c := range map[string]struct {
|
||||
body string
|
||||
want int
|
||||
}{
|
||||
"bad json": {`{"method":`, 400},
|
||||
"CONNECT": {`{"method":"CONNECT","url":"http://example.com/"}`, 400},
|
||||
"ftp url": {`{"method":"GET","url":"ftp://example.com/x"}`, 400},
|
||||
"no host": {`{"method":"GET","url":"http:///x"}`, 400},
|
||||
"bad base64": {`{"method":"POST","url":"http://example.com/","body":"%%%","body_base64":true}`, 400},
|
||||
"header CRLF": {`{"method":"GET","url":"http://example.com/","headers":[{"name":"X-A","value":"a\r\nb"}]}`, 400},
|
||||
"too large": {`{"method":"POST","url":"http://example.com/","body":"` + strings.Repeat("a", 17<<20) + `"}`, 413},
|
||||
"proxy down": {`{"method":"GET","url":"http://127.0.0.1:1/"}`, 502},
|
||||
"body too big": {`{"method":"POST","url":"http://example.com/","body":"` + base64.StdEncoding.EncodeToString(make([]byte, 9<<20)) + `","body_base64":true}`, 413},
|
||||
} {
|
||||
if w := replay(c.body); w.Code != c.want {
|
||||
t.Errorf("%s: %d %s, want %d", name, w.Code, strings.TrimSpace(w.Body.String()), c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrafficReplayRouteEndToEnd(t *testing.T) {
|
||||
s, replay := replayServer(t, true, true)
|
||||
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
w.Header().Set("X-Seen", r.Method+" "+r.Header.Get("X-Custom"))
|
||||
_, _ = w.Write(append([]byte("got:"), b...))
|
||||
}))
|
||||
t.Cleanup(target.Close)
|
||||
url := target.URL + "/replayed?x=1"
|
||||
req, _ := json.Marshal(map[string]any{
|
||||
"method": "PUT", "url": url,
|
||||
"headers": []map[string]string{{"name": "X-Custom", "value": "edited"}},
|
||||
"body": base64.StdEncoding.EncodeToString([]byte("payload")), "body_base64": true,
|
||||
})
|
||||
w := replay(string(req))
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("replay = %d %s", w.Code, w.Body)
|
||||
}
|
||||
var res struct {
|
||||
Status int `json:"status"`
|
||||
Headers []traffic.ReplayHeader `json:"headers"`
|
||||
Raw string `json:"raw"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
Truncated bool `json:"truncated"`
|
||||
Recorded bool `json:"recorded"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res.Status != 200 || res.Truncated || !res.Recorded || !strings.Contains(res.Raw, "X-Seen: PUT edited") || !strings.HasSuffix(res.Raw, "got:payload") {
|
||||
t.Fatalf("result %+v", res)
|
||||
}
|
||||
seen := false
|
||||
for _, h := range res.Headers {
|
||||
seen = seen || (h.Name == "X-Seen" && h.Value == "PUT edited")
|
||||
}
|
||||
if !seen {
|
||||
t.Fatalf("headers %+v lack X-Seen", res.Headers)
|
||||
}
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
var n int
|
||||
if err := s.m.Traffic().DB().QueryRow(`SELECT COUNT(*) FROM exchanges WHERE method='PUT' AND url=?`, url).Scan(&n); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n == 1 {
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("replayed exchange not recorded (%d rows)", n)
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
package traffic
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Replay sends an edited HTTP request through the local recording proxy, so the
|
||||
// exchange is captured by the proxy's sink like any other traffic and shows up
|
||||
// in the Traffic list. It is driven only by an operator (POST /api/traffic/replay).
|
||||
|
||||
const (
|
||||
// maxReplayBody bounds the request body an operator can replay.
|
||||
maxReplayBody = 8 * 1024 * 1024
|
||||
// maxReplayResponse bounds the response body returned to the caller; the
|
||||
// recorded exchange keeps whatever the proxy captured.
|
||||
maxReplayResponse = 2 * 1024 * 1024
|
||||
// maxReplayHeaders bounds the number of request header lines.
|
||||
maxReplayHeaders = 200
|
||||
)
|
||||
|
||||
// ErrReplayInvalid marks a replay request rejected before anything is sent.
|
||||
var ErrReplayInvalid = errors.New("invalid replay request")
|
||||
|
||||
// ErrReplayTooLarge marks a request body over maxReplayBody.
|
||||
var ErrReplayTooLarge = errors.New("replay body too large")
|
||||
|
||||
// ReplayHeader is one request header line, kept as a list so duplicates and
|
||||
// their order survive.
|
||||
type ReplayHeader struct {
|
||||
Name string `json:"name"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
// ReplayRequest is the edited request to send.
|
||||
type ReplayRequest struct {
|
||||
Method string
|
||||
URL string
|
||||
Headers []ReplayHeader
|
||||
Body []byte
|
||||
}
|
||||
|
||||
// ReplayResult is the response as received by the client side of the proxy.
|
||||
type ReplayResult struct {
|
||||
Status int
|
||||
// Headers are the response headers, keys sorted, every value kept.
|
||||
Headers []ReplayHeader
|
||||
Raw string // status line, headers and (possibly truncated) body
|
||||
DurationMS int64
|
||||
Truncated bool // the body exceeded maxReplayResponse and was cut
|
||||
// Recorded reports whether the proxy records this exchange: false for an
|
||||
// https host the proxy tunnels without interception (passthrough).
|
||||
Recorded bool
|
||||
}
|
||||
|
||||
// replayDropped are the headers the client must not send as given: hop-by-hop
|
||||
// fields and framing that net/http recomputes (Content-Length,
|
||||
// Transfer-Encoding). Headers named in Connection are dropped too. Upgrade is
|
||||
// rejected rather than dropped, see Replay.
|
||||
var replayDropped = map[string]bool{
|
||||
"Content-Length": true,
|
||||
"Transfer-Encoding": true,
|
||||
"Connection": true,
|
||||
"Proxy-Connection": true,
|
||||
"Proxy-Authorization": true,
|
||||
"Keep-Alive": true,
|
||||
"Te": true,
|
||||
"Trailer": true,
|
||||
"Expect": true,
|
||||
}
|
||||
|
||||
// replayRecorded reports whether the proxy records a request to scheme://host:
|
||||
// plain http is always intercepted, https is unless the host is in passthrough.
|
||||
func (t *Traffic) replayRecorded(scheme, host string) bool {
|
||||
if scheme != "https" {
|
||||
return true
|
||||
}
|
||||
_, tunneled := t.pass.Load(hostOnly(host))
|
||||
return !tunneled
|
||||
}
|
||||
|
||||
func replayInvalid(format string, args ...any) error {
|
||||
return fmt.Errorf("%w: %s", ErrReplayInvalid, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// validHeaderName reports whether name is an RFC 9110 field name (a token).
|
||||
func validHeaderName(name string) bool {
|
||||
if name == "" {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < len(name); i++ {
|
||||
c := name[i]
|
||||
switch {
|
||||
case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9':
|
||||
case strings.IndexByte("!#$%&'*+-.^_`|~", c) >= 0:
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Replay sends req through the recording proxy and returns the response. The
|
||||
// proxy's sink records the exchange; Replay never calls record() itself. A host
|
||||
// the proxy tunnels without interception (passthrough, see maybePassthrough)
|
||||
// still answers, but that exchange is not recorded (Recorded is false).
|
||||
//
|
||||
// Redirects are not followed, no cookie jar is kept, and compression is not
|
||||
// negotiated: the edited headers go out as given, without an Accept-Encoding
|
||||
// added by the transport.
|
||||
func (t *Traffic) Replay(ctx context.Context, req ReplayRequest) (ReplayResult, error) {
|
||||
method := strings.TrimSpace(req.Method)
|
||||
if method == "" || strings.EqualFold(method, http.MethodConnect) {
|
||||
return ReplayResult{}, replayInvalid("method must be set and cannot be CONNECT")
|
||||
}
|
||||
if !validHeaderName(method) {
|
||||
return ReplayResult{}, replayInvalid("method %q is not a valid token", method)
|
||||
}
|
||||
target, err := url.Parse(strings.TrimSpace(req.URL))
|
||||
if err != nil {
|
||||
return ReplayResult{}, replayInvalid("url: %v", err)
|
||||
}
|
||||
if target.Scheme != "http" && target.Scheme != "https" {
|
||||
return ReplayResult{}, replayInvalid("url must be absolute http or https")
|
||||
}
|
||||
if target.Host == "" || target.Hostname() == "" {
|
||||
return ReplayResult{}, replayInvalid("url has no host")
|
||||
}
|
||||
if target.User != nil {
|
||||
return ReplayResult{}, replayInvalid("url must not carry user info; send an Authorization header instead")
|
||||
}
|
||||
if len(req.Body) > maxReplayBody {
|
||||
return ReplayResult{}, fmt.Errorf("%w: body is %d bytes, over the %d-byte limit", ErrReplayTooLarge, len(req.Body), maxReplayBody)
|
||||
}
|
||||
if len(req.Headers) > maxReplayHeaders {
|
||||
return ReplayResult{}, replayInvalid("%d headers, over the limit of %d", len(req.Headers), maxReplayHeaders)
|
||||
}
|
||||
|
||||
// Headers named in Connection are hop-by-hop too (RFC 9110 §7.6.1).
|
||||
connectionNamed := map[string]bool{}
|
||||
for _, h := range req.Headers {
|
||||
if http.CanonicalHeaderKey(strings.TrimSpace(h.Name)) != "Connection" {
|
||||
continue
|
||||
}
|
||||
for _, token := range strings.Split(h.Value, ",") {
|
||||
if token = strings.TrimSpace(token); token != "" {
|
||||
connectionNamed[http.CanonicalHeaderKey(token)] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
header := http.Header{}
|
||||
host := ""
|
||||
for _, h := range req.Headers {
|
||||
name := strings.TrimSpace(h.Name)
|
||||
if !validHeaderName(name) {
|
||||
return ReplayResult{}, replayInvalid("invalid header name %q", h.Name)
|
||||
}
|
||||
if strings.ContainsAny(h.Value, "\r\n\x00") {
|
||||
return ReplayResult{}, replayInvalid("header %s contains a line break", name)
|
||||
}
|
||||
canonical := http.CanonicalHeaderKey(name)
|
||||
switch {
|
||||
case canonical == "Upgrade":
|
||||
// A protocol upgrade (WebSocket, h2c) cannot be replayed as one request.
|
||||
return ReplayResult{}, replayInvalid("Upgrade requests cannot be replayed")
|
||||
case canonical == "Host":
|
||||
// A Host override is refused because the URL and the destination diverge.
|
||||
// For plain http, net/http puts Request.Host in the absolute-form request
|
||||
// target it sends to the proxy, so the proxy dials the Host value and ignores
|
||||
// the URL host. For https, the CONNECT tunnel stays on the URL host but
|
||||
// go-mitmproxy records the exchange under the inner Host (attacker.ServeHTTP
|
||||
// sets URL.Host from req.Host), so the recorded URL is not the host contacted.
|
||||
// Either way the replay would not do what the URL says; edit the URL instead.
|
||||
host = strings.TrimSpace(h.Value)
|
||||
if !strings.EqualFold(host, target.Host) {
|
||||
return ReplayResult{}, replayInvalid("Host %q differs from the URL host %q; the replay would contact or record a different host than the URL says, so edit the URL instead", host, target.Host)
|
||||
}
|
||||
case replayDropped[canonical], connectionNamed[canonical]:
|
||||
// recomputed by net/http, or hop-by-hop
|
||||
default:
|
||||
header.Add(canonical, h.Value)
|
||||
}
|
||||
}
|
||||
if _, ok := header["User-Agent"]; !ok {
|
||||
// An empty value keeps net/http from adding its default User-Agent: a
|
||||
// header the operator removed stays removed.
|
||||
header["User-Agent"] = []string{""}
|
||||
}
|
||||
|
||||
client, closeIdle, err := t.replayClient()
|
||||
if err != nil {
|
||||
return ReplayResult{}, err
|
||||
}
|
||||
defer closeIdle()
|
||||
|
||||
var body io.Reader
|
||||
if len(req.Body) > 0 {
|
||||
body = bytes.NewReader(req.Body)
|
||||
}
|
||||
httpReq, err := http.NewRequestWithContext(ctx, method, target.String(), body)
|
||||
if err != nil {
|
||||
return ReplayResult{}, replayInvalid("%v", err)
|
||||
}
|
||||
httpReq.Header = header
|
||||
if host != "" {
|
||||
httpReq.Host = host
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
resp, err := client.Do(httpReq)
|
||||
if err != nil {
|
||||
// errors.Is(err, context.DeadlineExceeded) stays true through *url.Error.
|
||||
return ReplayResult{}, fmt.Errorf("replay %s %s: %w", method, target.Redacted(), err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
data, err := io.ReadAll(io.LimitReader(resp.Body, maxReplayResponse+1))
|
||||
if err != nil {
|
||||
return ReplayResult{}, fmt.Errorf("replay %s %s: reading response: %w", method, target.Redacted(), err)
|
||||
}
|
||||
res := ReplayResult{Status: resp.StatusCode, Recorded: t.replayRecorded(target.Scheme, target.Host)}
|
||||
keys := make([]string, 0, len(resp.Header))
|
||||
for k := range resp.Header {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
res.Headers = make([]ReplayHeader, 0, len(keys))
|
||||
for _, k := range keys {
|
||||
for _, v := range resp.Header[k] {
|
||||
res.Headers = append(res.Headers, ReplayHeader{Name: k, Value: v})
|
||||
}
|
||||
}
|
||||
if len(data) > maxReplayResponse {
|
||||
data = data[:maxReplayResponse]
|
||||
res.Truncated = true
|
||||
}
|
||||
res.DurationMS = time.Since(start).Milliseconds()
|
||||
res.Raw = fmt.Sprintf("%s %s\n%s\n%s", resp.Proto, resp.Status, headerLines(resp.Header), data)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// replayClient builds a one-shot client that goes through the local proxy and
|
||||
// trusts its CA. The system roots stay trusted too, so a passthrough host (whose
|
||||
// real certificate reaches the client) still verifies.
|
||||
func (t *Traffic) replayClient() (*http.Client, func(), error) {
|
||||
proxyURL, err := url.Parse(t.ProxyAddr())
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("proxy address: %w", err)
|
||||
}
|
||||
if proxyURL.Hostname() == "0.0.0.0" {
|
||||
proxyURL.Host = "127.0.0.1:" + proxyURL.Port()
|
||||
}
|
||||
pem, err := os.ReadFile(t.CACertPath())
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("proxy CA: %w", err)
|
||||
}
|
||||
roots, err := x509.SystemCertPool()
|
||||
if err != nil || roots == nil {
|
||||
roots = x509.NewCertPool()
|
||||
}
|
||||
if !roots.AppendCertsFromPEM(pem) {
|
||||
return nil, nil, fmt.Errorf("proxy CA: no certificate in %s", t.CACertPath())
|
||||
}
|
||||
transport := &http.Transport{
|
||||
Proxy: http.ProxyURL(proxyURL),
|
||||
TLSClientConfig: &tls.Config{RootCAs: roots, MinVersion: tls.VersionTLS12},
|
||||
DisableCompression: true,
|
||||
}
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
|
||||
}
|
||||
return client, transport.CloseIdleConnections, nil
|
||||
}
|
||||
@@ -0,0 +1,265 @@
|
||||
package traffic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// echoed is what the echo target saw.
|
||||
type echoed struct {
|
||||
Method string `json:"method"`
|
||||
Path string `json:"path"`
|
||||
Query string `json:"query"`
|
||||
Custom string `json:"custom"`
|
||||
Connection string `json:"connection"`
|
||||
ContentLength int64 `json:"content_length"`
|
||||
Body string `json:"body"`
|
||||
Host string `json:"host"`
|
||||
XHop string `json:"x_hop"`
|
||||
ProxyAuth string `json:"proxy_auth"`
|
||||
}
|
||||
|
||||
func echoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("X-Echo", "yes")
|
||||
_ = json.NewEncoder(w).Encode(echoed{
|
||||
Method: r.Method, Path: r.URL.Path, Query: r.URL.RawQuery, Custom: r.Header.Get("X-Custom"),
|
||||
Connection: r.Header.Get("Connection"), ContentLength: r.ContentLength, Body: string(b), Host: r.Host,
|
||||
XHop: r.Header.Get("X-Hop"), ProxyAuth: r.Header.Get("Proxy-Authorization"),
|
||||
})
|
||||
}
|
||||
|
||||
// replayBody returns the JSON body of a replayed echo response.
|
||||
func replayBody(t *testing.T, res ReplayResult) echoed {
|
||||
t.Helper()
|
||||
i := strings.Index(res.Raw, "\n{")
|
||||
if i < 0 {
|
||||
t.Fatalf("no JSON body in raw response:\n%s", res.Raw)
|
||||
}
|
||||
var e echoed
|
||||
if err := json.Unmarshal([]byte(strings.TrimSpace(res.Raw[i+1:])), &e); err != nil {
|
||||
t.Fatalf("decode echo: %v in %q", err, res.Raw)
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func recordedExchanges(t *testing.T, tr *Traffic, method, url string) int {
|
||||
t.Helper()
|
||||
var n int
|
||||
if err := tr.DB().QueryRow(`SELECT COUNT(*) FROM exchanges WHERE method=? AND url=?`, method, url).Scan(&n); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func TestReplayThroughProxy(t *testing.T) {
|
||||
tr, _, _ := startProxy(t)
|
||||
plain := httptest.NewServer(http.HandlerFunc(echoHandler))
|
||||
t.Cleanup(plain.Close)
|
||||
tlsSrv := httptest.NewTLSServer(http.HandlerFunc(echoHandler))
|
||||
t.Cleanup(tlsSrv.Close)
|
||||
|
||||
for _, tc := range []struct{ name, base string }{
|
||||
{"http", plain.URL},
|
||||
{"https", localhostURL(tlsSrv.URL)}, // trusted through the proxy CA
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
target := tc.base + "/edited/path?q=1&r=two"
|
||||
res, err := tr.Replay(t.Context(), ReplayRequest{
|
||||
Method: "PATCH",
|
||||
URL: target,
|
||||
Headers: []ReplayHeader{{Name: "X-Custom", Value: "edited"}, {Name: "Content-Type", Value: "text/plain"}},
|
||||
Body: []byte("edited body"),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res.Status != 200 || res.Truncated || !res.Recorded || !strings.HasPrefix(res.Raw, "HTTP/1.1 200 OK\n") || !strings.Contains(res.Raw, "X-Echo: yes") {
|
||||
t.Fatalf("result status=%d truncated=%v recorded=%v raw:\n%s", res.Status, res.Truncated, res.Recorded, res.Raw)
|
||||
}
|
||||
echoHeader := false
|
||||
for i, h := range res.Headers {
|
||||
if i > 0 && res.Headers[i-1].Name > h.Name {
|
||||
t.Fatalf("headers not sorted: %+v", res.Headers)
|
||||
}
|
||||
echoHeader = echoHeader || (h.Name == "X-Echo" && h.Value == "yes")
|
||||
}
|
||||
if !echoHeader {
|
||||
t.Fatalf("headers %+v lack X-Echo: yes", res.Headers)
|
||||
}
|
||||
got := replayBody(t, res)
|
||||
if got.Method != "PATCH" || got.Path != "/edited/path" || got.Query != "q=1&r=two" || got.Custom != "edited" || got.Body != "edited body" {
|
||||
t.Fatalf("target received %+v", got)
|
||||
}
|
||||
waitFor(t, "replayed exchange recorded", func() bool { return recordedExchanges(t, tr, "PATCH", target) == 1 })
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplayDropsHopByHopHeaders(t *testing.T) {
|
||||
tr, _, _ := startProxy(t)
|
||||
srv := httptest.NewServer(http.HandlerFunc(echoHandler))
|
||||
t.Cleanup(srv.Close)
|
||||
res, err := tr.Replay(t.Context(), ReplayRequest{
|
||||
Method: "POST",
|
||||
URL: srv.URL + "/hop",
|
||||
Headers: []ReplayHeader{
|
||||
{Name: "Connection", Value: "close, X-Hop"},
|
||||
{Name: "X-Hop", Value: "v"},
|
||||
{Name: "Proxy-Authorization", Value: "Basic dXNlcjpwYXNz"},
|
||||
{Name: "Content-Length", Value: "999"},
|
||||
{Name: "Keep-Alive", Value: "timeout=5"},
|
||||
{Name: "Host", Value: strings.TrimPrefix(srv.URL, "http://")},
|
||||
},
|
||||
Body: []byte("abc"),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := replayBody(t, res)
|
||||
if got.Connection != "" || got.ContentLength != 3 || got.Body != "abc" {
|
||||
t.Fatalf("target received connection=%q content_length=%d body=%q, want no Connection and a recomputed length", got.Connection, got.ContentLength, got.Body)
|
||||
}
|
||||
if got.XHop != "" || got.ProxyAuth != "" {
|
||||
t.Fatalf("target received X-Hop=%q Proxy-Authorization=%q, want neither", got.XHop, got.ProxyAuth)
|
||||
}
|
||||
if got.Host != strings.TrimPrefix(srv.URL, "http://") {
|
||||
t.Fatalf("target saw Host %q", got.Host)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplayDoesNotFollowRedirects(t *testing.T) {
|
||||
tr, _, _ := startProxy(t)
|
||||
var followed atomic.Bool
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/next" {
|
||||
followed.Store(true)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/next", http.StatusFound)
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
res, err := tr.Replay(t.Context(), ReplayRequest{Method: "GET", URL: srv.URL + "/start"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res.Status != 302 || !strings.Contains(res.Raw, "Location: /next") || followed.Load() {
|
||||
t.Fatalf("status %d followed=%v raw:\n%s", res.Status, followed.Load(), res.Raw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplayTruncatesLargeResponse(t *testing.T) {
|
||||
tr, _, _ := startProxy(t)
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = io.WriteString(w, strings.Repeat("x", maxReplayResponse+100))
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
res, err := tr.Replay(t.Context(), ReplayRequest{Method: "GET", URL: srv.URL + "/big"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
head, body, _ := strings.Cut(res.Raw, "\n\n")
|
||||
if !res.Truncated || len(body) != maxReplayResponse || head == "" {
|
||||
t.Fatalf("truncated=%v body %d bytes, want %d", res.Truncated, len(body), maxReplayResponse)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplayTimeoutIsRecognizable(t *testing.T) {
|
||||
tr, _, _ := startProxy(t)
|
||||
release := make(chan struct{})
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
select {
|
||||
case <-release:
|
||||
case <-time.After(5 * time.Second):
|
||||
}
|
||||
}))
|
||||
t.Cleanup(func() { close(release); srv.Close() })
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 200*time.Millisecond)
|
||||
defer cancel()
|
||||
_, err := tr.Replay(ctx, ReplayRequest{Method: "GET", URL: srv.URL + "/slow"})
|
||||
if !errors.Is(err, context.DeadlineExceeded) || errors.Is(err, ErrReplayInvalid) {
|
||||
t.Fatalf("err = %v, want a deadline error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A passthrough host is tunneled, not intercepted: the client sees the target's
|
||||
// own certificate (here an untrusted httptest one, so the TLS handshake fails)
|
||||
// and nothing is recorded. Pins the documented limitation.
|
||||
func TestReplayPassthroughHostIsNotIntercepted(t *testing.T) {
|
||||
tr, _, _ := startProxy(t)
|
||||
srv := httptest.NewTLSServer(http.HandlerFunc(echoHandler))
|
||||
t.Cleanup(srv.Close)
|
||||
tr.pass.Store("localhost", struct{}{})
|
||||
target := localhostURL(srv.URL) + "/tunneled"
|
||||
_, err := tr.Replay(t.Context(), ReplayRequest{Method: "GET", URL: target})
|
||||
if err == nil || !strings.Contains(err.Error(), "certificate") {
|
||||
t.Fatalf("err = %v, want the target's own certificate to be rejected", err)
|
||||
}
|
||||
if n := recordedExchanges(t, tr, "GET", target); n != 0 {
|
||||
t.Fatalf("%d exchanges recorded for a passthrough host", n)
|
||||
}
|
||||
}
|
||||
|
||||
// Validation happens before anything is sent, so the proxy need not run.
|
||||
func TestReplayRejectsInvalidRequests(t *testing.T) {
|
||||
tr, err := Open(t.TempDir(), "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = tr.Close() })
|
||||
many := make([]ReplayHeader, maxReplayHeaders+1)
|
||||
for i := range many {
|
||||
many[i] = ReplayHeader{Name: fmt.Sprintf("X-H%d", i), Value: "v"}
|
||||
}
|
||||
for name, req := range map[string]ReplayRequest{
|
||||
"ftp scheme": {Method: "GET", URL: "ftp://example.com/x"},
|
||||
"relative url": {Method: "GET", URL: "/x"},
|
||||
"no host": {Method: "GET", URL: "http:///x"},
|
||||
"user info": {Method: "GET", URL: "http://user:pass@example.com/"},
|
||||
"empty method": {Method: " ", URL: "http://example.com/"},
|
||||
"CONNECT": {Method: "CONNECT", URL: "http://example.com/"},
|
||||
"method not token": {Method: "GE T", URL: "http://example.com/"},
|
||||
"too many headers": {Method: "GET", URL: "http://example.com/", Headers: many},
|
||||
"CR in header": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "X-A", Value: "a\r\nX-Injected: 1"}}},
|
||||
"LF in header": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "X-A", Value: "a\nb"}}},
|
||||
"invalid name": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "Bad Name", Value: "v"}}},
|
||||
"upgrade websocket": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "Upgrade", Value: "websocket"}}},
|
||||
"host differs": {Method: "GET", URL: "http://example.com/", Headers: []ReplayHeader{{Name: "Host", Value: "other.example"}}},
|
||||
} {
|
||||
if _, err := tr.Replay(t.Context(), req); !errors.Is(err, ErrReplayInvalid) {
|
||||
t.Errorf("%s: err = %v, want ErrReplayInvalid", name, err)
|
||||
}
|
||||
}
|
||||
_, err = tr.Replay(t.Context(), ReplayRequest{Method: "POST", URL: "http://example.com/", Body: make([]byte, maxReplayBody+1)})
|
||||
if !errors.Is(err, ErrReplayTooLarge) || errors.Is(err, ErrReplayInvalid) {
|
||||
t.Errorf("body too large: err = %v, want ErrReplayTooLarge", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Only an https host in passthrough is reported as not recorded.
|
||||
func TestReplayRecordedHelper(t *testing.T) {
|
||||
tr := &Traffic{}
|
||||
tr.pass.Store("localhost", struct{}{})
|
||||
for _, c := range []struct {
|
||||
scheme, host string
|
||||
want bool
|
||||
}{
|
||||
{"https", "localhost:443", false},
|
||||
{"https", "example.com:443", true},
|
||||
{"http", "localhost:80", true},
|
||||
} {
|
||||
if got := tr.replayRecorded(c.scheme, c.host); got != c.want {
|
||||
t.Errorf("replayRecorded(%s, %s) = %v, want %v", c.scheme, c.host, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
"use client";
|
||||
|
||||
import * as React from "react";
|
||||
|
||||
import { Loader2Icon, RefreshCwIcon } from "lucide-react";
|
||||
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { api } from "@/lib/api";
|
||||
import type { TrafficStreamMessage } from "@/lib/types";
|
||||
|
||||
const PAGE_SIZE = 50;
|
||||
// Binary payloads loaded in full are shown as hex of their first 64 KiB.
|
||||
const HEX_PREVIEW_BYTES = 64 * 1024;
|
||||
|
||||
function fmtBytes(n: number) {
|
||||
if (n <= 0) return "0 B";
|
||||
const units = ["B", "KB", "MB", "GB"];
|
||||
const i = Math.min(Math.floor(Math.log(n) / Math.log(1024)), units.length - 1);
|
||||
const v = n / 1024 ** i;
|
||||
return `${i === 0 ? v : v.toFixed(1)} ${units[i]}`;
|
||||
}
|
||||
|
||||
function fmtDirection(direction: string) {
|
||||
if (direction === "c2s") return "client -> server";
|
||||
if (direction === "s2c") return "server -> client";
|
||||
return direction;
|
||||
}
|
||||
|
||||
// Hex dump: 16 bytes per line, offset first.
|
||||
function hexDump(bytes: Uint8Array) {
|
||||
const lines: string[] = [];
|
||||
for (let i = 0; i < bytes.length; i += 16) {
|
||||
const row = Array.from(bytes.subarray(i, i + 16), (b) => b.toString(16).padStart(2, "0")).join(" ");
|
||||
lines.push(`${i.toString(16).padStart(8, "0")} ${row}`);
|
||||
}
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
type FullPayload = { loading: boolean; text?: string; error?: string };
|
||||
|
||||
const payloadClass =
|
||||
"max-h-64 overflow-auto rounded-md bg-muted/50 p-2 font-mono text-xs break-all whitespace-pre-wrap";
|
||||
|
||||
function StreamMessageRow({ message }: { message: TrafficStreamMessage }) {
|
||||
const [full, setFull] = React.useState<FullPayload | null>(null);
|
||||
const binary = message.type === "binary";
|
||||
|
||||
const loadFull = () => {
|
||||
if (!message.blob) return;
|
||||
setFull({ loading: true });
|
||||
api
|
||||
.trafficBlob(message.blob)
|
||||
.then((bytes) => {
|
||||
const text = binary
|
||||
? `${hexDump(bytes.subarray(0, HEX_PREVIEW_BYTES))}\n(${bytes.length} bytes total)`
|
||||
: new TextDecoder().decode(bytes);
|
||||
setFull({ loading: false, text });
|
||||
})
|
||||
.catch((e: Error) => setFull({ loading: false, error: e.message }));
|
||||
};
|
||||
|
||||
if (message.type === "meta") {
|
||||
return (
|
||||
<div className="mx-5 my-3 space-y-1 rounded-md border border-amber-500/40 bg-amber-500/10 p-3">
|
||||
<div className="font-medium text-amber-700 text-xs dark:text-amber-400">
|
||||
#{message.seq} Recording limit reached: later messages were not stored
|
||||
</div>
|
||||
<pre className={payloadClass}>{message.payload}</pre>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-1.5 border-b px-5 py-3 last:border-b-0">
|
||||
<div className="flex flex-wrap items-center gap-2 text-xs">
|
||||
<span className="font-mono text-muted-foreground tabular-nums">#{message.seq}</span>
|
||||
<Badge variant="outline">{fmtDirection(message.direction)}</Badge>
|
||||
<Badge variant="secondary">{message.type}</Badge>
|
||||
{binary && message.encoding === "base64" && <Badge variant="outline">base64</Badge>}
|
||||
{message.truncated && <Badge variant="destructive">truncated</Badge>}
|
||||
{message.event && <span className="font-mono">event: {message.event}</span>}
|
||||
{message.event_id && <span className="font-mono">id: {message.event_id}</span>}
|
||||
{message.retry ? <span className="font-mono">retry: {message.retry}</span> : null}
|
||||
<span className="ml-auto text-muted-foreground tabular-nums">
|
||||
{fmtBytes(message.size)} · {new Date(message.ts * 1000).toLocaleTimeString()}
|
||||
</span>
|
||||
</div>
|
||||
<pre className={payloadClass}>{full?.text ?? message.payload}</pre>
|
||||
{message.blob && (
|
||||
<div className="flex flex-wrap items-center gap-2 text-muted-foreground text-xs">
|
||||
<span className="break-all font-mono">blob {message.blob}</span>
|
||||
{!full?.text && (
|
||||
<Button variant="outline" size="xs" disabled={full?.loading} onClick={loadFull}>
|
||||
{full?.loading && <Loader2Icon className="animate-spin" />}
|
||||
Load full payload
|
||||
</Button>
|
||||
)}
|
||||
{full?.error && <span className="text-destructive">{full.error}</span>}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// StreamMessagesPanel lists the messages of one WebSocket or SSE exchange, a
|
||||
// page at a time. It does not poll: Refresh reloads from the start.
|
||||
export function StreamMessagesPanel({ exchangeId, kind }: { exchangeId: string; kind: "websocket" | "sse" }) {
|
||||
const [items, setItems] = React.useState<TrafficStreamMessage[]>([]);
|
||||
const [total, setTotal] = React.useState(0);
|
||||
const [loading, setLoading] = React.useState(true);
|
||||
const [loadingMore, setLoadingMore] = React.useState(false);
|
||||
const [error, setError] = React.useState<string | null>(null);
|
||||
// Answers for an older exchange (or an older refresh) are dropped.
|
||||
const generation = React.useRef(0);
|
||||
|
||||
const load = React.useCallback(
|
||||
(offset: number) => {
|
||||
const gen = offset === 0 ? ++generation.current : generation.current;
|
||||
if (offset === 0) {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
} else {
|
||||
setLoadingMore(true);
|
||||
}
|
||||
api
|
||||
.trafficStream(exchangeId, offset, PAGE_SIZE)
|
||||
.then((page) => {
|
||||
if (gen !== generation.current) return;
|
||||
const next = page.items ?? [];
|
||||
setItems((prev) => (offset === 0 ? next : [...prev, ...next]));
|
||||
setTotal(page.total ?? 0);
|
||||
})
|
||||
.catch((e: Error) => {
|
||||
if (gen === generation.current) setError(e.message);
|
||||
})
|
||||
.finally(() => {
|
||||
if (gen !== generation.current) return;
|
||||
setLoading(false);
|
||||
setLoadingMore(false);
|
||||
});
|
||||
},
|
||||
[exchangeId],
|
||||
);
|
||||
|
||||
React.useEffect(() => {
|
||||
setItems([]);
|
||||
setTotal(0);
|
||||
load(0);
|
||||
}, [load]);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col">
|
||||
<div className="flex items-center gap-2 px-5 py-3 text-muted-foreground text-xs">
|
||||
<span>
|
||||
{kind === "websocket" ? "WebSocket" : "Server-Sent Events"} · {items.length} of {total} messages
|
||||
</span>
|
||||
<Button variant="outline" size="xs" className="ml-auto" disabled={loading} onClick={() => load(0)}>
|
||||
<RefreshCwIcon className={loading ? "animate-spin" : undefined} />
|
||||
Refresh
|
||||
</Button>
|
||||
</div>
|
||||
{renderBody()}
|
||||
</div>
|
||||
);
|
||||
|
||||
function renderBody() {
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="flex items-center gap-2 p-5 text-muted-foreground text-xs">
|
||||
<Loader2Icon className="size-3.5 animate-spin" />
|
||||
Loading messages…
|
||||
</div>
|
||||
);
|
||||
}
|
||||
if (error) {
|
||||
return <div className="p-5 text-destructive text-xs">Failed to load messages: {error}</div>;
|
||||
}
|
||||
if (items.length === 0) {
|
||||
return <div className="p-5 text-muted-foreground text-xs">No messages recorded for this stream.</div>;
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<div className="border-t">
|
||||
{items.map((m) => (
|
||||
<StreamMessageRow key={`${exchangeId}:${m.seq}`} message={m} />
|
||||
))}
|
||||
</div>
|
||||
{items.length < total && (
|
||||
<div className="flex justify-center p-4">
|
||||
<Button variant="outline" size="sm" disabled={loadingMore} onClick={() => load(items.length)}>
|
||||
{loadingMore && <Loader2Icon className="animate-spin" />}
|
||||
Load more
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -42,10 +42,12 @@ import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
||||
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
||||
import { api } from "@/lib/api";
|
||||
import { setVisibleInterval } from "@/lib/polling";
|
||||
import { useStoredSortPreference } from "@/lib/sort-preference";
|
||||
import type { TrafficDetail, TrafficExchange, TrafficHost, TrafficResp } from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { setVisibleInterval } from "@/lib/polling";
|
||||
|
||||
import { StreamMessagesPanel } from "./_components/stream-messages";
|
||||
|
||||
function fmtTime(ts: string) {
|
||||
return new Date(ts).toLocaleString("zh-CN", {
|
||||
@@ -141,6 +143,9 @@ export default function TrafficPage() {
|
||||
const [selected, setSelected] = React.useState<TrafficExchange | null>(null);
|
||||
const [detail, setDetail] = React.useState<TrafficDetail | null>(null);
|
||||
const [detailLoading, setDetailLoading] = React.useState(false);
|
||||
const [detailTab, setDetailTab] = React.useState("request");
|
||||
const streamKind = detail?.stream_kind === "websocket" || detail?.stream_kind === "sse" ? detail.stream_kind : null;
|
||||
const hasStream = streamKind !== null;
|
||||
|
||||
const [hosts, setHosts] = React.useState<TrafficHost[]>([]); // target picker
|
||||
const [selectedHosts, setSelectedHosts] = React.useState<string[]>([]); // checked in picker
|
||||
@@ -298,8 +303,10 @@ export default function TrafficPage() {
|
||||
.finally(() => setDeleting(false));
|
||||
};
|
||||
|
||||
// Lazy-load the raw request/response for the selected exchange.
|
||||
// Lazy-load the raw request/response for the selected exchange. A new
|
||||
// selection opens on Request; a WebSocket/SSE exchange then opens on Stream.
|
||||
React.useEffect(() => {
|
||||
setDetailTab("request");
|
||||
if (!selected) {
|
||||
setDetail(null);
|
||||
return;
|
||||
@@ -310,7 +317,9 @@ export default function TrafficPage() {
|
||||
api
|
||||
.trafficExchange(selected.id)
|
||||
.then((d) => {
|
||||
if (alive) setDetail(d);
|
||||
if (!alive) return;
|
||||
setDetail(d);
|
||||
if (d.stream_kind === "websocket" || d.stream_kind === "sse") setDetailTab("stream");
|
||||
})
|
||||
.catch(() => {
|
||||
if (alive) setDetail({ req: "(failed to load the messages)", resp: "" });
|
||||
@@ -742,10 +751,18 @@ export default function TrafficPage() {
|
||||
<SheetTitle className="break-all font-mono">{selected.host}</SheetTitle>
|
||||
<SheetDescription className="break-all font-mono">{selected.url}</SheetDescription>
|
||||
</SheetHeader>
|
||||
<Tabs defaultValue="request" className="min-h-0 flex-1 gap-0">
|
||||
<TabsList className="mx-5 mt-4 grid w-auto grid-cols-2">
|
||||
<Tabs value={detailTab} onValueChange={setDetailTab} className="min-h-0 flex-1 gap-0">
|
||||
<TabsList className={cn("mx-5 mt-4 grid w-auto", hasStream ? "grid-cols-3" : "grid-cols-2")}>
|
||||
<TabsTrigger value="request">Request</TabsTrigger>
|
||||
<TabsTrigger value="response">Response</TabsTrigger>
|
||||
{hasStream && (
|
||||
<TabsTrigger value="stream">
|
||||
Stream
|
||||
{(detail?.stream_count ?? 0) > 0 && (
|
||||
<span className="text-muted-foreground text-xs tabular-nums">{detail?.stream_count}</span>
|
||||
)}
|
||||
</TabsTrigger>
|
||||
)}
|
||||
</TabsList>
|
||||
<TabsContent value="request" className="min-h-0 overflow-auto">
|
||||
{detailLoading ? (
|
||||
@@ -767,6 +784,11 @@ export default function TrafficPage() {
|
||||
<HttpCodeBlock raw={detail?.resp ?? ""} />
|
||||
)}
|
||||
</TabsContent>
|
||||
{streamKind && (
|
||||
<TabsContent value="stream" className="min-h-0 overflow-auto">
|
||||
<StreamMessagesPanel exchangeId={selected.id} kind={streamKind} />
|
||||
</TabsContent>
|
||||
)}
|
||||
</Tabs>
|
||||
</>
|
||||
)}
|
||||
|
||||
@@ -102,6 +102,7 @@ import type {
|
||||
TrafficEvidenceRole,
|
||||
TrafficHost,
|
||||
TrafficResp,
|
||||
TrafficStreamPage,
|
||||
UsageStats,
|
||||
WorkspaceFile,
|
||||
WorkspaceListing,
|
||||
@@ -816,6 +817,20 @@ export const api = {
|
||||
(opts.order ? `&order=${encodeURIComponent(opts.order)}` : ""),
|
||||
),
|
||||
trafficExchange: (id: string) => get<TrafficDetail>(`/traffic/exchange?id=${encodeURIComponent(id)}`),
|
||||
// One page of a WebSocket/SSE exchange's messages, in order.
|
||||
trafficStream: (id: string, offset = 0, limit = 50) =>
|
||||
get<TrafficStreamPage>(`/traffic/stream?exchange=${encodeURIComponent(id)}&offset=${offset}&limit=${limit}`),
|
||||
// Raw bytes of one stored blob (a full message payload or an oversized body).
|
||||
// The JSON-only `get` cannot carry binary, hence the direct fetch.
|
||||
trafficBlob: async (hash: string) => {
|
||||
if (MOCK) throw new Error("Blob download is not available in mock mode");
|
||||
const token = getToken();
|
||||
const r = await fetch(`/api/traffic/blob?hash=${encodeURIComponent(hash)}`, {
|
||||
headers: { ...(token ? { Authorization: `Bearer ${token}` } : {}) },
|
||||
});
|
||||
if (!r.ok) throw new Error(`Blob download failed: ${r.status}`);
|
||||
return new Uint8Array(await r.arrayBuffer());
|
||||
},
|
||||
trafficHosts: () => get<{ hosts: TrafficHost[] }>(`/traffic/hosts`),
|
||||
trafficDeleteHost: (host: string) => del<{ deleted: number }>(`/traffic?host=${encodeURIComponent(host)}`),
|
||||
trafficDeleteHosts: (hosts: string[]) => del<{ deleted: number }>(`/traffic/hosts`, { hosts }),
|
||||
|
||||
@@ -911,6 +911,34 @@ export interface TrafficResp {
|
||||
export interface TrafficDetail {
|
||||
req: string;
|
||||
resp: string;
|
||||
// WebSocket/SSE exchanges carry their messages separately (trafficStream);
|
||||
// stream_kind is "" for an ordinary exchange.
|
||||
stream_count?: number;
|
||||
stream_kind?: "websocket" | "sse" | "";
|
||||
}
|
||||
|
||||
// One recorded WebSocket message or SSE event. ts is in Unix seconds; seq
|
||||
// orders. payload is a preview of at most 4 KiB (base64 when encoding is
|
||||
// "base64"); blob names the full payload when it was stored separately.
|
||||
export interface TrafficStreamMessage {
|
||||
seq: number;
|
||||
ts: number;
|
||||
direction: "c2s" | "s2c" | "meta" | (string & {});
|
||||
kind: number;
|
||||
type: "text" | "binary" | "event" | "meta" | (string & {});
|
||||
event?: string;
|
||||
event_id?: string;
|
||||
retry?: number;
|
||||
size: number;
|
||||
payload: string;
|
||||
encoding?: string;
|
||||
blob?: string;
|
||||
truncated?: boolean;
|
||||
}
|
||||
|
||||
export interface TrafficStreamPage {
|
||||
items: TrafficStreamMessage[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
// One distinct recorded host with its exchange count (target picker).
|
||||
|
||||
Reference in New Issue
Block a user