Skip to content

Commit

Permalink
Merge branch 'main' into mapremote
Browse files Browse the repository at this point in the history
  • Loading branch information
lqqyt2423 committed Mar 7, 2023
2 parents d1cfe58 + b957993 commit b152023
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
27 changes: 4 additions & 23 deletions proxy/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,10 @@ 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: useProxy,
Proxy: clientProxy(connCtx.proxy.Opts.Upstream),
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
c, err := (&net.Dialer{}).DialContext(ctx, network, addr)
if err != nil {
Expand Down Expand Up @@ -374,21 +367,9 @@ func getProxyConn(proxyUrl *url.URL, address string) (net.Conn, error) {

func getConnFrom(address string, upstream string) (net.Conn, error) {
clientReq := &http.Request{URL: &url.URL{Scheme: "https", Host: address}}

var proxyUrl *url.URL
var err error

if len(upstream) > 0 {
upstreamUrl, _ := url.Parse(upstream)
proxyUrl, err = http.ProxyURL(upstreamUrl)(clientReq)
if err != nil {
return nil, err
}
} else {
proxyUrl, err = http.ProxyFromEnvironment(clientReq)
if err != nil {
return nil, err
}
proxyUrl, err := clientProxy(upstream)(clientReq)
if err != nil {
return nil, err
}

var conn net.Conn
Expand Down
6 changes: 3 additions & 3 deletions proxy/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ type Flow struct {

// https://docs.mitmproxy.org/stable/overview-features/#streaming
// 如果为 true,则不缓冲 Request.Body 和 Response.Body,且不进入之后的 Addon.Request 和 Addon.Response
Stream bool

done chan struct{}
Stream bool
UseSeparateClient bool // use separate http client to send http request
done chan struct{}
}

func newFlow() *Flow {
Expand Down
13 changes: 13 additions & 0 deletions proxy/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"io"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
Expand Down Expand Up @@ -124,3 +126,14 @@ func getTlsKeyLogWriter() io.Writer {
})
return tlsKeyLogWriter
}

func clientProxy(upstream string) func(*http.Request) (*url.URL, error) {
var useProxy func(*http.Request) (*url.URL, error)
if len(upstream) > 0 {
upstreamUrl, _ := url.Parse(upstream)
useProxy = http.ProxyURL(upstreamUrl)
} else {
useProxy = http.ProxyFromEnvironment
}
return useProxy
}
25 changes: 24 additions & 1 deletion proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package proxy
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"io"
"net"
Expand All @@ -25,6 +26,7 @@ type Proxy struct {
Version string
Addons []Addon

client *http.Client
server *http.Server
interceptor *middle
shouldIntercept func(address string) bool
Expand All @@ -41,6 +43,22 @@ func NewProxy(opts *Options) (*Proxy, error) {
Addons: make([]Addon, 0),
}

proxy.client = &http.Client{
Transport: &http.Transport{
Proxy: clientProxy(opts.Upstream),
ForceAttemptHTTP2: false, // disable http2
DisableCompression: true, // To get the original response from the server, set Transport.DisableCompression to true.
TLSClientConfig: &tls.Config{
InsecureSkipVerify: opts.SslInsecure,
KeyLogWriter: getTlsKeyLogWriter(),
},
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
// 禁止自动重定向
return http.ErrUseLastResponse
},
}

proxy.server = &http.Server{
Addr: opts.Addr,
Handler: proxy,
Expand Down Expand Up @@ -222,7 +240,12 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
}

f.ConnContext.initHttpServerConn()
proxyRes, err := f.ConnContext.ServerConn.client.Do(proxyReq)
var proxyRes *http.Response
if f.UseSeparateClient {
proxyRes, err = proxy.client.Do(proxyReq)
} else {
proxyRes, err = f.ConnContext.ServerConn.client.Do(proxyReq)
}
if err != nil {
logErr(log, err)
res.WriteHeader(502)
Expand Down

0 comments on commit b152023

Please sign in to comment.