From 650f1f07146aa9d9845dd5ee2795948f455935ff Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 3 Oct 2022 21:24:09 +0400 Subject: [PATCH] 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 {