2020-02-15 21:42:46 +08:00
|
|
|
package resolver
|
2019-06-29 00:58:59 +08:00
|
|
|
|
|
|
|
import (
|
2021-04-01 18:03:30 +08:00
|
|
|
"context"
|
2019-06-29 00:58:59 +08:00
|
|
|
"errors"
|
2022-08-13 13:07:35 +08:00
|
|
|
"fmt"
|
2021-04-01 21:20:44 +08:00
|
|
|
"math/rand"
|
2019-06-29 00:58:59 +08:00
|
|
|
"net"
|
2019-09-27 10:33:37 +08:00
|
|
|
"strings"
|
2021-04-01 18:03:30 +08:00
|
|
|
"time"
|
2020-02-15 21:42:46 +08:00
|
|
|
|
2020-05-28 12:13:05 +08:00
|
|
|
"github.com/Dreamacro/clash/component/trie"
|
2020-02-15 21:42:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultResolver aim to resolve ip
|
|
|
|
DefaultResolver Resolver
|
|
|
|
|
2020-06-18 18:11:02 +08:00
|
|
|
// DisableIPv6 means don't resolve ipv6 host
|
|
|
|
// default value is true
|
|
|
|
DisableIPv6 = true
|
|
|
|
|
2020-02-15 21:42:46 +08:00
|
|
|
// DefaultHosts aim to resolve hosts
|
|
|
|
DefaultHosts = trie.New()
|
2021-04-01 18:03:30 +08:00
|
|
|
|
|
|
|
// DefaultDNSTimeout defined the default dns request timeout
|
|
|
|
DefaultDNSTimeout = time.Second * 5
|
2019-06-29 00:58:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-06-18 18:11:02 +08:00
|
|
|
ErrIPNotFound = errors.New("couldn't find ip")
|
|
|
|
ErrIPVersion = errors.New("ip version error")
|
|
|
|
ErrIPv6Disabled = errors.New("ipv6 disabled")
|
2019-06-29 00:58:59 +08:00
|
|
|
)
|
|
|
|
|
2020-02-15 21:42:46 +08:00
|
|
|
type Resolver interface {
|
2022-08-13 13:07:35 +08:00
|
|
|
LookupIP(ctx context.Context, host string) ([]net.IP, error)
|
|
|
|
LookupIPv4(ctx context.Context, host string) ([]net.IP, error)
|
|
|
|
LookupIPv6(ctx context.Context, host string) ([]net.IP, error)
|
2020-02-15 21:42:46 +08:00
|
|
|
ResolveIP(host string) (ip net.IP, err error)
|
|
|
|
ResolveIPv4(host string) (ip net.IP, err error)
|
|
|
|
ResolveIPv6(host string) (ip net.IP, err error)
|
|
|
|
}
|
|
|
|
|
2022-08-13 13:07:35 +08:00
|
|
|
// LookupIPv4 with a host, return ipv4 list
|
|
|
|
func LookupIPv4(ctx context.Context, host string) ([]net.IP, error) {
|
2019-09-11 17:00:55 +08:00
|
|
|
if node := DefaultHosts.Search(host); node != nil {
|
|
|
|
if ip := node.Data.(net.IP).To4(); ip != nil {
|
2022-08-13 13:07:35 +08:00
|
|
|
return []net.IP{ip}, nil
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.ParseIP(host)
|
2019-09-27 10:33:37 +08:00
|
|
|
if ip != nil {
|
|
|
|
if !strings.Contains(host, ":") {
|
2022-08-13 13:07:35 +08:00
|
|
|
return []net.IP{ip}, nil
|
2019-09-27 10:33:37 +08:00
|
|
|
}
|
2020-02-15 21:42:46 +08:00
|
|
|
return nil, ErrIPVersion
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if DefaultResolver != nil {
|
2022-08-13 13:07:35 +08:00
|
|
|
return DefaultResolver.LookupIPv4(ctx, host)
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2021-04-01 18:03:30 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), DefaultDNSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip4", host)
|
2019-09-11 17:00:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-04-01 18:03:30 +08:00
|
|
|
} else if len(ipAddrs) == 0 {
|
|
|
|
return nil, ErrIPNotFound
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2022-08-13 13:07:35 +08:00
|
|
|
return ipAddrs, nil
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2022-08-13 13:07:35 +08:00
|
|
|
// ResolveIPv4 with a host, return ipv4
|
|
|
|
func ResolveIPv4(host string) (net.IP, error) {
|
|
|
|
ips, err := LookupIPv4(context.Background(), host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(ips) == 0 {
|
|
|
|
return nil, fmt.Errorf("%w: %s", ErrIPNotFound, host)
|
|
|
|
}
|
|
|
|
return ips[rand.Intn(len(ips))], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupIPv6 with a host, return ipv6 list
|
|
|
|
func LookupIPv6(ctx context.Context, host string) ([]net.IP, error) {
|
2020-06-18 18:11:02 +08:00
|
|
|
if DisableIPv6 {
|
|
|
|
return nil, ErrIPv6Disabled
|
|
|
|
}
|
|
|
|
|
2019-09-11 17:00:55 +08:00
|
|
|
if node := DefaultHosts.Search(host); node != nil {
|
|
|
|
if ip := node.Data.(net.IP).To16(); ip != nil {
|
2022-08-13 13:07:35 +08:00
|
|
|
return []net.IP{ip}, nil
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.ParseIP(host)
|
2019-09-27 10:33:37 +08:00
|
|
|
if ip != nil {
|
|
|
|
if strings.Contains(host, ":") {
|
2022-08-13 13:07:35 +08:00
|
|
|
return []net.IP{ip}, nil
|
2019-09-27 10:33:37 +08:00
|
|
|
}
|
2020-02-15 21:42:46 +08:00
|
|
|
return nil, ErrIPVersion
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if DefaultResolver != nil {
|
2022-08-13 13:07:35 +08:00
|
|
|
return DefaultResolver.LookupIPv6(ctx, host)
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2021-04-01 18:03:30 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), DefaultDNSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip6", host)
|
2019-09-11 17:00:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-04-01 18:03:30 +08:00
|
|
|
} else if len(ipAddrs) == 0 {
|
|
|
|
return nil, ErrIPNotFound
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2022-08-13 13:07:35 +08:00
|
|
|
return ipAddrs, nil
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2022-08-13 13:07:35 +08:00
|
|
|
// ResolveIPv6 with a host, return ipv6
|
|
|
|
func ResolveIPv6(host string) (net.IP, error) {
|
|
|
|
ips, err := LookupIPv6(context.Background(), host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(ips) == 0 {
|
|
|
|
return nil, fmt.Errorf("%w: %s", ErrIPNotFound, host)
|
|
|
|
}
|
|
|
|
return ips[rand.Intn(len(ips))], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupIPWithResolver same as ResolveIP, but with a resolver
|
|
|
|
func LookupIPWithResolver(ctx context.Context, host string, r Resolver) ([]net.IP, error) {
|
2019-09-11 17:00:55 +08:00
|
|
|
if node := DefaultHosts.Search(host); node != nil {
|
2022-08-13 13:07:35 +08:00
|
|
|
return []net.IP{node.Data.(net.IP)}, nil
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2021-04-01 21:20:44 +08:00
|
|
|
if r != nil {
|
2020-06-18 18:11:02 +08:00
|
|
|
if DisableIPv6 {
|
2022-08-13 13:07:35 +08:00
|
|
|
return r.LookupIPv4(ctx, host)
|
2020-06-18 18:11:02 +08:00
|
|
|
}
|
2022-08-13 13:07:35 +08:00
|
|
|
return r.LookupIP(ctx, host)
|
2020-06-18 18:11:02 +08:00
|
|
|
} else if DisableIPv6 {
|
2022-08-13 13:07:35 +08:00
|
|
|
return LookupIPv4(ctx, host)
|
2019-06-29 00:58:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
if ip != nil {
|
2022-08-13 13:07:35 +08:00
|
|
|
return []net.IP{ip}, nil
|
2019-06-29 00:58:59 +08:00
|
|
|
}
|
|
|
|
|
2022-08-13 13:07:35 +08:00
|
|
|
ips, err := net.DefaultResolver.LookupIP(ctx, "ip", host)
|
2019-06-29 00:58:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-08-13 13:07:35 +08:00
|
|
|
} else if len(ips) == 0 {
|
|
|
|
return nil, ErrIPNotFound
|
2019-06-29 00:58:59 +08:00
|
|
|
}
|
|
|
|
|
2022-08-13 13:07:35 +08:00
|
|
|
return ips, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveIP with a host, return ip
|
|
|
|
func LookupIP(ctx context.Context, host string) ([]net.IP, error) {
|
|
|
|
return LookupIPWithResolver(ctx, host, DefaultResolver)
|
2019-06-29 00:58:59 +08:00
|
|
|
}
|
2021-04-01 21:20:44 +08:00
|
|
|
|
|
|
|
// ResolveIP with a host, return ip
|
|
|
|
func ResolveIP(host string) (net.IP, error) {
|
2022-08-13 13:07:35 +08:00
|
|
|
ips, err := LookupIP(context.Background(), host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(ips) == 0 {
|
|
|
|
return nil, fmt.Errorf("%w: %s", ErrIPNotFound, host)
|
|
|
|
}
|
|
|
|
return ips[rand.Intn(len(ips))], nil
|
2021-04-01 21:20:44 +08:00
|
|
|
}
|