address.go 4.3 KB

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