2015-09-13 18:01:50 +00:00
|
|
|
package net
|
2015-09-10 22:24:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2017-12-10 23:41:40 +00:00
|
|
|
"strings"
|
2015-09-10 22:24:18 +00:00
|
|
|
|
2018-07-12 21:38:10 +00:00
|
|
|
"v2ray.com/core/common/compare"
|
2015-09-10 22:24:18 +00:00
|
|
|
)
|
|
|
|
|
2016-05-16 06:09:28 +00:00
|
|
|
var (
|
2016-12-21 14:37:16 +00:00
|
|
|
// LocalHostIP is a constant value for localhost IP in IPv4.
|
|
|
|
LocalHostIP = IPAddress([]byte{127, 0, 0, 1})
|
|
|
|
|
|
|
|
// AnyIP is a constant value for any IP in IPv4.
|
|
|
|
AnyIP = IPAddress([]byte{0, 0, 0, 0})
|
|
|
|
|
|
|
|
// LocalHostDomain is a constant value for localhost domain.
|
2016-12-14 22:04:02 +00:00
|
|
|
LocalHostDomain = DomainAddress("localhost")
|
2017-02-07 23:26:40 +00:00
|
|
|
|
|
|
|
// LocalHostIPv6 is a constant value for localhost IP in IPv6.
|
|
|
|
LocalHostIPv6 = IPAddress([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1})
|
2016-05-16 06:09:28 +00:00
|
|
|
)
|
|
|
|
|
2017-02-20 10:25:05 +00:00
|
|
|
// AddressFamily is the type of address.
|
2018-03-13 08:02:34 +00:00
|
|
|
type AddressFamily byte
|
2016-08-14 15:07:52 +00:00
|
|
|
|
|
|
|
const (
|
2017-02-20 10:25:05 +00:00
|
|
|
// AddressFamilyIPv4 represents address as IPv4
|
|
|
|
AddressFamilyIPv4 = AddressFamily(0)
|
|
|
|
|
|
|
|
// AddressFamilyIPv6 represents address as IPv6
|
|
|
|
AddressFamilyIPv6 = AddressFamily(1)
|
|
|
|
|
|
|
|
// AddressFamilyDomain represents address as Domain
|
2016-08-14 15:07:52 +00:00
|
|
|
AddressFamilyDomain = AddressFamily(2)
|
|
|
|
)
|
|
|
|
|
2017-09-11 19:56:20 +00:00
|
|
|
// IsIPv4 returns true if current AddressFamily is IPv4.
|
|
|
|
func (af AddressFamily) IsIPv4() bool {
|
|
|
|
return af == AddressFamilyIPv4
|
2016-08-14 16:14:12 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 19:56:20 +00:00
|
|
|
// IsIPv6 returns true if current AddressFamily is IPv6.
|
|
|
|
func (af AddressFamily) IsIPv6() bool {
|
|
|
|
return af == AddressFamilyIPv6
|
2016-08-14 16:14:12 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 10:13:36 +00:00
|
|
|
// IsIP returns true if current AddressFamily is IPv6 or IPv4.
|
|
|
|
func (af AddressFamily) IsIP() bool {
|
|
|
|
return af == AddressFamilyIPv4 || af == AddressFamilyIPv6
|
|
|
|
}
|
|
|
|
|
2017-09-11 19:56:20 +00:00
|
|
|
// IsDomain returns true if current AddressFamily is Domain.
|
|
|
|
func (af AddressFamily) IsDomain() bool {
|
|
|
|
return af == AddressFamilyDomain
|
2016-08-14 16:14:12 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 10:15:25 +00: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 14:55:45 +00:00
|
|
|
type Address interface {
|
2015-12-02 11:47:54 +00:00
|
|
|
IP() net.IP // IP of this Address
|
|
|
|
Domain() string // Domain of this Address
|
2016-08-14 16:14:12 +00:00
|
|
|
Family() AddressFamily
|
2015-09-20 14:55:45 +00:00
|
|
|
|
2015-09-21 09:51:18 +00:00
|
|
|
String() string // String representation of this Address
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 00:16:06 +00:00
|
|
|
func isAlphaNum(c byte) bool {
|
|
|
|
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|
|
|
|
}
|
|
|
|
|
2016-02-06 10:01:18 +00: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-19 00:21:07 +00:00
|
|
|
func ParseAddress(addr string) Address {
|
2017-11-21 17:46:28 +00:00
|
|
|
// Handle IPv6 address in form as "[2001:4860:0:2001::68]"
|
|
|
|
lenAddr := len(addr)
|
|
|
|
if lenAddr > 0 && addr[0] == '[' && addr[lenAddr-1] == ']' {
|
|
|
|
addr = addr[1 : lenAddr-1]
|
2018-11-05 00:16:06 +00:00
|
|
|
lenAddr -= 2
|
|
|
|
}
|
|
|
|
|
|
|
|
if lenAddr > 0 && (!isAlphaNum(addr[0]) || !isAlphaNum(addr[len(addr)-1])) {
|
|
|
|
addr = strings.TrimSpace(addr)
|
2017-11-21 17:46:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-19 00:21:07 +00:00
|
|
|
ip := net.ParseIP(addr)
|
|
|
|
if ip != nil {
|
|
|
|
return IPAddress(ip)
|
|
|
|
}
|
|
|
|
return DomainAddress(addr)
|
|
|
|
}
|
|
|
|
|
2016-02-06 10:01:18 +00:00
|
|
|
// IPAddress creates an Address with given IP.
|
2015-12-16 22:53:38 +00:00
|
|
|
func IPAddress(ip []byte) Address {
|
2015-09-20 14:55:45 +00:00
|
|
|
switch len(ip) {
|
|
|
|
case net.IPv4len:
|
2016-02-06 10:01:18 +00:00
|
|
|
var addr ipv4Address = [4]byte{ip[0], ip[1], ip[2], ip[3]}
|
2016-11-27 07:47:03 +00:00
|
|
|
return addr
|
2015-09-20 14:55:45 +00:00
|
|
|
case net.IPv6len:
|
2018-07-12 21:38:10 +00:00
|
|
|
if compare.BytesAll(ip[0:10], 0) && compare.BytesAll(ip[10:12], 0xff) {
|
2015-12-16 22:53:38 +00:00
|
|
|
return IPAddress(ip[12:16])
|
2015-10-14 06:43:04 +00:00
|
|
|
}
|
2016-02-06 10:01:18 +00:00
|
|
|
var addr ipv6Address = [16]byte{
|
2016-01-15 12:16:07 +00: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 14:55:45 +00:00
|
|
|
}
|
2016-11-27 07:47:03 +00:00
|
|
|
return addr
|
2015-09-20 14:55:45 +00:00
|
|
|
default:
|
2017-12-19 20:28:12 +00:00
|
|
|
newError("invalid IP format: ", ip).AtError().WriteToLog()
|
2015-09-24 10:54:10 +00:00
|
|
|
return nil
|
2015-09-16 15:07:05 +00:00
|
|
|
}
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 10:01:18 +00:00
|
|
|
// DomainAddress creates an Address with given domain.
|
2015-12-16 22:53:38 +00:00
|
|
|
func DomainAddress(domain string) Address {
|
2016-12-21 14:37:16 +00:00
|
|
|
return domainAddress(domain)
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 10:01:18 +00:00
|
|
|
type ipv4Address [4]byte
|
2015-09-11 12:12:26 +00:00
|
|
|
|
2017-09-11 19:56:20 +00:00
|
|
|
func (a ipv4Address) IP() net.IP {
|
|
|
|
return net.IP(a[:])
|
2015-09-20 14:55:45 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 14:37:16 +00:00
|
|
|
func (ipv4Address) Domain() string {
|
2015-09-20 14:55:45 +00:00
|
|
|
panic("Calling Domain() on an IPv4Address.")
|
|
|
|
}
|
|
|
|
|
2016-12-21 14:37:16 +00:00
|
|
|
func (ipv4Address) Family() AddressFamily {
|
2016-08-14 16:14:12 +00:00
|
|
|
return AddressFamilyIPv4
|
2015-09-20 14:55:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 19:56:20 +00:00
|
|
|
func (a ipv4Address) String() string {
|
|
|
|
return a.IP().String()
|
2015-09-20 14:55:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 10:01:18 +00:00
|
|
|
type ipv6Address [16]byte
|
2015-09-20 14:55:45 +00:00
|
|
|
|
2017-09-11 19:56:20 +00:00
|
|
|
func (a ipv6Address) IP() net.IP {
|
|
|
|
return net.IP(a[:])
|
2015-09-20 14:55:45 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 14:37:16 +00:00
|
|
|
func (ipv6Address) Domain() string {
|
2015-09-20 14:55:45 +00:00
|
|
|
panic("Calling Domain() on an IPv6Address.")
|
|
|
|
}
|
|
|
|
|
2016-12-21 14:37:16 +00:00
|
|
|
func (ipv6Address) Family() AddressFamily {
|
2016-08-14 16:14:12 +00:00
|
|
|
return AddressFamilyIPv6
|
2015-09-20 14:55:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 19:56:20 +00:00
|
|
|
func (a ipv6Address) String() string {
|
|
|
|
return "[" + a.IP().String() + "]"
|
2015-09-20 14:55:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-06 10:01:18 +00:00
|
|
|
type domainAddress string
|
2015-09-20 14:55:45 +00:00
|
|
|
|
2016-12-21 14:37:16 +00:00
|
|
|
func (domainAddress) IP() net.IP {
|
2015-09-20 14:55:45 +00:00
|
|
|
panic("Calling IP() on a DomainAddress.")
|
|
|
|
}
|
|
|
|
|
2017-09-11 19:56:20 +00:00
|
|
|
func (a domainAddress) Domain() string {
|
|
|
|
return string(a)
|
2015-09-20 14:55:45 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 14:37:16 +00:00
|
|
|
func (domainAddress) Family() AddressFamily {
|
2016-08-14 16:14:12 +00:00
|
|
|
return AddressFamilyDomain
|
2015-09-20 14:55:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 19:56:20 +00:00
|
|
|
func (a domainAddress) String() string {
|
|
|
|
return a.Domain()
|
2015-09-10 22:24:18 +00:00
|
|
|
}
|
2016-01-20 16:31:43 +00:00
|
|
|
|
2017-09-11 19:56:20 +00:00
|
|
|
// AsAddress translates IPOrDomain to Address.
|
|
|
|
func (d *IPOrDomain) AsAddress() Address {
|
|
|
|
if d == nil {
|
2016-09-22 14:49:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-09-11 19:56:20 +00:00
|
|
|
switch addr := d.Address.(type) {
|
2016-10-12 16:43:55 +00:00
|
|
|
case *IPOrDomain_Ip:
|
2016-08-26 22:04:35 +00:00
|
|
|
return IPAddress(addr.Ip)
|
2016-10-12 16:43:55 +00:00
|
|
|
case *IPOrDomain_Domain:
|
2016-08-26 22:04:35 +00:00
|
|
|
return DomainAddress(addr.Domain)
|
|
|
|
}
|
2016-10-12 16:43:55 +00:00
|
|
|
panic("Common|Net: Invalid address.")
|
2016-08-26 22:04:35 +00:00
|
|
|
}
|
2016-12-14 22:04:02 +00:00
|
|
|
|
2017-09-11 19:56:20 +00:00
|
|
|
// NewIPOrDomain translates Address to IPOrDomain
|
2016-12-14 22:04:02 +00:00
|
|
|
func NewIPOrDomain(addr Address) *IPOrDomain {
|
|
|
|
switch addr.Family() {
|
|
|
|
case AddressFamilyDomain:
|
|
|
|
return &IPOrDomain{
|
|
|
|
Address: &IPOrDomain_Domain{
|
|
|
|
Domain: addr.Domain(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
case AddressFamilyIPv4, AddressFamilyIPv6:
|
|
|
|
return &IPOrDomain{
|
|
|
|
Address: &IPOrDomain_Ip{
|
|
|
|
Ip: addr.IP(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("Unknown Address type.")
|
|
|
|
}
|
|
|
|
}
|