address.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package net
  2. import (
  3. "net"
  4. "github.com/v2ray/v2ray-core/common/log"
  5. )
  6. // Address represents a network address to be communicated with. It may be an IP address or domain
  7. // address, not both. This interface doesn't resolve IP address for a given domain.
  8. type Address interface {
  9. IP() net.IP // IP of this Address
  10. Domain() string // Domain of this Address
  11. IsIPv4() bool // True if this Address is an IPv4 address
  12. IsIPv6() bool // True if this Address is an IPv6 address
  13. IsDomain() bool // True if this Address is an domain address
  14. String() string // String representation of this Address
  15. }
  16. func allZeros(data []byte) bool {
  17. for _, v := range data {
  18. if v != 0 {
  19. return false
  20. }
  21. }
  22. return true
  23. }
  24. // IPAddress creates an Address with given IP and port.
  25. func IPAddress(ip []byte) Address {
  26. switch len(ip) {
  27. case net.IPv4len:
  28. return &IPv4Address{
  29. ip: [4]byte{ip[0], ip[1], ip[2], ip[3]},
  30. }
  31. case net.IPv6len:
  32. if allZeros(ip[0:10]) && ip[10] == 0xff && ip[11] == 0xff {
  33. return IPAddress(ip[12:16])
  34. }
  35. return &IPv6Address{
  36. ip: [16]byte{
  37. ip[0], ip[1], ip[2], ip[3],
  38. ip[4], ip[5], ip[6], ip[7],
  39. ip[8], ip[9], ip[10], ip[11],
  40. ip[12], ip[13], ip[14], ip[15],
  41. },
  42. }
  43. default:
  44. log.Error("Invalid IP format: %v", ip)
  45. return nil
  46. }
  47. }
  48. // DomainAddress creates an Address with given domain and port.
  49. func DomainAddress(domain string) Address {
  50. return &DomainAddressImpl{
  51. domain: domain,
  52. }
  53. }
  54. type address struct {
  55. }
  56. type IPv4Address struct {
  57. ip [4]byte
  58. }
  59. func (addr *IPv4Address) IP() net.IP {
  60. return net.IP(addr.ip[:])
  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. type IPv6Address struct {
  78. ip [16]byte
  79. }
  80. func (addr *IPv6Address) IP() net.IP {
  81. return net.IP(addr.ip[:])
  82. }
  83. func (addr *IPv6Address) Domain() string {
  84. panic("Calling Domain() on an IPv6Address.")
  85. }
  86. func (addr *IPv6Address) IsIPv4() bool {
  87. return false
  88. }
  89. func (addr *IPv6Address) IsIPv6() bool {
  90. return true
  91. }
  92. func (addr *IPv6Address) IsDomain() bool {
  93. return false
  94. }
  95. func (this *IPv6Address) String() string {
  96. return "[" + this.IP().String() + "]"
  97. }
  98. type DomainAddressImpl struct {
  99. domain string
  100. }
  101. func (addr *DomainAddressImpl) IP() net.IP {
  102. panic("Calling IP() on a DomainAddress.")
  103. }
  104. func (addr *DomainAddressImpl) Domain() string {
  105. return addr.domain
  106. }
  107. func (addr *DomainAddressImpl) IsIPv4() bool {
  108. return false
  109. }
  110. func (addr *DomainAddressImpl) IsIPv6() bool {
  111. return false
  112. }
  113. func (addr *DomainAddressImpl) IsDomain() bool {
  114. return true
  115. }
  116. func (this *DomainAddressImpl) String() string {
  117. return this.domain
  118. }