2018-03-01 07:16:52 -05:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
gotls "crypto/tls"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"sync"
|
|
|
|
|
2020-12-04 09:32:55 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
|
|
|
|
2018-03-01 07:16:52 -05:00
|
|
|
"golang.org/x/net/http2"
|
2021-02-16 15:31:50 -05:00
|
|
|
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/buf"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet/tls"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/pipe"
|
2018-03-01 07:16:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-03-21 18:28:28 -04:00
|
|
|
globalDialerMap map[net.Destination]*http.Client
|
2020-05-31 05:35:21 -04:00
|
|
|
globalDialerAccess sync.Mutex
|
2018-03-01 07:16:52 -05:00
|
|
|
)
|
|
|
|
|
2020-12-04 09:32:55 -05:00
|
|
|
func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.Config) *http.Client {
|
2020-05-31 05:35:21 -04:00
|
|
|
globalDialerAccess.Lock()
|
|
|
|
defer globalDialerAccess.Unlock()
|
2018-03-01 07:16:52 -05:00
|
|
|
|
2018-03-21 18:28:28 -04:00
|
|
|
if globalDialerMap == nil {
|
|
|
|
globalDialerMap = make(map[net.Destination]*http.Client)
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:16:52 -05:00
|
|
|
if client, found := globalDialerMap[dest]; found {
|
2020-12-04 09:32:55 -05:00
|
|
|
return client
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
transport := &http2.Transport{
|
|
|
|
DialTLS: func(network string, addr string, tlsConfig *gotls.Config) (net.Conn, error) {
|
|
|
|
rawHost, rawPort, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(rawPort) == 0 {
|
|
|
|
rawPort = "443"
|
|
|
|
}
|
|
|
|
port, err := net.PortFromString(rawPort)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
address := net.ParseAddress(rawHost)
|
|
|
|
|
2021-06-22 07:20:27 -04:00
|
|
|
detachedContext := core.ToBackgroundDetachedContext(ctx)
|
|
|
|
pconn, err := internet.DialSystem(detachedContext, net.TCPDestination(address, port), nil)
|
2018-03-01 07:16:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-31 05:35:21 -04:00
|
|
|
|
|
|
|
cn := gotls.Client(pconn, tlsConfig)
|
|
|
|
if err := cn.Handshake(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !tlsConfig.InsecureSkipVerify {
|
|
|
|
if err := cn.VerifyHostname(tlsConfig.ServerName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state := cn.ConnectionState()
|
|
|
|
if p := state.NegotiatedProtocol; p != http2.NextProtoTLS {
|
|
|
|
return nil, newError("http2: unexpected ALPN protocol " + p + "; want q" + http2.NextProtoTLS).AtError()
|
|
|
|
}
|
|
|
|
return cn, nil
|
2018-03-01 07:16:52 -05:00
|
|
|
},
|
2020-05-31 05:35:21 -04:00
|
|
|
TLSClientConfig: tlsSettings.GetTLSConfig(tls.WithDestination(dest)),
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
|
|
|
globalDialerMap[dest] = client
|
2020-12-04 09:32:55 -05:00
|
|
|
return client
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dial dials a new TCP connection to the given destination.
|
2018-11-21 08:54:40 -05:00
|
|
|
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
|
|
|
|
httpSettings := streamSettings.ProtocolSettings.(*Config)
|
|
|
|
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
|
|
|
|
if tlsConfig == nil {
|
|
|
|
return nil, newError("TLS must be enabled for http transport.").AtWarning()
|
2018-03-01 08:22:33 -05:00
|
|
|
}
|
2020-12-04 09:32:55 -05:00
|
|
|
client := getHTTPClient(ctx, dest, tlsConfig)
|
2018-03-01 07:16:52 -05:00
|
|
|
|
2018-05-25 06:08:28 -04:00
|
|
|
opts := pipe.OptionsFromContext(ctx)
|
|
|
|
preader, pwriter := pipe.New(opts...)
|
2018-04-20 18:54:53 -04:00
|
|
|
breader := &buf.BufferedReader{Reader: preader}
|
2021-03-01 15:48:30 -05:00
|
|
|
|
|
|
|
httpMethod := "PUT"
|
|
|
|
if httpSettings.Method != "" {
|
|
|
|
httpMethod = httpSettings.Method
|
|
|
|
}
|
|
|
|
|
|
|
|
httpHeaders := make(http.Header)
|
|
|
|
|
|
|
|
for _, httpHeader := range httpSettings.Header {
|
|
|
|
for _, httpHeaderValue := range httpHeader.Value {
|
|
|
|
httpHeaders.Set(httpHeader.Name, httpHeaderValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:16:52 -05:00
|
|
|
request := &http.Request{
|
2021-03-01 15:48:30 -05:00
|
|
|
Method: httpMethod,
|
2018-03-01 08:22:33 -05:00
|
|
|
Host: httpSettings.getRandomHost(),
|
2018-04-20 18:54:53 -04:00
|
|
|
Body: breader,
|
2018-03-01 07:16:52 -05:00
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: dest.NetAddr(),
|
2018-03-01 08:22:33 -05:00
|
|
|
Path: httpSettings.getNormalizedPath(),
|
2018-03-01 07:16:52 -05:00
|
|
|
},
|
|
|
|
Proto: "HTTP/2",
|
|
|
|
ProtoMajor: 2,
|
|
|
|
ProtoMinor: 0,
|
2021-03-01 15:48:30 -05:00
|
|
|
Header: httpHeaders,
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|
2018-04-20 10:10:32 -04:00
|
|
|
// Disable any compression method from server.
|
|
|
|
request.Header.Set("Accept-Encoding", "identity")
|
|
|
|
|
2020-11-21 16:05:01 -05:00
|
|
|
response, err := client.Do(request) // nolint: bodyclose
|
2018-03-01 07:16:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to dial to ", dest).Base(err).AtWarning()
|
|
|
|
}
|
|
|
|
if response.StatusCode != 200 {
|
|
|
|
return nil, newError("unexpected status", response.StatusCode).AtWarning()
|
|
|
|
}
|
|
|
|
|
2018-04-16 08:57:13 -04:00
|
|
|
bwriter := buf.NewBufferedWriter(pwriter)
|
|
|
|
common.Must(bwriter.SetBuffered(false))
|
2018-04-20 15:30:58 -04:00
|
|
|
return net.NewConnection(
|
|
|
|
net.ConnectionOutput(response.Body),
|
|
|
|
net.ConnectionInput(bwriter),
|
2019-01-01 14:16:04 -05:00
|
|
|
net.ConnectionOnClose(common.ChainedClosable{breader, bwriter, response.Body}),
|
2018-04-20 15:30:58 -04:00
|
|
|
), nil
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-08-06 07:48:35 -04:00
|
|
|
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|