|
| 1 | +package forwardproxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "log/slog" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/kagenti/kagenti-extensions/authbridge/authlib/pipeline" |
| 12 | + "github.com/kagenti/kagenti-extensions/authbridge/authlib/session" |
| 13 | +) |
| 14 | + |
| 15 | +// HandleTransparentConn processes one outbound connection captured by an |
| 16 | +// iptables REDIRECT (proxy-sidecar enforce-redirect mode). It is the |
| 17 | +// transparent-listener analogue of handleConnect, and shares its semantics: |
| 18 | +// the same outbound pipeline gates the connection on destination/identity, and |
| 19 | +// the bytes are then blind-tunnelled, preserving the agent's end-to-end TLS |
| 20 | +// (token-exchange and protocol parsers are no-ops on opaque TLS, exactly as on |
| 21 | +// the CONNECT path). |
| 22 | +// |
| 23 | +// The crucial difference from handleConnect: there is NO HTTP CONNECT request. |
| 24 | +// The agent believes it is talking directly to dst, so the proxy must emit no |
| 25 | +// protocol bytes back — no "200 Connection Established", no hijack. It simply |
| 26 | +// gates, dials dst, and copies bytes both ways. dst is "host:port" recovered |
| 27 | +// from SO_ORIGINAL_DST by the transparent listener. |
| 28 | +// |
| 29 | +// HandleTransparentConn owns clientConn's lifecycle and always closes it. |
| 30 | +func (s *Server) HandleTransparentConn(clientConn net.Conn, dst string) { |
| 31 | + defer func() { _ = clientConn.Close() }() |
| 32 | + |
| 33 | + // Background context: there is no inbound *http.Request to tie cancellation |
| 34 | + // to. Tunnel teardown (either side closing) is what ends the connection; |
| 35 | + // the pipeline Run/Finish calls are short and don't need request scoping. |
| 36 | + ctx := context.Background() |
| 37 | + |
| 38 | + pctx := &pipeline.Context{ |
| 39 | + Direction: pipeline.Outbound, |
| 40 | + Method: http.MethodConnect, // synthetic: opaque tunnel, parity with handleConnect |
| 41 | + Scheme: "tcp", // marker: bytes are opaque, not HTTP |
| 42 | + Host: dst, |
| 43 | + Headers: http.Header{}, |
| 44 | + Shared: s.Shared, |
| 45 | + StartedAt: time.Now(), |
| 46 | + } |
| 47 | + defer func() { |
| 48 | + s.OutboundPipeline.RunFinish(ctx, pctx, pipeline.OutcomeFromContext(pctx)) |
| 49 | + }() |
| 50 | + |
| 51 | + if s.Sessions != nil { |
| 52 | + if aid := s.Sessions.ActiveSession(); aid != "" { |
| 53 | + pctx.Session = s.Sessions.View(aid) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Gate on host/identity before opening the tunnel — identical to the |
| 58 | + // CONNECT path. Parsers see no body and degrade gracefully. |
| 59 | + action := s.OutboundPipeline.Run(ctx, pctx) |
| 60 | + if action.Type == pipeline.Reject { |
| 61 | + s.recordOutboundReject(pctx, action) |
| 62 | + slog.Warn("transparent-proxy: outbound rejected by policy", "host", dst) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + upstream, err := net.DialTimeout("tcp", dst, connectDialTimeout) |
| 67 | + if err != nil { |
| 68 | + slog.Warn("transparent-proxy: upstream dial failed", "host", dst, "error", err) |
| 69 | + return |
| 70 | + } |
| 71 | + defer func() { _ = upstream.Close() }() |
| 72 | + |
| 73 | + enableKeepalive(upstream) |
| 74 | + enableKeepalive(clientConn) |
| 75 | + |
| 76 | + s.recordTunnelOpened(pctx) |
| 77 | + tunnel(clientConn, upstream) |
| 78 | +} |
| 79 | + |
| 80 | +// recordTunnelOpened emits the SessionRequest event for an opened opaque |
| 81 | +// tunnel (CONNECT or transparent-redirect). Shared by handleConnect and |
| 82 | +// HandleTransparentConn. MCP/Inference snapshots are nil by definition (the |
| 83 | +// bytes are opaque); Invocations from gate plugins and plugin-public Plugins |
| 84 | +// entries are still meaningful. |
| 85 | +func (s *Server) recordTunnelOpened(pctx *pipeline.Context) { |
| 86 | + if s.Sessions == nil { |
| 87 | + return |
| 88 | + } |
| 89 | + sid := s.Sessions.ActiveSession() |
| 90 | + if sid == "" { |
| 91 | + sid = session.DefaultSessionID |
| 92 | + } |
| 93 | + plugins := pipeline.SnapshotPlugins(pctx.Extensions.Custom) |
| 94 | + ev := pipeline.SessionEvent{ |
| 95 | + At: time.Now(), |
| 96 | + Direction: pipeline.Outbound, |
| 97 | + Phase: pipeline.SessionRequest, |
| 98 | + Invocations: pipeline.SnapshotInvocations(pctx.Extensions.Invocations, pipeline.InvocationPhaseRequest), |
| 99 | + Plugins: plugins, |
| 100 | + Identity: pipeline.SnapshotIdentity(pctx), |
| 101 | + Host: pctx.Host, |
| 102 | + } |
| 103 | + if ev.Invocations != nil || plugins != nil { |
| 104 | + s.Sessions.Append(sid, ev) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// tunnel bidirectionally copies between two connections until either side |
| 109 | +// closes, then propagates the close to the other so both io.Copy goroutines |
| 110 | +// exit. Close-on-each-side is idempotent on net.Conn. Shared by handleConnect |
| 111 | +// and HandleTransparentConn. |
| 112 | +func tunnel(a, b net.Conn) { |
| 113 | + go func() { |
| 114 | + _, _ = io.Copy(b, a) |
| 115 | + _ = b.Close() |
| 116 | + _ = a.Close() |
| 117 | + }() |
| 118 | + _, _ = io.Copy(a, b) |
| 119 | + _ = a.Close() |
| 120 | + _ = b.Close() |
| 121 | +} |
0 commit comments