address.go 3.6 KB

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