Skip to content

Commit 4b7947c

Browse files
authored
SplitHTTP server: add ok padding (XTLS#3614)
1 parent 4c82ef8 commit 4b7947c

5 files changed

Lines changed: 80 additions & 43 deletions

File tree

infra/conf/transport_internet.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,22 @@ type SplitHTTPConfig struct {
229229
Host string `json:"host"`
230230
Path string `json:"path"`
231231
Headers map[string]string `json:"headers"`
232-
ScMaxConcurrentPosts Int32Range `json:"scMaxConcurrentPosts"`
233-
ScMaxEachPostBytes Int32Range `json:"scMaxEachPostBytes"`
234-
ScMinPostsIntervalMs Int32Range `json:"scMinPostsIntervalMs"`
232+
ScMaxConcurrentPosts *Int32Range `json:"scMaxConcurrentPosts"`
233+
ScMaxEachPostBytes *Int32Range `json:"scMaxEachPostBytes"`
234+
ScMinPostsIntervalMs *Int32Range `json:"scMinPostsIntervalMs"`
235235
NoSSEHeader bool `json:"noSSEHeader"`
236+
ResponseOkPadding *Int32Range `json:"responseOkPadding"`
237+
}
238+
239+
func splithttpNewRandRangeConfig(input *Int32Range) *splithttp.RandRangeConfig {
240+
if input == nil {
241+
return nil
242+
}
243+
244+
return &splithttp.RandRangeConfig{
245+
From: input.From,
246+
To: input.To,
247+
}
236248
}
237249

238250
// Build implements Buildable.
@@ -246,22 +258,14 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
246258
c.Host = c.Headers["Host"]
247259
}
248260
config := &splithttp.Config{
249-
Path: c.Path,
250-
Host: c.Host,
251-
Header: c.Headers,
252-
ScMaxConcurrentPosts: &splithttp.RandRangeConfig{
253-
From: c.ScMaxConcurrentPosts.From,
254-
To: c.ScMaxConcurrentPosts.To,
255-
},
256-
ScMaxEachPostBytes: &splithttp.RandRangeConfig{
257-
From: c.ScMaxEachPostBytes.From,
258-
To: c.ScMaxEachPostBytes.To,
259-
},
260-
ScMinPostsIntervalMs: &splithttp.RandRangeConfig{
261-
From: c.ScMinPostsIntervalMs.From,
262-
To: c.ScMinPostsIntervalMs.To,
263-
},
264-
NoSSEHeader: c.NoSSEHeader,
261+
Path: c.Path,
262+
Host: c.Host,
263+
Header: c.Headers,
264+
ScMaxConcurrentPosts: splithttpNewRandRangeConfig(c.ScMaxConcurrentPosts),
265+
ScMaxEachPostBytes: splithttpNewRandRangeConfig(c.ScMaxEachPostBytes),
266+
ScMinPostsIntervalMs: splithttpNewRandRangeConfig(c.ScMinPostsIntervalMs),
267+
NoSSEHeader: c.NoSSEHeader,
268+
ResponseOkPadding: splithttpNewRandRangeConfig(c.ResponseOkPadding),
265269
}
266270
return config, nil
267271
}

transport/internet/splithttp/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ func (c *Config) GetNormalizedScMinPostsIntervalMs() RandRangeConfig {
6969
return *c.ScMinPostsIntervalMs
7070
}
7171

72+
func (c *Config) GetNormalizedResponseOkPadding() RandRangeConfig {
73+
if c.ResponseOkPadding == nil || c.ResponseOkPadding.To == 0 {
74+
return RandRangeConfig{
75+
From: 100,
76+
To: 1000,
77+
}
78+
}
79+
80+
return *c.ResponseOkPadding
81+
}
82+
7283
func init() {
7384
common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} {
7485
return new(Config)

transport/internet/splithttp/config.pb.go

Lines changed: 40 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transport/internet/splithttp/config.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ message Config {
1414
RandRangeConfig scMaxEachPostBytes = 5;
1515
RandRangeConfig scMinPostsIntervalMs = 6;
1616
bool noSSEHeader = 7;
17+
RandRangeConfig responseOkPadding = 8;
1718
}
1819

1920
message RandRangeConfig {

transport/internet/splithttp/hub.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
124124

125125
currentSession := h.upsertSession(sessionId)
126126
scMaxEachPostBytes := int(h.ln.config.GetNormalizedScMaxEachPostBytes().To)
127+
responseOkPadding := h.ln.config.GetNormalizedResponseOkPadding()
127128

128129
if request.Method == "POST" {
129130
seq := ""
@@ -192,6 +193,10 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
192193
// send a chunk immediately to enable CDN streaming.
193194
// many CDN buffer the response headers until the origin starts sending
194195
// the body, with no way to turn it off.
196+
padding := int(responseOkPadding.roll())
197+
for i := 0; i < padding; i++ {
198+
writer.Write([]byte("o"))
199+
}
195200
writer.Write([]byte("ok"))
196201
responseFlusher.Flush()
197202

0 commit comments

Comments
 (0)