network.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. )
  12. // Network represents a communication network on internet.
  13. type Network string
  14. func (this Network) AsList() *NetworkList {
  15. list := NetworkList([]Network{this})
  16. return &list
  17. }
  18. // NetworkList is a list of Networks.
  19. type NetworkList []Network
  20. // NewNetworkList construsts a NetWorklist from the given StringListeralList.
  21. func NewNetworkList(networks collect.StringList) NetworkList {
  22. list := NetworkList(make([]Network, networks.Len()))
  23. for idx, network := range networks {
  24. list[idx] = Network(strings.ToLower(strings.TrimSpace(network)))
  25. }
  26. return list
  27. }
  28. // HashNetwork returns true if the given network is in this NetworkList.
  29. func (this *NetworkList) HasNetwork(network Network) bool {
  30. for _, value := range *this {
  31. if string(value) == string(network) {
  32. return true
  33. }
  34. }
  35. return false
  36. }