mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-02-20 23:47:21 -05:00
simplify port range
This commit is contained in:
parent
5ceac7a6e2
commit
393a64820f
@ -82,7 +82,7 @@ type FieldRule struct {
|
|||||||
Rule
|
Rule
|
||||||
Domain []DomainMatcher
|
Domain []DomainMatcher
|
||||||
IP []*net.IPNet
|
IP []*net.IPNet
|
||||||
Port v2net.PortRange
|
Port *v2net.PortRange
|
||||||
Network v2net.NetworkList
|
Network v2net.NetworkList
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,7 +122,7 @@ func (this *FieldRule) Apply(dest v2net.Destination) bool {
|
|||||||
|
|
||||||
if this.Port != nil {
|
if this.Port != nil {
|
||||||
port := dest.Port()
|
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
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -141,7 +141,7 @@ func (this *FieldRule) UnmarshalJSON(data []byte) error {
|
|||||||
Rule
|
Rule
|
||||||
Domain *StringList `json:"domain"`
|
Domain *StringList `json:"domain"`
|
||||||
IP *StringList `json:"ip"`
|
IP *StringList `json:"ip"`
|
||||||
Port *v2netjson.PortRange `json:"port"`
|
Port *v2net.PortRange `json:"port"`
|
||||||
Network *v2netjson.NetworkList `json:"network"`
|
Network *v2netjson.NetworkList `json:"network"`
|
||||||
}
|
}
|
||||||
rawFieldRule := RawFieldRule{}
|
rawFieldRule := RawFieldRule{}
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
|
|
||||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||||
"github.com/v2ray/v2ray-core/testing/assert"
|
"github.com/v2ray/v2ray-core/testing/assert"
|
||||||
)
|
)
|
||||||
@ -47,9 +46,9 @@ func TestPortMatching(t *testing.T) {
|
|||||||
v2testing.Current(t)
|
v2testing.Current(t)
|
||||||
|
|
||||||
rule := &FieldRule{
|
rule := &FieldRule{
|
||||||
Port: &v2nettesting.PortRange{
|
Port: &v2net.PortRange{
|
||||||
FromValue: 0,
|
From: 0,
|
||||||
ToValue: 100,
|
To: 100,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
dest := v2net.TCPDestination(v2net.DomainAddress("www.v2ray.com"), 80)
|
dest := v2net.TCPDestination(v2net.DomainAddress("www.v2ray.com"), 80)
|
||||||
|
@ -21,3 +21,8 @@ func (this Port) Bytes() []byte {
|
|||||||
func (this Port) String() string {
|
func (this Port) String() string {
|
||||||
return serial.Uint16Literal(this).String()
|
return serial.Uint16Literal(this).String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PortRange struct {
|
||||||
|
From Port
|
||||||
|
To Port
|
||||||
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
package json
|
// +build json
|
||||||
|
|
||||||
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -7,26 +9,12 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
"github.com/v2ray/v2ray-core/common/log"
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
InvalidPortRange = errors.New("Invalid port range.")
|
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 {
|
func (this *PortRange) UnmarshalJSON(data []byte) error {
|
||||||
var maybeint int
|
var maybeint int
|
||||||
err := json.Unmarshal(data, &maybeint)
|
err := json.Unmarshal(data, &maybeint)
|
||||||
@ -35,8 +23,8 @@ func (this *PortRange) UnmarshalJSON(data []byte) error {
|
|||||||
log.Error("Invalid port [%s]", string(data))
|
log.Error("Invalid port [%s]", string(data))
|
||||||
return InvalidPortRange
|
return InvalidPortRange
|
||||||
}
|
}
|
||||||
this.from = v2net.Port(maybeint)
|
this.From = Port(maybeint)
|
||||||
this.to = v2net.Port(maybeint)
|
this.To = Port(maybeint)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,8 +38,8 @@ func (this *PortRange) UnmarshalJSON(data []byte) error {
|
|||||||
log.Error("Invalid from port %s", pair[0])
|
log.Error("Invalid from port %s", pair[0])
|
||||||
return InvalidPortRange
|
return InvalidPortRange
|
||||||
}
|
}
|
||||||
this.from = v2net.Port(value)
|
this.From = Port(value)
|
||||||
this.to = v2net.Port(value)
|
this.To = Port(value)
|
||||||
return nil
|
return nil
|
||||||
} else if len(pair) == 2 {
|
} else if len(pair) == 2 {
|
||||||
from, err := strconv.Atoi(pair[0])
|
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])
|
log.Error("Invalid from port %s", pair[0])
|
||||||
return InvalidPortRange
|
return InvalidPortRange
|
||||||
}
|
}
|
||||||
this.from = v2net.Port(from)
|
this.From = Port(from)
|
||||||
|
|
||||||
to, err := strconv.Atoi(pair[1])
|
to, err := strconv.Atoi(pair[1])
|
||||||
if err != nil || to <= 0 || to >= 65535 {
|
if err != nil || to <= 0 || to >= 65535 {
|
||||||
log.Error("Invalid to port %s", pair[1])
|
log.Error("Invalid to port %s", pair[1])
|
||||||
return InvalidPortRange
|
return InvalidPortRange
|
||||||
}
|
}
|
||||||
this.to = v2net.Port(to)
|
this.To = Port(to)
|
||||||
|
|
||||||
if this.from > this.to {
|
if this.From > this.To {
|
||||||
log.Error("Invalid port range %d -> %d", this.from, this.to)
|
log.Error("Invalid port range %d -> %d", this.From, this.To)
|
||||||
return InvalidPortRange
|
return InvalidPortRange
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
@ -1,9 +1,12 @@
|
|||||||
package json
|
// +build json
|
||||||
|
|
||||||
|
package net_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/v2ray/v2ray-core/common/net"
|
||||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||||
"github.com/v2ray/v2ray-core/testing/assert"
|
"github.com/v2ray/v2ray-core/testing/assert"
|
||||||
)
|
)
|
||||||
@ -15,8 +18,8 @@ func TestIntPort(t *testing.T) {
|
|||||||
err := json.Unmarshal([]byte("1234"), &portRange)
|
err := json.Unmarshal([]byte("1234"), &portRange)
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
assert.Uint16(portRange.from.Value()).Equals(uint16(1234))
|
assert.Uint16(portRange.From.Value()).Equals(uint16(1234))
|
||||||
assert.Uint16(portRange.to.Value()).Equals(uint16(1234))
|
assert.Uint16(portRange.To.Value()).Equals(uint16(1234))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOverRangeIntPort(t *testing.T) {
|
func TestOverRangeIntPort(t *testing.T) {
|
||||||
@ -37,8 +40,8 @@ func TestSingleStringPort(t *testing.T) {
|
|||||||
err := json.Unmarshal([]byte("\"1234\""), &portRange)
|
err := json.Unmarshal([]byte("\"1234\""), &portRange)
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
assert.Uint16(portRange.from.Value()).Equals(uint16(1234))
|
assert.Uint16(portRange.From.Value()).Equals(uint16(1234))
|
||||||
assert.Uint16(portRange.to.Value()).Equals(uint16(1234))
|
assert.Uint16(portRange.To.Value()).Equals(uint16(1234))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringPairPort(t *testing.T) {
|
func TestStringPairPort(t *testing.T) {
|
||||||
@ -48,8 +51,8 @@ func TestStringPairPort(t *testing.T) {
|
|||||||
err := json.Unmarshal([]byte("\"1234-5678\""), &portRange)
|
err := json.Unmarshal([]byte("\"1234-5678\""), &portRange)
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
assert.Uint16(portRange.from.Value()).Equals(uint16(1234))
|
assert.Uint16(portRange.From.Value()).Equals(uint16(1234))
|
||||||
assert.Uint16(portRange.to.Value()).Equals(uint16(5678))
|
assert.Uint16(portRange.To.Value()).Equals(uint16(5678))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOverRangeStringPort(t *testing.T) {
|
func TestOverRangeStringPort(t *testing.T) {
|
@ -1,6 +0,0 @@
|
|||||||
package net
|
|
||||||
|
|
||||||
type PortRange interface {
|
|
||||||
From() Port
|
|
||||||
To() Port
|
|
||||||
}
|
|
@ -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
|
|
||||||
}
|
|
@ -23,8 +23,8 @@ type InboundDetourHandler struct {
|
|||||||
|
|
||||||
func (this *InboundDetourHandler) Initialize() error {
|
func (this *InboundDetourHandler) Initialize() error {
|
||||||
ports := this.config.PortRange()
|
ports := this.config.PortRange()
|
||||||
this.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To()-ports.From()+1)
|
this.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To-ports.From+1)
|
||||||
for i := ports.From(); i <= ports.To(); i++ {
|
for i := ports.From; i <= ports.To; i++ {
|
||||||
ichConfig := this.config.Settings()
|
ichConfig := this.config.Settings()
|
||||||
ich, err := proxyrepo.CreateInboundConnectionHandler(this.config.Protocol(), this.space, ichConfig)
|
ich, err := proxyrepo.CreateInboundConnectionHandler(this.config.Protocol(), this.space, ichConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
v2netjson "github.com/v2ray/v2ray-core/common/net/json"
|
|
||||||
"github.com/v2ray/v2ray-core/shell/point"
|
"github.com/v2ray/v2ray-core/shell/point"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -28,7 +27,7 @@ func (this *InboundDetourAllocationConfig) Concurrency() int {
|
|||||||
|
|
||||||
type InboundDetourConfig struct {
|
type InboundDetourConfig struct {
|
||||||
ProtocolValue string `json:"protocol"`
|
ProtocolValue string `json:"protocol"`
|
||||||
PortRangeValue *v2netjson.PortRange `json:"port"`
|
PortRangeValue *v2net.PortRange `json:"port"`
|
||||||
SettingsValue json.RawMessage `json:"settings"`
|
SettingsValue json.RawMessage `json:"settings"`
|
||||||
TagValue string `json:"tag"`
|
TagValue string `json:"tag"`
|
||||||
AllocationValue *InboundDetourAllocationConfig `json:"allocate"`
|
AllocationValue *InboundDetourAllocationConfig `json:"allocate"`
|
||||||
@ -43,7 +42,7 @@ func (this *InboundDetourConfig) Protocol() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *InboundDetourConfig) PortRange() v2net.PortRange {
|
func (this *InboundDetourConfig) PortRange() v2net.PortRange {
|
||||||
return this.PortRangeValue
|
return *this.PortRangeValue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *InboundDetourConfig) Settings() []byte {
|
func (this *InboundDetourConfig) Settings() []byte {
|
||||||
|
@ -39,19 +39,6 @@ func (this *LogConfig) LogLevel() log.LogLevel {
|
|||||||
return this.LogLevelValue
|
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 {
|
type InboundDetourAllocationConfig struct {
|
||||||
StrategyValue string
|
StrategyValue string
|
||||||
ConcurrencyValue int
|
ConcurrencyValue int
|
||||||
@ -72,7 +59,7 @@ func (this *InboundDetourAllocationConfig) Concurrency() int {
|
|||||||
|
|
||||||
type InboundDetourConfig struct {
|
type InboundDetourConfig struct {
|
||||||
*ConnectionConfig
|
*ConnectionConfig
|
||||||
PortRangeValue *PortRange
|
PortRangeValue *v2net.PortRange
|
||||||
TagValue string
|
TagValue string
|
||||||
AllocationStrategy *InboundDetourAllocationConfig
|
AllocationStrategy *InboundDetourAllocationConfig
|
||||||
}
|
}
|
||||||
@ -86,7 +73,7 @@ func (this *InboundDetourConfig) Tag() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *InboundDetourConfig) PortRange() v2net.PortRange {
|
func (this *InboundDetourConfig) PortRange() v2net.PortRange {
|
||||||
return this.PortRangeValue
|
return *this.PortRangeValue
|
||||||
}
|
}
|
||||||
|
|
||||||
type OutboundDetourConfig struct {
|
type OutboundDetourConfig struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user