From ab3c00e96b3af6885c09acfdd1f8d36e60d90bad Mon Sep 17 00:00:00 2001 From: mmmray <142015632+mmmray@users.noreply.github.com> Date: Tue, 3 Sep 2024 04:25:15 +0200 Subject: [PATCH] SplitHTTP: Remove `ok` compatibility logic (#3753) Remove some code that was added to maintain compatibility with older Xray versions. This breaks compatibility with Xray-core v1.8.23 or older. --- transport/internet/splithttp/dialer.go | 4 +- transport/internet/splithttp/hub.go | 8 ---- .../internet/splithttp/strip_ok_reader.go | 48 ------------------- 3 files changed, 1 insertion(+), 59 deletions(-) delete mode 100644 transport/internet/splithttp/strip_ok_reader.go diff --git a/transport/internet/splithttp/dialer.go b/transport/internet/splithttp/dialer.go index 35e8c7b43cdc..69a3087003db 100644 --- a/transport/internet/splithttp/dialer.go +++ b/transport/internet/splithttp/dialer.go @@ -285,13 +285,11 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me } }() - lazyRawDownload, remoteAddr, localAddr, err := httpClient.OpenDownload(context.WithoutCancel(ctx), requestURL.String()) + reader, remoteAddr, localAddr, err := httpClient.OpenDownload(context.WithoutCancel(ctx), requestURL.String()) if err != nil { return nil, err } - reader := &stripOkReader{ReadCloser: lazyRawDownload} - writer := uploadWriter{ uploadPipeWriter, maxUploadSize, diff --git a/transport/internet/splithttp/hub.go b/transport/internet/splithttp/hub.go index 4a05e1c32fde..423bf6e37fae 100644 --- a/transport/internet/splithttp/hub.go +++ b/transport/internet/splithttp/hub.go @@ -196,14 +196,6 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req h.config.WriteResponseHeader(writer) writer.WriteHeader(http.StatusOK) - if _, ok := request.URL.Query()["x_padding"]; !ok { - // in earlier versions, this initial body data was used to immediately - // start a 200 OK on all CDN. but xray client since 1.8.16 does not - // actually require an immediate 200 OK, but now requires these - // additional bytes "ok". xray client 1.8.24+ doesn't require "ok" - // anymore, and so this line should be removed in later versions. - writer.Write([]byte("ok")) - } responseFlusher.Flush() diff --git a/transport/internet/splithttp/strip_ok_reader.go b/transport/internet/splithttp/strip_ok_reader.go deleted file mode 100644 index 5dbbe22bebc4..000000000000 --- a/transport/internet/splithttp/strip_ok_reader.go +++ /dev/null @@ -1,48 +0,0 @@ -package splithttp - -import ( - "bytes" - "io" - - "github.com/xtls/xray-core/common/errors" -) - -// in older versions of splithttp, the server would respond with `ok` to flush -// out HTTP response headers early. Response headers and a 200 OK were required -// to initiate the connection. Later versions of splithttp dropped this -// requirement, and in xray 1.8.24 the server stopped sending "ok" if it sees -// x_padding. For compatibility, we need to remove "ok" from the underlying -// reader if it exists, and otherwise forward the stream as-is. -type stripOkReader struct { - io.ReadCloser - firstDone bool - prefixRead []byte -} - -func (r *stripOkReader) Read(b []byte) (int, error) { - if !r.firstDone { - r.firstDone = true - - // skip "ok" response - prefixRead := []byte{0, 0} - _, err := io.ReadFull(r.ReadCloser, prefixRead) - if err != nil { - return 0, errors.New("failed to read initial response").Base(err) - } - - if !bytes.Equal(prefixRead, []byte("ok")) { - // we read some garbage byte that may not have been "ok" at - // all. return a reader that replays what we have read so far - r.prefixRead = prefixRead - } - } - - if len(r.prefixRead) > 0 { - n := copy(b, r.prefixRead) - r.prefixRead = r.prefixRead[n:] - return n, nil - } - - n, err := r.ReadCloser.Read(b) - return n, err -}