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-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/common/collect"
|
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
|
|
|
|
case "kcp":
|
|
|
|
return Network_KCP
|
|
|
|
case "ws":
|
|
|
|
return Network_WebSocket
|
|
|
|
default:
|
|
|
|
return Network_Unknown
|
|
|
|
}
|
|
|
|
}
|
2015-10-30 14:56:46 +00:00
|
|
|
|
2016-01-17 15:20:49 +00:00
|
|
|
func (this Network) AsList() *NetworkList {
|
2016-09-22 10:14:50 +00:00
|
|
|
return &NetworkList{
|
|
|
|
Network: []Network{this},
|
|
|
|
}
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 08:44:44 +00:00
|
|
|
func (this Network) SystemString() string {
|
|
|
|
switch this {
|
|
|
|
case Network_TCP, Network_RawTCP:
|
|
|
|
return "tcp"
|
|
|
|
case Network_UDP, Network_KCP:
|
|
|
|
return "udp"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
2016-06-03 22:38:22 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 09:53:05 +00:00
|
|
|
func (this Network) UrlPrefix() string {
|
|
|
|
switch this {
|
|
|
|
case Network_TCP, Network_RawTCP:
|
|
|
|
return "tcp"
|
|
|
|
case Network_UDP:
|
|
|
|
return "udp"
|
|
|
|
case Network_KCP:
|
|
|
|
return "kcp"
|
|
|
|
case Network_WebSocket:
|
|
|
|
return "ws"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 10:09:53 +00:00
|
|
|
// NewNetworkList construsts a NetWorklist from the given StringListeralList.
|
2016-09-22 10:14:50 +00:00
|
|
|
func NewNetworkList(networks collect.StringList) *NetworkList {
|
|
|
|
list := &NetworkList{
|
|
|
|
Network: make([]Network, networks.Len()),
|
|
|
|
}
|
2016-01-15 13:34:33 +00:00
|
|
|
for idx, network := range networks {
|
2016-09-22 10:14:50 +00:00
|
|
|
list.Network[idx] = ParseNetwork(network)
|
2016-01-15 13:34:33 +00:00
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2016-02-06 10:09:53 +00:00
|
|
|
// HashNetwork returns true if the given network is in this NetworkList.
|
2016-01-15 13:34:33 +00:00
|
|
|
func (this *NetworkList) HasNetwork(network Network) bool {
|
2016-09-22 10:14:50 +00:00
|
|
|
for _, value := range this.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
|
|
|
}
|