2024-06-18 08:36:36 +03:00
|
|
|
package splithttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
gotls "crypto/tls"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2024-07-17 15:10:48 +03:00
|
|
|
"github.com/quic-go/quic-go"
|
|
|
|
"github.com/quic-go/quic-go/http3"
|
2024-06-18 08:36:36 +03:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
2024-06-29 21:32:57 +03:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2024-06-18 08:36:36 +03:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/signal/semaphore"
|
|
|
|
"github.com/xtls/xray-core/common/uuid"
|
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
2024-07-11 10:56:20 +03:00
|
|
|
"github.com/xtls/xray-core/transport/internet/browser_dialer"
|
2024-10-30 05:31:05 +03:00
|
|
|
"github.com/xtls/xray-core/transport/internet/reality"
|
2024-06-18 08:36:36 +03:00
|
|
|
"github.com/xtls/xray-core/transport/internet/stat"
|
|
|
|
"github.com/xtls/xray-core/transport/internet/tls"
|
|
|
|
"github.com/xtls/xray-core/transport/pipe"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
)
|
|
|
|
|
2024-08-10 08:40:48 +03:00
|
|
|
// defines the maximum time an idle TCP session can survive in the tunnel, so
|
|
|
|
// it should be consistent across HTTP versions and with other transports.
|
|
|
|
const connIdleTimeout = 300 * time.Second
|
|
|
|
|
|
|
|
// consistent with quic-go
|
|
|
|
const h3KeepalivePeriod = 10 * time.Second
|
|
|
|
|
|
|
|
// consistent with chrome
|
|
|
|
const h2KeepalivePeriod = 45 * time.Second
|
|
|
|
|
2024-06-18 08:36:36 +03:00
|
|
|
type dialerConf struct {
|
|
|
|
net.Destination
|
|
|
|
*internet.MemoryStreamConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2024-09-16 15:42:01 +03:00
|
|
|
globalDialerMap map[dialerConf]*muxManager
|
2024-06-18 08:36:36 +03:00
|
|
|
globalDialerAccess sync.Mutex
|
|
|
|
)
|
|
|
|
|
2024-09-16 15:42:01 +03:00
|
|
|
func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (DialerClient, *muxResource) {
|
2024-10-30 05:31:05 +03:00
|
|
|
realityConfig := reality.ConfigFromStreamSettings(streamSettings)
|
|
|
|
|
|
|
|
if browser_dialer.HasBrowserDialer() && realityConfig != nil {
|
2024-09-16 15:42:01 +03:00
|
|
|
return &BrowserDialerClient{}, nil
|
2024-07-11 10:56:20 +03:00
|
|
|
}
|
|
|
|
|
2024-06-18 08:36:36 +03:00
|
|
|
globalDialerAccess.Lock()
|
|
|
|
defer globalDialerAccess.Unlock()
|
|
|
|
|
|
|
|
if globalDialerMap == nil {
|
2024-09-16 15:42:01 +03:00
|
|
|
globalDialerMap = make(map[dialerConf]*muxManager)
|
|
|
|
}
|
|
|
|
|
|
|
|
key := dialerConf{dest, streamSettings}
|
|
|
|
|
|
|
|
muxManager, found := globalDialerMap[key]
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
transportConfig := streamSettings.ProtocolSettings.(*Config)
|
|
|
|
var mux Multiplexing
|
|
|
|
if transportConfig.Xmux != nil {
|
|
|
|
mux = *transportConfig.Xmux
|
|
|
|
}
|
|
|
|
|
|
|
|
muxManager = NewMuxManager(mux, func() interface{} {
|
|
|
|
return createHTTPClient(dest, streamSettings)
|
|
|
|
})
|
|
|
|
globalDialerMap[key] = muxManager
|
2024-06-18 08:36:36 +03:00
|
|
|
}
|
|
|
|
|
2024-09-16 15:42:01 +03:00
|
|
|
res := muxManager.GetResource(ctx)
|
|
|
|
return res.Resource.(DialerClient), res
|
|
|
|
}
|
|
|
|
|
|
|
|
func createHTTPClient(dest net.Destination, streamSettings *internet.MemoryStreamConfig) DialerClient {
|
|
|
|
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
|
2024-10-30 05:31:05 +03:00
|
|
|
realityConfig := reality.ConfigFromStreamSettings(streamSettings)
|
|
|
|
|
|
|
|
isH2 := false
|
|
|
|
isH3 := false
|
|
|
|
|
|
|
|
if tlsConfig != nil {
|
|
|
|
isH2 = !(len(tlsConfig.NextProtocol) == 1 && tlsConfig.NextProtocol[0] == "http/1.1")
|
|
|
|
isH3 = len(tlsConfig.NextProtocol) == 1 && tlsConfig.NextProtocol[0] == "h3"
|
|
|
|
} else if realityConfig != nil {
|
|
|
|
isH2 = true
|
|
|
|
isH3 = false
|
|
|
|
}
|
2024-09-16 15:42:01 +03:00
|
|
|
|
2024-07-21 11:55:03 +03:00
|
|
|
if isH3 {
|
|
|
|
dest.Network = net.Network_UDP
|
|
|
|
}
|
2024-06-18 08:36:36 +03:00
|
|
|
|
|
|
|
var gotlsConfig *gotls.Config
|
|
|
|
|
|
|
|
if tlsConfig != nil {
|
|
|
|
gotlsConfig = tlsConfig.GetTLSConfig(tls.WithDestination(dest))
|
|
|
|
}
|
|
|
|
|
2024-09-16 15:42:01 +03:00
|
|
|
transportConfig := streamSettings.ProtocolSettings.(*Config)
|
|
|
|
|
2024-06-18 08:36:36 +03:00
|
|
|
dialContext := func(ctxInner context.Context) (net.Conn, error) {
|
2024-06-21 02:30:51 +03:00
|
|
|
conn, err := internet.DialSystem(ctxInner, dest, streamSettings.SocketSettings)
|
2024-06-18 08:36:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-10-30 05:31:05 +03:00
|
|
|
if realityConfig != nil {
|
|
|
|
return reality.UClient(conn, realityConfig, ctxInner, dest)
|
|
|
|
}
|
|
|
|
|
2024-06-18 08:36:36 +03:00
|
|
|
if gotlsConfig != nil {
|
|
|
|
if fingerprint := tls.GetFingerprint(tlsConfig.Fingerprint); fingerprint != nil {
|
|
|
|
conn = tls.UClient(conn, gotlsConfig, fingerprint)
|
2024-06-21 02:30:51 +03:00
|
|
|
if err := conn.(*tls.UConn).HandshakeContext(ctxInner); err != nil {
|
2024-06-18 08:36:36 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
conn = tls.Client(conn, gotlsConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2024-09-16 15:42:01 +03:00
|
|
|
var transport http.RoundTripper
|
2024-06-18 08:36:36 +03:00
|
|
|
|
2024-07-17 15:10:48 +03:00
|
|
|
if isH3 {
|
2024-08-10 08:40:48 +03:00
|
|
|
quicConfig := &quic.Config{
|
|
|
|
MaxIdleTimeout: connIdleTimeout,
|
|
|
|
|
|
|
|
// these two are defaults of quic-go/http3. the default of quic-go (no
|
|
|
|
// http3) is different, so it is hardcoded here for clarity.
|
|
|
|
// https://github.com/quic-go/quic-go/blob/b8ea5c798155950fb5bbfdd06cad1939c9355878/http3/client.go#L36-L39
|
|
|
|
MaxIncomingStreams: -1,
|
|
|
|
KeepAlivePeriod: h3KeepalivePeriod,
|
|
|
|
}
|
2024-09-16 15:42:01 +03:00
|
|
|
transport = &http3.RoundTripper{
|
2024-08-10 08:40:48 +03:00
|
|
|
QUICConfig: quicConfig,
|
2024-07-17 15:10:48 +03:00
|
|
|
TLSClientConfig: gotlsConfig,
|
|
|
|
Dial: func(ctx context.Context, addr string, tlsCfg *gotls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
|
|
|
|
conn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-19 20:52:34 +03:00
|
|
|
|
2024-08-11 19:58:52 +03:00
|
|
|
var udpConn net.PacketConn
|
2024-07-19 20:52:34 +03:00
|
|
|
var udpAddr *net.UDPAddr
|
|
|
|
|
|
|
|
switch c := conn.(type) {
|
|
|
|
case *internet.PacketConnWrapper:
|
|
|
|
var ok bool
|
|
|
|
udpConn, ok = c.Conn.(*net.UDPConn)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("PacketConnWrapper does not contain a UDP connection")
|
|
|
|
}
|
|
|
|
udpAddr, err = net.ResolveUDPAddr("udp", c.Dest.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case *net.UDPConn:
|
|
|
|
udpConn = c
|
|
|
|
udpAddr, err = net.ResolveUDPAddr("udp", c.RemoteAddr().String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
2024-08-11 19:58:52 +03:00
|
|
|
udpConn = &internet.FakePacketConn{c}
|
|
|
|
udpAddr, err = net.ResolveUDPAddr("udp", c.RemoteAddr().String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-17 15:10:48 +03:00
|
|
|
}
|
2024-07-19 20:52:34 +03:00
|
|
|
|
|
|
|
return quic.DialEarly(ctx, udpConn, udpAddr, tlsCfg, cfg)
|
2024-07-17 15:10:48 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
} else if isH2 {
|
2024-09-16 15:42:01 +03:00
|
|
|
transport = &http2.Transport{
|
2024-06-18 08:36:36 +03:00
|
|
|
DialTLSContext: func(ctxInner context.Context, network string, addr string, cfg *gotls.Config) (net.Conn, error) {
|
|
|
|
return dialContext(ctxInner)
|
|
|
|
},
|
2024-08-10 08:40:48 +03:00
|
|
|
IdleConnTimeout: connIdleTimeout,
|
|
|
|
ReadIdleTimeout: h2KeepalivePeriod,
|
2024-06-18 08:36:36 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
httpDialContext := func(ctxInner context.Context, network string, addr string) (net.Conn, error) {
|
|
|
|
return dialContext(ctxInner)
|
|
|
|
}
|
|
|
|
|
2024-09-16 15:42:01 +03:00
|
|
|
transport = &http.Transport{
|
2024-06-18 08:36:36 +03:00
|
|
|
DialTLSContext: httpDialContext,
|
|
|
|
DialContext: httpDialContext,
|
2024-08-10 08:40:48 +03:00
|
|
|
IdleConnTimeout: connIdleTimeout,
|
2024-06-18 08:36:36 +03:00
|
|
|
// chunked transfer download with keepalives is buggy with
|
|
|
|
// http.Client and our custom dial context.
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-11 10:56:20 +03:00
|
|
|
client := &DefaultDialerClient{
|
2024-09-16 15:42:01 +03:00
|
|
|
transportConfig: transportConfig,
|
|
|
|
client: &http.Client{
|
|
|
|
Transport: transport,
|
2024-06-18 08:36:36 +03:00
|
|
|
},
|
2024-06-23 20:05:37 +03:00
|
|
|
isH2: isH2,
|
2024-07-17 15:10:48 +03:00
|
|
|
isH3: isH3,
|
2024-06-18 08:36:36 +03:00
|
|
|
uploadRawPool: &sync.Pool{},
|
|
|
|
dialUploadConn: dialContext,
|
|
|
|
}
|
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(internet.RegisterTransportDialer(protocolName, Dial))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
|
2024-06-29 21:32:57 +03:00
|
|
|
errors.LogInfo(ctx, "dialing splithttp to ", dest)
|
2024-06-18 08:36:36 +03:00
|
|
|
|
|
|
|
var requestURL url.URL
|
|
|
|
|
|
|
|
transportConfiguration := streamSettings.ProtocolSettings.(*Config)
|
|
|
|
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
|
2024-10-30 05:31:05 +03:00
|
|
|
realityConfig := reality.ConfigFromStreamSettings(streamSettings)
|
2024-06-18 08:36:36 +03:00
|
|
|
|
2024-07-29 13:10:29 +03:00
|
|
|
scMaxConcurrentPosts := transportConfiguration.GetNormalizedScMaxConcurrentPosts()
|
|
|
|
scMaxEachPostBytes := transportConfiguration.GetNormalizedScMaxEachPostBytes()
|
|
|
|
scMinPostsIntervalMs := transportConfiguration.GetNormalizedScMinPostsIntervalMs()
|
2024-06-18 08:36:36 +03:00
|
|
|
|
2024-10-30 05:31:05 +03:00
|
|
|
if tlsConfig != nil || realityConfig != nil {
|
2024-06-18 08:36:36 +03:00
|
|
|
requestURL.Scheme = "https"
|
|
|
|
} else {
|
|
|
|
requestURL.Scheme = "http"
|
|
|
|
}
|
|
|
|
requestURL.Host = transportConfiguration.Host
|
|
|
|
if requestURL.Host == "" {
|
|
|
|
requestURL.Host = dest.NetAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionIdUuid := uuid.New()
|
2024-08-11 00:47:42 +03:00
|
|
|
requestURL.Path = transportConfiguration.GetNormalizedPath() + sessionIdUuid.String()
|
|
|
|
requestURL.RawQuery = transportConfiguration.GetNormalizedQuery()
|
2024-07-29 07:35:17 +03:00
|
|
|
|
2024-11-03 10:25:41 +03:00
|
|
|
httpClient, muxRes := getHTTPClient(ctx, dest, streamSettings)
|
2024-06-18 08:36:36 +03:00
|
|
|
|
2024-11-09 14:05:41 +03:00
|
|
|
httpClient2 := httpClient
|
|
|
|
requestURL2 := requestURL
|
2024-11-03 10:25:41 +03:00
|
|
|
var muxRes2 *muxResource
|
2024-10-31 10:31:19 +03:00
|
|
|
if transportConfiguration.DownloadSettings != nil {
|
2024-11-03 10:25:41 +03:00
|
|
|
globalDialerAccess.Lock()
|
|
|
|
if streamSettings.DownloadSettings == nil {
|
|
|
|
streamSettings.DownloadSettings = common.Must2(internet.ToMemoryStreamConfig(transportConfiguration.DownloadSettings)).(*internet.MemoryStreamConfig)
|
|
|
|
}
|
|
|
|
globalDialerAccess.Unlock()
|
|
|
|
memory2 := streamSettings.DownloadSettings
|
2024-11-04 08:00:18 +03:00
|
|
|
httpClient2, muxRes2 = getHTTPClient(ctx, *memory2.Destination, memory2) // just panic
|
2024-10-31 10:31:19 +03:00
|
|
|
if tls.ConfigFromStreamSettings(memory2) != nil || reality.ConfigFromStreamSettings(memory2) != nil {
|
|
|
|
requestURL2.Scheme = "https"
|
|
|
|
} else {
|
|
|
|
requestURL2.Scheme = "http"
|
|
|
|
}
|
|
|
|
config2 := memory2.ProtocolSettings.(*Config)
|
|
|
|
requestURL2.Host = config2.Host
|
|
|
|
if requestURL2.Host == "" {
|
2024-11-04 08:00:18 +03:00
|
|
|
requestURL2.Host = memory2.Destination.NetAddr()
|
2024-10-31 10:31:19 +03:00
|
|
|
}
|
2024-11-07 06:50:28 +03:00
|
|
|
requestURL2.Path = config2.GetNormalizedPath() + sessionIdUuid.String()
|
2024-10-31 10:31:19 +03:00
|
|
|
requestURL2.RawQuery = config2.GetNormalizedQuery()
|
|
|
|
}
|
|
|
|
|
2024-11-09 14:05:41 +03:00
|
|
|
reader, remoteAddr, localAddr, err := httpClient2.OpenDownload(context.WithoutCancel(ctx), requestURL2.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-18 08:36:36 +03:00
|
|
|
|
2024-11-03 10:25:41 +03:00
|
|
|
if muxRes != nil {
|
|
|
|
muxRes.OpenRequests.Add(1)
|
|
|
|
}
|
|
|
|
if muxRes2 != nil {
|
|
|
|
muxRes2.OpenRequests.Add(1)
|
2024-09-16 15:42:01 +03:00
|
|
|
}
|
2024-11-09 14:05:41 +03:00
|
|
|
closed := false
|
2024-09-16 15:42:01 +03:00
|
|
|
|
2024-11-09 14:05:41 +03:00
|
|
|
conn := splitConn{
|
|
|
|
writer: nil,
|
|
|
|
reader: reader,
|
|
|
|
remoteAddr: remoteAddr,
|
|
|
|
localAddr: localAddr,
|
|
|
|
onClose: func() {
|
|
|
|
if closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
closed = true
|
|
|
|
if muxRes != nil {
|
|
|
|
muxRes.OpenRequests.Add(-1)
|
|
|
|
}
|
|
|
|
if muxRes2 != nil {
|
|
|
|
muxRes2.OpenRequests.Add(-1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mode := transportConfiguration.Mode
|
|
|
|
if mode == "auto" && realityConfig != nil {
|
|
|
|
mode = "stream-up"
|
|
|
|
}
|
|
|
|
if mode == "stream-up" {
|
|
|
|
conn.writer = httpClient.OpenUpload(ctx, requestURL.String())
|
|
|
|
return stat.Connection(&conn), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
maxUploadSize := scMaxEachPostBytes.roll()
|
|
|
|
// WithSizeLimit(0) will still allow single bytes to pass, and a lot of
|
|
|
|
// code relies on this behavior. Subtract 1 so that together with
|
|
|
|
// uploadWriter wrapper, exact size limits can be enforced
|
|
|
|
uploadPipeReader, uploadPipeWriter := pipe.New(pipe.WithSizeLimit(maxUploadSize - 1))
|
2024-09-16 15:42:01 +03:00
|
|
|
|
2024-11-09 14:05:41 +03:00
|
|
|
conn.writer = uploadWriter{
|
|
|
|
uploadPipeWriter,
|
|
|
|
maxUploadSize,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2024-07-29 13:10:29 +03:00
|
|
|
requestsLimiter := semaphore.New(int(scMaxConcurrentPosts.roll()))
|
2024-06-18 08:36:36 +03:00
|
|
|
var requestCounter int64
|
|
|
|
|
2024-07-27 15:52:36 +03:00
|
|
|
lastWrite := time.Now()
|
|
|
|
|
2024-06-18 08:36:36 +03:00
|
|
|
// by offloading the uploads into a buffered pipe, multiple conn.Write
|
|
|
|
// calls get automatically batched together into larger POST requests.
|
|
|
|
// without batching, bandwidth is extremely limited.
|
|
|
|
for {
|
|
|
|
chunk, err := uploadPipeReader.ReadMultiBuffer()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
<-requestsLimiter.Wait()
|
|
|
|
|
2024-07-11 10:56:20 +03:00
|
|
|
seq := requestCounter
|
2024-06-18 08:36:36 +03:00
|
|
|
requestCounter += 1
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer requestsLimiter.Signal()
|
2024-07-11 10:56:20 +03:00
|
|
|
|
2024-08-11 00:47:42 +03:00
|
|
|
// this intentionally makes a shallow-copy of the struct so we
|
|
|
|
// can reassign Path (potentially concurrently)
|
|
|
|
url := requestURL
|
|
|
|
url.Path += "/" + strconv.FormatInt(seq, 10)
|
|
|
|
// reassign query to get different padding
|
|
|
|
url.RawQuery = transportConfiguration.GetNormalizedQuery()
|
|
|
|
|
2024-07-11 10:56:20 +03:00
|
|
|
err := httpClient.SendUploadRequest(
|
|
|
|
context.WithoutCancel(ctx),
|
2024-08-11 00:47:42 +03:00
|
|
|
url.String(),
|
2024-07-11 10:56:20 +03:00
|
|
|
&buf.MultiBufferContainer{MultiBuffer: chunk},
|
|
|
|
int64(chunk.Len()),
|
|
|
|
)
|
2024-06-18 08:36:36 +03:00
|
|
|
if err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
errors.LogInfoInner(ctx, err, "failed to send upload")
|
2024-06-18 08:36:36 +03:00
|
|
|
uploadPipeReader.Interrupt()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-07-29 13:10:29 +03:00
|
|
|
if scMinPostsIntervalMs.From > 0 {
|
|
|
|
roll := time.Duration(scMinPostsIntervalMs.roll()) * time.Millisecond
|
2024-07-27 15:52:36 +03:00
|
|
|
if time.Since(lastWrite) < roll {
|
|
|
|
time.Sleep(roll)
|
|
|
|
}
|
|
|
|
|
|
|
|
lastWrite = time.Now()
|
|
|
|
}
|
2024-06-18 08:36:36 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return stat.Connection(&conn), nil
|
|
|
|
}
|
2024-08-17 14:01:58 +03:00
|
|
|
|
|
|
|
// A wrapper around pipe that ensures the size limit is exactly honored.
|
|
|
|
//
|
|
|
|
// The MultiBuffer pipe accepts any single WriteMultiBuffer call even if that
|
|
|
|
// single MultiBuffer exceeds the size limit, and then starts blocking on the
|
|
|
|
// next WriteMultiBuffer call. This means that ReadMultiBuffer can return more
|
|
|
|
// bytes than the size limit. We work around this by splitting a potentially
|
|
|
|
// too large write up into multiple.
|
|
|
|
type uploadWriter struct {
|
|
|
|
*pipe.Writer
|
|
|
|
maxLen int32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w uploadWriter) Write(b []byte) (int, error) {
|
|
|
|
capacity := int(w.maxLen - w.Len())
|
|
|
|
if capacity > 0 && capacity < len(b) {
|
|
|
|
b = b[:capacity]
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer := buf.New()
|
|
|
|
n, err := buffer.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.WriteMultiBuffer([]*buf.Buffer{buffer})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|