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/tools/conf/common.go

180 lines
3.7 KiB

package conf
import (
"encoding/json"
"strings"
"v2ray.com/core/common/errors"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
)
type StringList []string
func NewStringList(raw []string) *StringList {
list := StringList(raw)
return &list
}
8 years ago
func (v StringList) Len() int {
return len(v)
}
8 years ago
func (v *StringList) UnmarshalJSON(data []byte) error {
var strarray []string
if err := json.Unmarshal(data, &strarray); err == nil {
8 years ago
*v = *NewStringList(strarray)
return nil
}
var rawstr string
if err := json.Unmarshal(data, &rawstr); err == nil {
strlist := strings.Split(rawstr, ",")
8 years ago
*v = *NewStringList(strlist)
return nil
}
return errors.New("Config: Unknown format of a string list: " + string(data))
}
type Address struct {
v2net.Address
}
8 years ago
func (v *Address) UnmarshalJSON(data []byte) error {
var rawStr string
if err := json.Unmarshal(data, &rawStr); err != nil {
return err
}
8 years ago
v.Address = v2net.ParseAddress(rawStr)
return nil
}
8 years ago
func (v *Address) Build() *v2net.IPOrDomain {
return v2net.NewIPOrDomain(v.Address)
}
type Network string
8 years ago
func (v Network) Build() v2net.Network {
return v2net.ParseNetwork(string(v))
}
type NetworkList []Network
8 years ago
func (v *NetworkList) UnmarshalJSON(data []byte) error {
var strarray []Network
if err := json.Unmarshal(data, &strarray); err == nil {
nl := NetworkList(strarray)
8 years ago
*v = nl
return nil
}
var rawstr Network
if err := json.Unmarshal(data, &rawstr); err == nil {
strlist := strings.Split(string(rawstr), ",")
nl := make([]Network, len(strlist))
for idx, network := range strlist {
nl[idx] = Network(network)
}
8 years ago
*v = nl
return nil
}
return errors.New("Config: Unknown format of a string list: " + string(data))
}
8 years ago
func (v *NetworkList) Build() *v2net.NetworkList {
if v == nil {
return &v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP},
}
}
list := new(v2net.NetworkList)
8 years ago
for _, network := range *v {
list.Network = append(list.Network, network.Build())
}
return list
}
func parseIntPort(data []byte) (v2net.Port, error) {
var intPort uint32
err := json.Unmarshal(data, &intPort)
if err != nil {
return v2net.Port(0), err
}
return v2net.PortFromInt(intPort)
}
func parseStringPort(data []byte) (v2net.Port, v2net.Port, error) {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return v2net.Port(0), v2net.Port(0), err
}
pair := strings.SplitN(s, "-", 2)
if len(pair) == 0 {
return v2net.Port(0), v2net.Port(0), errors.New("Config: Invalid port range: ", s)
}
if len(pair) == 1 {
port, err := v2net.PortFromString(pair[0])
return port, port, err
}
fromPort, err := v2net.PortFromString(pair[0])
if err != nil {
return v2net.Port(0), v2net.Port(0), err
}
toPort, err := v2net.PortFromString(pair[1])
if err != nil {
return v2net.Port(0), v2net.Port(0), err
}
return fromPort, toPort, nil
}
type PortRange struct {
From uint32
To uint32
}
8 years ago
func (v *PortRange) Build() *v2net.PortRange {
return &v2net.PortRange{
8 years ago
From: v.From,
To: v.To,
}
}
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
8 years ago
func (v *PortRange) UnmarshalJSON(data []byte) error {
port, err := parseIntPort(data)
if err == nil {
8 years ago
v.From = uint32(port)
v.To = uint32(port)
return nil
}
from, to, err := parseStringPort(data)
if err == nil {
8 years ago
v.From = uint32(from)
v.To = uint32(to)
if v.From > v.To {
return errors.New("Config: Invalid port range ", v.From, " -> ", v.To)
}
return nil
}
return errors.New("Config: Invalid port range: ", string(data))
}
type User struct {
EmailString string `json:"email"`
LevelByte byte `json:"level"`
}
8 years ago
func (v *User) Build() *protocol.User {
return &protocol.User{
8 years ago
Email: v.EmailString,
Level: uint32(v.LevelByte),
}
}