Skip to content

Commit 0ac13bd

Browse files
authored
XHTTP transport: Bugfixes for obfuscations (#5720)
#5720 (comment)
1 parent eec2802 commit 0ac13bd

File tree

12 files changed

+554
-409
lines changed

12 files changed

+554
-409
lines changed

infra/conf/transport_internet.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,14 @@ type SplitHTTPConfig struct {
230230
SeqKey string `json:"seqKey"`
231231
UplinkDataPlacement string `json:"uplinkDataPlacement"`
232232
UplinkDataKey string `json:"uplinkDataKey"`
233-
UplinkChunkSize uint32 `json:"uplinkChunkSize"`
233+
UplinkChunkSize Int32Range `json:"uplinkChunkSize"`
234234
NoGRPCHeader bool `json:"noGRPCHeader"`
235235
NoSSEHeader bool `json:"noSSEHeader"`
236236
ScMaxEachPostBytes Int32Range `json:"scMaxEachPostBytes"`
237237
ScMinPostsIntervalMs Int32Range `json:"scMinPostsIntervalMs"`
238238
ScMaxBufferedPosts int64 `json:"scMaxBufferedPosts"`
239239
ScStreamUpServerSecs Int32Range `json:"scStreamUpServerSecs"`
240+
ServerMaxHeaderBytes int32 `json:"serverMaxHeaderBytes"`
240241
Xmux XmuxConfig `json:"xmux"`
241242
DownloadSettings *StreamConfig `json:"downloadSettings"`
242243
Extra json.RawMessage `json:"extra"`
@@ -316,9 +317,9 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
316317

317318
switch c.UplinkDataPlacement {
318319
case "":
319-
c.UplinkDataPlacement = "body"
320-
case "body":
321-
case "cookie", "header":
320+
c.UplinkDataPlacement = splithttp.PlacementAuto
321+
case splithttp.PlacementAuto, splithttp.PlacementBody:
322+
case splithttp.PlacementCookie, splithttp.PlacementHeader:
322323
if c.Mode != "packet-up" {
323324
return nil, errors.New("UplinkDataPlacement can be " + c.UplinkDataPlacement + " only in packet-up mode")
324325
}
@@ -347,9 +348,6 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
347348
case "":
348349
c.SeqPlacement = "path"
349350
case "path", "cookie", "header", "query":
350-
if c.SessionPlacement == "path" {
351-
return nil, errors.New("SeqPlacement must be path when SessionPlacement is path")
352-
}
353351
default:
354352
return nil, errors.New("unsupported seq placement: " + c.SeqPlacement)
355353
}
@@ -372,24 +370,17 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
372370
}
373371
}
374372

375-
if c.UplinkDataPlacement != "body" && c.UplinkDataKey == "" {
373+
if c.UplinkDataPlacement != splithttp.PlacementBody && c.UplinkDataKey == "" {
376374
switch c.UplinkDataPlacement {
377-
case "cookie":
375+
case splithttp.PlacementCookie:
378376
c.UplinkDataKey = "x_data"
379-
case "header":
377+
case splithttp.PlacementAuto, splithttp.PlacementHeader:
380378
c.UplinkDataKey = "X-Data"
381379
}
382380
}
383381

384-
if c.UplinkChunkSize == 0 {
385-
switch c.UplinkDataPlacement {
386-
case "cookie":
387-
c.UplinkChunkSize = 3 * 1024 // 3KB
388-
case "header":
389-
c.UplinkChunkSize = 4 * 1024 // 4KB
390-
}
391-
} else if c.UplinkChunkSize < 64 {
392-
c.UplinkChunkSize = 64
382+
if c.ServerMaxHeaderBytes < 0 {
383+
return nil, errors.New("invalid negative value of maxHeaderBytes")
393384
}
394385

395386
if c.Xmux.MaxConnections.To > 0 && c.Xmux.MaxConcurrency.To > 0 {
@@ -422,13 +413,14 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
422413
SeqKey: c.SeqKey,
423414
UplinkDataPlacement: c.UplinkDataPlacement,
424415
UplinkDataKey: c.UplinkDataKey,
425-
UplinkChunkSize: c.UplinkChunkSize,
416+
UplinkChunkSize: newRangeConfig(c.UplinkChunkSize),
426417
NoGRPCHeader: c.NoGRPCHeader,
427418
NoSSEHeader: c.NoSSEHeader,
428419
ScMaxEachPostBytes: newRangeConfig(c.ScMaxEachPostBytes),
429420
ScMinPostsIntervalMs: newRangeConfig(c.ScMinPostsIntervalMs),
430421
ScMaxBufferedPosts: c.ScMaxBufferedPosts,
431422
ScStreamUpServerSecs: newRangeConfig(c.ScStreamUpServerSecs),
423+
ServerMaxHeaderBytes: c.ServerMaxHeaderBytes,
432424
Xmux: &splithttp.XmuxConfig{
433425
MaxConcurrency: newRangeConfig(c.Xmux.MaxConcurrency),
434426
MaxConnections: newRangeConfig(c.Xmux.MaxConnections),

transport/internet/browser_dialer/dialer.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type task struct {
2222
Method string `json:"method"`
2323
URL string `json:"url"`
2424
Extra any `json:"extra,omitempty"`
25+
StreamResponse bool `json:"streamResponse"`
2526
}
2627

2728
var conns chan *websocket.Conn
@@ -52,6 +53,7 @@ func init() {
5253
}
5354
}
5455
} else {
56+
w.Header().Set("Access-Control-Allow-Origin", "*");
5557
w.Write(webpage)
5658
}
5759
}))
@@ -70,6 +72,7 @@ func DialWS(uri string, ed []byte) (*websocket.Conn, error) {
7072
task := task{
7173
Method: "WS",
7274
URL: uri,
75+
StreamResponse: true,
7376
}
7477

7578
if ed != nil {
@@ -84,9 +87,10 @@ func DialWS(uri string, ed []byte) (*websocket.Conn, error) {
8487
type httpExtra struct {
8588
Referrer string `json:"referrer,omitempty"`
8689
Headers map[string]string `json:"headers,omitempty"`
90+
Cookies map[string]string `json:"cookies,omitempty"`
8791
}
8892

89-
func httpExtraFromHeaders(headers http.Header) *httpExtra {
93+
func httpExtraFromHeadersAndCookies(headers http.Header, cookies []*http.Cookie) *httpExtra {
9094
if len(headers) == 0 {
9195
return nil
9296
}
@@ -104,24 +108,37 @@ func httpExtraFromHeaders(headers http.Header) *httpExtra {
104108
}
105109
}
106110

111+
if len(cookies) > 0 {
112+
extra.Cookies = make(map[string]string)
113+
for _, cookie := range cookies {
114+
extra.Cookies[cookie.Name] = cookie.Value
115+
}
116+
}
117+
107118
return &extra
108119
}
109120

110-
func DialGet(uri string, headers http.Header) (*websocket.Conn, error) {
121+
func DialGet(uri string, headers http.Header, cookies []*http.Cookie) (*websocket.Conn, error) {
111122
task := task{
112123
Method: "GET",
113124
URL: uri,
114-
Extra: httpExtraFromHeaders(headers),
125+
Extra: httpExtraFromHeadersAndCookies(headers, cookies),
126+
StreamResponse: true,
115127
}
116128

117129
return dialTask(task)
118130
}
119131

120-
func DialPost(uri string, headers http.Header, payload []byte) error {
132+
func DialPacket(method string, uri string, headers http.Header, cookies []*http.Cookie, payload []byte) error {
133+
return dialWithBody(method, uri, headers, cookies, payload)
134+
}
135+
136+
func dialWithBody(method string, uri string, headers http.Header, cookies []*http.Cookie, payload []byte) error {
121137
task := task{
122-
Method: "POST",
138+
Method: method,
123139
URL: uri,
124-
Extra: httpExtraFromHeaders(headers),
140+
Extra: httpExtraFromHeadersAndCookies(headers, cookies),
141+
StreamResponse: false,
125142
}
126143

127144
conn, err := dialTask(task)

0 commit comments

Comments
 (0)