network.go 1.0 KB

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