github.com/yaling888/clash@v1.53.0/listener/http/client.go (about)

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