1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-10 14:26:26 -05:00
v2fly/common/net/network.go
2022-08-19 20:41:18 +08:00

71 lines
1.4 KiB
Go

package net
import (
"encoding/json"
"strings"
"github.com/golang/protobuf/jsonpb"
)
func (n Network) SystemString() string {
switch n {
case Network_TCP:
return "tcp"
case Network_UDP:
return "udp"
case Network_UNIX:
return "unix"
default:
return "unknown"
}
}
func (nl *NetworkList) UnmarshalJSONPB(unmarshaler *jsonpb.Unmarshaler, bytes []byte) error {
var networkStrList []string
if err := json.Unmarshal(bytes, &networkStrList); err == nil {
nl.Network = ParseNetworkStringList(networkStrList)
return nil
}
var networkStr string
if err := json.Unmarshal(bytes, &networkStr); err == nil {
strList := strings.Split(networkStr, ",")
nl.Network = ParseNetworkStringList(strList)
return nil
}
return newError("unknown format of a string list: " + string(bytes))
}
// HasNetwork returns true if the network list has a certain network.
func HasNetwork(list []Network, network Network) bool {
for _, value := range list {
if value == network {
return true
}
}
return false
}
func ParseNetwork(net string) Network {
switch strings.ToLower(net) {
case "tcp":
return Network_TCP
case "udp":
return Network_UDP
case "unix":
return Network_UNIX
default:
return Network_Unknown
}
}
func ParseNetworkStringList(strList []string) []Network {
list := make([]Network, len(strList))
for idx, str := range strList {
list[idx] = ParseNetwork(str)
}
return list
}