Pārlūkot izejas kodu

simplify port range

v2ray 9 gadi atpakaļ
vecāks
revīzija
393a64820f

+ 3 - 3
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{}

+ 3 - 4
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)

+ 5 - 0
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
+}

+ 11 - 23
common/net/json/portrange.go → 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

+ 10 - 7
common/net/json/portrange_test.go → 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) {

+ 0 - 6
common/net/portrange.go

@@ -1,6 +0,0 @@
-package net
-
-type PortRange interface {
-	From() Port
-	To() Port
-}

+ 0 - 18
common/net/testing/portrange.go

@@ -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
-}

+ 2 - 2
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 {

+ 2 - 3
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 {

+ 2 - 15
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 {