You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/common/net/port_json.go

70 lines
1.4 KiB

// +build json
package net
9 years ago
import (
"encoding/json"
"strings"
"github.com/v2ray/v2ray-core/common/log"
)
func parseIntPort(data []byte) (Port, error) {
var intPort int
err := json.Unmarshal(data, &intPort)
if err != nil {
return Port(0), err
}
return PortFromInt(intPort)
}
9 years ago
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), ErrInvalidPortRange
}
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
9 years ago
func (this *PortRange) UnmarshalJSON(data []byte) error {
port, err := parseIntPort(data)
9 years ago
if err == nil {
this.From = port
this.To = port
9 years ago
return nil
}
from, to, err := parseStringPort(data)
9 years ago
if err == nil {
this.From = from
this.To = to
if this.From > this.To {
log.Error("Invalid port range ", this.From, " -> ", this.To)
return ErrInvalidPortRange
9 years ago
}
return nil
9 years ago
}
log.Error("Invalid port range: ", string(data))
return ErrInvalidPortRange
9 years ago
}