Skip to content

Commit

Permalink
proxy: Unify timeouts of different protocols for reading the first pa…
Browse files Browse the repository at this point in the history
…yload
  • Loading branch information
lrh2000 authored and xiaokangwang committed Jun 25, 2022
1 parent 17d8526 commit 396ee77
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
6 changes: 3 additions & 3 deletions proxy/http/client.go
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"
"net/url"
"sync"
"time"

"golang.org/x/net/http2"

Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions proxy/proxy.go
Expand Up @@ -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"
Expand All @@ -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().
Expand Down
4 changes: 2 additions & 2 deletions proxy/shadowsocks/client.go
Expand Up @@ -2,7 +2,6 @@ package shadowsocks

import (
"context"
"time"

core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
Expand All @@ -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"
Expand Down Expand Up @@ -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()
}

Expand Down
4 changes: 2 additions & 2 deletions proxy/trojan/client.go
Expand Up @@ -2,7 +2,6 @@ package trojan

import (
"context"
"time"

core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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()
}

Expand Down
4 changes: 2 additions & 2 deletions proxy/vless/outbound/outbound.go
Expand Up @@ -4,7 +4,6 @@ package outbound

import (
"context"
"time"

core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
Expand All @@ -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"
Expand Down Expand Up @@ -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 // ...
}

Expand Down
7 changes: 3 additions & 4 deletions proxy/vmess/outbound/outbound.go
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 396ee77

Please sign in to comment.