Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy/http: Avoid getting stuck when using server-first protocols #1517

Merged
merged 2 commits into from Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 18 additions & 8 deletions proxy/http/client.go
Expand Up @@ -22,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 @@ -79,14 +80,23 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
var user *protocol.MemoryUser
var conn internet.Connection

mbuf, _ := link.Reader.ReadMultiBuffer()
len := mbuf.Len()
firstPayload := bytespool.Alloc(len)
mbuf, _ = buf.SplitBytes(mbuf, firstPayload)
firstPayload = firstPayload[:len]

buf.ReleaseMulti(mbuf)
defer bytespool.Free(firstPayload)
var firstPayload []byte

if reader, ok := link.Reader.(buf.TimeoutReader); ok {
// 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(proxy.FirstPayloadTimeout); mbuf != nil {
mlen := mbuf.Len()
firstPayload = bytespool.Alloc(mlen)
mbuf, _ = buf.SplitBytes(mbuf, firstPayload)
firstPayload = firstPayload[:mlen]

buf.ReleaseMulti(mbuf)
defer bytespool.Free(firstPayload)
}
}

if err := retry.ExponentialBackoff(5, 100).On(func() error {
server := c.serverPicker.PickServer()
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