address.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package net
  2. import (
  3. "net"
  4. "github.com/v2ray/v2ray-core/common/log"
  5. "github.com/v2ray/v2ray-core/common/predicate"
  6. )
  7. var (
  8. LocalHostIP = IPAddress([]byte{127, 0, 0, 1})
  9. AnyIP = IPAddress([]byte{0, 0, 0, 0})
  10. )
  11. type AddressFamily int
  12. const (
  13. AddressFamilyIPv4 = AddressFamily(0)
  14. AddressFamilyIPv6 = AddressFamily(1)
  15. AddressFamilyDomain = AddressFamily(2)
  16. )
  17. func (this AddressFamily) Either(fs ...AddressFamily) bool {
  18. for _, f := range fs {
  19. if this == f {
  20. return true
  21. }
  22. }
  23. return false
  24. }
  25. func (this AddressFamily) IsIPv4() bool {
  26. return this == AddressFamilyIPv4
  27. }
  28. func (this AddressFamily) IsIPv6() bool {
  29. return this == AddressFamilyIPv6
  30. }
  31. func (this AddressFamily) IsDomain() bool {
  32. return this == AddressFamilyDomain
  33. }
  34. // Address represents a network address to be communicated with. It may be an IP address or domain
  35. // address, not both. This interface doesn't resolve IP address for a given domain.
  36. type Address interface {
  37. IP() net.IP // IP of this Address
  38. Domain() string // Domain of this Address
  39. Family() AddressFamily
  40. String() string // String representation of this Address
  41. Equals(Address) bool
  42. }
  43. // ParseAddress parses a string into an Address. The return value will be an IPAddress when
  44. // the string is in the form of IPv4 or IPv6 address, or a DomainAddress otherwise.
  45. func ParseAddress(addr string) Address {
  46. ip := net.ParseIP(addr)
  47. if ip != nil {
  48. return IPAddress(ip)
  49. }
  50. return DomainAddress(addr)
  51. }
  52. // IPAddress creates an Address with given IP.
  53. func IPAddress(ip []byte) Address {
  54. switch len(ip) {
  55. case net.IPv4len:
  56. var addr ipv4Address = [4]byte{ip[0], ip[1], ip[2], ip[3]}
  57. return &addr
  58. case net.IPv6len:
  59. if predicate.BytesAll(ip[0:10], 0) && predicate.BytesAll(ip[10:12], 0xff) {
  60. return IPAddress(ip[12:16])
  61. }
  62. var addr ipv6Address = [16]byte{
  63. ip[0], ip[1], ip[2], ip[3],
  64. ip[4], ip[5], ip[6], ip[7],
  65. ip[8], ip[9], ip[10], ip[11],
  66. ip[12], ip[13], ip[14], ip[15],
  67. }
  68. return &addr
  69. default:
  70. log.Error("Invalid IP format: ", ip)
  71. return nil
  72. }
  73. }
  74. // DomainAddress creates an Address with given domain.
  75. func DomainAddress(domain string) Address {
  76. var addr domainAddress = domainAddress(domain)
  77. return &addr
  78. }
  79. type ipv4Address [4]byte
  80. func (addr *ipv4Address) IP() net.IP {
  81. return net.IP(addr[:])
  82. }
  83. func (addr *ipv4Address) Domain() string {
  84. panic("Calling Domain() on an IPv4Address.")
  85. }
  86. func (addr *ipv4Address) Family() AddressFamily {
  87. return AddressFamilyIPv4
  88. }
  89. func (this *ipv4Address) String() string {
  90. return this.IP().String()
  91. }
  92. func (this *ipv4Address) Equals(another Address) bool {
  93. anotherIPv4, ok := another.(*ipv4Address)
  94. if !ok {
  95. return false
  96. }
  97. return this[0] == anotherIPv4[0] &&
  98. this[1] == anotherIPv4[1] &&
  99. this[2] == anotherIPv4[2] &&
  100. this[3] == anotherIPv4[3]
  101. }
  102. type ipv6Address [16]byte
  103. func (addr *ipv6Address) IP() net.IP {
  104. return net.IP(addr[:])
  105. }
  106. func (addr *ipv6Address) Domain() string {
  107. panic("Calling Domain() on an IPv6Address.")
  108. }
  109. func (this *ipv6Address) Family() AddressFamily {
  110. return AddressFamilyIPv6
  111. }
  112. func (this *ipv6Address) String() string {
  113. return "[" + this.IP().String() + "]"
  114. }
  115. func (this *ipv6Address) Equals(another Address) bool {
  116. anotherIPv6, ok := another.(*ipv6Address)
  117. if !ok {
  118. return false
  119. }
  120. return this[0] == anotherIPv6[0] &&
  121. this[1] == anotherIPv6[1] &&
  122. this[2] == anotherIPv6[2] &&
  123. this[3] == anotherIPv6[3] &&
  124. this[4] == anotherIPv6[4] &&
  125. this[5] == anotherIPv6[5] &&
  126. this[6] == anotherIPv6[6] &&
  127. this[7] == anotherIPv6[7] &&
  128. this[8] == anotherIPv6[8] &&
  129. this[9] == anotherIPv6[9] &&
  130. this[10] == anotherIPv6[10] &&
  131. this[11] == anotherIPv6[11] &&
  132. this[12] == anotherIPv6[12] &&
  133. this[13] == anotherIPv6[13] &&
  134. this[14] == anotherIPv6[14] &&
  135. this[15] == anotherIPv6[15]
  136. }
  137. type domainAddress string
  138. func (addr *domainAddress) IP() net.IP {
  139. panic("Calling IP() on a DomainAddress.")
  140. }
  141. func (addr *domainAddress) Domain() string {
  142. return string(*addr)
  143. }
  144. func (addr *domainAddress) Family() AddressFamily {
  145. return AddressFamilyDomain
  146. }
  147. func (this *domainAddress) String() string {
  148. return this.Domain()
  149. }
  150. func (this *domainAddress) Equals(another Address) bool {
  151. anotherDomain, ok := another.(*domainAddress)
  152. if !ok {
  153. return false
  154. }
  155. return this.Domain() == anotherDomain.Domain()
  156. }