Merge pull request #30 from t1nky/main

Upstream proxy
addon-dailer
liqiang 2 years ago committed by GitHub
commit b2bf9175bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"context" "context"
"crypto/tls" "crypto/tls"
"encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"net" "net"
@ -105,10 +106,17 @@ func (connCtx *ConnContext) initHttpServerConn() {
return 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 := newServerConn()
serverConn.client = &http.Client{ serverConn.client = &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment, Proxy: useProxy,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
c, err := (&net.Dialer{}).DialContext(ctx, network, addr) c, err := (&net.Dialer{}).DialContext(ctx, network, addr)
if err != nil { if err != nil {
@ -151,10 +159,23 @@ func (connCtx *ConnContext) initServerTcpConn(req *http.Request) error {
// test is use proxy // test is use proxy
clientReq := &http.Request{URL: &url.URL{Scheme: "https", Host: ServerConn.Address}} clientReq := &http.Request{URL: &url.URL{Scheme: "https", Host: ServerConn.Address}}
proxyUrl, err := http.ProxyFromEnvironment(clientReq)
if err != nil { var proxyUrl *url.URL
return err 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 var plainConn net.Conn
if proxyUrl != nil { if proxyUrl != nil {
plainConn, err = getProxyConn(proxyUrl, ServerConn.Address) plainConn, err = getProxyConn(proxyUrl, ServerConn.Address)
@ -188,6 +209,10 @@ func getProxyConn(proxyUrl *url.URL, address string) (net.Conn, error) {
Method: "CONNECT", Method: "CONNECT",
URL: &url.URL{Opaque: address}, URL: &url.URL{Opaque: address},
Host: address, Host: address,
Header: http.Header{},
}
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) connectCtx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel() defer cancel()

@ -16,6 +16,7 @@ type Options struct {
StreamLargeBodies int64 // 当请求或响应体大于此字节时,转为 stream 模式 StreamLargeBodies int64 // 当请求或响应体大于此字节时,转为 stream 模式
SslInsecure bool SslInsecure bool
CaRootPath string CaRootPath string
Upstream string
} }
type Proxy struct { type Proxy struct {

Loading…
Cancel
Save