github.com/imannamdari/v2ray-core/v5@v5.0.5/common/net/network.go (about) 1 package net 2 3 import ( 4 "encoding/json" 5 "strings" 6 7 "github.com/golang/protobuf/jsonpb" 8 ) 9 10 func (n Network) SystemString() string { 11 switch n { 12 case Network_TCP: 13 return "tcp" 14 case Network_UDP: 15 return "udp" 16 case Network_UNIX: 17 return "unix" 18 default: 19 return "unknown" 20 } 21 } 22 23 func (nl *NetworkList) UnmarshalJSONPB(unmarshaler *jsonpb.Unmarshaler, bytes []byte) error { 24 var networkStrList []string 25 if err := json.Unmarshal(bytes, &networkStrList); err == nil { 26 nl.Network = ParseNetworkStringList(networkStrList) 27 return nil 28 } 29 30 var networkStr string 31 if err := json.Unmarshal(bytes, &networkStr); err == nil { 32 strList := strings.Split(networkStr, ",") 33 nl.Network = ParseNetworkStringList(strList) 34 return nil 35 } 36 37 return newError("unknown format of a string list: " + string(bytes)) 38 } 39 40 // HasNetwork returns true if the network list has a certain network. 41 func HasNetwork(list []Network, network Network) bool { 42 for _, value := range list { 43 if value == network { 44 return true 45 } 46 } 47 return false 48 } 49 50 func ParseNetwork(net string) Network { 51 switch strings.ToLower(net) { 52 case "tcp": 53 return Network_TCP 54 case "udp": 55 return Network_UDP 56 case "unix": 57 return Network_UNIX 58 default: 59 return Network_Unknown 60 } 61 } 62 63 func ParseNetworkStringList(strList []string) []Network { 64 list := make([]Network, len(strList)) 65 for idx, str := range strList { 66 list[idx] = ParseNetwork(str) 67 } 68 69 return list 70 }