2020-11-25 11:01:53 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
gotls "crypto/tls"
|
2023-03-02 14:50:26 +00:00
|
|
|
"io"
|
2020-11-25 11:01:53 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"sync"
|
2021-07-03 08:01:59 +00:00
|
|
|
"time"
|
2020-11-25 11:01:53 +00:00
|
|
|
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
2020-12-27 19:20:39 +00:00
|
|
|
"github.com/xtls/xray-core/common/net/cnc"
|
2021-03-01 02:43:27 +00:00
|
|
|
"github.com/xtls/xray-core/common/session"
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
2023-02-15 16:07:12 +00:00
|
|
|
"github.com/xtls/xray-core/transport/internet/reality"
|
2021-12-15 00:28:47 +00:00
|
|
|
"github.com/xtls/xray-core/transport/internet/stat"
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/transport/internet/tls"
|
|
|
|
"github.com/xtls/xray-core/transport/pipe"
|
2022-05-18 07:29:01 +00:00
|
|
|
"golang.org/x/net/http2"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
2021-03-14 15:02:07 +00:00
|
|
|
type dialerConf struct {
|
|
|
|
net.Destination
|
2021-07-03 08:01:59 +00:00
|
|
|
*internet.MemoryStreamConfig
|
2021-03-14 15:02:07 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
var (
|
2021-03-14 15:02:07 +00:00
|
|
|
globalDialerMap map[dialerConf]*http.Client
|
2020-11-25 11:01:53 +00:00
|
|
|
globalDialerAccess sync.Mutex
|
|
|
|
)
|
|
|
|
|
2021-07-03 08:01:59 +00:00
|
|
|
func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (*http.Client, error) {
|
2020-11-25 11:01:53 +00:00
|
|
|
globalDialerAccess.Lock()
|
|
|
|
defer globalDialerAccess.Unlock()
|
|
|
|
|
|
|
|
if globalDialerMap == nil {
|
2021-03-14 15:02:07 +00:00
|
|
|
globalDialerMap = make(map[dialerConf]*http.Client)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-03 08:01:59 +00:00
|
|
|
httpSettings := streamSettings.ProtocolSettings.(*Config)
|
2022-10-13 12:04:00 +00:00
|
|
|
tlsConfigs := tls.ConfigFromStreamSettings(streamSettings)
|
2023-02-15 16:07:12 +00:00
|
|
|
realityConfigs := reality.ConfigFromStreamSettings(streamSettings)
|
|
|
|
if tlsConfigs == nil && realityConfigs == nil {
|
|
|
|
return nil, newError("TLS or REALITY must be enabled for http transport.").AtWarning()
|
2021-07-03 08:01:59 +00:00
|
|
|
}
|
|
|
|
sockopt := streamSettings.SocketSettings
|
|
|
|
|
|
|
|
if client, found := globalDialerMap[dialerConf{dest, streamSettings}]; found {
|
2020-11-25 11:01:53 +00:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
transport := &http2.Transport{
|
2023-08-27 05:55:58 +00:00
|
|
|
DialTLSContext: func(hctx context.Context, string, addr string, tlsConfig *gotls.Config) (net.Conn, error) {
|
2020-11-25 11:01:53 +00:00
|
|
|
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)
|
|
|
|
|
2023-08-27 05:55:58 +00:00
|
|
|
hctx = session.ContextWithID(hctx, session.IDFromContext(ctx))
|
|
|
|
hctx = session.ContextWithOutbound(hctx, session.OutboundFromContext(ctx))
|
|
|
|
hctx = session.ContextWithTimeoutOnly(hctx, true)
|
2021-03-01 02:43:27 +00:00
|
|
|
|
2023-08-27 05:55:58 +00:00
|
|
|
pconn, err := internet.DialSystem(hctx, net.TCPDestination(address, port), sockopt)
|
2020-11-25 11:01:53 +00:00
|
|
|
if err != nil {
|
2021-03-01 02:43:27 +00:00
|
|
|
newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
|
2020-11-25 11:01:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:07:12 +00:00
|
|
|
if realityConfigs != nil {
|
2023-08-27 05:55:58 +00:00
|
|
|
return reality.UClient(pconn, realityConfigs, hctx, dest)
|
2023-02-15 16:07:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 12:04:00 +00:00
|
|
|
var cn tls.Interface
|
2023-02-01 12:58:17 +00:00
|
|
|
if fingerprint := tls.GetFingerprint(tlsConfigs.Fingerprint); fingerprint != nil {
|
2022-10-13 12:04:00 +00:00
|
|
|
cn = tls.UClient(pconn, tlsConfig, fingerprint).(*tls.UConn)
|
|
|
|
} else {
|
|
|
|
cn = tls.Client(pconn, tlsConfig).(*tls.Conn)
|
|
|
|
}
|
2024-02-03 11:45:37 +00:00
|
|
|
if err := cn.HandshakeContext(ctx); err != nil {
|
2021-03-01 02:43:27 +00:00
|
|
|
newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
|
2020-11-25 11:01:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !tlsConfig.InsecureSkipVerify {
|
|
|
|
if err := cn.VerifyHostname(tlsConfig.ServerName); err != nil {
|
2021-03-01 02:43:27 +00:00
|
|
|
newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
|
2020-11-25 11:01:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-10-13 12:04:00 +00:00
|
|
|
negotiatedProtocol, negotiatedProtocolIsMutual := cn.NegotiatedProtocol()
|
|
|
|
if negotiatedProtocol != http2.NextProtoTLS {
|
|
|
|
return nil, newError("http2: unexpected ALPN protocol " + negotiatedProtocol + "; want q" + http2.NextProtoTLS).AtError()
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
2022-10-13 12:04:00 +00:00
|
|
|
if !negotiatedProtocolIsMutual {
|
2020-11-25 11:01:53 +00:00
|
|
|
return nil, newError("http2: could not negotiate protocol mutually").AtError()
|
|
|
|
}
|
|
|
|
return cn, nil
|
|
|
|
},
|
2023-02-15 16:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tlsConfigs != nil {
|
|
|
|
transport.TLSClientConfig = tlsConfigs.GetTLSConfig(tls.WithDestination(dest))
|
2021-07-03 08:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if httpSettings.IdleTimeout > 0 || httpSettings.HealthCheckTimeout > 0 {
|
|
|
|
transport.ReadIdleTimeout = time.Second * time.Duration(httpSettings.IdleTimeout)
|
|
|
|
transport.PingTimeout = time.Second * time.Duration(httpSettings.HealthCheckTimeout)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
2021-07-03 08:01:59 +00:00
|
|
|
globalDialerMap[dialerConf{dest, streamSettings}] = client
|
2020-11-25 11:01:53 +00:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial dials a new TCP connection to the given destination.
|
2021-09-20 12:11:21 +00:00
|
|
|
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
|
2020-11-25 11:01:53 +00:00
|
|
|
httpSettings := streamSettings.ProtocolSettings.(*Config)
|
2021-07-03 08:01:59 +00:00
|
|
|
client, err := getHTTPClient(ctx, dest, streamSettings)
|
2020-11-25 11:01:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := pipe.OptionsFromContext(ctx)
|
|
|
|
preader, pwriter := pipe.New(opts...)
|
|
|
|
breader := &buf.BufferedReader{Reader: preader}
|
2021-10-12 15:58:12 +00: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
request := &http.Request{
|
2021-10-12 15:58:12 +00:00
|
|
|
Method: httpMethod,
|
2020-11-25 11:01:53 +00:00
|
|
|
Host: httpSettings.getRandomHost(),
|
|
|
|
Body: breader,
|
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: dest.NetAddr(),
|
|
|
|
Path: httpSettings.getNormalizedPath(),
|
|
|
|
},
|
|
|
|
Proto: "HTTP/2",
|
|
|
|
ProtoMajor: 2,
|
|
|
|
ProtoMinor: 0,
|
2021-10-12 15:58:12 +00:00
|
|
|
Header: httpHeaders,
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
// Disable any compression method from server.
|
|
|
|
request.Header.Set("Accept-Encoding", "identity")
|
|
|
|
|
2023-03-02 14:50:26 +00:00
|
|
|
wrc := &WaitReadCloser{Wait: make(chan struct{})}
|
|
|
|
go func() {
|
|
|
|
response, err := client.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
newError("failed to dial to ", dest).Base(err).AtWarning().WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
wrc.Close()
|
2023-07-21 22:06:25 +00:00
|
|
|
{
|
|
|
|
// Abandon `client` if `client.Do(request)` failed
|
|
|
|
// See https://github.com/golang/go/issues/30702
|
|
|
|
globalDialerAccess.Lock()
|
|
|
|
if globalDialerMap[dialerConf{dest, streamSettings}] == client {
|
|
|
|
delete(globalDialerMap, dialerConf{dest, streamSettings})
|
|
|
|
}
|
|
|
|
globalDialerAccess.Unlock()
|
|
|
|
}
|
2023-03-02 14:50:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if response.StatusCode != 200 {
|
|
|
|
newError("unexpected status", response.StatusCode).AtWarning().WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
wrc.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wrc.Set(response.Body)
|
|
|
|
}()
|
2020-11-25 11:01:53 +00:00
|
|
|
|
|
|
|
bwriter := buf.NewBufferedWriter(pwriter)
|
|
|
|
common.Must(bwriter.SetBuffered(false))
|
2020-12-27 19:20:39 +00:00
|
|
|
return cnc.NewConnection(
|
2023-03-02 14:50:26 +00:00
|
|
|
cnc.ConnectionOutput(wrc),
|
2020-12-27 19:20:39 +00:00
|
|
|
cnc.ConnectionInput(bwriter),
|
2023-03-02 14:50:26 +00:00
|
|
|
cnc.ConnectionOnClose(common.ChainedClosable{breader, bwriter, wrc}),
|
2020-11-25 11:01:53 +00:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
|
|
|
|
}
|
2023-03-02 14:50:26 +00:00
|
|
|
|
|
|
|
type WaitReadCloser struct {
|
|
|
|
Wait chan struct{}
|
|
|
|
io.ReadCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WaitReadCloser) Set(rc io.ReadCloser) {
|
|
|
|
w.ReadCloser = rc
|
|
|
|
defer func() {
|
2023-03-08 14:06:20 +00:00
|
|
|
if recover() != nil {
|
2023-03-02 14:50:26 +00:00
|
|
|
rc.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
close(w.Wait)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WaitReadCloser) Read(b []byte) (int, error) {
|
|
|
|
if w.ReadCloser == nil {
|
|
|
|
if <-w.Wait; w.ReadCloser == nil {
|
|
|
|
return 0, io.ErrClosedPipe
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return w.ReadCloser.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WaitReadCloser) Close() error {
|
|
|
|
if w.ReadCloser != nil {
|
|
|
|
return w.ReadCloser.Close()
|
|
|
|
}
|
|
|
|
defer func() {
|
2023-03-08 14:06:20 +00:00
|
|
|
if recover() != nil && w.ReadCloser != nil {
|
|
|
|
w.ReadCloser.Close()
|
2023-03-02 14:50:26 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
close(w.Wait)
|
|
|
|
return nil
|
|
|
|
}
|