Skip to content

Commit

Permalink
XHTTP config: Add keepAlivePeriod for client (#4075)
Browse files Browse the repository at this point in the history
Closes #4053

---------

Co-authored-by: RPRX <[email protected]>
  • Loading branch information
Fangliding and RPRX committed Nov 29, 2024
1 parent f7bd98b commit c87cf8f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 46 deletions.
2 changes: 2 additions & 0 deletions infra/conf/transport_internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ type SplitHTTPConfig struct {
Mode string `json:"mode"`
Extra json.RawMessage `json:"extra"`
NoGRPCHeader bool `json:"noGRPCHeader"`
KeepAlivePeriod int64 `json:"keepAlivePeriod"`
}

type Xmux struct {
Expand Down Expand Up @@ -324,6 +325,7 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
Xmux: &muxProtobuf,
Mode: c.Mode,
NoGRPCHeader: c.NoGRPCHeader,
KeepAlivePeriod: c.KeepAlivePeriod,
}
var err error
if c.DownloadSettings != nil {
Expand Down
92 changes: 51 additions & 41 deletions transport/internet/splithttp/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions transport/internet/splithttp/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ message Config {
xray.transport.internet.StreamConfig downloadSettings = 10;
string mode = 11;
bool noGRPCHeader = 12;
int64 keepAlivePeriod = 13;
}

message RandRangeConfig {
Expand Down
24 changes: 19 additions & 5 deletions transport/internet/splithttp/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import (
const connIdleTimeout = 300 * time.Second

// consistent with quic-go
const h3KeepalivePeriod = 10 * time.Second
const quicgoH3KeepAlivePeriod = 10 * time.Second

// consistent with chrome
const h2KeepalivePeriod = 45 * time.Second
const chromeH2KeepAlivePeriod = 45 * time.Second

type dialerConf struct {
net.Destination
Expand Down Expand Up @@ -133,17 +133,25 @@ func createHTTPClient(dest net.Destination, streamSettings *internet.MemoryStrea
return conn, nil
}

keepAlivePeriod := time.Duration(streamSettings.ProtocolSettings.(*Config).KeepAlivePeriod) * time.Second

var transport http.RoundTripper

if isH3 {
if keepAlivePeriod == 0 {
keepAlivePeriod = quicgoH3KeepAlivePeriod
}
if keepAlivePeriod < 0 {
keepAlivePeriod = 0
}
quicConfig := &quic.Config{
MaxIdleTimeout: connIdleTimeout,

// these two are defaults of quic-go/http3. the default of quic-go (no
// http3) is different, so it is hardcoded here for clarity.
// https://github.com/quic-go/quic-go/blob/b8ea5c798155950fb5bbfdd06cad1939c9355878/http3/client.go#L36-L39
MaxIncomingStreams: -1,
KeepAlivePeriod: h3KeepalivePeriod,
KeepAlivePeriod: keepAlivePeriod,
}
transport = &http3.RoundTripper{
QUICConfig: quicConfig,
Expand Down Expand Up @@ -186,12 +194,18 @@ func createHTTPClient(dest net.Destination, streamSettings *internet.MemoryStrea
},
}
} else if isH2 {
if keepAlivePeriod == 0 {
keepAlivePeriod = chromeH2KeepAlivePeriod
}
if keepAlivePeriod < 0 {
keepAlivePeriod = 0
}
transport = &http2.Transport{
DialTLSContext: func(ctxInner context.Context, network string, addr string, cfg *gotls.Config) (net.Conn, error) {
return dialContext(ctxInner)
},
IdleConnTimeout: connIdleTimeout,
ReadIdleTimeout: h2KeepalivePeriod,
ReadIdleTimeout: keepAlivePeriod,
}
} else {
httpDialContext := func(ctxInner context.Context, network string, addr string) (net.Conn, error) {
Expand All @@ -202,7 +216,7 @@ func createHTTPClient(dest net.Destination, streamSettings *internet.MemoryStrea
DialTLSContext: httpDialContext,
DialContext: httpDialContext,
IdleConnTimeout: connIdleTimeout,
// chunked transfer download with keepalives is buggy with
// chunked transfer download with KeepAlives is buggy with
// http.Client and our custom dial context.
DisableKeepAlives: true,
}
Expand Down

0 comments on commit c87cf8f

Please sign in to comment.