mirror of https://github.com/v2ray/v2ray-core
Port range
parent
1c09b70931
commit
1708c48f5b
|
@ -0,0 +1,79 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
)
|
||||
|
||||
var (
|
||||
InvalidPortRange = errors.New("Invalid port range.")
|
||||
)
|
||||
|
||||
type PortRange struct {
|
||||
from uint16
|
||||
to uint16
|
||||
}
|
||||
|
||||
func (this *PortRange) From() uint16 {
|
||||
return this.from
|
||||
}
|
||||
|
||||
func (this *PortRange) To() uint16 {
|
||||
return this.to
|
||||
}
|
||||
|
||||
func (this *PortRange) UnmarshalJSON(data []byte) error {
|
||||
var maybeint int
|
||||
err := json.Unmarshal(data, &maybeint)
|
||||
if err == nil {
|
||||
if maybeint <= 0 || maybeint >= 65535 {
|
||||
log.Error("Invalid port [%s]", string(data))
|
||||
return InvalidPortRange
|
||||
}
|
||||
this.from = uint16(maybeint)
|
||||
this.to = uint16(maybeint)
|
||||
return nil
|
||||
}
|
||||
|
||||
var maybestring string
|
||||
err = json.Unmarshal(data, &maybestring)
|
||||
if err == nil {
|
||||
pair := strings.SplitN(maybestring, "-", 2)
|
||||
if len(pair) == 1 {
|
||||
value, err := strconv.Atoi(pair[0])
|
||||
if err != nil || value <= 0 || value >= 65535 {
|
||||
log.Error("Invalid from port %s", pair[0])
|
||||
return InvalidPortRange
|
||||
}
|
||||
this.from = uint16(value)
|
||||
this.to = uint16(value)
|
||||
return nil
|
||||
} else if len(pair) == 2 {
|
||||
from, err := strconv.Atoi(pair[0])
|
||||
if err != nil || from <= 0 || from >= 65535 {
|
||||
log.Error("Invalid from port %s", pair[0])
|
||||
return InvalidPortRange
|
||||
}
|
||||
this.from = 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)
|
||||
|
||||
if this.from > this.to {
|
||||
log.Error("Invalid port range %d -> %d", this.from, this.to)
|
||||
return InvalidPortRange
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return InvalidPortRange
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/v2ray/v2ray-core/testing/unit"
|
||||
)
|
||||
|
||||
func TestIntPort(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
var portRange PortRange
|
||||
err := json.Unmarshal([]byte("1234"), &portRange)
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
assert.Uint16(portRange.from).Equals(uint16(1234))
|
||||
assert.Uint16(portRange.to).Equals(uint16(1234))
|
||||
}
|
||||
|
||||
func TestOverRangeIntPort(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
var portRange PortRange
|
||||
err := json.Unmarshal([]byte("70000"), &portRange)
|
||||
assert.Error(err).Equals(InvalidPortRange)
|
||||
|
||||
err = json.Unmarshal([]byte("-1"), &portRange)
|
||||
assert.Error(err).Equals(InvalidPortRange)
|
||||
}
|
||||
|
||||
func TestSingleStringPort(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
var portRange PortRange
|
||||
err := json.Unmarshal([]byte("\"1234\""), &portRange)
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
assert.Uint16(portRange.from).Equals(uint16(1234))
|
||||
assert.Uint16(portRange.to).Equals(uint16(1234))
|
||||
}
|
||||
|
||||
func TestStringPairPort(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
var portRange PortRange
|
||||
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))
|
||||
}
|
||||
|
||||
func TestOverRangeStringPort(t *testing.T) {
|
||||
assert := unit.Assert(t)
|
||||
|
||||
var portRange PortRange
|
||||
err := json.Unmarshal([]byte("\"-1\""), &portRange)
|
||||
assert.Error(err).Equals(InvalidPortRange)
|
||||
|
||||
err = json.Unmarshal([]byte("\"70000-80000\""), &portRange)
|
||||
assert.Error(err).Equals(InvalidPortRange)
|
||||
|
||||
err = json.Unmarshal([]byte("\"1-90000\""), &portRange)
|
||||
assert.Error(err).Equals(InvalidPortRange)
|
||||
|
||||
err = json.Unmarshal([]byte("\"700-600\""), &portRange)
|
||||
assert.Error(err).Equals(InvalidPortRange)
|
||||
}
|
Loading…
Reference in New Issue