github.com/metacubex/mihomo@v1.18.5/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/metacubex/mihomo/adapter/inbound"
    11  	N "github.com/metacubex/mihomo/common/net"
    12  	C "github.com/metacubex/mihomo/constant"
    13  	"github.com/metacubex/mihomo/transport/socks5"
    14  )
    15  
    16  func newClient(srcConn net.Conn, tunnel C.Tunnel, additions ...inbound.Addition) *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 := N.Pipe()
    35  
    36  				go tunnel.HandleTCPConn(inbound.NewHTTP(dstAddr, srcConn, right, additions...))
    37  
    38  				return left, nil
    39  			},
    40  		},
    41  		CheckRedirect: func(req *http.Request, via []*http.Request) error {
    42  			return http.ErrUseLastResponse
    43  		},
    44  	}
    45  }