From 29d7865d78289b07376e65cd1e68f25692bda950 Mon Sep 17 00:00:00 2001 From: RPRX <63339210+RPRX@users.noreply.github.com> Date: Mon, 10 Apr 2023 10:15:16 +0800 Subject: [PATCH] Refine "only" in Mux config --- app/proxyman/config.pb.go | 2 +- app/proxyman/config.proto | 2 +- infra/conf/xray.go | 15 +++++++++++---- infra/conf/xray_test.go | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/proxyman/config.pb.go b/app/proxyman/config.pb.go index 03613e88..c3b221c4 100644 --- a/app/proxyman/config.pb.go +++ b/app/proxyman/config.pb.go @@ -595,7 +595,7 @@ type MultiplexingConfig struct { Enabled bool `protobuf:"varint,1,opt,name=enabled,proto3" json:"enabled,omitempty"` // Max number of concurrent connections that one Mux connection can handle. Concurrency uint32 `protobuf:"varint,2,opt,name=concurrency,proto3" json:"concurrency,omitempty"` - // Both(0), TCP(1), UDP(2). + // Both(0), TCP(net.Network_TCP), UDP(net.Network_UDP). Only uint32 `protobuf:"varint,3,opt,name=only,proto3" json:"only,omitempty"` } diff --git a/app/proxyman/config.proto b/app/proxyman/config.proto index 54f63436..8f282aa2 100644 --- a/app/proxyman/config.proto +++ b/app/proxyman/config.proto @@ -98,6 +98,6 @@ message MultiplexingConfig { bool enabled = 1; // Max number of concurrent connections that one Mux connection can handle. uint32 concurrency = 2; - // Both(0), TCP(1), UDP(2). + // Both(0), TCP(net.Network_TCP), UDP(net.Network_UDP). uint32 only = 3; } diff --git a/infra/conf/xray.go b/infra/conf/xray.go index 2306e380..63ffccf5 100644 --- a/infra/conf/xray.go +++ b/infra/conf/xray.go @@ -114,9 +114,9 @@ type MuxConfig struct { } // Build creates MultiplexingConfig, Concurrency < 0 completely disables mux. -func (m *MuxConfig) Build() *proxyman.MultiplexingConfig { +func (m *MuxConfig) Build() (*proxyman.MultiplexingConfig, error) { if m.Concurrency < 0 { - return nil + return nil, nil } if m.Concurrency == 0 { m.Concurrency = 8 @@ -128,13 +128,16 @@ func (m *MuxConfig) Build() *proxyman.MultiplexingConfig { } switch strings.ToLower(m.Only) { + case "", "both": case "tcp": config.Only = uint32(net.Network_TCP) case "udp": config.Only = uint32(net.Network_UDP) + default: + return nil, newError(`unknown "only": `, m.Only) } - return config + return config, nil } type InboundDetourAllocationConfig struct { @@ -348,7 +351,11 @@ func (c *OutboundDetourConfig) Build() (*core.OutboundHandlerConfig, error) { } if c.MuxSettings != nil { - senderSettings.MultiplexSettings = c.MuxSettings.Build() + ms, err := c.MuxSettings.Build() + if err != nil { + return nil, newError("failed to build Mux config.").Base(err) + } + senderSettings.MultiplexSettings = ms } settings := []byte("{}") diff --git a/infra/conf/xray_test.go b/infra/conf/xray_test.go index 8c8151de..02d868fe 100644 --- a/infra/conf/xray_test.go +++ b/infra/conf/xray_test.go @@ -357,7 +357,7 @@ func TestMuxConfig_Build(t *testing.T) { t.Run(tt.name, func(t *testing.T) { m := &MuxConfig{} common.Must(json.Unmarshal([]byte(tt.fields), m)) - if got := m.Build(); !reflect.DeepEqual(got, tt.want) { + if got, _ := m.Build(); !reflect.DeepEqual(got, tt.want) { t.Errorf("MuxConfig.Build() = %v, want %v", got, tt.want) } })