github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/infra/conf/common.go (about)

     1  package conf
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"strings"
     7  
     8  	"v2ray.com/core/common/net"
     9  	"v2ray.com/core/common/protocol"
    10  )
    11  
    12  type StringList []string
    13  
    14  func NewStringList(raw []string) *StringList {
    15  	list := StringList(raw)
    16  	return &list
    17  }
    18  
    19  func (v StringList) Len() int {
    20  	return len(v)
    21  }
    22  
    23  func (v *StringList) UnmarshalJSON(data []byte) error {
    24  	var strarray []string
    25  	if err := json.Unmarshal(data, &strarray); err == nil {
    26  		*v = *NewStringList(strarray)
    27  		return nil
    28  	}
    29  
    30  	var rawstr string
    31  	if err := json.Unmarshal(data, &rawstr); err == nil {
    32  		strlist := strings.Split(rawstr, ",")
    33  		*v = *NewStringList(strlist)
    34  		return nil
    35  	}
    36  	return newError("unknown format of a string list: " + string(data))
    37  }
    38  
    39  type Address struct {
    40  	net.Address
    41  }
    42  
    43  func (v *Address) UnmarshalJSON(data []byte) error {
    44  	var rawStr string
    45  	if err := json.Unmarshal(data, &rawStr); err != nil {
    46  		return newError("invalid address: ", string(data)).Base(err)
    47  	}
    48  	v.Address = net.ParseAddress(rawStr)
    49  
    50  	return nil
    51  }
    52  
    53  func (v *Address) Build() *net.IPOrDomain {
    54  	return net.NewIPOrDomain(v.Address)
    55  }
    56  
    57  type Network string
    58  
    59  func (v Network) Build() net.Network {
    60  	switch strings.ToLower(string(v)) {
    61  	case "tcp":
    62  		return net.Network_TCP
    63  	case "udp":
    64  		return net.Network_UDP
    65  	default:
    66  		return net.Network_Unknown
    67  	}
    68  }
    69  
    70  type NetworkList []Network
    71  
    72  func (v *NetworkList) UnmarshalJSON(data []byte) error {
    73  	var strarray []Network
    74  	if err := json.Unmarshal(data, &strarray); err == nil {
    75  		nl := NetworkList(strarray)
    76  		*v = nl
    77  		return nil
    78  	}
    79  
    80  	var rawstr Network
    81  	if err := json.Unmarshal(data, &rawstr); err == nil {
    82  		strlist := strings.Split(string(rawstr), ",")
    83  		nl := make([]Network, len(strlist))
    84  		for idx, network := range strlist {
    85  			nl[idx] = Network(network)
    86  		}
    87  		*v = nl
    88  		return nil
    89  	}
    90  	return newError("unknown format of a string list: " + string(data))
    91  }
    92  
    93  func (v *NetworkList) Build() []net.Network {
    94  	if v == nil {
    95  		return []net.Network{net.Network_TCP}
    96  	}
    97  
    98  	list := make([]net.Network, 0, len(*v))
    99  	for _, network := range *v {
   100  		list = append(list, network.Build())
   101  	}
   102  	return list
   103  }
   104  
   105  func parseIntPort(data []byte) (net.Port, error) {
   106  	var intPort uint32
   107  	err := json.Unmarshal(data, &intPort)
   108  	if err != nil {
   109  		return net.Port(0), err
   110  	}
   111  	return net.PortFromInt(intPort)
   112  }
   113  
   114  func parseStringPort(s string) (net.Port, net.Port, error) {
   115  	if strings.HasPrefix(s, "env:") {
   116  		s = s[4:]
   117  		s = os.Getenv(s)
   118  	}
   119  
   120  	pair := strings.SplitN(s, "-", 2)
   121  	if len(pair) == 0 {
   122  		return net.Port(0), net.Port(0), newError("invalid port range: ", s)
   123  	}
   124  	if len(pair) == 1 {
   125  		port, err := net.PortFromString(pair[0])
   126  		return port, port, err
   127  	}
   128  
   129  	fromPort, err := net.PortFromString(pair[0])
   130  	if err != nil {
   131  		return net.Port(0), net.Port(0), err
   132  	}
   133  	toPort, err := net.PortFromString(pair[1])
   134  	if err != nil {
   135  		return net.Port(0), net.Port(0), err
   136  	}
   137  	return fromPort, toPort, nil
   138  }
   139  
   140  func parseJSONStringPort(data []byte) (net.Port, net.Port, error) {
   141  	var s string
   142  	err := json.Unmarshal(data, &s)
   143  	if err != nil {
   144  		return net.Port(0), net.Port(0), err
   145  	}
   146  	return parseStringPort(s)
   147  }
   148  
   149  type PortRange struct {
   150  	From uint32
   151  	To   uint32
   152  }
   153  
   154  func (v *PortRange) Build() *net.PortRange {
   155  	return &net.PortRange{
   156  		From: v.From,
   157  		To:   v.To,
   158  	}
   159  }
   160  
   161  // UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
   162  func (v *PortRange) UnmarshalJSON(data []byte) error {
   163  	port, err := parseIntPort(data)
   164  	if err == nil {
   165  		v.From = uint32(port)
   166  		v.To = uint32(port)
   167  		return nil
   168  	}
   169  
   170  	from, to, err := parseJSONStringPort(data)
   171  	if err == nil {
   172  		v.From = uint32(from)
   173  		v.To = uint32(to)
   174  		if v.From > v.To {
   175  			return newError("invalid port range ", v.From, " -> ", v.To)
   176  		}
   177  		return nil
   178  	}
   179  
   180  	return newError("invalid port range: ", string(data))
   181  }
   182  
   183  type PortList struct {
   184  	Range []PortRange
   185  }
   186  
   187  func (list *PortList) Build() *net.PortList {
   188  	portList := new(net.PortList)
   189  	for _, r := range list.Range {
   190  		portList.Range = append(portList.Range, r.Build())
   191  	}
   192  	return portList
   193  }
   194  
   195  // UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
   196  func (list *PortList) UnmarshalJSON(data []byte) error {
   197  	var listStr string
   198  	var number uint32
   199  	if err := json.Unmarshal(data, &listStr); err != nil {
   200  		if err2 := json.Unmarshal(data, &number); err2 != nil {
   201  			return newError("invalid port: ", string(data)).Base(err2)
   202  		}
   203  	}
   204  	rangelist := strings.Split(listStr, ",")
   205  	for _, rangeStr := range rangelist {
   206  		trimmed := strings.TrimSpace(rangeStr)
   207  		if len(trimmed) > 0 {
   208  			if strings.Contains(trimmed, "-") {
   209  				from, to, err := parseStringPort(trimmed)
   210  				if err != nil {
   211  					return newError("invalid port range: ", trimmed).Base(err)
   212  				}
   213  				list.Range = append(list.Range, PortRange{From: uint32(from), To: uint32(to)})
   214  			} else {
   215  				port, err := parseIntPort([]byte(trimmed))
   216  				if err != nil {
   217  					return newError("invalid port: ", trimmed).Base(err)
   218  				}
   219  				list.Range = append(list.Range, PortRange{From: uint32(port), To: uint32(port)})
   220  			}
   221  		}
   222  	}
   223  	if number != 0 {
   224  		list.Range = append(list.Range, PortRange{From: uint32(number), To: uint32(number)})
   225  	}
   226  	return nil
   227  }
   228  
   229  type User struct {
   230  	EmailString string `json:"email"`
   231  	LevelByte   byte   `json:"level"`
   232  }
   233  
   234  func (v *User) Build() *protocol.User {
   235  	return &protocol.User{
   236  		Email: v.EmailString,
   237  		Level: uint32(v.LevelByte),
   238  	}
   239  }