add proxy.SetShouldInterceptRule

addon-dailer
lqqyt2423 2 years ago
parent fd778d51be
commit 869f9a3e8d

@ -157,31 +157,7 @@ func (connCtx *ConnContext) initServerTcpConn(req *http.Request) error {
connCtx.ServerConn = ServerConn connCtx.ServerConn = ServerConn
ServerConn.Address = connCtx.pipeConn.host ServerConn.Address = connCtx.pipeConn.host
// test is use proxy plainConn, err := getConnFrom(req.Host, connCtx.proxy.Opts.Upstream)
clientReq := &http.Request{URL: &url.URL{Scheme: "https", Host: ServerConn.Address}}
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)
} else {
plainConn, err = (&net.Dialer{}).DialContext(context.Background(), "tcp", ServerConn.Address)
}
if err != nil { if err != nil {
return err return err
} }
@ -393,3 +369,31 @@ func getProxyConn(proxyUrl *url.URL, address string) (net.Conn, error) {
} }
return conn, nil return conn, nil
} }
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
}
}
var conn net.Conn
if proxyUrl != nil {
conn, err = getProxyConn(proxyUrl, address)
} else {
conn, err = (&net.Dialer{}).DialContext(context.Background(), "tcp", address)
}
return conn, err
}

@ -25,8 +25,9 @@ type Proxy struct {
Version string Version string
Addons []Addon Addons []Addon
server *http.Server server *http.Server
interceptor *middle interceptor *middle
shouldIntercept func(address string) bool
} }
func NewProxy(opts *Options) (*Proxy, error) { func NewProxy(opts *Options) (*Proxy, error) {
@ -282,7 +283,15 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) {
"host": req.Host, "host": req.Host,
}) })
conn, err := proxy.interceptor.dial(req) var conn net.Conn
var err error
if proxy.shouldIntercept == nil || proxy.shouldIntercept(req.Host) {
log.Debugf("begin intercept %v", req.Host)
conn, err = proxy.interceptor.dial(req)
} else {
log.Debugf("begin transpond %v", req.Host)
conn, err = getConnFrom(req.Host, proxy.Opts.Upstream)
}
if err != nil { if err != nil {
log.Error(err) log.Error(err)
res.WriteHeader(502) res.WriteHeader(502)
@ -313,3 +322,7 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) {
func (proxy *Proxy) GetCertificate() x509.Certificate { func (proxy *Proxy) GetCertificate() x509.Certificate {
return proxy.interceptor.ca.RootCert return proxy.interceptor.ca.RootCert
} }
func (proxy *Proxy) SetShouldInterceptRule(rule func(address string) bool) {
proxy.shouldIntercept = rule
}

Loading…
Cancel
Save