address.go 3.7 KB

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