mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-19 06:30:54 +08:00
The sink runs the request phase in the Request hook and the response phase at the start of the Response hook; at SSEStart only status and header actions apply. WebSocketStart comes after the client is upgraded, so the handshake is traced "not rewritten: stream" and recorded as sent. Exchanges keep what the client sent and what the target answered. The original of a rewritten request lives in a per-flow snapshot slot (entry or tombstone, 1024 entries / 64 MiB, 4096 tombstones), reserved before the request is changed, released on Response, RequestError, SSEStart, a 5 MiB response, or by a sweep of finished flows (ClientDisconnected, 1 s debounce, 1 min period). Under pressure an entry older than 15 minutes is turned into a tombstone in place, only when that frees room for the new reservation; its exchange is recorded "original request unavailable". record(), recordStreamStart() and insertExchange() clear any trace under the id before INSERT OR REPLACE; the trace (exchange_rewrites, created with CREATE TABLE IF NOT EXISTS) is written in the exchange's transaction, its spilled values registered in blob_refs, and purged with the exchange. The full-text index covers the originals only. Logs of a rewritten flow name the host, the step or error class and the rule ids, never the URL or the error text. A rewritten request's transport error never switches its host to passthrough. traffic_get shows the rewritten flag and rule ids. Without a rule that applies, what is recorded is unchanged byte for byte.
68 lines
2.3 KiB
Go
68 lines
2.3 KiB
Go
package traffic
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/Autumn-27/artex/egress"
|
|
mproxy "github.com/lqqyt2423/go-mitmproxy/proxy"
|
|
)
|
|
|
|
// paceWaitMax bounds the pacing wait of one request. Past it the request is
|
|
// sent unpaced: pacing never blocks a command indefinitely.
|
|
const paceWaitMax = 30 * time.Second
|
|
|
|
// Requestheaders runs for every request the proxy sees: the CONNECT that opens
|
|
// a tunnel, each request inside an intercepted tunnel, and each plain-HTTP
|
|
// request. The recording proxy takes no credentials, so a Proxy-Authorization
|
|
// header is only ever meant for it (clients send the proxy URL's userinfo
|
|
// there): it is dropped here, before anything is forwarded, and never reaches
|
|
// the target or the recording. When it carries the pace marker
|
|
// (egress.PaceUser), the client connection is marked; every request on it,
|
|
// including those inside its tunnel, is then paced by the current noise
|
|
// profile before it goes upstream. Unmarked connections are not paced here.
|
|
func (s *sink) Requestheaders(f *mproxy.Flow) {
|
|
if f.Request == nil || f.ConnContext == nil || f.ConnContext.ClientConn == nil {
|
|
return
|
|
}
|
|
id := f.ConnContext.ClientConn.Id
|
|
if egress.IsPaceMarker(f.Request.Header.Get("Proxy-Authorization")) {
|
|
s.paced.Store(id, struct{}{})
|
|
}
|
|
f.Request.Header.Del("Proxy-Authorization")
|
|
if f.Request.Method == http.MethodConnect || f.Request.URL == nil {
|
|
return
|
|
}
|
|
if _, marked := s.paced.Load(id); marked {
|
|
pace(f)
|
|
}
|
|
}
|
|
|
|
// ClientDisconnected forgets a closed connection's mark, and asks for a sweep
|
|
// of the request snapshots its flows may have left (rewrite_flow.go).
|
|
func (s *sink) ClientDisconnected(c *mproxy.ClientConn) {
|
|
if c != nil {
|
|
s.paced.Delete(c.Id)
|
|
}
|
|
if s.t != nil {
|
|
s.t.snaps.requestSweep()
|
|
}
|
|
}
|
|
|
|
// pace waits for the request's host under the current noise profile, bounded
|
|
// by paceWaitMax and by the request's own context. Off returns at once.
|
|
func pace(f *mproxy.Flow) {
|
|
ctx := context.Background()
|
|
if raw := f.Request.Raw(); raw != nil {
|
|
ctx = raw.Context()
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, paceWaitMax)
|
|
defer cancel()
|
|
host := egress.HostKey(f.Request.URL)
|
|
if err := egress.Wait(ctx, host); err != nil {
|
|
log.Printf("[traffic] pacing a Bash request to %s stopped waiting (%v); sending it unpaced", host, err)
|
|
}
|
|
}
|