mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-12-22 19:33:32 +02:00
XHTTP: Add "stream-one" mode for client & server (#4071)
""Breaking"": Client uses "stream-one" mode by default when using **REALITY** ("stream-up" if "downloadSettings" exists)
This commit is contained in:
parent
d8934cf839
commit
f7bd98b13c
5 changed files with 141 additions and 25 deletions
|
@ -307,7 +307,7 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
|
||||||
switch c.Mode {
|
switch c.Mode {
|
||||||
case "":
|
case "":
|
||||||
c.Mode = "auto"
|
c.Mode = "auto"
|
||||||
case "auto", "packet-up", "stream-up":
|
case "auto", "packet-up", "stream-up", "stream-one":
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("unsupported mode: " + c.Mode)
|
return nil, errors.New("unsupported mode: " + c.Mode)
|
||||||
}
|
}
|
||||||
|
@ -327,6 +327,9 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
if c.DownloadSettings != nil {
|
if c.DownloadSettings != nil {
|
||||||
|
if c.Mode == "stream-one" {
|
||||||
|
return nil, errors.New(`Can not use "downloadSettings" in "stream-one" mode.`)
|
||||||
|
}
|
||||||
if c.Extra != nil {
|
if c.Extra != nil {
|
||||||
c.DownloadSettings.SocketSettings = nil
|
c.DownloadSettings.SocketSettings = nil
|
||||||
}
|
}
|
||||||
|
@ -707,8 +710,10 @@ func (p TransportProtocol) Build() (string, error) {
|
||||||
case "ws", "websocket":
|
case "ws", "websocket":
|
||||||
return "websocket", nil
|
return "websocket", nil
|
||||||
case "h2", "h3", "http":
|
case "h2", "h3", "http":
|
||||||
|
errors.PrintDeprecatedFeatureWarning("HTTP transport", "XHTTP transport")
|
||||||
return "http", nil
|
return "http", nil
|
||||||
case "grpc":
|
case "grpc":
|
||||||
|
errors.PrintMigrateFeatureInfo("gRPC transport", "XHTTP transport")
|
||||||
return "grpc", nil
|
return "grpc", nil
|
||||||
case "httpupgrade":
|
case "httpupgrade":
|
||||||
return "httpupgrade", nil
|
return "httpupgrade", nil
|
||||||
|
|
|
@ -14,6 +14,10 @@ import (
|
||||||
// has no fields because everything is global state :O)
|
// has no fields because everything is global state :O)
|
||||||
type BrowserDialerClient struct{}
|
type BrowserDialerClient struct{}
|
||||||
|
|
||||||
|
func (c *BrowserDialerClient) Open(ctx context.Context, pureURL string) (io.WriteCloser, io.ReadCloser) {
|
||||||
|
panic("not implemented yet")
|
||||||
|
}
|
||||||
|
|
||||||
func (c *BrowserDialerClient) OpenUpload(ctx context.Context, baseURL string) io.WriteCloser {
|
func (c *BrowserDialerClient) OpenUpload(ctx context.Context, baseURL string) io.WriteCloser {
|
||||||
panic("not implemented yet")
|
panic("not implemented yet")
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,10 @@ type DialerClient interface {
|
||||||
// (ctx, baseURL) -> uploadWriter
|
// (ctx, baseURL) -> uploadWriter
|
||||||
// baseURL already contains sessionId
|
// baseURL already contains sessionId
|
||||||
OpenUpload(context.Context, string) io.WriteCloser
|
OpenUpload(context.Context, string) io.WriteCloser
|
||||||
|
|
||||||
|
// (ctx, pureURL) -> (uploadWriter, downloadReader)
|
||||||
|
// pureURL can not contain sessionId
|
||||||
|
Open(context.Context, string) (io.WriteCloser, io.ReadCloser)
|
||||||
}
|
}
|
||||||
|
|
||||||
// implements splithttp.DialerClient in terms of direct network connections
|
// implements splithttp.DialerClient in terms of direct network connections
|
||||||
|
@ -42,6 +46,30 @@ type DefaultDialerClient struct {
|
||||||
dialUploadConn func(ctxInner context.Context) (net.Conn, error)
|
dialUploadConn func(ctxInner context.Context) (net.Conn, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *DefaultDialerClient) Open(ctx context.Context, pureURL string) (io.WriteCloser, io.ReadCloser) {
|
||||||
|
reader, writer := io.Pipe()
|
||||||
|
req, _ := http.NewRequestWithContext(ctx, "POST", pureURL, reader)
|
||||||
|
req.Header = c.transportConfig.GetRequestHeader()
|
||||||
|
if !c.transportConfig.NoGRPCHeader {
|
||||||
|
req.Header.Set("Content-Type", "application/grpc")
|
||||||
|
}
|
||||||
|
wrc := &WaitReadCloser{Wait: make(chan struct{})}
|
||||||
|
go func() {
|
||||||
|
response, err := c.client.Do(req)
|
||||||
|
if err != nil || response.StatusCode != 200 {
|
||||||
|
if err != nil {
|
||||||
|
errors.LogInfoInner(ctx, err, "failed to open ", pureURL)
|
||||||
|
} else {
|
||||||
|
errors.LogInfo(ctx, "unexpected status ", response.StatusCode)
|
||||||
|
}
|
||||||
|
wrc.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
wrc.Set(response.Body)
|
||||||
|
}()
|
||||||
|
return writer, wrc
|
||||||
|
}
|
||||||
|
|
||||||
func (c *DefaultDialerClient) OpenUpload(ctx context.Context, baseURL string) io.WriteCloser {
|
func (c *DefaultDialerClient) OpenUpload(ctx context.Context, baseURL string) io.WriteCloser {
|
||||||
reader, writer := io.Pipe()
|
reader, writer := io.Pipe()
|
||||||
req, _ := http.NewRequestWithContext(ctx, "POST", baseURL, reader)
|
req, _ := http.NewRequestWithContext(ctx, "POST", baseURL, reader)
|
||||||
|
@ -226,3 +254,40 @@ func (c downloadBody) Close() error {
|
||||||
c.cancel()
|
c.cancel()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type WaitReadCloser struct {
|
||||||
|
Wait chan struct{}
|
||||||
|
io.ReadCloser
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WaitReadCloser) Set(rc io.ReadCloser) {
|
||||||
|
w.ReadCloser = rc
|
||||||
|
defer func() {
|
||||||
|
if recover() != nil {
|
||||||
|
rc.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
close(w.Wait)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WaitReadCloser) Read(b []byte) (int, error) {
|
||||||
|
if w.ReadCloser == nil {
|
||||||
|
if <-w.Wait; w.ReadCloser == nil {
|
||||||
|
return 0, io.ErrClosedPipe
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return w.ReadCloser.Read(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WaitReadCloser) Close() error {
|
||||||
|
if w.ReadCloser != nil {
|
||||||
|
return w.ReadCloser.Close()
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if recover() != nil && w.ReadCloser != nil {
|
||||||
|
w.ReadCloser.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
close(w.Wait)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package splithttp
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
gotls "crypto/tls"
|
gotls "crypto/tls"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -279,10 +280,34 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
|
||||||
requestURL2.RawQuery = config2.GetNormalizedQuery()
|
requestURL2.RawQuery = config2.GetNormalizedQuery()
|
||||||
}
|
}
|
||||||
|
|
||||||
reader, remoteAddr, localAddr, err := httpClient2.OpenDownload(context.WithoutCancel(ctx), requestURL2.String())
|
mode := transportConfiguration.Mode
|
||||||
|
if mode == "" || mode == "auto" {
|
||||||
|
mode = "packet-up"
|
||||||
|
if (tlsConfig != nil && (len(tlsConfig.NextProtocol) != 1 || tlsConfig.NextProtocol[0] == "h2")) || realityConfig != nil {
|
||||||
|
mode = "stream-up"
|
||||||
|
}
|
||||||
|
if realityConfig != nil && transportConfiguration.DownloadSettings == nil {
|
||||||
|
mode = "stream-one"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
errors.LogInfo(ctx, "XHTTP is using mode: "+mode)
|
||||||
|
|
||||||
|
var writer io.WriteCloser
|
||||||
|
var reader io.ReadCloser
|
||||||
|
var remoteAddr, localAddr net.Addr
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if mode == "stream-one" {
|
||||||
|
requestURL.Path = transportConfiguration.GetNormalizedPath()
|
||||||
|
writer, reader = httpClient.Open(context.WithoutCancel(ctx), requestURL.String())
|
||||||
|
remoteAddr = &net.TCPAddr{}
|
||||||
|
localAddr = &net.TCPAddr{}
|
||||||
|
} else {
|
||||||
|
reader, remoteAddr, localAddr, err = httpClient2.OpenDownload(context.WithoutCancel(ctx), requestURL2.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if muxRes != nil {
|
if muxRes != nil {
|
||||||
muxRes.OpenRequests.Add(1)
|
muxRes.OpenRequests.Add(1)
|
||||||
|
@ -293,7 +318,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
|
||||||
closed := false
|
closed := false
|
||||||
|
|
||||||
conn := splitConn{
|
conn := splitConn{
|
||||||
writer: nil,
|
writer: writer,
|
||||||
reader: reader,
|
reader: reader,
|
||||||
remoteAddr: remoteAddr,
|
remoteAddr: remoteAddr,
|
||||||
localAddr: localAddr,
|
localAddr: localAddr,
|
||||||
|
@ -311,14 +336,9 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
mode := transportConfiguration.Mode
|
if mode == "stream-one" {
|
||||||
if mode == "auto" {
|
return stat.Connection(&conn), nil
|
||||||
mode = "packet-up"
|
|
||||||
if (tlsConfig != nil && len(tlsConfig.NextProtocol) != 1) || realityConfig != nil {
|
|
||||||
mode = "stream-up"
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
errors.LogInfo(ctx, "XHTTP is using mode: "+mode)
|
|
||||||
if mode == "stream-up" {
|
if mode == "stream-up" {
|
||||||
conn.writer = httpClient.OpenUpload(ctx, requestURL.String())
|
conn.writer = httpClient.OpenUpload(ctx, requestURL.String())
|
||||||
return stat.Connection(&conn), nil
|
return stat.Connection(&conn), nil
|
||||||
|
|
|
@ -102,14 +102,22 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
|
||||||
|
|
||||||
h.config.WriteResponseHeader(writer)
|
h.config.WriteResponseHeader(writer)
|
||||||
|
|
||||||
|
validRange := h.config.GetNormalizedXPaddingBytes()
|
||||||
|
x_padding := int32(len(request.URL.Query().Get("x_padding")))
|
||||||
|
if validRange.To > 0 && (x_padding < validRange.From || x_padding > validRange.To) {
|
||||||
|
errors.LogInfo(context.Background(), "invalid x_padding length:", x_padding)
|
||||||
|
writer.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
sessionId := ""
|
sessionId := ""
|
||||||
subpath := strings.Split(request.URL.Path[len(h.path):], "/")
|
subpath := strings.Split(request.URL.Path[len(h.path):], "/")
|
||||||
if len(subpath) > 0 {
|
if len(subpath) > 0 {
|
||||||
sessionId = subpath[0]
|
sessionId = subpath[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
if sessionId == "" {
|
if sessionId == "" && h.config.Mode != "" && h.config.Mode != "auto" && h.config.Mode != "stream-one" && h.config.Mode != "stream-up" {
|
||||||
errors.LogInfo(context.Background(), "no sessionid on request:", request.URL.Path)
|
errors.LogInfo(context.Background(), "stream-one mode is not allowed")
|
||||||
writer.WriteHeader(http.StatusBadRequest)
|
writer.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -126,17 +134,20 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
currentSession := h.upsertSession(sessionId)
|
var currentSession *httpSession
|
||||||
|
if sessionId != "" {
|
||||||
|
currentSession = h.upsertSession(sessionId)
|
||||||
|
}
|
||||||
scMaxEachPostBytes := int(h.ln.config.GetNormalizedScMaxEachPostBytes().To)
|
scMaxEachPostBytes := int(h.ln.config.GetNormalizedScMaxEachPostBytes().To)
|
||||||
|
|
||||||
if request.Method == "POST" {
|
if request.Method == "POST" && sessionId != "" {
|
||||||
seq := ""
|
seq := ""
|
||||||
if len(subpath) > 1 {
|
if len(subpath) > 1 {
|
||||||
seq = subpath[1]
|
seq = subpath[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
if seq == "" {
|
if seq == "" {
|
||||||
if h.config.Mode == "packet-up" {
|
if h.config.Mode != "" && h.config.Mode != "auto" && h.config.Mode != "stream-up" {
|
||||||
errors.LogInfo(context.Background(), "stream-up mode is not allowed")
|
errors.LogInfo(context.Background(), "stream-up mode is not allowed")
|
||||||
writer.WriteHeader(http.StatusBadRequest)
|
writer.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
@ -148,13 +159,16 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
|
||||||
errors.LogInfoInner(context.Background(), err, "failed to upload (PushReader)")
|
errors.LogInfoInner(context.Background(), err, "failed to upload (PushReader)")
|
||||||
writer.WriteHeader(http.StatusConflict)
|
writer.WriteHeader(http.StatusConflict)
|
||||||
} else {
|
} else {
|
||||||
|
if request.Header.Get("Content-Type") == "application/grpc" {
|
||||||
|
writer.Header().Set("Content-Type", "application/grpc")
|
||||||
|
}
|
||||||
writer.WriteHeader(http.StatusOK)
|
writer.WriteHeader(http.StatusOK)
|
||||||
<-request.Context().Done()
|
<-request.Context().Done()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.config.Mode == "stream-up" {
|
if h.config.Mode != "" && h.config.Mode != "auto" && h.config.Mode != "packet-up" {
|
||||||
errors.LogInfo(context.Background(), "packet-up mode is not allowed")
|
errors.LogInfo(context.Background(), "packet-up mode is not allowed")
|
||||||
writer.WriteHeader(http.StatusBadRequest)
|
writer.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
@ -193,16 +207,18 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.WriteHeader(http.StatusOK)
|
writer.WriteHeader(http.StatusOK)
|
||||||
} else if request.Method == "GET" {
|
} else if request.Method == "GET" || sessionId == "" {
|
||||||
responseFlusher, ok := writer.(http.Flusher)
|
responseFlusher, ok := writer.(http.Flusher)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic("expected http.ResponseWriter to be an http.Flusher")
|
panic("expected http.ResponseWriter to be an http.Flusher")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if sessionId != "" {
|
||||||
// after GET is done, the connection is finished. disable automatic
|
// after GET is done, the connection is finished. disable automatic
|
||||||
// session reaping, and handle it in defer
|
// session reaping, and handle it in defer
|
||||||
currentSession.isFullyConnected.Close()
|
currentSession.isFullyConnected.Close()
|
||||||
defer h.sessions.Delete(sessionId)
|
defer h.sessions.Delete(sessionId)
|
||||||
|
}
|
||||||
|
|
||||||
// magic header instructs nginx + apache to not buffer response body
|
// magic header instructs nginx + apache to not buffer response body
|
||||||
writer.Header().Set("X-Accel-Buffering", "no")
|
writer.Header().Set("X-Accel-Buffering", "no")
|
||||||
|
@ -210,7 +226,10 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
|
||||||
// Should be able to prevent overloading the cache, or stop CDNs from
|
// Should be able to prevent overloading the cache, or stop CDNs from
|
||||||
// teeing the response stream into their cache, causing slowdowns.
|
// teeing the response stream into their cache, causing slowdowns.
|
||||||
writer.Header().Set("Cache-Control", "no-store")
|
writer.Header().Set("Cache-Control", "no-store")
|
||||||
if !h.config.NoSSEHeader {
|
|
||||||
|
if request.Header.Get("Content-Type") == "application/grpc" {
|
||||||
|
writer.Header().Set("Content-Type", "application/grpc")
|
||||||
|
} else if !h.config.NoSSEHeader {
|
||||||
// magic header to make the HTTP middle box consider this as SSE to disable buffer
|
// magic header to make the HTTP middle box consider this as SSE to disable buffer
|
||||||
writer.Header().Set("Content-Type", "text/event-stream")
|
writer.Header().Set("Content-Type", "text/event-stream")
|
||||||
}
|
}
|
||||||
|
@ -227,9 +246,12 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
|
||||||
downloadDone: downloadDone,
|
downloadDone: downloadDone,
|
||||||
responseFlusher: responseFlusher,
|
responseFlusher: responseFlusher,
|
||||||
},
|
},
|
||||||
reader: currentSession.uploadQueue,
|
reader: request.Body,
|
||||||
remoteAddr: remoteAddr,
|
remoteAddr: remoteAddr,
|
||||||
}
|
}
|
||||||
|
if sessionId != "" {
|
||||||
|
conn.reader = currentSession.uploadQueue
|
||||||
|
}
|
||||||
|
|
||||||
h.ln.addConn(stat.Connection(&conn))
|
h.ln.addConn(stat.Connection(&conn))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue