mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-03 16:24:04 +00:00
simplify port range
This commit is contained in:
1 parent
5ceac7a6e2
commit
393a64820f
10 files changed
+38
-81
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
@@ -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) {
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user