Refine "only" in Mux config

This commit is contained in:
RPRX 2023-04-10 10:15:16 +08:00 committed by GitHub
parent 05d24d6827
commit 29d7865d78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 7 deletions

View file

@ -595,7 +595,7 @@ type MultiplexingConfig struct {
Enabled bool `protobuf:"varint,1,opt,name=enabled,proto3" json:"enabled,omitempty"` Enabled bool `protobuf:"varint,1,opt,name=enabled,proto3" json:"enabled,omitempty"`
// Max number of concurrent connections that one Mux connection can handle. // Max number of concurrent connections that one Mux connection can handle.
Concurrency uint32 `protobuf:"varint,2,opt,name=concurrency,proto3" json:"concurrency,omitempty"` 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"` Only uint32 `protobuf:"varint,3,opt,name=only,proto3" json:"only,omitempty"`
} }

View file

@ -98,6 +98,6 @@ message MultiplexingConfig {
bool enabled = 1; bool enabled = 1;
// Max number of concurrent connections that one Mux connection can handle. // Max number of concurrent connections that one Mux connection can handle.
uint32 concurrency = 2; uint32 concurrency = 2;
// Both(0), TCP(1), UDP(2). // Both(0), TCP(net.Network_TCP), UDP(net.Network_UDP).
uint32 only = 3; uint32 only = 3;
} }

View file

@ -114,9 +114,9 @@ type MuxConfig struct {
} }
// Build creates MultiplexingConfig, Concurrency < 0 completely disables mux. // 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 { if m.Concurrency < 0 {
return nil return nil, nil
} }
if m.Concurrency == 0 { if m.Concurrency == 0 {
m.Concurrency = 8 m.Concurrency = 8
@ -128,13 +128,16 @@ func (m *MuxConfig) Build() *proxyman.MultiplexingConfig {
} }
switch strings.ToLower(m.Only) { switch strings.ToLower(m.Only) {
case "", "both":
case "tcp": case "tcp":
config.Only = uint32(net.Network_TCP) config.Only = uint32(net.Network_TCP)
case "udp": case "udp":
config.Only = uint32(net.Network_UDP) config.Only = uint32(net.Network_UDP)
default:
return nil, newError(`unknown "only": `, m.Only)
} }
return config return config, nil
} }
type InboundDetourAllocationConfig struct { type InboundDetourAllocationConfig struct {
@ -348,7 +351,11 @@ func (c *OutboundDetourConfig) Build() (*core.OutboundHandlerConfig, error) {
} }
if c.MuxSettings != nil { 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("{}") settings := []byte("{}")

View file

@ -357,7 +357,7 @@ func TestMuxConfig_Build(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
m := &MuxConfig{} m := &MuxConfig{}
common.Must(json.Unmarshal([]byte(tt.fields), m)) 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) t.Errorf("MuxConfig.Build() = %v, want %v", got, tt.want)
} }
}) })