address.go 3.5 KB

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