network.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package net
  2. import (
  3. "strings"
  4. "v2ray.com/core/common/collect"
  5. )
  6. func ParseNetwork(nwStr string) Network {
  7. if network, found := Network_value[nwStr]; found {
  8. return Network(network)
  9. }
  10. switch strings.ToLower(nwStr) {
  11. case "tcp":
  12. return Network_TCP
  13. case "udp":
  14. return Network_UDP
  15. case "kcp":
  16. return Network_KCP
  17. case "ws":
  18. return Network_WebSocket
  19. default:
  20. return Network_Unknown
  21. }
  22. }
  23. func (this Network) AsList() *NetworkList {
  24. list := NetworkList([]Network{this})
  25. return &list
  26. }
  27. func (this Network) SystemString() string {
  28. switch this {
  29. case Network_TCP, Network_RawTCP:
  30. return "tcp"
  31. case Network_UDP, Network_KCP:
  32. return "udp"
  33. default:
  34. return "unknown"
  35. }
  36. }
  37. func (this Network) UrlPrefix() string {
  38. switch this {
  39. case Network_TCP, Network_RawTCP:
  40. return "tcp"
  41. case Network_UDP:
  42. return "udp"
  43. case Network_KCP:
  44. return "kcp"
  45. case Network_WebSocket:
  46. return "ws"
  47. default:
  48. return "unknown"
  49. }
  50. }
  51. // NetworkList is a list of Networks.
  52. type NetworkList []Network
  53. // NewNetworkList construsts a NetWorklist from the given StringListeralList.
  54. func NewNetworkList(networks collect.StringList) NetworkList {
  55. list := NetworkList(make([]Network, networks.Len()))
  56. for idx, network := range networks {
  57. list[idx] = ParseNetwork(network)
  58. }
  59. return list
  60. }
  61. // HashNetwork returns true if the given network is in this NetworkList.
  62. func (this *NetworkList) HasNetwork(network Network) bool {
  63. for _, value := range *this {
  64. if string(value) == string(network) {
  65. return true
  66. }
  67. }
  68. return false
  69. }