From 650f1f07146aa9d9845dd5ee2795948f455935ff Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 3 Oct 2022 21:24:09 +0400 Subject: [PATCH 1/6] Added proxy --- proxy/connection.go | 9 ++++++++- proxy/proxy.go | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/proxy/connection.go b/proxy/connection.go index cb99e9d..f97a081 100644 --- a/proxy/connection.go +++ b/proxy/connection.go @@ -105,10 +105,17 @@ func (connCtx *ConnContext) initHttpServerConn() { return } + var useProxy func(*http.Request) (*url.URL, error) + if len(connCtx.proxy.Opts.Upstream) > 0 { + upstreamUrl, _ := url.Parse(connCtx.proxy.Opts.Upstream) + useProxy = http.ProxyURL(upstreamUrl) + } else { + useProxy = http.ProxyFromEnvironment + } serverConn := newServerConn() serverConn.client = &http.Client{ Transport: &http.Transport{ - Proxy: http.ProxyFromEnvironment, + Proxy: useProxy, DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { c, err := (&net.Dialer{}).DialContext(ctx, network, addr) if err != nil { diff --git a/proxy/proxy.go b/proxy/proxy.go index 956653f..1bf8f20 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -16,6 +16,7 @@ type Options struct { StreamLargeBodies int64 // 当请求或响应体大于此字节时,转为 stream 模式 SslInsecure bool CaRootPath string + Upstream string } type Proxy struct { From d46408d1c1643f7317c1ba2d8a9bb6b6f8bd530a Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 3 Oct 2022 21:28:35 +0400 Subject: [PATCH 2/6] Mod changed --- go.mod | 2 ++ 1 file changed, 2 insertions(+) diff --git a/go.mod b/go.mod index 1813e26..4aa8542 100644 --- a/go.mod +++ b/go.mod @@ -14,3 +14,5 @@ require ( golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect ) + +replace github.com/lqqyt2423/go-mitmproxy/v1.3.1 => github.com/t1nky/go-mitmproxy main From f48f17fba787891020b2bb401e7327154b9ec14d Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 3 Oct 2022 21:31:27 +0400 Subject: [PATCH 3/6] Go mod change --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 4aa8542..d03f4cb 100644 --- a/go.mod +++ b/go.mod @@ -15,4 +15,4 @@ require ( gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect ) -replace github.com/lqqyt2423/go-mitmproxy/v1.3.1 => github.com/t1nky/go-mitmproxy main +replace github.com/lqqyt2423/go-mitmproxy => ./go-mitmproxy \ No newline at end of file From 4446e0d4fc4686d8ec680bb20d9e60126e8a7e39 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 3 Oct 2022 21:38:19 +0400 Subject: [PATCH 4/6] Removed replace --- go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.mod b/go.mod index d03f4cb..1813e26 100644 --- a/go.mod +++ b/go.mod @@ -14,5 +14,3 @@ require ( golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect ) - -replace github.com/lqqyt2423/go-mitmproxy => ./go-mitmproxy \ No newline at end of file From 8a945462ffe57b9fb939a4fa35d9499fb2a1c8bc Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 3 Oct 2022 22:56:36 +0400 Subject: [PATCH 5/6] Added auth in getProxyConn --- proxy/connection.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/proxy/connection.go b/proxy/connection.go index f97a081..958f703 100644 --- a/proxy/connection.go +++ b/proxy/connection.go @@ -4,6 +4,7 @@ import ( "bufio" "context" "crypto/tls" + "encoding/base64" "encoding/json" "errors" "net" @@ -158,10 +159,23 @@ func (connCtx *ConnContext) initServerTcpConn(req *http.Request) error { // test is use proxy clientReq := &http.Request{URL: &url.URL{Scheme: "https", Host: ServerConn.Address}} - proxyUrl, err := http.ProxyFromEnvironment(clientReq) - if err != nil { - return err + + var proxyUrl *url.URL + var err error + + if len(connCtx.proxy.Opts.Upstream) > 0 { + upstreamUrl, _ := url.Parse(connCtx.proxy.Opts.Upstream) + proxyUrl, err = http.ProxyURL(upstreamUrl)(clientReq) + if err != nil { + return err + } + } else { + proxyUrl, err = http.ProxyFromEnvironment(clientReq) + if err != nil { + return err + } } + var plainConn net.Conn if proxyUrl != nil { plainConn, err = getProxyConn(proxyUrl, ServerConn.Address) @@ -196,6 +210,9 @@ func getProxyConn(proxyUrl *url.URL, address string) (net.Conn, error) { URL: &url.URL{Opaque: address}, Host: address, } + if proxyUrl.User != nil { + connectReq.Header.Set("Proxy-Authorization", "Basic"+base64.StdEncoding.EncodeToString([]byte(proxyUrl.User.String()))) + } connectCtx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) defer cancel() didReadResponse := make(chan struct{}) // closed after CONNECT write+read is done or fails From cbb89711bbbc610ae316bc34ba5254c907befcfb Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 3 Oct 2022 23:02:57 +0400 Subject: [PATCH 6/6] Added proxy auth --- proxy/connection.go | 1 + 1 file changed, 1 insertion(+) diff --git a/proxy/connection.go b/proxy/connection.go index 958f703..0561ac4 100644 --- a/proxy/connection.go +++ b/proxy/connection.go @@ -209,6 +209,7 @@ func getProxyConn(proxyUrl *url.URL, address string) (net.Conn, error) { Method: "CONNECT", URL: &url.URL{Opaque: address}, Host: address, + Header: http.Header{}, } if proxyUrl.User != nil { connectReq.Header.Set("Proxy-Authorization", "Basic"+base64.StdEncoding.EncodeToString([]byte(proxyUrl.User.String())))