From 393a64820fb025346d41e5087cebb93fe7682d2d Mon Sep 17 00:00:00 2001 From: v2ray Date: Fri, 15 Jan 2016 13:39:36 +0100 Subject: [PATCH] simplify port range --- app/router/rules/json/fieldrule.go | 6 ++-- app/router/rules/json/fieldrule_test.go | 7 ++-- common/net/port.go | 5 +++ .../net/{json/portrange.go => port_json.go} | 34 ++++++------------- .../portrange_test.go => port_json_test.go} | 17 ++++++---- common/net/portrange.go | 6 ---- common/net/testing/portrange.go | 18 ---------- shell/point/inbound_detour.go | 4 +-- shell/point/json/inbound_detour.go | 5 ++- shell/point/testing/mocks/config.go | 17 ++-------- 10 files changed, 38 insertions(+), 81 deletions(-) rename common/net/{json/portrange.go => port_json.go} (69%) rename common/net/{json/portrange_test.go => port_json_test.go} (76%) delete mode 100644 common/net/portrange.go delete mode 100644 common/net/testing/portrange.go diff --git a/app/router/rules/json/fieldrule.go b/app/router/rules/json/fieldrule.go index d74b5aaac..2369f6a35 100644 --- a/app/router/rules/json/fieldrule.go +++ b/app/router/rules/json/fieldrule.go @@ -82,7 +82,7 @@ type FieldRule struct { Rule Domain []DomainMatcher IP []*net.IPNet - Port v2net.PortRange + Port *v2net.PortRange Network v2net.NetworkList } @@ -122,7 +122,7 @@ func (this *FieldRule) Apply(dest v2net.Destination) bool { if this.Port != nil { port := dest.Port() - if port.Value() < this.Port.From().Value() || port.Value() > this.Port.To().Value() { + if port.Value() < this.Port.From.Value() || port.Value() > this.Port.To.Value() { return false } } @@ -141,7 +141,7 @@ func (this *FieldRule) UnmarshalJSON(data []byte) error { Rule Domain *StringList `json:"domain"` IP *StringList `json:"ip"` - Port *v2netjson.PortRange `json:"port"` + Port *v2net.PortRange `json:"port"` Network *v2netjson.NetworkList `json:"network"` } rawFieldRule := RawFieldRule{} diff --git a/app/router/rules/json/fieldrule_test.go b/app/router/rules/json/fieldrule_test.go index 02876f162..d06f94e47 100644 --- a/app/router/rules/json/fieldrule_test.go +++ b/app/router/rules/json/fieldrule_test.go @@ -5,7 +5,6 @@ import ( "testing" v2net "github.com/v2ray/v2ray-core/common/net" - v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -47,9 +46,9 @@ func TestPortMatching(t *testing.T) { v2testing.Current(t) rule := &FieldRule{ - Port: &v2nettesting.PortRange{ - FromValue: 0, - ToValue: 100, + Port: &v2net.PortRange{ + From: 0, + To: 100, }, } dest := v2net.TCPDestination(v2net.DomainAddress("www.v2ray.com"), 80) diff --git a/common/net/port.go b/common/net/port.go index 00f510b25..5efb191dc 100644 --- a/common/net/port.go +++ b/common/net/port.go @@ -21,3 +21,8 @@ func (this Port) Bytes() []byte { func (this Port) String() string { return serial.Uint16Literal(this).String() } + +type PortRange struct { + From Port + To Port +} diff --git a/common/net/json/portrange.go b/common/net/port_json.go similarity index 69% rename from common/net/json/portrange.go rename to common/net/port_json.go index c72aa0774..6b2999494 100644 --- a/common/net/json/portrange.go +++ b/common/net/port_json.go @@ -1,4 +1,6 @@ -package json +// +build json + +package net import ( "encoding/json" @@ -7,26 +9,12 @@ import ( "strings" "github.com/v2ray/v2ray-core/common/log" - v2net "github.com/v2ray/v2ray-core/common/net" ) var ( InvalidPortRange = errors.New("Invalid port range.") ) -type PortRange struct { - from v2net.Port - to v2net.Port -} - -func (this *PortRange) From() v2net.Port { - return this.from -} - -func (this *PortRange) To() v2net.Port { - return this.to -} - func (this *PortRange) UnmarshalJSON(data []byte) error { var maybeint int err := json.Unmarshal(data, &maybeint) @@ -35,8 +23,8 @@ func (this *PortRange) UnmarshalJSON(data []byte) error { log.Error("Invalid port [%s]", string(data)) return InvalidPortRange } - this.from = v2net.Port(maybeint) - this.to = v2net.Port(maybeint) + this.From = Port(maybeint) + this.To = Port(maybeint) return nil } @@ -50,8 +38,8 @@ func (this *PortRange) UnmarshalJSON(data []byte) error { log.Error("Invalid from port %s", pair[0]) return InvalidPortRange } - this.from = v2net.Port(value) - this.to = v2net.Port(value) + this.From = Port(value) + this.To = Port(value) return nil } else if len(pair) == 2 { from, err := strconv.Atoi(pair[0]) @@ -59,17 +47,17 @@ func (this *PortRange) UnmarshalJSON(data []byte) error { log.Error("Invalid from port %s", pair[0]) return InvalidPortRange } - this.from = v2net.Port(from) + this.From = Port(from) to, err := strconv.Atoi(pair[1]) if err != nil || to <= 0 || to >= 65535 { log.Error("Invalid to port %s", pair[1]) return InvalidPortRange } - this.to = v2net.Port(to) + this.To = Port(to) - if this.from > this.to { - log.Error("Invalid port range %d -> %d", this.from, this.to) + if this.From > this.To { + log.Error("Invalid port range %d -> %d", this.From, this.To) return InvalidPortRange } return nil diff --git a/common/net/json/portrange_test.go b/common/net/port_json_test.go similarity index 76% rename from common/net/json/portrange_test.go rename to common/net/port_json_test.go index eb3641c79..1f9e46bc1 100644 --- a/common/net/json/portrange_test.go +++ b/common/net/port_json_test.go @@ -1,9 +1,12 @@ -package json +// +build json + +package net_test import ( "encoding/json" "testing" + . "github.com/v2ray/v2ray-core/common/net" v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -15,8 +18,8 @@ func TestIntPort(t *testing.T) { err := json.Unmarshal([]byte("1234"), &portRange) assert.Error(err).IsNil() - assert.Uint16(portRange.from.Value()).Equals(uint16(1234)) - assert.Uint16(portRange.to.Value()).Equals(uint16(1234)) + assert.Uint16(portRange.From.Value()).Equals(uint16(1234)) + assert.Uint16(portRange.To.Value()).Equals(uint16(1234)) } func TestOverRangeIntPort(t *testing.T) { @@ -37,8 +40,8 @@ func TestSingleStringPort(t *testing.T) { err := json.Unmarshal([]byte("\"1234\""), &portRange) assert.Error(err).IsNil() - assert.Uint16(portRange.from.Value()).Equals(uint16(1234)) - assert.Uint16(portRange.to.Value()).Equals(uint16(1234)) + assert.Uint16(portRange.From.Value()).Equals(uint16(1234)) + assert.Uint16(portRange.To.Value()).Equals(uint16(1234)) } func TestStringPairPort(t *testing.T) { @@ -48,8 +51,8 @@ func TestStringPairPort(t *testing.T) { err := json.Unmarshal([]byte("\"1234-5678\""), &portRange) assert.Error(err).IsNil() - assert.Uint16(portRange.from.Value()).Equals(uint16(1234)) - assert.Uint16(portRange.to.Value()).Equals(uint16(5678)) + assert.Uint16(portRange.From.Value()).Equals(uint16(1234)) + assert.Uint16(portRange.To.Value()).Equals(uint16(5678)) } func TestOverRangeStringPort(t *testing.T) { diff --git a/common/net/portrange.go b/common/net/portrange.go deleted file mode 100644 index d42c6d1af..000000000 --- a/common/net/portrange.go +++ /dev/null @@ -1,6 +0,0 @@ -package net - -type PortRange interface { - From() Port - To() Port -} diff --git a/common/net/testing/portrange.go b/common/net/testing/portrange.go deleted file mode 100644 index 6b1e9f738..000000000 --- a/common/net/testing/portrange.go +++ /dev/null @@ -1,18 +0,0 @@ -package testing - -import ( - v2net "github.com/v2ray/v2ray-core/common/net" -) - -type PortRange struct { - FromValue v2net.Port - ToValue v2net.Port -} - -func (this *PortRange) From() v2net.Port { - return this.FromValue -} - -func (this *PortRange) To() v2net.Port { - return this.ToValue -} diff --git a/shell/point/inbound_detour.go b/shell/point/inbound_detour.go index 0803fc0cb..92639fd23 100644 --- a/shell/point/inbound_detour.go +++ b/shell/point/inbound_detour.go @@ -23,8 +23,8 @@ type InboundDetourHandler struct { func (this *InboundDetourHandler) Initialize() error { ports := this.config.PortRange() - this.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To()-ports.From()+1) - for i := ports.From(); i <= ports.To(); i++ { + this.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To-ports.From+1) + for i := ports.From; i <= ports.To; i++ { ichConfig := this.config.Settings() ich, err := proxyrepo.CreateInboundConnectionHandler(this.config.Protocol(), this.space, ichConfig) if err != nil { diff --git a/shell/point/json/inbound_detour.go b/shell/point/json/inbound_detour.go index 79998c857..ab33ed97d 100644 --- a/shell/point/json/inbound_detour.go +++ b/shell/point/json/inbound_detour.go @@ -4,7 +4,6 @@ import ( "encoding/json" v2net "github.com/v2ray/v2ray-core/common/net" - v2netjson "github.com/v2ray/v2ray-core/common/net/json" "github.com/v2ray/v2ray-core/shell/point" ) @@ -28,7 +27,7 @@ func (this *InboundDetourAllocationConfig) Concurrency() int { type InboundDetourConfig struct { ProtocolValue string `json:"protocol"` - PortRangeValue *v2netjson.PortRange `json:"port"` + PortRangeValue *v2net.PortRange `json:"port"` SettingsValue json.RawMessage `json:"settings"` TagValue string `json:"tag"` AllocationValue *InboundDetourAllocationConfig `json:"allocate"` @@ -43,7 +42,7 @@ func (this *InboundDetourConfig) Protocol() string { } func (this *InboundDetourConfig) PortRange() v2net.PortRange { - return this.PortRangeValue + return *this.PortRangeValue } func (this *InboundDetourConfig) Settings() []byte { diff --git a/shell/point/testing/mocks/config.go b/shell/point/testing/mocks/config.go index b2b8d6392..0b76e416d 100644 --- a/shell/point/testing/mocks/config.go +++ b/shell/point/testing/mocks/config.go @@ -39,19 +39,6 @@ func (this *LogConfig) LogLevel() log.LogLevel { return this.LogLevelValue } -type PortRange struct { - FromValue v2net.Port - ToValue v2net.Port -} - -func (this *PortRange) From() v2net.Port { - return this.FromValue -} - -func (this *PortRange) To() v2net.Port { - return this.ToValue -} - type InboundDetourAllocationConfig struct { StrategyValue string ConcurrencyValue int @@ -72,7 +59,7 @@ func (this *InboundDetourAllocationConfig) Concurrency() int { type InboundDetourConfig struct { *ConnectionConfig - PortRangeValue *PortRange + PortRangeValue *v2net.PortRange TagValue string AllocationStrategy *InboundDetourAllocationConfig } @@ -86,7 +73,7 @@ func (this *InboundDetourConfig) Tag() string { } func (this *InboundDetourConfig) PortRange() v2net.PortRange { - return this.PortRangeValue + return *this.PortRangeValue } type OutboundDetourConfig struct {