2024-06-18 05:36:36 +00:00
|
|
|
package splithttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
gotls "crypto/tls"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
2024-06-29 18:32:57 +00:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2024-06-18 05:36:36 +00:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/signal/semaphore"
|
|
|
|
"github.com/xtls/xray-core/common/uuid"
|
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
2024-07-11 07:56:20 +00:00
|
|
|
"github.com/xtls/xray-core/transport/internet/browser_dialer"
|
2024-06-18 05:36:36 +00:00
|
|
|
"github.com/xtls/xray-core/transport/internet/stat"
|
|
|
|
"github.com/xtls/xray-core/transport/internet/tls"
|
|
|
|
"github.com/xtls/xray-core/transport/pipe"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type dialerConf struct {
|
|
|
|
net.Destination
|
|
|
|
*internet.MemoryStreamConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2024-07-11 07:56:20 +00:00
|
|
|
globalDialerMap map[dialerConf]DialerClient
|
2024-06-18 05:36:36 +00:00
|
|
|
globalDialerAccess sync.Mutex
|
|
|
|
)
|
|
|
|
|
2024-07-11 07:56:20 +00:00
|
|
|
func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) DialerClient {
|
|
|
|
if browser_dialer.HasBrowserDialer() {
|
|
|
|
return &BrowserDialerClient{}
|
|
|
|
}
|
|
|
|
|
2024-06-18 05:36:36 +00:00
|
|
|
globalDialerAccess.Lock()
|
|
|
|
defer globalDialerAccess.Unlock()
|
|
|
|
|
|
|
|
if globalDialerMap == nil {
|
2024-07-11 07:56:20 +00:00
|
|
|
globalDialerMap = make(map[dialerConf]DialerClient)
|
2024-06-18 05:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if client, found := globalDialerMap[dialerConf{dest, streamSettings}]; found {
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2024-07-11 07:56:20 +00:00
|
|
|
if browser_dialer.HasBrowserDialer() {
|
|
|
|
return &BrowserDialerClient{}
|
|
|
|
}
|
|
|
|
|
2024-06-18 05:36:36 +00:00
|
|
|
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
|
2024-06-23 17:05:37 +00:00
|
|
|
isH2 := tlsConfig != nil && !(len(tlsConfig.NextProtocol) == 1 && tlsConfig.NextProtocol[0] == "http/1.1")
|
2024-06-18 05:36:36 +00:00
|
|
|
|
|
|
|
var gotlsConfig *gotls.Config
|
|
|
|
|
|
|
|
if tlsConfig != nil {
|
|
|
|
gotlsConfig = tlsConfig.GetTLSConfig(tls.WithDestination(dest))
|
|
|
|
}
|
|
|
|
|
|
|
|
dialContext := func(ctxInner context.Context) (net.Conn, error) {
|
2024-06-20 23:30:51 +00:00
|
|
|
conn, err := internet.DialSystem(ctxInner, dest, streamSettings.SocketSettings)
|
2024-06-18 05:36:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if gotlsConfig != nil {
|
|
|
|
if fingerprint := tls.GetFingerprint(tlsConfig.Fingerprint); fingerprint != nil {
|
|
|
|
conn = tls.UClient(conn, gotlsConfig, fingerprint)
|
2024-06-20 23:30:51 +00:00
|
|
|
if err := conn.(*tls.UConn).HandshakeContext(ctxInner); err != nil {
|
2024-06-18 05:36:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
conn = tls.Client(conn, gotlsConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var uploadTransport http.RoundTripper
|
|
|
|
var downloadTransport http.RoundTripper
|
|
|
|
|
2024-06-23 17:05:37 +00:00
|
|
|
if isH2 {
|
2024-06-18 05:36:36 +00:00
|
|
|
downloadTransport = &http2.Transport{
|
|
|
|
DialTLSContext: func(ctxInner context.Context, network string, addr string, cfg *gotls.Config) (net.Conn, error) {
|
|
|
|
return dialContext(ctxInner)
|
|
|
|
},
|
|
|
|
IdleConnTimeout: 90 * time.Second,
|
|
|
|
}
|
|
|
|
uploadTransport = downloadTransport
|
|
|
|
} else {
|
|
|
|
httpDialContext := func(ctxInner context.Context, network string, addr string) (net.Conn, error) {
|
|
|
|
return dialContext(ctxInner)
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadTransport = &http.Transport{
|
|
|
|
DialTLSContext: httpDialContext,
|
|
|
|
DialContext: httpDialContext,
|
|
|
|
IdleConnTimeout: 90 * time.Second,
|
|
|
|
// chunked transfer download with keepalives is buggy with
|
|
|
|
// http.Client and our custom dial context.
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// we use uploadRawPool for that
|
|
|
|
uploadTransport = nil
|
|
|
|
}
|
|
|
|
|
2024-07-11 07:56:20 +00:00
|
|
|
client := &DefaultDialerClient{
|
|
|
|
transportConfig: streamSettings.ProtocolSettings.(*Config),
|
2024-06-18 05:36:36 +00:00
|
|
|
download: &http.Client{
|
|
|
|
Transport: downloadTransport,
|
|
|
|
},
|
|
|
|
upload: &http.Client{
|
|
|
|
Transport: uploadTransport,
|
|
|
|
},
|
2024-06-23 17:05:37 +00:00
|
|
|
isH2: isH2,
|
2024-06-18 05:36:36 +00:00
|
|
|
uploadRawPool: &sync.Pool{},
|
|
|
|
dialUploadConn: dialContext,
|
|
|
|
}
|
|
|
|
|
|
|
|
globalDialerMap[dialerConf{dest, streamSettings}] = client
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogInfo(ctx, "dialing splithttp to ", dest)
|
2024-06-18 05:36:36 +00:00
|
|
|
|
|
|
|
var requestURL url.URL
|
|
|
|
|
|
|
|
transportConfiguration := streamSettings.ProtocolSettings.(*Config)
|
|
|
|
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
|
|
|
|
|
|
|
|
maxConcurrentUploads := transportConfiguration.GetNormalizedMaxConcurrentUploads()
|
|
|
|
maxUploadSize := transportConfiguration.GetNormalizedMaxUploadSize()
|
|
|
|
|
|
|
|
if tlsConfig != nil {
|
|
|
|
requestURL.Scheme = "https"
|
|
|
|
} else {
|
|
|
|
requestURL.Scheme = "http"
|
|
|
|
}
|
|
|
|
requestURL.Host = transportConfiguration.Host
|
|
|
|
if requestURL.Host == "" {
|
|
|
|
requestURL.Host = dest.NetAddr()
|
|
|
|
}
|
|
|
|
requestURL.Path = transportConfiguration.GetNormalizedPath()
|
|
|
|
|
|
|
|
httpClient := getHTTPClient(ctx, dest, streamSettings)
|
|
|
|
|
|
|
|
sessionIdUuid := uuid.New()
|
|
|
|
sessionId := sessionIdUuid.String()
|
2024-07-11 07:56:20 +00:00
|
|
|
baseURL := requestURL.String() + sessionId
|
2024-06-18 05:36:36 +00:00
|
|
|
|
|
|
|
uploadPipeReader, uploadPipeWriter := pipe.New(pipe.WithSizeLimit(maxUploadSize))
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
requestsLimiter := semaphore.New(int(maxConcurrentUploads))
|
|
|
|
var requestCounter int64
|
|
|
|
|
|
|
|
// by offloading the uploads into a buffered pipe, multiple conn.Write
|
|
|
|
// calls get automatically batched together into larger POST requests.
|
|
|
|
// without batching, bandwidth is extremely limited.
|
|
|
|
for {
|
|
|
|
chunk, err := uploadPipeReader.ReadMultiBuffer()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
<-requestsLimiter.Wait()
|
|
|
|
|
2024-07-11 07:56:20 +00:00
|
|
|
seq := requestCounter
|
2024-06-18 05:36:36 +00:00
|
|
|
requestCounter += 1
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer requestsLimiter.Signal()
|
2024-07-11 07:56:20 +00:00
|
|
|
|
|
|
|
err := httpClient.SendUploadRequest(
|
|
|
|
context.WithoutCancel(ctx),
|
|
|
|
baseURL+"/"+strconv.FormatInt(seq, 10),
|
|
|
|
&buf.MultiBufferContainer{MultiBuffer: chunk},
|
|
|
|
int64(chunk.Len()),
|
|
|
|
)
|
|
|
|
|
2024-06-18 05:36:36 +00:00
|
|
|
if err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogInfoInner(ctx, err, "failed to send upload")
|
2024-06-18 05:36:36 +00:00
|
|
|
uploadPipeReader.Interrupt()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-07-11 07:56:20 +00:00
|
|
|
lazyRawDownload, remoteAddr, localAddr, err := httpClient.OpenDownload(context.WithoutCancel(ctx), baseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-18 05:36:36 +00:00
|
|
|
|
2024-06-20 23:30:51 +00:00
|
|
|
lazyDownload := &LazyReader{
|
|
|
|
CreateReader: func() (io.ReadCloser, error) {
|
2024-07-11 07:56:20 +00:00
|
|
|
// skip "ooooooooook" response
|
|
|
|
trashHeader := []byte{0}
|
|
|
|
for {
|
|
|
|
_, err := io.ReadFull(lazyRawDownload, trashHeader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("failed to read initial response").Base(err)
|
|
|
|
}
|
|
|
|
if trashHeader[0] == 'k' {
|
|
|
|
break
|
|
|
|
}
|
2024-06-20 23:30:51 +00:00
|
|
|
}
|
2024-07-11 07:56:20 +00:00
|
|
|
|
|
|
|
return lazyRawDownload, nil
|
2024-06-20 23:30:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-07-11 07:56:20 +00:00
|
|
|
// necessary in order to send larger chunks in upload
|
|
|
|
bufferedUploadPipeWriter := buf.NewBufferedWriter(uploadPipeWriter)
|
|
|
|
bufferedUploadPipeWriter.SetBuffered(false)
|
|
|
|
|
2024-06-18 05:36:36 +00:00
|
|
|
conn := splitConn{
|
|
|
|
writer: bufferedUploadPipeWriter,
|
2024-06-20 23:30:51 +00:00
|
|
|
reader: lazyDownload,
|
2024-06-18 05:36:36 +00:00
|
|
|
remoteAddr: remoteAddr,
|
|
|
|
localAddr: localAddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat.Connection(&conn), nil
|
|
|
|
}
|