1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-22 11:44:50 -04:00

proxy: Unify timeouts of different protocols for reading the first payload

This commit is contained in:
lrh2000 2022-01-04 01:38:35 +00:00 committed by Shelikhoo
parent 17d85263e2
commit 396ee770b7
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
6 changed files with 16 additions and 13 deletions

View File

@ -8,7 +8,6 @@ import (
"net/http"
"net/url"
"sync"
"time"
"golang.org/x/net/http2"
@ -23,6 +22,7 @@ import (
"github.com/v2fly/v2ray-core/v5/common/signal"
"github.com/v2fly/v2ray-core/v5/common/task"
"github.com/v2fly/v2ray-core/v5/features/policy"
"github.com/v2fly/v2ray-core/v5/proxy"
"github.com/v2fly/v2ray-core/v5/transport"
"github.com/v2fly/v2ray-core/v5/transport/internet"
"github.com/v2fly/v2ray-core/v5/transport/internet/tls"
@ -83,11 +83,11 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
var firstPayload []byte
if reader, ok := link.Reader.(buf.TimeoutReader); ok {
// 0-RTT optimization for HTTP/2: If the payload comes within 50 ms, it can be
// 0-RTT optimization for HTTP/2: If the payload comes very soon, it can be
// transmitted together. Note we should not get stuck here, as the payload may
// not exist (considering to access MySQL database via a HTTP proxy, where the
// server sends hello to the client first).
if mbuf, _ := reader.ReadMultiBufferTimeout(50 * time.Millisecond); mbuf != nil {
if mbuf, _ := reader.ReadMultiBufferTimeout(proxy.FirstPayloadTimeout); mbuf != nil {
mlen := mbuf.Len()
firstPayload = bytespool.Alloc(mlen)
mbuf, _ = buf.SplitBytes(mbuf, firstPayload)

View File

@ -7,6 +7,7 @@ package proxy
import (
"context"
"time"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/protocol"
@ -15,6 +16,9 @@ import (
"github.com/v2fly/v2ray-core/v5/transport/internet"
)
// A timeout for reading the first payload from the client, used in 0-RTT optimizations.
const FirstPayloadTimeout = 100 * time.Millisecond
// An Inbound processes inbound connections.
type Inbound interface {
// Network returns a list of networks that this inbound supports. Connections with not-supported networks will not be passed into Process().

View File

@ -2,7 +2,6 @@ package shadowsocks
import (
"context"
"time"
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
@ -15,6 +14,7 @@ import (
"github.com/v2fly/v2ray-core/v5/common/signal"
"github.com/v2fly/v2ray-core/v5/common/task"
"github.com/v2fly/v2ray-core/v5/features/policy"
"github.com/v2fly/v2ray-core/v5/proxy"
"github.com/v2fly/v2ray-core/v5/transport"
"github.com/v2fly/v2ray-core/v5/transport/internet"
"github.com/v2fly/v2ray-core/v5/transport/internet/udp"
@ -132,7 +132,7 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
return newError("failed to write request").Base(err)
}
if err = buf.CopyOnceTimeout(link.Reader, bodyWriter, time.Millisecond*100); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
if err = buf.CopyOnceTimeout(link.Reader, bodyWriter, proxy.FirstPayloadTimeout); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
return newError("failed to write A request payload").Base(err).AtWarning()
}

View File

@ -2,7 +2,6 @@ package trojan
import (
"context"
"time"
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
@ -14,6 +13,7 @@ import (
"github.com/v2fly/v2ray-core/v5/common/signal"
"github.com/v2fly/v2ray-core/v5/common/task"
"github.com/v2fly/v2ray-core/v5/features/policy"
"github.com/v2fly/v2ray-core/v5/proxy"
"github.com/v2fly/v2ray-core/v5/transport"
"github.com/v2fly/v2ray-core/v5/transport/internet"
)
@ -99,7 +99,7 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
}
// write some request payload to buffer
if err = buf.CopyOnceTimeout(link.Reader, bodyWriter, time.Millisecond*100); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
if err = buf.CopyOnceTimeout(link.Reader, bodyWriter, proxy.FirstPayloadTimeout); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
return newError("failed to write A request payload").Base(err).AtWarning()
}

View File

@ -4,7 +4,6 @@ package outbound
import (
"context"
"time"
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
@ -17,6 +16,7 @@ import (
"github.com/v2fly/v2ray-core/v5/common/signal"
"github.com/v2fly/v2ray-core/v5/common/task"
"github.com/v2fly/v2ray-core/v5/features/policy"
"github.com/v2fly/v2ray-core/v5/proxy"
"github.com/v2fly/v2ray-core/v5/proxy/vless"
"github.com/v2fly/v2ray-core/v5/proxy/vless/encoding"
"github.com/v2fly/v2ray-core/v5/transport"
@ -139,7 +139,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
// default: serverWriter := bufferWriter
serverWriter := encoding.EncodeBodyAddons(bufferWriter, request, requestAddons)
if err := buf.CopyOnceTimeout(clientReader, serverWriter, time.Millisecond*100); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
if err := buf.CopyOnceTimeout(clientReader, serverWriter, proxy.FirstPayloadTimeout); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
return err // ...
}

View File

@ -6,9 +6,6 @@ import (
"context"
"crypto/hmac"
"crypto/sha256"
"hash/crc64"
"time"
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/buf"
@ -21,10 +18,12 @@ import (
"github.com/v2fly/v2ray-core/v5/common/signal"
"github.com/v2fly/v2ray-core/v5/common/task"
"github.com/v2fly/v2ray-core/v5/features/policy"
"github.com/v2fly/v2ray-core/v5/proxy"
"github.com/v2fly/v2ray-core/v5/proxy/vmess"
"github.com/v2fly/v2ray-core/v5/proxy/vmess/encoding"
"github.com/v2fly/v2ray-core/v5/transport"
"github.com/v2fly/v2ray-core/v5/transport/internet"
"hash/crc64"
)
// Handler is an outbound connection handler for VMess protocol.
@ -153,7 +152,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
if err != nil {
return newError("failed to start encoding").Base(err)
}
if err := buf.CopyOnceTimeout(input, bodyWriter, time.Millisecond*100); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
if err := buf.CopyOnceTimeout(input, bodyWriter, proxy.FirstPayloadTimeout); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
return newError("failed to write first payload").Base(err)
}