mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-03 00:22:05 +00:00
Add Port as a type
This commit is contained in:
1 parent
e8e6cf1429
commit
cee85bdf26
16 files changed
+125
-98
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -14,15 +15,15 @@ var (
|
||||
)
|
||||
|
||||
type PortRange struct {
|
||||
from uint16
|
||||
to uint16
|
||||
from v2net.Port
|
||||
to v2net.Port
|
||||
}
|
||||
|
||||
func (this *PortRange) From() uint16 {
|
||||
func (this *PortRange) From() v2net.Port {
|
||||
return this.from
|
||||
}
|
||||
|
||||
func (this *PortRange) To() uint16 {
|
||||
func (this *PortRange) To() v2net.Port {
|
||||
return this.to
|
||||
}
|
||||
|
||||
@@ -34,8 +35,8 @@ func (this *PortRange) UnmarshalJSON(data []byte) error {
|
||||
log.Error("Invalid port [%s]", string(data))
|
||||
return InvalidPortRange
|
||||
}
|
||||
this.from = uint16(maybeint)
|
||||
this.to = uint16(maybeint)
|
||||
this.from = v2net.Port(uint16(maybeint))
|
||||
this.to = v2net.Port(uint16(maybeint))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -49,8 +50,8 @@ func (this *PortRange) UnmarshalJSON(data []byte) error {
|
||||
log.Error("Invalid from port %s", pair[0])
|
||||
return InvalidPortRange
|
||||
}
|
||||
this.from = uint16(value)
|
||||
this.to = uint16(value)
|
||||
this.from = v2net.Port(uint16(value))
|
||||
this.to = v2net.Port(uint16(value))
|
||||
return nil
|
||||
} else if len(pair) == 2 {
|
||||
from, err := strconv.Atoi(pair[0])
|
||||
@@ -58,14 +59,14 @@ func (this *PortRange) UnmarshalJSON(data []byte) error {
|
||||
log.Error("Invalid from port %s", pair[0])
|
||||
return InvalidPortRange
|
||||
}
|
||||
this.from = uint16(from)
|
||||
this.from = v2net.Port(uint16(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 = uint16(to)
|
||||
this.to = v2net.Port(uint16(to))
|
||||
|
||||
if this.from > this.to {
|
||||
log.Error("Invalid port range %d -> %d", this.from, this.to)
|
||||
|
||||
@@ -14,8 +14,8 @@ func TestIntPort(t *testing.T) {
|
||||
err := json.Unmarshal([]byte("1234"), &portRange)
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
assert.Uint16(portRange.from).Equals(uint16(1234))
|
||||
assert.Uint16(portRange.to).Equals(uint16(1234))
|
||||
assert.Uint16(portRange.from.Value()).Equals(uint16(1234))
|
||||
assert.Uint16(portRange.to.Value()).Equals(uint16(1234))
|
||||
}
|
||||
|
||||
func TestOverRangeIntPort(t *testing.T) {
|
||||
@@ -36,8 +36,8 @@ func TestSingleStringPort(t *testing.T) {
|
||||
err := json.Unmarshal([]byte("\"1234\""), &portRange)
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
assert.Uint16(portRange.from).Equals(uint16(1234))
|
||||
assert.Uint16(portRange.to).Equals(uint16(1234))
|
||||
assert.Uint16(portRange.from.Value()).Equals(uint16(1234))
|
||||
assert.Uint16(portRange.to.Value()).Equals(uint16(1234))
|
||||
}
|
||||
|
||||
func TestStringPairPort(t *testing.T) {
|
||||
@@ -47,8 +47,8 @@ func TestStringPairPort(t *testing.T) {
|
||||
err := json.Unmarshal([]byte("\"1234-5678\""), &portRange)
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
assert.Uint16(portRange.from).Equals(uint16(1234))
|
||||
assert.Uint16(portRange.to).Equals(uint16(5678))
|
||||
assert.Uint16(portRange.from.Value()).Equals(uint16(1234))
|
||||
assert.Uint16(portRange.to.Value()).Equals(uint16(5678))
|
||||
}
|
||||
|
||||
func TestOverRangeStringPort(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user