|
|
|
@ -16,6 +16,7 @@ import (
|
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
|
"github.com/xtls/xray-core/common/session"
|
|
|
|
|
"github.com/xtls/xray-core/common/signal/done"
|
|
|
|
|
"github.com/xtls/xray-core/common/signal/semaphore"
|
|
|
|
|
"github.com/xtls/xray-core/common/uuid"
|
|
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
|
|
|
@ -44,18 +45,6 @@ var (
|
|
|
|
|
globalDialerAccess sync.Mutex
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func destroyHTTPClient(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) {
|
|
|
|
|
globalDialerAccess.Lock()
|
|
|
|
|
defer globalDialerAccess.Unlock()
|
|
|
|
|
|
|
|
|
|
if globalDialerMap == nil {
|
|
|
|
|
globalDialerMap = make(map[dialerConf]reusedClient)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete(globalDialerMap, dialerConf{dest, streamSettings})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) reusedClient {
|
|
|
|
|
globalDialerAccess.Lock()
|
|
|
|
|
defer globalDialerAccess.Unlock()
|
|
|
|
@ -77,7 +66,7 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dialContext := func(ctxInner context.Context) (net.Conn, error) {
|
|
|
|
|
conn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
|
|
|
|
|
conn, err := internet.DialSystem(ctxInner, dest, streamSettings.SocketSettings)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -85,7 +74,7 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
|
|
|
|
|
if gotlsConfig != nil {
|
|
|
|
|
if fingerprint := tls.GetFingerprint(tlsConfig.Fingerprint); fingerprint != nil {
|
|
|
|
|
conn = tls.UClient(conn, gotlsConfig, fingerprint)
|
|
|
|
|
if err := conn.(*tls.UConn).HandshakeContext(ctx); err != nil {
|
|
|
|
|
if err := conn.(*tls.UConn).HandshakeContext(ctxInner); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
@ -171,49 +160,73 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
|
|
|
|
|
|
|
|
|
|
var remoteAddr gonet.Addr
|
|
|
|
|
var localAddr gonet.Addr
|
|
|
|
|
// this is done when the TCP/UDP connection to the server was established,
|
|
|
|
|
// and we can unblock the Dial function and print correct net addresses in
|
|
|
|
|
// logs
|
|
|
|
|
gotConn := done.New()
|
|
|
|
|
|
|
|
|
|
var downResponse io.ReadCloser
|
|
|
|
|
gotDownResponse := done.New()
|
|
|
|
|
|
|
|
|
|
sessionIdUuid := uuid.New()
|
|
|
|
|
sessionId := sessionIdUuid.String()
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
trace := &httptrace.ClientTrace{
|
|
|
|
|
GotConn: func(connInfo httptrace.GotConnInfo) {
|
|
|
|
|
remoteAddr = connInfo.Conn.RemoteAddr()
|
|
|
|
|
localAddr = connInfo.Conn.LocalAddr()
|
|
|
|
|
gotConn.Close()
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sessionIdUuid := uuid.New()
|
|
|
|
|
sessionId := sessionIdUuid.String()
|
|
|
|
|
// in case we hit an error, we want to unblock this part
|
|
|
|
|
defer gotConn.Close()
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(
|
|
|
|
|
httptrace.WithClientTrace(ctx, trace),
|
|
|
|
|
httptrace.WithClientTrace(context.WithoutCancel(ctx), trace),
|
|
|
|
|
"GET",
|
|
|
|
|
requestURL.String()+"?session="+sessionId,
|
|
|
|
|
requestURL.String()+sessionId,
|
|
|
|
|
nil,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
newError("failed to construct download http request").Base(err).WriteToLog()
|
|
|
|
|
gotDownResponse.Close()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req.Header = transportConfiguration.GetRequestHeader()
|
|
|
|
|
|
|
|
|
|
downResponse, err := httpClient.download.Do(req)
|
|
|
|
|
response, err := httpClient.download.Do(req)
|
|
|
|
|
gotConn.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
// workaround for various connection pool related issues, mostly around
|
|
|
|
|
// HTTP/1.1. if the http client ever fails to send a request, we simply
|
|
|
|
|
// delete it entirely.
|
|
|
|
|
// in HTTP/1.1, it was observed that pool connections would immediately
|
|
|
|
|
// fail with "context canceled" if the previous http response body was
|
|
|
|
|
// not explicitly BOTH drained and closed. at the same time, sometimes
|
|
|
|
|
// the draining itself takes forever and causes more problems.
|
|
|
|
|
// see also https://github.com/golang/go/issues/60240
|
|
|
|
|
destroyHTTPClient(ctx, dest, streamSettings)
|
|
|
|
|
return nil, newError("failed to send download http request, destroying client").Base(err)
|
|
|
|
|
newError("failed to send download http request").Base(err).WriteToLog()
|
|
|
|
|
gotDownResponse.Close()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if downResponse.StatusCode != 200 {
|
|
|
|
|
downResponse.Body.Close()
|
|
|
|
|
return nil, newError("invalid status code on download:", downResponse.Status)
|
|
|
|
|
if response.StatusCode != 200 {
|
|
|
|
|
response.Body.Close()
|
|
|
|
|
newError("invalid status code on download:", response.Status).WriteToLog()
|
|
|
|
|
gotDownResponse.Close()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uploadUrl := requestURL.String() + "?session=" + sessionId + "&seq="
|
|
|
|
|
// skip "ok" response
|
|
|
|
|
trashHeader := []byte{0, 0}
|
|
|
|
|
_, err = io.ReadFull(response.Body, trashHeader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Body.Close()
|
|
|
|
|
newError("failed to read initial response").Base(err).WriteToLog()
|
|
|
|
|
gotDownResponse.Close()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
downResponse = response.Body
|
|
|
|
|
gotDownResponse.Close()
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
uploadUrl := requestURL.String() + sessionId + "/"
|
|
|
|
|
|
|
|
|
|
uploadPipeReader, uploadPipeWriter := pipe.New(pipe.WithSizeLimit(maxUploadSize))
|
|
|
|
|
|
|
|
|
@ -266,7 +279,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
|
uploadConn = httpClient.uploadRawPool.Get()
|
|
|
|
|
if uploadConn == nil {
|
|
|
|
|
uploadConn, err = httpClient.dialUploadConn(ctx)
|
|
|
|
|
uploadConn, err = httpClient.dialUploadConn(context.WithoutCancel(ctx))
|
|
|
|
|
if err != nil {
|
|
|
|
|
newError("failed to connect upload").Base(err).WriteToLog()
|
|
|
|
|
uploadPipeReader.Interrupt()
|
|
|
|
@ -293,21 +306,27 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// skip "ok" response
|
|
|
|
|
trashHeader := []byte{0, 0}
|
|
|
|
|
_, err = io.ReadFull(downResponse.Body, trashHeader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
downResponse.Body.Close()
|
|
|
|
|
return nil, newError("failed to read initial response")
|
|
|
|
|
}
|
|
|
|
|
// we want to block Dial until we know the remote address of the server,
|
|
|
|
|
// for logging purposes
|
|
|
|
|
<-gotConn.Wait()
|
|
|
|
|
|
|
|
|
|
// necessary in order to send larger chunks in upload
|
|
|
|
|
bufferedUploadPipeWriter := buf.NewBufferedWriter(uploadPipeWriter)
|
|
|
|
|
bufferedUploadPipeWriter.SetBuffered(false)
|
|
|
|
|
|
|
|
|
|
lazyDownload := &LazyReader{
|
|
|
|
|
CreateReader: func() (io.ReadCloser, error) {
|
|
|
|
|
<-gotDownResponse.Wait()
|
|
|
|
|
if downResponse == nil {
|
|
|
|
|
return nil, newError("downResponse failed")
|
|
|
|
|
}
|
|
|
|
|
return downResponse, nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn := splitConn{
|
|
|
|
|
writer: bufferedUploadPipeWriter,
|
|
|
|
|
reader: downResponse.Body,
|
|
|
|
|
reader: lazyDownload,
|
|
|
|
|
remoteAddr: remoteAddr,
|
|
|
|
|
localAddr: localAddr,
|
|
|
|
|
}
|
|
|
|
|