1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 03:37:37 -05:00
v2fly/common/net/address.go

170 lines
3.5 KiB
Go
Raw Normal View History

2015-09-13 14:01:50 -04:00
package net
2015-09-10 18:24:18 -04:00
import (
"net"
"github.com/v2ray/v2ray-core/common/log"
2016-01-21 06:52:09 -05:00
"github.com/v2ray/v2ray-core/common/serial"
2015-09-10 18:24:18 -04:00
)
2015-09-21 06:15:25 -04:00
// Address represents a network address to be communicated with. It may be an IP address or domain
// address, not both. This interface doesn't resolve IP address for a given domain.
type Address interface {
2015-12-02 06:47:54 -05:00
IP() net.IP // IP of this Address
Domain() string // Domain of this Address
2015-09-21 06:15:25 -04:00
IsIPv4() bool // True if this Address is an IPv4 address
IsIPv6() bool // True if this Address is an IPv6 address
2015-09-21 05:51:18 -04:00
IsDomain() bool // True if this Address is an domain address
2015-09-21 05:51:18 -04:00
String() string // String representation of this Address
Equals(Address) bool
2015-09-10 18:24:18 -04:00
}
2016-01-18 19:21:07 -05:00
func ParseAddress(addr string) Address {
ip := net.ParseIP(addr)
if ip != nil {
return IPAddress(ip)
}
return DomainAddress(addr)
}
2015-09-21 05:51:18 -04:00
// IPAddress creates an Address with given IP and port.
2015-12-16 17:53:38 -05:00
func IPAddress(ip []byte) Address {
switch len(ip) {
case net.IPv4len:
2016-01-15 07:16:07 -05:00
var addr IPv4Address = [4]byte{ip[0], ip[1], ip[2], ip[3]}
return &addr
case net.IPv6len:
2016-01-21 06:52:09 -05:00
if serial.BytesLiteral(ip[0:10]).All(0) && serial.BytesLiteral(ip[10:12]).All(0xff) {
2015-12-16 17:53:38 -05:00
return IPAddress(ip[12:16])
2015-10-14 02:43:04 -04:00
}
2016-01-15 07:16:07 -05:00
var addr IPv6Address = [16]byte{
ip[0], ip[1], ip[2], ip[3],
ip[4], ip[5], ip[6], ip[7],
ip[8], ip[9], ip[10], ip[11],
ip[12], ip[13], ip[14], ip[15],
}
2016-01-15 07:16:07 -05:00
return &addr
default:
2016-01-18 06:24:33 -05:00
log.Error("Invalid IP format: ", ip)
2015-09-24 06:54:10 -04:00
return nil
}
2015-09-10 18:24:18 -04:00
}
2015-09-21 05:51:18 -04:00
// DomainAddress creates an Address with given domain and port.
2015-12-16 17:53:38 -05:00
func DomainAddress(domain string) Address {
2016-01-15 07:16:07 -05:00
var addr DomainAddressImpl = DomainAddressImpl(domain)
return &addr
2015-09-10 18:24:18 -04:00
}
2016-01-15 07:16:07 -05:00
type IPv4Address [4]byte
2015-09-11 08:12:26 -04:00
2015-12-02 06:47:54 -05:00
func (addr *IPv4Address) IP() net.IP {
2016-01-15 07:16:07 -05:00
return net.IP(addr[:])
}
2015-12-02 06:47:54 -05:00
func (addr *IPv4Address) Domain() string {
panic("Calling Domain() on an IPv4Address.")
}
2015-12-02 06:47:54 -05:00
func (addr *IPv4Address) IsIPv4() bool {
return true
}
2015-12-02 06:47:54 -05:00
func (addr *IPv4Address) IsIPv6() bool {
return false
}
2015-12-02 06:47:54 -05:00
func (addr *IPv4Address) IsDomain() bool {
return false
}
2015-12-02 06:47:54 -05:00
func (this *IPv4Address) String() string {
2015-12-16 17:53:38 -05:00
return this.IP().String()
}
func (this *IPv4Address) Equals(another Address) bool {
anotherIPv4, ok := another.(*IPv4Address)
if !ok {
return false
}
return this[0] == anotherIPv4[0] &&
this[1] == anotherIPv4[1] &&
this[2] == anotherIPv4[2] &&
this[3] == anotherIPv4[3]
}
2016-01-15 07:16:07 -05:00
type IPv6Address [16]byte
2015-12-02 06:47:54 -05:00
func (addr *IPv6Address) IP() net.IP {
2016-01-15 07:16:07 -05:00
return net.IP(addr[:])
}
2015-12-02 06:47:54 -05:00
func (addr *IPv6Address) Domain() string {
panic("Calling Domain() on an IPv6Address.")
}
2015-12-02 06:47:54 -05:00
func (addr *IPv6Address) IsIPv4() bool {
return false
}
2015-12-02 06:47:54 -05:00
func (addr *IPv6Address) IsIPv6() bool {
return true
}
2015-12-02 06:47:54 -05:00
func (addr *IPv6Address) IsDomain() bool {
return false
}
2015-12-02 06:47:54 -05:00
func (this *IPv6Address) String() string {
2015-12-16 17:53:38 -05:00
return "[" + this.IP().String() + "]"
}
func (this *IPv6Address) Equals(another Address) bool {
anotherIPv6, ok := another.(*IPv6Address)
if !ok {
return false
}
for idx, v := range *this {
if anotherIPv6[idx] != v {
return false
}
}
return true
}
2016-01-15 07:16:07 -05:00
type DomainAddressImpl string
2015-12-02 06:47:54 -05:00
func (addr *DomainAddressImpl) IP() net.IP {
panic("Calling IP() on a DomainAddress.")
}
2015-12-02 06:47:54 -05:00
func (addr *DomainAddressImpl) Domain() string {
2016-01-15 07:16:07 -05:00
return string(*addr)
}
2015-12-02 06:47:54 -05:00
func (addr *DomainAddressImpl) IsIPv4() bool {
return false
}
2015-12-02 06:47:54 -05:00
func (addr *DomainAddressImpl) IsIPv6() bool {
return false
}
2015-12-02 06:47:54 -05:00
func (addr *DomainAddressImpl) IsDomain() bool {
return true
}
2015-12-02 06:47:54 -05:00
func (this *DomainAddressImpl) String() string {
2016-01-15 07:16:07 -05:00
return this.Domain()
2015-09-10 18:24:18 -04:00
}
func (this *DomainAddressImpl) Equals(another Address) bool {
anotherDomain, ok := another.(*DomainAddressImpl)
if !ok {
return false
}
return this.Domain() == anotherDomain.Domain()
}