From 7924b4665cf5e117f3100472a9759c34a8def13c Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sat, 22 Jul 2023 20:07:20 -0700 Subject: [PATCH] net/http: add enforcement hook to Transport.RoundTrip, like our net ones Updates tailscale/go#55 Updates tailscale/corp#12702 Signed-off-by: Brad Fitzpatrick --- src/net/http/tailscale.go | 13 +++++++++++++ src/net/http/transport.go | 5 +++++ 2 files changed, 18 insertions(+) create mode 100644 src/net/http/tailscale.go diff --git a/src/net/http/tailscale.go b/src/net/http/tailscale.go new file mode 100644 index 0000000000000..3e67c84661087 --- /dev/null +++ b/src/net/http/tailscale.go @@ -0,0 +1,13 @@ +package http + +var roundTripEnforcer func(*Request) error + +func SetRoundTripEnforcer(f func(*Request) error) { + if f == nil { + panic("nil func") + } + if roundTripEnforcer != nil { + panic("already called") + } + roundTripEnforcer = f +} diff --git a/src/net/http/transport.go b/src/net/http/transport.go index c07352b018e1e..4d9efe270840b 100644 --- a/src/net/http/transport.go +++ b/src/net/http/transport.go @@ -511,6 +511,11 @@ func (t *Transport) alternateRoundTripper(req *Request) RoundTripper { // roundTrip implements a RoundTripper over HTTP. func (t *Transport) roundTrip(req *Request) (*Response, error) { + if roundTripEnforcer != nil { + if err := roundTripEnforcer(req); err != nil { + return nil, err + } + } t.nextProtoOnce.Do(t.onceSetNextProtoDefaults) ctx := req.Context() trace := httptrace.ContextClientTrace(ctx)