mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 09:18:34 +00:00
SplitHTTP: Client supports HTTP/3 (#3543)
Closes https://github.com/XTLS/Xray-core/issues/3456 Co-authored-by: Fangliding <Fangliding.fshxy@outlook.com> Co-authored-by: mmmray <142015632+mmmray@users.noreply.github.com>
This commit is contained in:
parent
02cd3b8c74
commit
c40fc44a34
4 changed files with 36 additions and 8 deletions
|
@ -32,6 +32,7 @@ type DefaultDialerClient struct {
|
|||
download *http.Client
|
||||
upload *http.Client
|
||||
isH2 bool
|
||||
isH3 bool
|
||||
// pool of net.Conn, created using dialUploadConn
|
||||
uploadRawPool *sync.Pool
|
||||
dialUploadConn func(ctxInner context.Context) (net.Conn, error)
|
||||
|
@ -118,7 +119,7 @@ func (c *DefaultDialerClient) SendUploadRequest(ctx context.Context, url string,
|
|||
}
|
||||
req.Header = c.transportConfig.GetRequestHeader()
|
||||
|
||||
if c.isH2 {
|
||||
if c.isH2 || c.isH3 {
|
||||
resp, err := c.upload.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -10,6 +10,8 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/quic-go/quic-go"
|
||||
"github.com/quic-go/quic-go/http3"
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/buf"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
|
@ -50,12 +52,9 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
|
|||
return client
|
||||
}
|
||||
|
||||
if browser_dialer.HasBrowserDialer() {
|
||||
return &BrowserDialerClient{}
|
||||
}
|
||||
|
||||
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
|
||||
isH2 := tlsConfig != nil && !(len(tlsConfig.NextProtocol) == 1 && tlsConfig.NextProtocol[0] == "http/1.1")
|
||||
isH3 := tlsConfig != nil && (len(tlsConfig.NextProtocol) == 1 && tlsConfig.NextProtocol[0] == "h3")
|
||||
|
||||
var gotlsConfig *gotls.Config
|
||||
|
||||
|
@ -83,10 +82,35 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
|
|||
return conn, nil
|
||||
}
|
||||
|
||||
var uploadTransport http.RoundTripper
|
||||
var downloadTransport http.RoundTripper
|
||||
var uploadTransport http.RoundTripper
|
||||
|
||||
if isH2 {
|
||||
if isH3 {
|
||||
dest.Network = net.Network_UDP
|
||||
quicConfig := &quic.Config{
|
||||
HandshakeIdleTimeout: 10 * time.Second,
|
||||
MaxIdleTimeout: 90 * time.Second,
|
||||
KeepAlivePeriod: 3 * time.Second,
|
||||
Allow0RTT: true,
|
||||
}
|
||||
roundTripper := &http3.RoundTripper{
|
||||
TLSClientConfig: gotlsConfig,
|
||||
QUICConfig: quicConfig,
|
||||
Dial: func(ctx context.Context, addr string, tlsCfg *gotls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
|
||||
conn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
udpAddr, err := net.ResolveUDPAddr("udp", conn.RemoteAddr().String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return quic.DialEarly(ctx, conn.(*internet.PacketConnWrapper).Conn.(*net.UDPConn), udpAddr, tlsCfg, cfg)
|
||||
},
|
||||
}
|
||||
downloadTransport = roundTripper
|
||||
uploadTransport = roundTripper
|
||||
} else if isH2 {
|
||||
downloadTransport = &http2.Transport{
|
||||
DialTLSContext: func(ctxInner context.Context, network string, addr string, cfg *gotls.Config) (net.Conn, error) {
|
||||
return dialContext(ctxInner)
|
||||
|
@ -107,7 +131,6 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
|
|||
// http.Client and our custom dial context.
|
||||
DisableKeepAlives: true,
|
||||
}
|
||||
|
||||
// we use uploadRawPool for that
|
||||
uploadTransport = nil
|
||||
}
|
||||
|
@ -121,6 +144,7 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
|
|||
Transport: uploadTransport,
|
||||
},
|
||||
isH2: isH2,
|
||||
isH3: isH3,
|
||||
uploadRawPool: &sync.Pool{},
|
||||
dialUploadConn: dialContext,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue