mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-18 06:00:54 +08:00
Expire a MITM passthrough mark after one minute.
A host that cannot be intercepted is tunneled until the mark expires, then interception is tried again.
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
mproxy "github.com/lqqyt2423/go-mitmproxy/proxy"
|
||||
)
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
+27
-6
@@ -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),
|
||||
@@ -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
|
||||
@@ -454,9 +454,30 @@ 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 host connects straight to the target and is not recorded until then; requests still proceed): %v", host, passthroughTTL, err)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user