mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-30 13:48:36 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf3c512a70 |
+1
-5
@@ -241,7 +241,6 @@ type MultiplexConfig struct {
|
||||
Concurrency int16 `json:"concurrency"`
|
||||
XudpConcurrency int16 `json:"xudpConcurrency"`
|
||||
XudpProxyUDP443 string `json:"xudpProxyUDP443"`
|
||||
MaxReuseTimes int32 `json:"maxReuseTimes"`
|
||||
}
|
||||
|
||||
func (c *MultiplexConfig) Build() (*vless.Multiplex, error) {
|
||||
@@ -253,10 +252,7 @@ func (c *MultiplexConfig) Build() (*vless.Multiplex, error) {
|
||||
udp443 = "reject"
|
||||
}
|
||||
return multiplex.NormalizeConfig(&vless.Multiplex{
|
||||
Concurrency: int32(c.Concurrency),
|
||||
XudpConcurrency: int32(c.XudpConcurrency),
|
||||
XudpProxyUDP443: udp443,
|
||||
MaxReuseTimes: c.MaxReuseTimes,
|
||||
Concurrency: int32(c.Concurrency), XudpConcurrency: int32(c.XudpConcurrency), XudpProxyUDP443: udp443,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -236,11 +236,9 @@ func TestMultiplexConfig_Build(t *testing.T) {
|
||||
{name: "empty", fields: `{}`},
|
||||
{name: "both zero", fields: `{"concurrency":0,"xudpConcurrency":0}`},
|
||||
{name: "regular pool", fields: `{"concurrency":4}`, want: &vless.Multiplex{Concurrency: 4, XudpProxyUDP443: "reject"}},
|
||||
{name: "regular pool with maxReuseTimes", fields: `{"concurrency":4,"maxReuseTimes":128}`, want: &vless.Multiplex{Concurrency: 4, XudpProxyUDP443: "reject", MaxReuseTimes: 128}},
|
||||
{name: "XUDP only", fields: `{"xudpConcurrency":8}`, want: &vless.Multiplex{XudpConcurrency: 8, XudpProxyUDP443: "reject"}},
|
||||
{name: "negative concurrency", fields: `{"concurrency":-1,"xudpConcurrency":8}`, bad: true},
|
||||
{name: "negative XUDP concurrency", fields: `{"concurrency":4,"xudpConcurrency":-1}`, bad: true},
|
||||
{name: "negative maxReuseTimes", fields: `{"concurrency":4,"maxReuseTimes":-1}`, bad: true},
|
||||
{name: "invalid UDP policy", fields: `{"concurrency":4,"xudpProxyUDP443":"invalid"}`, bad: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
|
||||
@@ -189,7 +189,6 @@ type Multiplex struct {
|
||||
Concurrency int32 `protobuf:"varint,1,opt,name=concurrency,proto3" json:"concurrency,omitempty"`
|
||||
XudpConcurrency int32 `protobuf:"varint,2,opt,name=xudpConcurrency,proto3" json:"xudpConcurrency,omitempty"`
|
||||
XudpProxyUDP443 string `protobuf:"bytes,3,opt,name=xudpProxyUDP443,proto3" json:"xudpProxyUDP443,omitempty"`
|
||||
MaxReuseTimes int32 `protobuf:"varint,4,opt,name=maxReuseTimes,proto3" json:"maxReuseTimes,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
@@ -245,13 +244,6 @@ func (x *Multiplex) GetXudpProxyUDP443() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *Multiplex) GetMaxReuseTimes() int32 {
|
||||
if x != nil {
|
||||
return x.MaxReuseTimes
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
var File_proxy_vless_account_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_proxy_vless_account_proto_rawDesc = "" +
|
||||
@@ -271,12 +263,11 @@ const file_proxy_vless_account_proto_rawDesc = "" +
|
||||
"\apadding\x18\x06 \x01(\tR\apadding\x123\n" +
|
||||
"\areverse\x18\a \x01(\v2\x19.xray.proxy.vless.ReverseR\areverse\x12\x18\n" +
|
||||
"\atestpre\x18\b \x01(\rR\atestpre\x12\x1a\n" +
|
||||
"\btestseed\x18\t \x03(\rR\btestseed\"\xa7\x01\n" +
|
||||
"\btestseed\x18\t \x03(\rR\btestseed\"\x81\x01\n" +
|
||||
"\tMultiplex\x12 \n" +
|
||||
"\vconcurrency\x18\x01 \x01(\x05R\vconcurrency\x12(\n" +
|
||||
"\x0fxudpConcurrency\x18\x02 \x01(\x05R\x0fxudpConcurrency\x12(\n" +
|
||||
"\x0fxudpProxyUDP443\x18\x03 \x01(\tR\x0fxudpProxyUDP443\x12$\n" +
|
||||
"\rmaxReuseTimes\x18\x04 \x01(\x05R\rmaxReuseTimesBR\n" +
|
||||
"\x0fxudpProxyUDP443\x18\x03 \x01(\tR\x0fxudpProxyUDP443BR\n" +
|
||||
"\x14com.xray.proxy.vlessP\x01Z%github.com/xtls/xray-core/proxy/vless\xaa\x02\x10Xray.Proxy.Vlessb\x06proto3"
|
||||
|
||||
var (
|
||||
|
||||
@@ -34,5 +34,4 @@ message Multiplex {
|
||||
int32 concurrency = 1;
|
||||
int32 xudpConcurrency = 2;
|
||||
string xudpProxyUDP443 = 3;
|
||||
int32 maxReuseTimes = 4;
|
||||
}
|
||||
|
||||
@@ -18,8 +18,8 @@ import (
|
||||
|
||||
// NormalizeConfig validates the pool settings and returns nil when both pools are off.
|
||||
func NormalizeConfig(config *vless.Multiplex) (*vless.Multiplex, error) {
|
||||
if config.GetConcurrency() < 0 || config.GetXudpConcurrency() < 0 || config.GetMaxReuseTimes() < 0 {
|
||||
return nil, errors.New(`multiplex "concurrency", "xudpConcurrency" and "maxReuseTimes" must be non-negative`)
|
||||
if config.GetConcurrency() < 0 || config.GetXudpConcurrency() < 0 {
|
||||
return nil, errors.New(`multiplex "concurrency" and "xudpConcurrency" must be non-negative`)
|
||||
}
|
||||
switch config.GetXudpProxyUDP443() {
|
||||
case "", "reject", "allow", "skip":
|
||||
@@ -41,17 +41,17 @@ type Client struct {
|
||||
|
||||
func NewClient(ctx context.Context, config *vless.Multiplex, network string, open func(context.Context) (context.Context, *transport.Link, error)) *Client {
|
||||
c := &Client{config: config}
|
||||
pool := func(concurrency int32, maxReuseTimes uint32) *Pool {
|
||||
return NewPool(ctx, ClientStrategy{MaxConcurrency: uint32(concurrency), MaxReuseTimes: maxReuseTimes}, open)
|
||||
pool := func(concurrency int32) *Pool {
|
||||
return NewPool(ctx, ClientStrategy{MaxConcurrency: uint32(concurrency), MaxConnection: 1024}, open)
|
||||
}
|
||||
if concurrency := config.GetConcurrency(); concurrency > 0 {
|
||||
if network == "xdrive" {
|
||||
concurrency = max(concurrency, 128)
|
||||
}
|
||||
c.tcp = pool(concurrency, uint32(config.GetMaxReuseTimes()))
|
||||
c.tcp = pool(concurrency)
|
||||
}
|
||||
if concurrency := config.GetXudpConcurrency(); concurrency > 0 {
|
||||
c.udp = pool(concurrency, 1024)
|
||||
c.udp = pool(concurrency)
|
||||
}
|
||||
return c
|
||||
}
|
||||
@@ -211,7 +211,7 @@ func (p *Pool) Admit(ctx context.Context, s *Session) error {
|
||||
|
||||
type ClientStrategy struct {
|
||||
MaxConcurrency uint32
|
||||
MaxReuseTimes uint32
|
||||
MaxConnection uint32
|
||||
}
|
||||
|
||||
type ClientWorker struct {
|
||||
@@ -258,7 +258,7 @@ func (m *ClientWorker) TryAdmit(ctx context.Context, s *Session) bool {
|
||||
defer m.Unlock()
|
||||
if ctx.Err() != nil || m.Closed() || m.count == 65535 ||
|
||||
(m.strategy.MaxConcurrency > 0 && m.active >= int(m.strategy.MaxConcurrency)) ||
|
||||
(m.strategy.MaxReuseTimes > 0 && uint32(m.count) >= m.strategy.MaxReuseTimes) {
|
||||
(m.strategy.MaxConnection > 0 && uint32(m.count) >= m.strategy.MaxConnection) {
|
||||
return false
|
||||
}
|
||||
s.parent, s.ID, s.phase, s.done = m.carrier, m.count+1, outputOpen, make(chan struct{})
|
||||
|
||||
@@ -85,7 +85,7 @@ func TestClientWorkerClose(t *testing.T) {
|
||||
links := []*transport.Link{{Reader: r1, Writer: w1}, {Reader: r2, Writer: w2}}
|
||||
owner, stop := context.WithCancel(context.Background())
|
||||
defer stop()
|
||||
pool := NewPool(owner, ClientStrategy{MaxConcurrency: 4, MaxReuseTimes: 4}, func(ctx context.Context) (context.Context, *transport.Link, error) {
|
||||
pool := NewPool(owner, ClientStrategy{MaxConcurrency: 4, MaxConnection: 4}, func(ctx context.Context) (context.Context, *transport.Link, error) {
|
||||
if len(links) == 0 {
|
||||
return nil, nil, errors.New("unexpected opening")
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
_ "embed"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
@@ -24,7 +25,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
|
||||
if streamSettings.ProtocolSettings.(*Config).Ed > 0 {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
conn = &delayDialConn{
|
||||
dialed: make(chan bool, 1),
|
||||
dialed: make(chan bool),
|
||||
cancel: cancel,
|
||||
ctx: ctx,
|
||||
dest: dest,
|
||||
@@ -158,8 +159,8 @@ func dialWebSocket(ctx context.Context, dest net.Destination, streamSettings *in
|
||||
}
|
||||
|
||||
type delayDialConn struct {
|
||||
net.Conn
|
||||
closed bool
|
||||
conn atomic.Value // net.Conn
|
||||
closed atomic.Bool
|
||||
dialed chan bool
|
||||
cancel context.CancelFunc
|
||||
ctx context.Context
|
||||
@@ -171,62 +172,124 @@ type delayDialConn struct {
|
||||
// Without this method, Go promotes LocalAddr from the embedded net.Conn; the
|
||||
// embedded interface is nil before the first Write, so the promoted call panics.
|
||||
func (d *delayDialConn) LocalAddr() net.Addr {
|
||||
if d.Conn == nil {
|
||||
conn := d.conn.Load()
|
||||
if conn == nil {
|
||||
return nil
|
||||
}
|
||||
return d.Conn.LocalAddr()
|
||||
return (conn.(net.Conn)).LocalAddr()
|
||||
}
|
||||
|
||||
// RemoteAddr returns nil until the deferred WebSocket dial has completed.
|
||||
// See LocalAddr for why an explicit method is required here.
|
||||
func (d *delayDialConn) RemoteAddr() net.Addr {
|
||||
if d.Conn == nil {
|
||||
conn := d.conn.Load()
|
||||
if conn == nil {
|
||||
return nil
|
||||
}
|
||||
return d.Conn.RemoteAddr()
|
||||
return (conn.(net.Conn)).RemoteAddr()
|
||||
}
|
||||
|
||||
func (d *delayDialConn) Write(b []byte) (int, error) {
|
||||
if d.closed {
|
||||
if d.closed.Load() {
|
||||
return 0, io.ErrClosedPipe
|
||||
}
|
||||
if d.Conn == nil {
|
||||
conn := d.conn.Load()
|
||||
if conn == nil {
|
||||
ed := b
|
||||
if len(ed) > int(d.streamSettings.ProtocolSettings.(*Config).Ed) {
|
||||
ed = nil
|
||||
}
|
||||
var err error
|
||||
if d.Conn, err = dialWebSocket(d.ctx, d.dest, d.streamSettings, ed); err != nil {
|
||||
c, err := dialWebSocket(d.ctx, d.dest, d.streamSettings, ed)
|
||||
if err != nil {
|
||||
d.Close()
|
||||
return 0, errors.New("failed to dial WebSocket").Base(err)
|
||||
}
|
||||
d.dialed <- true
|
||||
d.conn.Store(c)
|
||||
// someone close the conn after dial success
|
||||
if d.closed.Load() {
|
||||
d.Close()
|
||||
return 0, io.ErrClosedPipe
|
||||
}
|
||||
conn = c
|
||||
close(d.dialed)
|
||||
if ed != nil {
|
||||
return len(ed), nil
|
||||
}
|
||||
}
|
||||
return d.Conn.Write(b)
|
||||
return (conn.(net.Conn)).Write(b)
|
||||
}
|
||||
|
||||
func (d *delayDialConn) Read(b []byte) (int, error) {
|
||||
if d.closed {
|
||||
if d.closed.Load() {
|
||||
return 0, io.ErrClosedPipe
|
||||
}
|
||||
if d.Conn == nil {
|
||||
conn := d.conn.Load()
|
||||
if conn == nil {
|
||||
select {
|
||||
case <-d.ctx.Done():
|
||||
return 0, io.ErrUnexpectedEOF
|
||||
case <-d.dialed:
|
||||
}
|
||||
conn = d.conn.Load()
|
||||
}
|
||||
return d.Conn.Read(b)
|
||||
return (conn.(net.Conn)).Read(b)
|
||||
}
|
||||
|
||||
func (d *delayDialConn) SetDeadline(t time.Time) error {
|
||||
if d.closed.Load() {
|
||||
return io.ErrClosedPipe
|
||||
}
|
||||
conn := d.conn.Load()
|
||||
if conn == nil {
|
||||
d.setDelayedDeadline(t, d.SetDeadline)
|
||||
return nil
|
||||
}
|
||||
return (conn.(net.Conn)).SetDeadline(t)
|
||||
}
|
||||
|
||||
func (d *delayDialConn) SetReadDeadline(t time.Time) error {
|
||||
if d.closed.Load() {
|
||||
return io.ErrClosedPipe
|
||||
}
|
||||
conn := d.conn.Load()
|
||||
if conn == nil {
|
||||
d.setDelayedDeadline(t, d.SetReadDeadline)
|
||||
return nil
|
||||
}
|
||||
return (conn.(net.Conn)).SetReadDeadline(t)
|
||||
}
|
||||
|
||||
func (d *delayDialConn) SetWriteDeadline(t time.Time) error {
|
||||
if d.closed.Load() {
|
||||
return io.ErrClosedPipe
|
||||
}
|
||||
conn := d.conn.Load()
|
||||
if conn == nil {
|
||||
d.setDelayedDeadline(t, d.SetWriteDeadline)
|
||||
return nil
|
||||
}
|
||||
return (conn.(net.Conn)).SetWriteDeadline(t)
|
||||
}
|
||||
|
||||
func (d *delayDialConn) setDelayedDeadline(t time.Time, f func(time.Time) error) {
|
||||
ctx, cancel := context.WithTimeout(d.ctx, time.Until(t))
|
||||
go func() {
|
||||
defer cancel()
|
||||
select {
|
||||
case <-d.dialed:
|
||||
f(t)
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (d *delayDialConn) Close() error {
|
||||
d.closed = true
|
||||
d.closed.Store(true)
|
||||
d.cancel()
|
||||
if d.Conn == nil {
|
||||
conn := d.conn.Load()
|
||||
if conn == nil {
|
||||
return nil
|
||||
}
|
||||
return d.Conn.Close()
|
||||
return (conn.(net.Conn)).Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user