From f9a6377450ae75f53382ce0305495e8f93a93a6f Mon Sep 17 00:00:00 2001 From: Barna Csorogi Date: Mon, 28 Mar 2016 18:11:58 +0200 Subject: [PATCH] fix mitm for http1 proxy connections Add back Hijacking support for http1.1 CONNECT requests. --- proxy/proxy.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index ab6581e..7dfc3ff 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -3,6 +3,7 @@ package proxy import ( "fmt" "log" + "net" "net/http" ) @@ -97,14 +98,23 @@ func (p *Proxy) handleConnect(w http.ResponseWriter, r *http.Request) error { return fmt.Errorf("CONNECT received but mitm is not enabled") } w.WriteHeader(http.StatusOK) - fw := w.(FlushWriter) - fw.Flush() - conn := newMitmConn(fw, r.Body, r.RemoteAddr) + var conn net.Conn + if h, ok := w.(http.Hijacker); ok { + conn, _, _ = h.Hijack() + } else { + fw := w.(FlushWriter) + fw.Flush() + mconn := newMitmConn(fw, r.Body, r.RemoteAddr) + conn = mconn + defer func() { + <-mconn.closed + }() + } sconn, err := p.ml.Serve(conn, r.Host) if err != nil { + conn.Close() return err } sconn.Close() // TODO: reuse this connection for https requests - <-conn.closed return nil }