mirror of https://github.com/v2ray/v2ray-core
simplify port range parsing
parent
0d8df60699
commit
e467042fbc
|
@ -1,15 +1,38 @@
|
||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/serial"
|
"github.com/v2ray/v2ray-core/common/serial"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrorInvalidPortRage indicates an error during port range parsing.
|
||||||
|
ErrorInvalidPortRange = errors.New("Invalid port range.")
|
||||||
|
)
|
||||||
|
|
||||||
type Port serial.Uint16Literal
|
type Port serial.Uint16Literal
|
||||||
|
|
||||||
func PortFromBytes(port []byte) Port {
|
func PortFromBytes(port []byte) Port {
|
||||||
return Port(serial.BytesLiteral(port).Uint16Value())
|
return Port(serial.BytesLiteral(port).Uint16Value())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PortFromInt(v int) (Port, error) {
|
||||||
|
if v <= 0 || v > 65535 {
|
||||||
|
return Port(0), ErrorInvalidPortRange
|
||||||
|
}
|
||||||
|
return Port(v), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func PortFromString(s string) (Port, error) {
|
||||||
|
v, err := strconv.Atoi(s)
|
||||||
|
if err != nil {
|
||||||
|
return Port(0), ErrorInvalidPortRange
|
||||||
|
}
|
||||||
|
return PortFromInt(v)
|
||||||
|
}
|
||||||
|
|
||||||
func (this Port) Value() uint16 {
|
func (this Port) Value() uint16 {
|
||||||
return uint16(this)
|
return uint16(this)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,66 +4,66 @@ package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
"github.com/v2ray/v2ray-core/common/log"
|
||||||
"github.com/v2ray/v2ray-core/common/serial"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
func parseIntPort(data []byte) (Port, error) {
|
||||||
ErrorInvalidPortRange = errors.New("Invalid port range.")
|
var intPort int
|
||||||
)
|
err := json.Unmarshal(data, &intPort)
|
||||||
|
if err != nil {
|
||||||
|
return Port(0), err
|
||||||
|
}
|
||||||
|
return PortFromInt(intPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseStringPort(data []byte) (Port, Port, error) {
|
||||||
|
var s string
|
||||||
|
err := json.Unmarshal(data, &s)
|
||||||
|
if err != nil {
|
||||||
|
return Port(0), Port(0), err
|
||||||
|
}
|
||||||
|
pair := strings.SplitN(s, "-", 2)
|
||||||
|
if len(pair) == 0 {
|
||||||
|
return Port(0), Port(0), ErrorInvalidPortRange
|
||||||
|
}
|
||||||
|
if len(pair) == 1 {
|
||||||
|
port, err := PortFromString(pair[0])
|
||||||
|
return port, port, err
|
||||||
|
}
|
||||||
|
|
||||||
|
fromPort, err := PortFromString(pair[0])
|
||||||
|
if err != nil {
|
||||||
|
return Port(0), Port(0), err
|
||||||
|
}
|
||||||
|
toPort, err := PortFromString(pair[1])
|
||||||
|
if err != nil {
|
||||||
|
return Port(0), Port(0), err
|
||||||
|
}
|
||||||
|
return fromPort, toPort, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
|
||||||
func (this *PortRange) UnmarshalJSON(data []byte) error {
|
func (this *PortRange) UnmarshalJSON(data []byte) error {
|
||||||
var maybeint int
|
port, err := parseIntPort(data)
|
||||||
err := json.Unmarshal(data, &maybeint)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if maybeint <= 0 || maybeint >= 65535 {
|
this.From = port
|
||||||
log.Error("Invalid port [", serial.BytesLiteral(data), "]")
|
this.To = port
|
||||||
return ErrorInvalidPortRange
|
|
||||||
}
|
|
||||||
this.From = Port(maybeint)
|
|
||||||
this.To = Port(maybeint)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var maybestring string
|
from, to, err := parseStringPort(data)
|
||||||
err = json.Unmarshal(data, &maybestring)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
pair := strings.SplitN(maybestring, "-", 2)
|
this.From = from
|
||||||
if len(pair) == 1 {
|
this.To = to
|
||||||
value, err := strconv.Atoi(pair[0])
|
if this.From > this.To {
|
||||||
if err != nil || value <= 0 || value >= 65535 {
|
log.Error("Invalid port range ", this.From, " -> ", this.To)
|
||||||
log.Error("Invalid from port ", pair[0])
|
return ErrorInvalidPortRange
|
||||||
return ErrorInvalidPortRange
|
|
||||||
}
|
|
||||||
this.From = Port(value)
|
|
||||||
this.To = Port(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 ", pair[0])
|
|
||||||
return ErrorInvalidPortRange
|
|
||||||
}
|
|
||||||
this.From = Port(from)
|
|
||||||
|
|
||||||
to, err := strconv.Atoi(pair[1])
|
|
||||||
if err != nil || to <= 0 || to >= 65535 {
|
|
||||||
log.Error("Invalid to port ", pair[1])
|
|
||||||
return ErrorInvalidPortRange
|
|
||||||
}
|
|
||||||
this.To = Port(to)
|
|
||||||
|
|
||||||
if this.From > this.To {
|
|
||||||
log.Error("Invalid port range ", this.From, " -> ", this.To)
|
|
||||||
return ErrorInvalidPortRange
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Error("Invalid port range: ", string(data))
|
||||||
return ErrorInvalidPortRange
|
return ErrorInvalidPortRange
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue