network.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package net
  2. import (
  3. "strings"
  4. "github.com/v2ray/v2ray-core/common/collect"
  5. )
  6. const (
  7. // TCPNetwork represents the TCP network.
  8. TCPNetwork = Network("tcp")
  9. // UDPNetwork represents the UDP network.
  10. UDPNetwork = Network("udp")
  11. // KCPNetwork represents the KCP network.
  12. KCPNetwork = Network("kcp")
  13. )
  14. // Network represents a communication network on internet.
  15. type Network string
  16. func (this Network) AsList() *NetworkList {
  17. list := NetworkList([]Network{this})
  18. return &list
  19. }
  20. func (this Network) String() string {
  21. return string(this)
  22. }
  23. // NetworkList is a list of Networks.
  24. type NetworkList []Network
  25. // NewNetworkList construsts a NetWorklist from the given StringListeralList.
  26. func NewNetworkList(networks collect.StringList) NetworkList {
  27. list := NetworkList(make([]Network, networks.Len()))
  28. for idx, network := range networks {
  29. list[idx] = Network(strings.ToLower(strings.TrimSpace(network)))
  30. }
  31. return list
  32. }
  33. // HashNetwork returns true if the given network is in this NetworkList.
  34. func (this *NetworkList) HasNetwork(network Network) bool {
  35. for _, value := range *this {
  36. if string(value) == string(network) {
  37. return true
  38. }
  39. }
  40. return false
  41. }