Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions infra/conf/transport_internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,12 @@ type UdpHop struct {
}

type HysteriaConfig struct {
Version int32 `json:"version"`
Auth string `json:"auth"`
Up Bandwidth `json:"up"`
Down Bandwidth `json:"down"`
UdpHop UdpHop `json:"udphop"`
Version int32 `json:"version"`
Auth string `json:"auth"`
Congestion string `json:"congestion"`
Up Bandwidth `json:"up"`
Down Bandwidth `json:"down"`
UdpHop UdpHop `json:"udphop"`

InitStreamReceiveWindow uint64 `json:"initStreamReceiveWindow"`
MaxStreamReceiveWindow uint64 `json:"maxStreamReceiveWindow"`
Expand All @@ -410,6 +411,7 @@ func (c *HysteriaConfig) Build() (proto.Message, error) {
if c.Version != 2 {
return nil, errors.New("version != 2")
}

up, err := c.Up.Bps()
if err != nil {
return nil, err
Expand All @@ -418,6 +420,12 @@ func (c *HysteriaConfig) Build() (proto.Message, error) {
if err != nil {
return nil, err
}

c.Congestion = strings.ToLower(c.Congestion)
if c.Congestion == "force-brutal" && up == 0 {
return nil, errors.New("force-brutal require up")
}

var hop *PortList
if err := json.Unmarshal(c.UdpHop.PortList, &hop); err != nil {
hop = &PortList{}
Expand Down Expand Up @@ -455,6 +463,7 @@ func (c *HysteriaConfig) Build() (proto.Message, error) {
config := &hysteria.Config{}
config.Version = c.Version
config.Auth = c.Auth
config.Congestion = c.Congestion
config.Up = up
config.Down = down
config.Ports = hop.Build().Ports()
Expand Down
4 changes: 4 additions & 0 deletions proxy/hysteria/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/xtls/xray-core/common/task"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/policy"
hyCtx "github.com/xtls/xray-core/proxy/hysteria/ctx"
"github.com/xtls/xray-core/transport"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/hysteria"
Expand Down Expand Up @@ -55,6 +56,9 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
ob.CanSpliceCopy = 3
target := ob.Target

if target.Network == net.Network_UDP {
hyCtx.ContextWithRequireDatagram(ctx)
}
conn, err := dialer.Dial(ctx, c.server.Destination)
if err != nil {
return errors.New("failed to find an available destination").AtWarning().Base(err)
Expand Down
20 changes: 20 additions & 0 deletions proxy/hysteria/ctx/ctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ctx

import (
"context"
)

type key int

const (
requireDatagram key = iota
)

func ContextWithRequireDatagram(ctx context.Context) context.Context {
return context.WithValue(ctx, requireDatagram, struct{}{})
}

func RequireDatagramFromContext(ctx context.Context) bool {
_, ok := ctx.Value(requireDatagram).(struct{})
return ok
}
61 changes: 36 additions & 25 deletions transport/internet/hysteria/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions transport/internet/hysteria/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ option java_multiple_files = true;
message Config {
int32 version = 1;
string auth = 2;
uint64 up = 3;
uint64 down = 4;
repeated uint32 ports = 5;
int64 interval = 6;
string congestion = 3;
uint64 up = 4;
uint64 down = 5;
repeated uint32 ports = 6;
int64 interval = 7;

uint64 init_stream_receive_window = 7;
uint64 max_stream_receive_window = 8;
uint64 init_conn_receive_window = 9;
uint64 max_conn_receive_window = 10;
int64 max_idle_timeout = 11;
int64 keep_alive_period = 12;
bool disable_path_mtu_discovery = 13;
uint64 init_stream_receive_window = 8;
uint64 max_stream_receive_window = 9;
uint64 init_conn_receive_window = 10;
uint64 max_conn_receive_window = 11;
int64 max_idle_timeout = 12;
int64 keep_alive_period = 13;
bool disable_path_mtu_discovery = 14;
}

3 changes: 3 additions & 0 deletions transport/internet/hysteria/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func (i *InterUdpConn) Read(p []byte) (int, error) {
return 0, io.EOF
}
n := copy(p, b)
if n != len(b) {
return 0, io.ErrShortBuffer
}
return n, nil
}

Expand Down
33 changes: 23 additions & 10 deletions transport/internet/hysteria/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"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/session"
"github.com/xtls/xray-core/common/task"
hyCtx "github.com/xtls/xray-core/proxy/hysteria/ctx"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/finalmask"
"github.com/xtls/xray-core/transport/internet/hysteria/congestion"
Expand Down Expand Up @@ -115,8 +115,8 @@ type client struct {
conn *quic.Conn
config *Config
tlsConfig *go_tls.Config
udpmaskManager *finalmask.UdpmaskManager
socketConfig *internet.SocketConfig
udpmaskManager *finalmask.UdpmaskManager
udpSM *udpSessionManager
mutex sync.Mutex
}
Expand Down Expand Up @@ -243,10 +243,25 @@ func (c *client) dial() error {
serverAuto := resp.Header.Get(CommonHeaderCCRX)
serverDown, _ := strconv.ParseUint(serverAuto, 10, 64)

if serverAuto == "auto" || c.config.Up == 0 || serverDown == 0 {
switch c.config.Congestion {
case "reno":
errors.LogDebug(c.ctx, "congestion reno")
case "bbr":
errors.LogDebug(c.ctx, "congestion bbr")
congestion.UseBBR(quicConn)
} else {
congestion.UseBrutal(quicConn, min(c.config.Up, serverDown))
case "brutal", "":
if serverAuto == "auto" || c.config.Up == 0 || serverDown == 0 {
errors.LogDebug(c.ctx, "congestion bbr")
congestion.UseBBR(quicConn)
} else {
errors.LogDebug(c.ctx, "congestion brutal bytes per second ", min(c.config.Up, serverDown))
congestion.UseBrutal(quicConn, min(c.config.Up, serverDown))
}
case "force-brutal":
errors.LogDebug(c.ctx, "congestion brutal bytes per second ", c.config.Up)
congestion.UseBrutal(quicConn, c.config.Up)
default:
errors.LogDebug(c.ctx, "congestion reno")
}

c.pktConn = pktConn
Expand Down Expand Up @@ -363,6 +378,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
return nil, errors.New("tls config is nil")
}

requireDatagram := hyCtx.RequireDatagramFromContext(ctx)
addr := dest.NetAddr()
config := streamSettings.ProtocolSettings.(*Config)

Expand All @@ -375,18 +391,15 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
dest: dest,
config: config,
tlsConfig: tlsConfig.GetTLSConfig(),
udpmaskManager: streamSettings.UdpmaskManager,
socketConfig: streamSettings.SocketSettings,
udpmaskManager: streamSettings.UdpmaskManager,
}
manger.m[addr] = c
}
c.setCtx(ctx)
manger.mutex.Unlock()

outbounds := session.OutboundsFromContext(ctx)
targetUdp := len(outbounds) > 0 && outbounds[len(outbounds)-1].Target.Network == net.Network_UDP

if targetUdp {
if requireDatagram {
return c.udp()
}
return c.tcp()
Expand Down
Loading