mihomo/component/resolver/resolver.go

264 lines
6.6 KiB
Go
Raw Normal View History

package resolver
import (
"context"
"errors"
"math/rand"
"net"
2022-04-06 04:25:53 +08:00
"net/netip"
"time"
2022-04-20 01:52:51 +08:00
"github.com/Dreamacro/clash/common/nnip"
"github.com/Dreamacro/clash/component/trie"
)
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
2020-06-18 18:11:02 +08:00
// DisableIPv6 means don't resolve ipv6 host
// default value is true
DisableIPv6 = true
// DefaultHosts aim to resolve hosts
2022-04-06 04:25:53 +08:00
DefaultHosts = trie.New[netip.Addr]()
// DefaultDNSTimeout defined the default dns request timeout
DefaultDNSTimeout = time.Second * 5
)
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")
)
type Resolver interface {
2022-04-20 01:52:51 +08:00
ResolveIP(host string) (ip netip.Addr, err error)
ResolveIPv4(host string) (ip netip.Addr, err error)
ResolveIPv6(host string) (ip netip.Addr, err error)
ResolveAllIP(host string) (ip []netip.Addr, err error)
ResolveAllIPv4(host string) (ips []netip.Addr, err error)
ResolveAllIPv6(host string) (ips []netip.Addr, err error)
}
2019-09-11 17:00:55 +08:00
// ResolveIPv4 with a host, return ipv4
2022-04-20 01:52:51 +08:00
func ResolveIPv4(host string) (netip.Addr, error) {
return ResolveIPv4WithResolver(host, DefaultResolver)
}
2022-04-20 01:52:51 +08:00
func ResolveIPv4WithResolver(host string, r Resolver) (netip.Addr, error) {
2022-04-29 13:03:55 +08:00
if ips, err := ResolveAllIPv4WithResolver(host, r); err == nil {
return ips[rand.Intn(len(ips))], nil
} else {
2022-07-21 09:02:58 +08:00
return netip.Addr{}, err
2022-04-29 13:03:55 +08:00
}
}
// ResolveIPv6 with a host, return ipv6
func ResolveIPv6(host string) (netip.Addr, error) {
return ResolveIPv6WithResolver(host, DefaultResolver)
}
func ResolveIPv6WithResolver(host string, r Resolver) (netip.Addr, error) {
if ips, err := ResolveAllIPv6WithResolver(host, r); err == nil {
return ips[rand.Intn(len(ips))], nil
} else {
return netip.Addr{}, err
}
}
// ResolveIPWithResolver same as ResolveIP, but with a resolver
func ResolveIPWithResolver(host string, r Resolver) (netip.Addr, error) {
2022-08-28 13:41:19 +08:00
if ip, err := ResolveIPv4WithResolver(host, r); err == nil {
return ip, nil
2022-04-29 13:03:55 +08:00
} else {
2022-08-28 13:41:19 +08:00
return ResolveIPv6WithResolver(host, r)
2022-04-29 13:03:55 +08:00
}
}
// ResolveIP with a host, return ip
func ResolveIP(host string) (netip.Addr, error) {
return ResolveIPWithResolver(host, DefaultResolver)
}
// ResolveIPv4ProxyServerHost proxies server host only
func ResolveIPv4ProxyServerHost(host string) (netip.Addr, error) {
if ProxyServerHostResolver != nil {
if ip, err := ResolveIPv4WithResolver(host, ProxyServerHostResolver); err != nil {
return ResolveIPv4(host)
} else {
return ip, nil
}
2022-04-29 13:03:55 +08:00
}
return ResolveIPv4(host)
}
// ResolveIPv6ProxyServerHost proxies server host only
func ResolveIPv6ProxyServerHost(host string) (netip.Addr, error) {
if ProxyServerHostResolver != nil {
if ip, err := ResolveIPv6WithResolver(host, ProxyServerHostResolver); err != nil {
return ResolveIPv6(host)
} else {
return ip, nil
}
2022-04-29 13:03:55 +08:00
}
return ResolveIPv6(host)
}
// ResolveProxyServerHost proxies server host only
func ResolveProxyServerHost(host string) (netip.Addr, error) {
if ProxyServerHostResolver != nil {
if ip, err := ResolveIPWithResolver(host, ProxyServerHostResolver); err != nil {
return ResolveIP(host)
} else {
return ip, err
}
2022-04-29 13:03:55 +08:00
}
return ResolveIP(host)
}
func ResolveAllIPv6WithResolver(host string, r Resolver) ([]netip.Addr, error) {
if DisableIPv6 {
return []netip.Addr{}, ErrIPv6Disabled
}
2019-09-11 17:00:55 +08:00
if node := DefaultHosts.Search(host); node != nil {
if ip := node.Data(); ip.Is6() {
2022-04-29 13:03:55 +08:00
return []netip.Addr{ip}, nil
2019-09-11 17:00:55 +08:00
}
}
2022-04-20 01:52:51 +08:00
ip, err := netip.ParseAddr(host)
if err == nil {
2022-04-29 13:03:55 +08:00
if ip.Is6() {
return []netip.Addr{ip}, nil
}
2022-04-29 13:03:55 +08:00
return []netip.Addr{}, ErrIPVersion
2019-09-11 17:00:55 +08:00
}
if r != nil {
2022-04-29 13:03:55 +08:00
return r.ResolveAllIPv6(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()
2022-04-29 13:03:55 +08:00
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip6", host)
2022-03-28 00:44:13 +08:00
if err != nil {
2022-04-29 13:03:55 +08:00
return []netip.Addr{}, err
2022-03-28 00:44:13 +08:00
} else if len(ipAddrs) == 0 {
2022-04-29 13:03:55 +08:00
return []netip.Addr{}, ErrIPNotFound
2022-04-20 01:52:51 +08:00
}
2022-04-29 13:03:55 +08:00
return []netip.Addr{netip.AddrFrom16(*(*[16]byte)(ipAddrs[rand.Intn(len(ipAddrs))]))}, nil
2019-09-11 17:00:55 +08:00
}
2022-04-29 13:03:55 +08:00
return []netip.Addr{}, ErrIPNotFound
2019-09-11 17:00:55 +08:00
}
2022-04-29 13:03:55 +08:00
func ResolveAllIPv4WithResolver(host string, r Resolver) ([]netip.Addr, error) {
2019-09-11 17:00:55 +08:00
if node := DefaultHosts.Search(host); node != nil {
if ip := node.Data(); ip.Is4() {
return []netip.Addr{node.Data()}, nil
2019-09-11 17:00:55 +08:00
}
}
2022-04-20 01:52:51 +08:00
ip, err := netip.ParseAddr(host)
if err == nil {
2022-06-17 21:44:06 +08:00
if ip.Is4() || ip.Is4In6() {
2022-04-29 13:03:55 +08:00
return []netip.Addr{ip}, nil
}
2022-04-29 13:03:55 +08:00
return []netip.Addr{}, ErrIPVersion
2019-09-11 17:00:55 +08:00
}
if r != nil {
2022-04-29 13:03:55 +08:00
return r.ResolveAllIPv4(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()
2022-04-29 13:03:55 +08:00
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip4", host)
2022-03-28 00:44:13 +08:00
if err != nil {
2022-04-29 13:03:55 +08:00
return []netip.Addr{}, err
2022-03-28 00:44:13 +08:00
} else if len(ipAddrs) == 0 {
2022-04-29 13:03:55 +08:00
return []netip.Addr{}, ErrIPNotFound
2022-03-28 00:44:13 +08:00
}
2022-04-29 13:03:55 +08:00
ip := ipAddrs[rand.Intn(len(ipAddrs))].To4()
if ip == nil {
return []netip.Addr{}, ErrIPVersion
}
return []netip.Addr{netip.AddrFrom4(*(*[4]byte)(ip))}, nil
2019-09-11 17:00:55 +08:00
}
2022-04-29 13:03:55 +08:00
return []netip.Addr{}, ErrIPNotFound
2019-09-11 17:00:55 +08:00
}
2022-04-29 13:03:55 +08:00
func ResolveAllIPWithResolver(host string, r Resolver) ([]netip.Addr, error) {
2019-09-11 17:00:55 +08:00
if node := DefaultHosts.Search(host); node != nil {
return []netip.Addr{node.Data()}, nil
2019-09-11 17:00:55 +08:00
}
2022-07-20 22:59:04 +08:00
ip, err := netip.ParseAddr(host)
if err == nil {
return []netip.Addr{ip}, nil
}
if r != nil {
2020-06-18 18:11:02 +08:00
if DisableIPv6 {
2022-04-29 13:03:55 +08:00
return r.ResolveAllIPv4(host)
2020-06-18 18:11:02 +08:00
}
2022-04-29 13:03:55 +08:00
return r.ResolveAllIP(host)
2020-06-18 18:11:02 +08:00
} else if DisableIPv6 {
2022-04-29 13:03:55 +08:00
return ResolveAllIPv4(host)
}
2022-03-28 00:44:13 +08:00
if DefaultResolver == nil {
ipAddr, err := net.ResolveIPAddr("ip", host)
if err != nil {
2022-04-29 13:03:55 +08:00
return []netip.Addr{}, err
2022-03-28 00:44:13 +08:00
}
2022-04-29 13:03:55 +08:00
return []netip.Addr{nnip.IpToAddr(ipAddr.IP)}, nil
}
2022-04-29 13:03:55 +08:00
return []netip.Addr{}, ErrIPNotFound
}
func ResolveAllIP(host string) ([]netip.Addr, error) {
return ResolveAllIPWithResolver(host, DefaultResolver)
}
func ResolveAllIPv4(host string) ([]netip.Addr, error) {
return ResolveAllIPv4WithResolver(host, DefaultResolver)
}
func ResolveAllIPv6(host string) ([]netip.Addr, error) {
return ResolveAllIPv6WithResolver(host, DefaultResolver)
}
func ResolveAllIPv6ProxyServerHost(host string) ([]netip.Addr, error) {
if ProxyServerHostResolver != nil {
return ResolveAllIPv6WithResolver(host, ProxyServerHostResolver)
}
return ResolveAllIPv6(host)
}
func ResolveAllIPv4ProxyServerHost(host string) ([]netip.Addr, error) {
if ProxyServerHostResolver != nil {
return ResolveAllIPv4WithResolver(host, ProxyServerHostResolver)
}
return ResolveAllIPv4(host)
}
func ResolveAllIPProxyServerHost(host string) ([]netip.Addr, error) {
if ProxyServerHostResolver != nil {
return ResolveAllIPWithResolver(host, ProxyServerHostResolver)
}
return ResolveAllIP(host)
}