github.com/chwjbn/xclash@v0.2.0/listener/http/client.go (about)

     1  package http
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"github.com/chwjbn/xclash/component/auth"
     7  	"net"
     8  	"net/http"
     9  	"time"
    10  
    11  	"github.com/chwjbn/xclash/adapter/inbound"
    12  	C "github.com/chwjbn/xclash/constant"
    13  	"github.com/chwjbn/xclash/transport/socks5"
    14  )
    15  
    16  func newHttpClient(source net.Addr, in chan<- C.ConnContext,authUser *auth.AuthUser) *http.Client {
    17  	return &http.Client{
    18  		Transport: &http.Transport{
    19  			// from http.DefaultTransport
    20  			MaxIdleConns:          100,
    21  			IdleConnTimeout:       90 * time.Second,
    22  			TLSHandshakeTimeout:   10 * time.Second,
    23  			ExpectContinueTimeout: 1 * time.Second,
    24  			DialContext: func(context context.Context, network, address string) (net.Conn, error) {
    25  				if network != "tcp" && network != "tcp4" && network != "tcp6" {
    26  					return nil, errors.New("unsupported network " + network)
    27  				}
    28  
    29  				dstAddr := socks5.ParseAddr(address)
    30  				if dstAddr == nil {
    31  					return nil, socks5.ErrAddressNotSupported
    32  				}
    33  
    34  				left, right := net.Pipe()
    35  
    36  				xConnCtx:=inbound.NewHTTP(dstAddr, source, right)
    37  				xConnCtx.SetAuthUser(authUser)
    38  
    39  				in <- xConnCtx
    40  
    41  				return left, nil
    42  			},
    43  		},
    44  		CheckRedirect: func(req *http.Request, via []*http.Request) error {
    45  			return http.ErrUseLastResponse
    46  		},
    47  	}
    48  }