mirror of https://github.com/v2ray/v2ray-core
doc for address
parent
dd8c7b94de
commit
749ca3deab
|
@ -21,6 +21,8 @@ type Address interface {
|
||||||
Equals(Address) bool
|
Equals(Address) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseAddress parses a string into an Address. The return value will be an IPAddress when
|
||||||
|
// the string is in the form of IPv4 or IPv6 address, or a DomainAddress otherwise.
|
||||||
func ParseAddress(addr string) Address {
|
func ParseAddress(addr string) Address {
|
||||||
ip := net.ParseIP(addr)
|
ip := net.ParseIP(addr)
|
||||||
if ip != nil {
|
if ip != nil {
|
||||||
|
@ -29,17 +31,17 @@ func ParseAddress(addr string) Address {
|
||||||
return DomainAddress(addr)
|
return DomainAddress(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IPAddress creates an Address with given IP and port.
|
// IPAddress creates an Address with given IP.
|
||||||
func IPAddress(ip []byte) Address {
|
func IPAddress(ip []byte) Address {
|
||||||
switch len(ip) {
|
switch len(ip) {
|
||||||
case net.IPv4len:
|
case net.IPv4len:
|
||||||
var addr IPv4Address = [4]byte{ip[0], ip[1], ip[2], ip[3]}
|
var addr ipv4Address = [4]byte{ip[0], ip[1], ip[2], ip[3]}
|
||||||
return &addr
|
return &addr
|
||||||
case net.IPv6len:
|
case net.IPv6len:
|
||||||
if serial.BytesLiteral(ip[0:10]).All(0) && serial.BytesLiteral(ip[10:12]).All(0xff) {
|
if serial.BytesLiteral(ip[0:10]).All(0) && serial.BytesLiteral(ip[10:12]).All(0xff) {
|
||||||
return IPAddress(ip[12:16])
|
return IPAddress(ip[12:16])
|
||||||
}
|
}
|
||||||
var addr IPv6Address = [16]byte{
|
var addr ipv6Address = [16]byte{
|
||||||
ip[0], ip[1], ip[2], ip[3],
|
ip[0], ip[1], ip[2], ip[3],
|
||||||
ip[4], ip[5], ip[6], ip[7],
|
ip[4], ip[5], ip[6], ip[7],
|
||||||
ip[8], ip[9], ip[10], ip[11],
|
ip[8], ip[9], ip[10], ip[11],
|
||||||
|
@ -52,40 +54,40 @@ func IPAddress(ip []byte) Address {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DomainAddress creates an Address with given domain and port.
|
// DomainAddress creates an Address with given domain.
|
||||||
func DomainAddress(domain string) Address {
|
func DomainAddress(domain string) Address {
|
||||||
var addr DomainAddressImpl = DomainAddressImpl(domain)
|
var addr domainAddress = domainAddress(domain)
|
||||||
return &addr
|
return &addr
|
||||||
}
|
}
|
||||||
|
|
||||||
type IPv4Address [4]byte
|
type ipv4Address [4]byte
|
||||||
|
|
||||||
func (addr *IPv4Address) IP() net.IP {
|
func (addr *ipv4Address) IP() net.IP {
|
||||||
return net.IP(addr[:])
|
return net.IP(addr[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addr *IPv4Address) Domain() string {
|
func (addr *ipv4Address) Domain() string {
|
||||||
panic("Calling Domain() on an IPv4Address.")
|
panic("Calling Domain() on an IPv4Address.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addr *IPv4Address) IsIPv4() bool {
|
func (addr *ipv4Address) IsIPv4() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addr *IPv4Address) IsIPv6() bool {
|
func (addr *ipv4Address) IsIPv6() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addr *IPv4Address) IsDomain() bool {
|
func (addr *ipv4Address) IsDomain() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IPv4Address) String() string {
|
func (this *ipv4Address) String() string {
|
||||||
return this.IP().String()
|
return this.IP().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IPv4Address) Equals(another Address) bool {
|
func (this *ipv4Address) Equals(another Address) bool {
|
||||||
anotherIPv4, ok := another.(*IPv4Address)
|
anotherIPv4, ok := another.(*ipv4Address)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -95,34 +97,34 @@ func (this *IPv4Address) Equals(another Address) bool {
|
||||||
this[3] == anotherIPv4[3]
|
this[3] == anotherIPv4[3]
|
||||||
}
|
}
|
||||||
|
|
||||||
type IPv6Address [16]byte
|
type ipv6Address [16]byte
|
||||||
|
|
||||||
func (addr *IPv6Address) IP() net.IP {
|
func (addr *ipv6Address) IP() net.IP {
|
||||||
return net.IP(addr[:])
|
return net.IP(addr[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addr *IPv6Address) Domain() string {
|
func (addr *ipv6Address) Domain() string {
|
||||||
panic("Calling Domain() on an IPv6Address.")
|
panic("Calling Domain() on an IPv6Address.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addr *IPv6Address) IsIPv4() bool {
|
func (addr *ipv6Address) IsIPv4() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addr *IPv6Address) IsIPv6() bool {
|
func (addr *ipv6Address) IsIPv6() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addr *IPv6Address) IsDomain() bool {
|
func (addr *ipv6Address) IsDomain() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IPv6Address) String() string {
|
func (this *ipv6Address) String() string {
|
||||||
return "[" + this.IP().String() + "]"
|
return "[" + this.IP().String() + "]"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *IPv6Address) Equals(another Address) bool {
|
func (this *ipv6Address) Equals(another Address) bool {
|
||||||
anotherIPv6, ok := another.(*IPv6Address)
|
anotherIPv6, ok := another.(*ipv6Address)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -134,34 +136,34 @@ func (this *IPv6Address) Equals(another Address) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
type DomainAddressImpl string
|
type domainAddress string
|
||||||
|
|
||||||
func (addr *DomainAddressImpl) IP() net.IP {
|
func (addr *domainAddress) IP() net.IP {
|
||||||
panic("Calling IP() on a DomainAddress.")
|
panic("Calling IP() on a DomainAddress.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addr *DomainAddressImpl) Domain() string {
|
func (addr *domainAddress) Domain() string {
|
||||||
return string(*addr)
|
return string(*addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addr *DomainAddressImpl) IsIPv4() bool {
|
func (addr *domainAddress) IsIPv4() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addr *DomainAddressImpl) IsIPv6() bool {
|
func (addr *domainAddress) IsIPv6() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (addr *DomainAddressImpl) IsDomain() bool {
|
func (addr *domainAddress) IsDomain() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *DomainAddressImpl) String() string {
|
func (this *domainAddress) String() string {
|
||||||
return this.Domain()
|
return this.Domain()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *DomainAddressImpl) Equals(another Address) bool {
|
func (this *domainAddress) Equals(another Address) bool {
|
||||||
anotherDomain, ok := another.(*DomainAddressImpl)
|
anotherDomain, ok := another.(*domainAddress)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue