2021-06-15 17:13:40 +08:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-09-13 23:46:39 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2021-06-15 17:13:40 +08:00
|
|
|
)
|
|
|
|
|
2023-06-13 23:25:32 +08:00
|
|
|
func newClient(source net.Addr, originTarget net.Addr, in chan<- C.ConnContext) *http.Client {
|
2021-06-15 17:13:40 +08:00
|
|
|
return &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
// from http.DefaultTransport
|
|
|
|
MaxIdleConns: 100,
|
|
|
|
IdleConnTimeout: 90 * time.Second,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
|
|
DialContext: func(context context.Context, network, address string) (net.Conn, error) {
|
|
|
|
if network != "tcp" && network != "tcp4" && network != "tcp6" {
|
|
|
|
return nil, errors.New("unsupported network " + network)
|
|
|
|
}
|
|
|
|
|
2021-09-13 23:46:39 +08:00
|
|
|
dstAddr := socks5.ParseAddr(address)
|
|
|
|
if dstAddr == nil {
|
|
|
|
return nil, socks5.ErrAddressNotSupported
|
|
|
|
}
|
|
|
|
|
2021-06-15 17:13:40 +08:00
|
|
|
left, right := net.Pipe()
|
|
|
|
|
2023-06-13 23:25:32 +08:00
|
|
|
in <- inbound.NewHTTP(dstAddr, source, originTarget, right)
|
2021-06-15 17:13:40 +08:00
|
|
|
|
|
|
|
return left, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|