2024-07-11 07:56:20 +00:00
|
|
|
package splithttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2024-09-16 12:33:03 +00:00
|
|
|
"fmt"
|
2024-07-11 07:56:20 +00:00
|
|
|
"io"
|
|
|
|
gonet "net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptrace"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/errors"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/signal/done"
|
|
|
|
)
|
|
|
|
|
|
|
|
// interface to abstract between use of browser dialer, vs net/http
|
|
|
|
type DialerClient interface {
|
2024-12-15 05:43:10 +00:00
|
|
|
IsClosed() bool
|
|
|
|
|
2024-12-20 14:35:33 +00:00
|
|
|
// ctx, url, body, uploadOnly
|
|
|
|
OpenStream(context.Context, string, io.Reader, bool) (io.ReadCloser, net.Addr, net.Addr, error)
|
2024-07-11 07:56:20 +00:00
|
|
|
|
2024-12-20 14:35:33 +00:00
|
|
|
// ctx, url, body, contentLength
|
|
|
|
PostPacket(context.Context, string, io.Reader, int64) error
|
2024-07-11 07:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// implements splithttp.DialerClient in terms of direct network connections
|
|
|
|
type DefaultDialerClient struct {
|
|
|
|
transportConfig *Config
|
2024-09-16 12:42:01 +00:00
|
|
|
client *http.Client
|
2024-12-15 05:43:10 +00:00
|
|
|
closed bool
|
2024-12-12 12:19:18 +00:00
|
|
|
httpVersion string
|
2024-07-11 07:56:20 +00:00
|
|
|
// pool of net.Conn, created using dialUploadConn
|
|
|
|
uploadRawPool *sync.Pool
|
|
|
|
dialUploadConn func(ctxInner context.Context) (net.Conn, error)
|
|
|
|
}
|
|
|
|
|
2024-12-15 05:43:10 +00:00
|
|
|
func (c *DefaultDialerClient) IsClosed() bool {
|
|
|
|
return c.closed
|
|
|
|
}
|
|
|
|
|
2024-12-20 14:35:33 +00:00
|
|
|
func (c *DefaultDialerClient) OpenStream(ctx context.Context, url string, body io.Reader, uploadOnly bool) (wrc io.ReadCloser, remoteAddr, localAddr gonet.Addr, err error) {
|
2024-07-11 07:56:20 +00:00
|
|
|
// this is done when the TCP/UDP connection to the server was established,
|
|
|
|
// and we can unblock the Dial function and print correct net addresses in
|
|
|
|
// logs
|
|
|
|
gotConn := done.New()
|
2024-12-20 14:35:33 +00:00
|
|
|
ctx = httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
|
|
|
|
GotConn: func(connInfo httptrace.GotConnInfo) {
|
|
|
|
remoteAddr = connInfo.Conn.RemoteAddr()
|
|
|
|
localAddr = connInfo.Conn.LocalAddr()
|
|
|
|
gotConn.Close()
|
|
|
|
},
|
|
|
|
})
|
2024-07-11 07:56:20 +00:00
|
|
|
|
2024-12-20 14:35:33 +00:00
|
|
|
method := "GET"
|
|
|
|
if body != nil {
|
|
|
|
method = "POST"
|
|
|
|
}
|
|
|
|
req, _ := http.NewRequestWithContext(ctx, method, url, body)
|
|
|
|
req.Header = c.transportConfig.GetRequestHeader()
|
|
|
|
if method == "POST" && !c.transportConfig.NoGRPCHeader {
|
|
|
|
req.Header.Set("Content-Type", "application/grpc")
|
|
|
|
}
|
2024-08-22 15:07:57 +00:00
|
|
|
|
2024-12-20 14:35:33 +00:00
|
|
|
wrc = &WaitReadCloser{Wait: make(chan struct{})}
|
2024-07-11 07:56:20 +00:00
|
|
|
go func() {
|
2024-12-20 14:35:33 +00:00
|
|
|
resp, err := c.client.Do(req)
|
2024-07-11 07:56:20 +00:00
|
|
|
if err != nil {
|
2024-12-20 14:35:33 +00:00
|
|
|
errors.LogInfoInner(ctx, err, "failed to "+method+" "+url)
|
|
|
|
gotConn.Close()
|
|
|
|
wrc.Close()
|
2024-07-11 07:56:20 +00:00
|
|
|
return
|
|
|
|
}
|
2024-12-20 14:35:33 +00:00
|
|
|
if resp.StatusCode != 200 && !uploadOnly {
|
2024-12-15 05:43:10 +00:00
|
|
|
// c.closed = true
|
2024-12-20 14:35:33 +00:00
|
|
|
errors.LogInfo(ctx, "unexpected status ", resp.StatusCode)
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 || uploadOnly {
|
|
|
|
resp.Body.Close()
|
|
|
|
wrc.Close()
|
2024-07-11 07:56:20 +00:00
|
|
|
return
|
|
|
|
}
|
2024-12-20 14:35:33 +00:00
|
|
|
wrc.(*WaitReadCloser).Set(resp.Body)
|
2024-07-11 07:56:20 +00:00
|
|
|
}()
|
|
|
|
|
2024-12-11 14:05:39 +00:00
|
|
|
<-gotConn.Wait()
|
2024-12-20 14:35:33 +00:00
|
|
|
return
|
2024-07-11 07:56:20 +00:00
|
|
|
}
|
|
|
|
|
2024-12-20 14:35:33 +00:00
|
|
|
func (c *DefaultDialerClient) PostPacket(ctx context.Context, url string, body io.Reader, contentLength int64) error {
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", url, body)
|
2024-07-11 07:56:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-22 20:19:31 +00:00
|
|
|
req.ContentLength = contentLength
|
2024-07-11 07:56:20 +00:00
|
|
|
req.Header = c.transportConfig.GetRequestHeader()
|
|
|
|
|
2024-12-12 12:19:18 +00:00
|
|
|
if c.httpVersion != "1.1" {
|
2024-09-16 12:42:01 +00:00
|
|
|
resp, err := c.client.Do(req)
|
2024-07-11 07:56:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
2024-12-15 05:43:10 +00:00
|
|
|
// c.closed = true
|
2024-07-11 07:56:20 +00:00
|
|
|
return errors.New("bad status code:", resp.Status)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// stringify the entire HTTP/1.1 request so it can be
|
|
|
|
// safely retried. if instead req.Write is called multiple
|
|
|
|
// times, the body is already drained after the first
|
|
|
|
// request
|
2024-09-16 12:33:03 +00:00
|
|
|
requestBuff := new(bytes.Buffer)
|
|
|
|
common.Must(req.Write(requestBuff))
|
2024-07-11 07:56:20 +00:00
|
|
|
|
|
|
|
var uploadConn any
|
2024-09-16 12:33:03 +00:00
|
|
|
var h1UploadConn *H1Conn
|
2024-07-11 07:56:20 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
uploadConn = c.uploadRawPool.Get()
|
|
|
|
newConnection := uploadConn == nil
|
|
|
|
if newConnection {
|
2024-09-16 12:33:03 +00:00
|
|
|
newConn, err := c.dialUploadConn(context.WithoutCancel(ctx))
|
2024-07-11 07:56:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-16 12:33:03 +00:00
|
|
|
h1UploadConn = NewH1Conn(newConn)
|
|
|
|
uploadConn = h1UploadConn
|
|
|
|
} else {
|
|
|
|
h1UploadConn = uploadConn.(*H1Conn)
|
|
|
|
|
|
|
|
// TODO: Replace 0 here with a config value later
|
|
|
|
// Or add some other condition for optimization purposes
|
|
|
|
if h1UploadConn.UnreadedResponsesCount > 0 {
|
|
|
|
resp, err := http.ReadResponse(h1UploadConn.RespBufReader, req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error while reading response: %s", err.Error())
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
2024-12-15 05:43:10 +00:00
|
|
|
// c.closed = true
|
|
|
|
// resp.Body.Close() // I'm not sure
|
2024-09-16 12:33:03 +00:00
|
|
|
return fmt.Errorf("got non-200 error response code: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
}
|
2024-07-11 07:56:20 +00:00
|
|
|
}
|
|
|
|
|
2024-09-16 12:33:03 +00:00
|
|
|
_, err := h1UploadConn.Write(requestBuff.Bytes())
|
2024-07-11 07:56:20 +00:00
|
|
|
// if the write failed, we try another connection from
|
|
|
|
// the pool, until the write on a new connection fails.
|
|
|
|
// failed writes to a pooled connection are normal when
|
|
|
|
// the connection has been closed in the meantime.
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
} else if newConnection {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.uploadRawPool.Put(uploadConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-22 15:07:57 +00:00
|
|
|
|
2024-11-27 20:19:18 +00:00
|
|
|
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
|
|
|
|
}
|