v2ray-core/tools/conf/common.go

179 lines
3.6 KiB
Go
Raw Normal View History

2016-10-16 12:22:21 +00:00
package conf
import (
"encoding/json"
"strings"
2017-01-13 20:58:18 +00:00
2016-10-16 12:22:21 +00:00
v2net "v2ray.com/core/common/net"
2016-10-17 12:35:13 +00:00
"v2ray.com/core/common/protocol"
2016-10-16 12:22:21 +00:00
)
2016-10-17 12:35:13 +00:00
type StringList []string
func NewStringList(raw []string) *StringList {
list := StringList(raw)
return &list
}
2016-11-27 20:39:09 +00:00
func (v StringList) Len() int {
return len(v)
2016-10-17 12:35:13 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *StringList) UnmarshalJSON(data []byte) error {
2016-10-17 12:35:13 +00:00
var strarray []string
if err := json.Unmarshal(data, &strarray); err == nil {
2016-11-27 20:39:09 +00:00
*v = *NewStringList(strarray)
2016-10-17 12:35:13 +00:00
return nil
}
var rawstr string
if err := json.Unmarshal(data, &rawstr); err == nil {
strlist := strings.Split(rawstr, ",")
2016-11-27 20:39:09 +00:00
*v = *NewStringList(strlist)
2016-10-17 12:35:13 +00:00
return nil
}
2017-04-09 13:04:04 +00:00
return newError("unknown format of a string list: " + string(data))
2016-10-17 12:35:13 +00:00
}
2016-10-16 12:22:21 +00:00
type Address struct {
v2net.Address
}
2016-11-27 20:39:09 +00:00
func (v *Address) UnmarshalJSON(data []byte) error {
2016-10-16 12:22:21 +00:00
var rawStr string
if err := json.Unmarshal(data, &rawStr); err != nil {
return err
}
2016-11-27 20:39:09 +00:00
v.Address = v2net.ParseAddress(rawStr)
2016-10-16 12:22:21 +00:00
return nil
}
2016-11-27 20:39:09 +00:00
func (v *Address) Build() *v2net.IPOrDomain {
2016-12-14 22:04:02 +00:00
return v2net.NewIPOrDomain(v.Address)
2016-10-16 12:22:21 +00:00
}
type Network string
2016-11-27 20:39:09 +00:00
func (v Network) Build() v2net.Network {
return v2net.ParseNetwork(string(v))
2016-10-16 12:22:21 +00:00
}
type NetworkList []Network
2016-11-27 20:39:09 +00:00
func (v *NetworkList) UnmarshalJSON(data []byte) error {
2016-10-16 12:22:21 +00:00
var strarray []Network
if err := json.Unmarshal(data, &strarray); err == nil {
nl := NetworkList(strarray)
2016-11-27 20:39:09 +00:00
*v = nl
2016-10-16 12:22:21 +00:00
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)
}
2016-11-27 20:39:09 +00:00
*v = nl
2016-10-16 12:22:21 +00:00
return nil
}
2017-04-09 13:04:04 +00:00
return newError("unknown format of a string list: " + string(data))
2016-10-16 12:22:21 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *NetworkList) Build() *v2net.NetworkList {
2017-01-13 20:58:18 +00:00
if v == nil {
return &v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP},
}
}
2016-10-16 12:22:21 +00:00
list := new(v2net.NetworkList)
2016-11-27 20:39:09 +00:00
for _, network := range *v {
2016-10-16 12:22:21 +00:00
list.Network = append(list.Network, network.Build())
}
return list
}
2016-10-17 12:35:13 +00:00
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 {
2017-04-08 23:43:25 +00:00
return v2net.Port(0), v2net.Port(0), newError("Config: Invalid port range: ", s)
2016-10-17 12:35:13 +00:00
}
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
}
2016-11-27 20:39:09 +00:00
func (v *PortRange) Build() *v2net.PortRange {
2016-10-17 12:35:13 +00:00
return &v2net.PortRange{
2016-11-27 20:39:09 +00:00
From: v.From,
To: v.To,
2016-10-17 12:35:13 +00:00
}
}
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
2016-11-27 20:39:09 +00:00
func (v *PortRange) UnmarshalJSON(data []byte) error {
2016-10-17 12:35:13 +00:00
port, err := parseIntPort(data)
if err == nil {
2016-11-27 20:39:09 +00:00
v.From = uint32(port)
v.To = uint32(port)
2016-10-17 12:35:13 +00:00
return nil
}
from, to, err := parseStringPort(data)
if err == nil {
2016-11-27 20:39:09 +00:00
v.From = uint32(from)
v.To = uint32(to)
if v.From > v.To {
2017-04-09 13:04:04 +00:00
return newError("invalid port range ", v.From, " -> ", v.To)
2016-10-17 12:35:13 +00:00
}
return nil
}
2017-04-09 13:04:04 +00:00
return newError("invalid port range: ", string(data))
2016-10-17 12:35:13 +00:00
}
type User struct {
EmailString string `json:"email"`
LevelByte byte `json:"level"`
}
2016-11-27 20:39:09 +00:00
func (v *User) Build() *protocol.User {
2016-10-17 12:35:13 +00:00
return &protocol.User{
2016-11-27 20:39:09 +00:00
Email: v.EmailString,
Level: uint32(v.LevelByte),
2016-10-17 12:35:13 +00:00
}
}