network.go 1.2 KB

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