network.go 1.1 KB

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