2015-09-13 14:01:50 -04:00
|
|
|
package net
|
2015-09-10 18:24:18 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/log"
|
|
|
|
"v2ray.com/core/common/predicate"
|
2015-09-10 18:24:18 -04:00
|
|
|
)
|
|
|
|
|
2016-05-16 02:09:28 -04:00
|
|
|
var (
|
|
|
|
LocalHostIP = IPAddress([]byte{127, 0, 0, 1})
|
2016-05-29 10:37:52 -04:00
|
|
|
AnyIP = IPAddress([]byte{0, 0, 0, 0})
|
2016-05-16 02:09:28 -04:00
|
|
|
)
|
|
|
|
|
2016-08-14 11:07:52 -04:00
|
|
|
type AddressFamily int
|
|
|
|
|
|
|
|
const (
|
|
|
|
AddressFamilyIPv4 = AddressFamily(0)
|
|
|
|
AddressFamilyIPv6 = AddressFamily(1)
|
|
|
|
AddressFamilyDomain = AddressFamily(2)
|
|
|
|
)
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v AddressFamily) Either(fs ...AddressFamily) bool {
|
2016-08-14 12:14:12 -04:00
|
|
|
for _, f := range fs {
|
2016-11-27 15:39:09 -05:00
|
|
|
if v == f {
|
2016-08-14 12:14:12 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v AddressFamily) IsIPv4() bool {
|
|
|
|
return v == AddressFamilyIPv4
|
2016-08-14 12:14:12 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v AddressFamily) IsIPv6() bool {
|
|
|
|
return v == AddressFamilyIPv6
|
2016-08-14 12:14:12 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v AddressFamily) IsDomain() bool {
|
|
|
|
return v == AddressFamilyDomain
|
2016-08-14 12:14:12 -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.
|
2015-09-20 10:55:45 -04:00
|
|
|
type Address interface {
|
2015-12-02 06:47:54 -05:00
|
|
|
IP() net.IP // IP of this Address
|
|
|
|
Domain() string // Domain of this Address
|
2016-08-14 12:14:12 -04:00
|
|
|
Family() AddressFamily
|
2015-09-20 10:55:45 -04:00
|
|
|
|
2015-09-21 05:51:18 -04:00
|
|
|
String() string // String representation of this Address
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 05:01:18 -05:00
|
|
|
// 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.
|
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)
|
|
|
|
}
|
|
|
|
|
2016-02-06 05:01:18 -05:00
|
|
|
// IPAddress creates an Address with given IP.
|
2015-12-16 17:53:38 -05:00
|
|
|
func IPAddress(ip []byte) Address {
|
2015-09-20 10:55:45 -04:00
|
|
|
switch len(ip) {
|
|
|
|
case net.IPv4len:
|
2016-02-06 05:01:18 -05:00
|
|
|
var addr ipv4Address = [4]byte{ip[0], ip[1], ip[2], ip[3]}
|
2016-11-27 02:47:03 -05:00
|
|
|
return addr
|
2015-09-20 10:55:45 -04:00
|
|
|
case net.IPv6len:
|
2016-05-24 16:09:22 -04:00
|
|
|
if predicate.BytesAll(ip[0:10], 0) && predicate.BytesAll(ip[10:12], 0xff) {
|
2015-12-16 17:53:38 -05:00
|
|
|
return IPAddress(ip[12:16])
|
2015-10-14 02:43:04 -04:00
|
|
|
}
|
2016-02-06 05:01:18 -05:00
|
|
|
var addr ipv6Address = [16]byte{
|
2016-01-15 07:16:07 -05:00
|
|
|
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],
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
2016-11-27 02:47:03 -05:00
|
|
|
return addr
|
2015-09-20 10:55:45 -04:00
|
|
|
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-16 11:07:05 -04:00
|
|
|
}
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 05:01:18 -05:00
|
|
|
// DomainAddress creates an Address with given domain.
|
2015-12-16 17:53:38 -05:00
|
|
|
func DomainAddress(domain string) Address {
|
2016-02-06 05:01:18 -05:00
|
|
|
var addr domainAddress = domainAddress(domain)
|
2016-11-27 02:47:03 -05:00
|
|
|
return addr
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 05:01:18 -05:00
|
|
|
type ipv4Address [4]byte
|
2015-09-11 08:12:26 -04:00
|
|
|
|
2016-11-27 02:47:03 -05:00
|
|
|
func (addr ipv4Address) IP() net.IP {
|
2016-01-15 07:16:07 -05:00
|
|
|
return net.IP(addr[:])
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 02:47:03 -05:00
|
|
|
func (addr ipv4Address) Domain() string {
|
2015-09-20 10:55:45 -04:00
|
|
|
panic("Calling Domain() on an IPv4Address.")
|
|
|
|
}
|
|
|
|
|
2016-11-27 02:47:03 -05:00
|
|
|
func (addr ipv4Address) Family() AddressFamily {
|
2016-08-14 12:14:12 -04:00
|
|
|
return AddressFamilyIPv4
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v ipv4Address) String() string {
|
|
|
|
return v.IP().String()
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 05:01:18 -05:00
|
|
|
type ipv6Address [16]byte
|
2015-09-20 10:55:45 -04:00
|
|
|
|
2016-11-27 02:47:03 -05:00
|
|
|
func (addr ipv6Address) IP() net.IP {
|
2016-01-15 07:16:07 -05:00
|
|
|
return net.IP(addr[:])
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 02:47:03 -05:00
|
|
|
func (addr ipv6Address) Domain() string {
|
2015-09-20 10:55:45 -04:00
|
|
|
panic("Calling Domain() on an IPv6Address.")
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v ipv6Address) Family() AddressFamily {
|
2016-08-14 12:14:12 -04:00
|
|
|
return AddressFamilyIPv6
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v ipv6Address) String() string {
|
|
|
|
return "[" + v.IP().String() + "]"
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 05:01:18 -05:00
|
|
|
type domainAddress string
|
2015-09-20 10:55:45 -04:00
|
|
|
|
2016-11-27 02:47:03 -05:00
|
|
|
func (addr domainAddress) IP() net.IP {
|
2015-09-20 10:55:45 -04:00
|
|
|
panic("Calling IP() on a DomainAddress.")
|
|
|
|
}
|
|
|
|
|
2016-11-27 02:47:03 -05:00
|
|
|
func (addr domainAddress) Domain() string {
|
|
|
|
return string(addr)
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 02:47:03 -05:00
|
|
|
func (addr domainAddress) Family() AddressFamily {
|
2016-08-14 12:14:12 -04:00
|
|
|
return AddressFamilyDomain
|
2015-09-20 10:55:45 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v domainAddress) String() string {
|
|
|
|
return v.Domain()
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
2016-01-20 11:31:43 -05:00
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *IPOrDomain) AsAddress() Address {
|
|
|
|
if v == nil {
|
2016-09-22 10:49:20 -04:00
|
|
|
return nil
|
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
switch addr := v.Address.(type) {
|
2016-10-12 12:43:55 -04:00
|
|
|
case *IPOrDomain_Ip:
|
2016-08-26 18:04:35 -04:00
|
|
|
return IPAddress(addr.Ip)
|
2016-10-12 12:43:55 -04:00
|
|
|
case *IPOrDomain_Domain:
|
2016-08-26 18:04:35 -04:00
|
|
|
return DomainAddress(addr.Domain)
|
|
|
|
}
|
2016-10-12 12:43:55 -04:00
|
|
|
panic("Common|Net: Invalid address.")
|
2016-08-26 18:04:35 -04:00
|
|
|
}
|