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"
|
2021-04-01 21:20:44 +08:00
|
|
|
"math/rand"
|
2019-06-29 00:58:59 +08:00
|
|
|
"net"
|
2022-04-06 04:25:53 +08:00
|
|
|
"net/netip"
|
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
|
|
|
|
|
2022-03-28 00:44:13 +08:00
|
|
|
// ProxyServerHostResolver resolve ip to proxies server host
|
|
|
|
ProxyServerHostResolver Resolver
|
2022-02-23 02:38:50 +08:00
|
|
|
|
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
|
2022-04-06 04:25:53 +08:00
|
|
|
DefaultHosts = trie.New[netip.Addr]()
|
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 {
|
|
|
|
ResolveIP(host string) (ip net.IP, err error)
|
|
|
|
ResolveIPv4(host string) (ip net.IP, err error)
|
|
|
|
ResolveIPv6(host string) (ip net.IP, err error)
|
|
|
|
}
|
|
|
|
|
2019-09-11 17:00:55 +08:00
|
|
|
// ResolveIPv4 with a host, return ipv4
|
|
|
|
func ResolveIPv4(host string) (net.IP, error) {
|
2022-02-23 02:38:50 +08:00
|
|
|
return ResolveIPv4WithResolver(host, DefaultResolver)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ResolveIPv4WithResolver(host string, r Resolver) (net.IP, error) {
|
2019-09-11 17:00:55 +08:00
|
|
|
if node := DefaultHosts.Search(host); node != nil {
|
2022-04-06 04:25:53 +08:00
|
|
|
if ip := node.Data; ip.Is4() {
|
|
|
|
return ip.AsSlice(), 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, ":") {
|
|
|
|
return ip, nil
|
|
|
|
}
|
2020-02-15 21:42:46 +08:00
|
|
|
return nil, ErrIPVersion
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2022-02-23 02:38:50 +08:00
|
|
|
if r != nil {
|
|
|
|
return r.ResolveIPv4(host)
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2022-03-28 00:44:13 +08:00
|
|
|
if DefaultResolver == nil {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), DefaultDNSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip4", host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(ipAddrs) == 0 {
|
|
|
|
return nil, ErrIPNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipAddrs[rand.Intn(len(ipAddrs))], nil
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2022-03-28 00:44:13 +08:00
|
|
|
return nil, ErrIPNotFound
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveIPv6 with a host, return ipv6
|
|
|
|
func ResolveIPv6(host string) (net.IP, error) {
|
2022-02-23 02:38:50 +08:00
|
|
|
return ResolveIPv6WithResolver(host, DefaultResolver)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ResolveIPv6WithResolver(host string, r Resolver) (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 {
|
2022-04-06 04:25:53 +08:00
|
|
|
if ip := node.Data; ip.Is6() {
|
|
|
|
return ip.AsSlice(), 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, ":") {
|
|
|
|
return ip, nil
|
|
|
|
}
|
2020-02-15 21:42:46 +08:00
|
|
|
return nil, ErrIPVersion
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2022-02-23 02:38:50 +08:00
|
|
|
if r != nil {
|
|
|
|
return r.ResolveIPv6(host)
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2022-03-28 00:44:13 +08:00
|
|
|
if DefaultResolver == nil {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), DefaultDNSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip6", host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(ipAddrs) == 0 {
|
|
|
|
return nil, ErrIPNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipAddrs[rand.Intn(len(ipAddrs))], nil
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2022-03-28 00:44:13 +08:00
|
|
|
return nil, ErrIPNotFound
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2021-04-01 21:20:44 +08:00
|
|
|
// ResolveIPWithResolver same as ResolveIP, but with a resolver
|
|
|
|
func ResolveIPWithResolver(host string, r Resolver) (net.IP, error) {
|
2019-09-11 17:00:55 +08:00
|
|
|
if node := DefaultHosts.Search(host); node != nil {
|
2022-04-06 04:25:53 +08:00
|
|
|
ip := node.Data
|
|
|
|
return ip.Unmap().AsSlice(), 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 {
|
2021-04-01 21:20:44 +08:00
|
|
|
return r.ResolveIPv4(host)
|
2020-06-18 18:11:02 +08:00
|
|
|
}
|
2021-04-01 21:20:44 +08:00
|
|
|
return r.ResolveIP(host)
|
2020-06-18 18:11:02 +08:00
|
|
|
} else if DisableIPv6 {
|
|
|
|
return ResolveIPv4(host)
|
2019-06-29 00:58:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
if ip != nil {
|
|
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
|
2022-03-28 00:44:13 +08:00
|
|
|
if DefaultResolver == nil {
|
|
|
|
ipAddr, err := net.ResolveIPAddr("ip", host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipAddr.IP, nil
|
2019-06-29 00:58:59 +08:00
|
|
|
}
|
|
|
|
|
2022-03-28 00:44:13 +08:00
|
|
|
return nil, ErrIPNotFound
|
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) {
|
|
|
|
return ResolveIPWithResolver(host, DefaultResolver)
|
|
|
|
}
|
2022-02-23 02:38:50 +08:00
|
|
|
|
2022-03-28 00:44:13 +08:00
|
|
|
// ResolveIPv4ProxyServerHost proxies server host only
|
|
|
|
func ResolveIPv4ProxyServerHost(host string) (net.IP, error) {
|
|
|
|
if ProxyServerHostResolver != nil {
|
|
|
|
return ResolveIPv4WithResolver(host, ProxyServerHostResolver)
|
|
|
|
}
|
|
|
|
return ResolveIPv4(host)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveIPv6ProxyServerHost proxies server host only
|
|
|
|
func ResolveIPv6ProxyServerHost(host string) (net.IP, error) {
|
|
|
|
if ProxyServerHostResolver != nil {
|
|
|
|
return ResolveIPv6WithResolver(host, ProxyServerHostResolver)
|
|
|
|
}
|
|
|
|
return ResolveIPv6(host)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveProxyServerHost proxies server host only
|
|
|
|
func ResolveProxyServerHost(host string) (net.IP, error) {
|
|
|
|
if ProxyServerHostResolver != nil {
|
|
|
|
return ResolveIPWithResolver(host, ProxyServerHostResolver)
|
|
|
|
}
|
|
|
|
return ResolveIP(host)
|
2022-02-23 02:38:50 +08:00
|
|
|
}
|