v2ray-core/common/net/network.go

67 lines
1.1 KiB
Go
Raw Normal View History

2015-10-30 14:56:46 +00:00
package net
2016-01-15 13:34:33 +00:00
import (
2016-05-24 20:41:51 +00:00
"strings"
2016-01-15 13:34:33 +00:00
)
2016-09-20 08:44:44 +00:00
func ParseNetwork(nwStr string) Network {
if network, found := Network_value[nwStr]; found {
return Network(network)
}
switch strings.ToLower(nwStr) {
case "tcp":
return Network_TCP
case "udp":
return Network_UDP
default:
return Network_Unknown
}
}
2015-10-30 14:56:46 +00:00
2016-11-27 20:39:09 +00:00
func (v Network) AsList() *NetworkList {
2016-09-22 10:14:50 +00:00
return &NetworkList{
2016-11-27 20:39:09 +00:00
Network: []Network{v},
2016-09-22 10:14:50 +00:00
}
2016-01-17 15:20:49 +00:00
}
2016-11-27 20:39:09 +00:00
func (v Network) SystemString() string {
switch v {
2017-01-03 13:53:59 +00:00
case Network_TCP:
2016-09-20 08:44:44 +00:00
return "tcp"
case Network_UDP:
2016-09-20 08:44:44 +00:00
return "udp"
default:
return "unknown"
}
2016-06-03 22:38:22 +00:00
}
2016-12-21 14:37:16 +00:00
func (v Network) URLPrefix() string {
2016-11-27 20:39:09 +00:00
switch v {
2017-01-03 13:53:59 +00:00
case Network_TCP:
2016-09-20 09:53:05 +00:00
return "tcp"
case Network_UDP:
return "udp"
default:
return "unknown"
}
}
2017-02-20 10:25:05 +00:00
// HasNetwork returns true if the given network is in v NetworkList.
2016-11-27 20:39:09 +00:00
func (v NetworkList) HasNetwork(network Network) bool {
for _, value := range v.Network {
2016-01-15 13:34:33 +00:00
if string(value) == string(network) {
return true
}
}
return false
2015-10-30 14:56:46 +00:00
}
2016-10-02 21:43:58 +00:00
2016-11-27 20:39:09 +00:00
func (v NetworkList) Get(idx int) Network {
return v.Network[idx]
2016-10-02 21:43:58 +00:00
}
2017-02-20 10:25:05 +00:00
// Size returns the number of networks in this network list.
func (v NetworkList) Size() int {
return len(v.Network)
}