1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-06 10:20:44 -05:00
v2fly/tools/conf/common.go

179 lines
3.7 KiB
Go
Raw Normal View History

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