Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions traffic/passthrough_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/url"
"testing"
"time"

mproxy "github.com/lqqyt2423/go-mitmproxy/proxy"
)
Expand Down Expand Up @@ -65,4 +66,23 @@ func TestMaybePassthroughFlagsHostOnce(t *testing.T) {
if _, tunnel := tr.pass.Load(hostOnly("target.test:443")); !tunnel {
t.Fatal("flagged host must be recognized for the CONNECT form with port")
}
if !tr.tunneling("target.test") {
t.Fatal("a fresh passthrough mark must tunnel")
}
}

func TestPassthroughMarkExpires(t *testing.T) {
tr := &Traffic{}
host := "expired.example"
tr.pass.Store(host, time.Now().Add(-time.Second))
if tr.tunneling(host) {
t.Fatal("expired passthrough mark still tunnels")
}
if _, ok := tr.pass.Load(host); ok {
t.Fatal("expired mark was kept")
}
tr.pass.Store(host, time.Now().Add(time.Minute))
if !tr.tunneling(host) {
t.Fatal("fresh passthrough mark should tunnel")
}
}
3 changes: 1 addition & 2 deletions traffic/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ func (t *Traffic) replayRecorded(scheme, host string) bool {
if scheme != "https" {
return true
}
_, tunneled := t.pass.Load(hostOnly(host))
return !tunneled
return !t.tunneling(hostOnly(host))
}

func replayInvalid(format string, args ...any) error {
Expand Down
12 changes: 10 additions & 2 deletions traffic/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ func TestReplayPassthroughHostIsNotIntercepted(t *testing.T) {
tr, _, _ := startProxy(t)
srv := httptest.NewTLSServer(http.HandlerFunc(echoHandler))
t.Cleanup(srv.Close)
tr.pass.Store("localhost", struct{}{})
tr.pass.Store("localhost", time.Now().Add(passthroughTTL))
if !tr.tunneling("localhost") {
t.Fatal("passthrough mark was not recognized before the replay")
}
target := localhostURL(srv.URL) + "/tunneled"
_, err := tr.Replay(t.Context(), ReplayRequest{Method: "GET", URL: target})
if err == nil || !strings.Contains(err.Error(), "certificate") {
Expand Down Expand Up @@ -249,14 +252,19 @@ func TestReplayRejectsInvalidRequests(t *testing.T) {
// Only an https host in passthrough is reported as not recorded.
func TestReplayRecordedHelper(t *testing.T) {
tr := &Traffic{}
tr.pass.Store("localhost", struct{}{})
tr.pass.Store("localhost", time.Now().Add(passthroughTTL))
if !tr.tunneling("localhost") {
t.Fatal("fresh passthrough mark was not tunneling")
}
tr.pass.Store("expired.example", time.Now().Add(-time.Second))
for _, c := range []struct {
scheme, host string
want bool
}{
{"https", "localhost:443", false},
{"https", "example.com:443", true},
{"http", "localhost:80", true},
{"https", "expired.example:443", 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)
Expand Down
43 changes: 35 additions & 8 deletions traffic/traffic.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ type Traffic struct {
closeOnce sync.Once
// pass is the set of hosts whose MITM interception failed for a proxy/protocol
// reason; connections to them are tunneled transparently (fail-open) so the
// request still reaches the target — unrecorded — instead of being killed.
pass sync.Map // hostname(string) -> struct{}
// request still reaches the target, unrecorded, instead of being killed.
// The value is the time the mark expires. After that, interception is tried again.
pass sync.Map // hostname(string) -> time.Time
// upstream is the global egress proxy every captured request is forwarded
// through (nil = dial targets directly). Both the intercepted and the
// transparent-passthrough paths honor it (go-mitmproxy's getUpstreamConn),
Expand Down Expand Up @@ -253,8 +254,7 @@ func Open(dir, addr string) (*Traffic, error) {
// can't intercept without breaking (see maybePassthrough). Those are tunneled
// transparently so the request still reaches the target instead of being killed.
p.SetShouldInterceptRule(func(req *http.Request) bool {
_, tunnel := t.pass.Load(hostOnly(req.Host))
return !tunnel
return !t.tunneling(hostOnly(req.Host))
})
p.AddAddon(&sink{t: t})
t.proxy = p
Expand Down Expand Up @@ -439,8 +439,10 @@ func (s *sink) Response(f *mproxy.Flow) {

// RequestError fires when a request through an established MITM tunnel fails. If
// the failure looks proxy/protocol-caused (h2 quirks, HEAD-with-body, protocol
// errors) — not a plain target-unreachable error — we flag the host for
// transparent passthrough so future requests to it succeed instead of dying.
// errors) and not a plain target-unreachable error, the host is tunneled for
// passthroughTTL. This request has already failed. Later requests in the window
// connect straight through; the first request after the window tries
// interception again.
func (s *sink) RequestError(f *mproxy.Flow, err error) { s.t.maybePassthrough(f, err) }

// maybePassthrough marks a host to be tunneled transparently on the next
Expand All @@ -454,9 +456,34 @@ func (t *Traffic) maybePassthrough(f *mproxy.Flow, err error) {
if host == "" {
return
}
if _, loaded := t.pass.LoadOrStore(host, struct{}{}); !loaded {
log.Printf("[traffic] MITM with %s failed, falling back to passthrough (this host now connects straight to the target and is no longer recorded, but requests proceed normally): %v", host, err)
if t.tunneling(host) {
return
}
until := time.Now().Add(passthroughTTL)
if _, loaded := t.pass.LoadOrStore(host, until); !loaded {
log.Printf("[traffic] MITM with %s failed, falling back to passthrough for %s (this request failed; the host connects straight to the target and is not recorded until then; the first request after the window fails again if interception still cannot handle it): %v", host, passthroughTTL, err)
}
}

// passthroughTTL is how long a host stays tunneled after a proxy-caused MITM
// failure. A host whose interception always fails pays one lost request per
// window: one per host per minute, where a mark that never expired cost one
// per process. The window is what lets a stale bypass end.
const passthroughTTL = time.Minute

// tunneling reports whether host is inside an unexpired passthrough mark.
// An expired mark is removed so the next connection tries interception again.
func (t *Traffic) tunneling(host string) bool {
v, ok := t.pass.Load(host)
if !ok {
return false
}
until, ok := v.(time.Time)
if !ok || !time.Now().Before(until) {
t.pass.CompareAndDelete(host, v)
return false
}
return true
}

// proxyCausedErr reports whether err indicates the interception layer (not the
Expand Down
Loading