address.go 4.1 KB

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