2020-11-25 14:01:53 +03:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
gotls "crypto/tls"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"sync"
|
|
|
|
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
2020-12-27 22:20:39 +03:00
|
|
|
"github.com/xtls/xray-core/common/net/cnc"
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
|
|
|
"github.com/xtls/xray-core/transport/internet/tls"
|
|
|
|
"github.com/xtls/xray-core/transport/pipe"
|
2020-11-25 14:01:53 +03:00
|
|
|
"golang.org/x/net/http2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
globalDialerMap map[net.Destination]*http.Client
|
|
|
|
globalDialerAccess sync.Mutex
|
|
|
|
)
|
|
|
|
|
2020-12-16 14:52:45 +03:00
|
|
|
func getHTTPClient(ctx context.Context, dest net.Destination, tlsSettings *tls.Config) (*http.Client, error) {
|
2020-11-25 14:01:53 +03:00
|
|
|
globalDialerAccess.Lock()
|
|
|
|
defer globalDialerAccess.Unlock()
|
|
|
|
|
|
|
|
if globalDialerMap == nil {
|
|
|
|
globalDialerMap = make(map[net.Destination]*http.Client)
|
|
|
|
}
|
|
|
|
|
|
|
|
if client, found := globalDialerMap[dest]; found {
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-12-16 14:52:45 +03:00
|
|
|
pconn, err := internet.DialSystem(ctx, net.TCPDestination(address, port), nil)
|
2020-11-25 14:01:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
if !state.NegotiatedProtocolIsMutual {
|
|
|
|
return nil, newError("http2: could not negotiate protocol mutually").AtError()
|
|
|
|
}
|
|
|
|
return cn, nil
|
|
|
|
},
|
|
|
|
TLSClientConfig: tlsSettings.GetTLSConfig(tls.WithDestination(dest)),
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
|
|
|
globalDialerMap[dest] = client
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial dials a new TCP connection to the given destination.
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
client, err := getHTTPClient(ctx, dest, tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := pipe.OptionsFromContext(ctx)
|
|
|
|
preader, pwriter := pipe.New(opts...)
|
|
|
|
breader := &buf.BufferedReader{Reader: preader}
|
|
|
|
request := &http.Request{
|
|
|
|
Method: "PUT",
|
|
|
|
Host: httpSettings.getRandomHost(),
|
|
|
|
Body: breader,
|
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: dest.NetAddr(),
|
|
|
|
Path: httpSettings.getNormalizedPath(),
|
|
|
|
},
|
|
|
|
Proto: "HTTP/2",
|
|
|
|
ProtoMajor: 2,
|
|
|
|
ProtoMinor: 0,
|
|
|
|
Header: make(http.Header),
|
|
|
|
}
|
|
|
|
// Disable any compression method from server.
|
|
|
|
request.Header.Set("Accept-Encoding", "identity")
|
|
|
|
|
|
|
|
response, err := client.Do(request)
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
bwriter := buf.NewBufferedWriter(pwriter)
|
|
|
|
common.Must(bwriter.SetBuffered(false))
|
2020-12-27 22:20:39 +03:00
|
|
|
return cnc.NewConnection(
|
|
|
|
cnc.ConnectionOutput(response.Body),
|
|
|
|
cnc.ConnectionInput(bwriter),
|
|
|
|
cnc.ConnectionOnClose(common.ChainedClosable{breader, bwriter, response.Body}),
|
2020-11-25 14:01:53 +03:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
|
|
|
|
}
|