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-11-12 21:31:07 +08:00
|
|
|
"fmt"
|
2019-06-29 00:58:59 +08:00
|
|
|
"net"
|
2022-04-06 04:25:53 +08:00
|
|
|
"net/netip"
|
2022-11-12 21:31:07 +08:00
|
|
|
"strings"
|
2021-04-01 18:03:30 +08:00
|
|
|
"time"
|
2020-02-15 21:42:46 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/common/utils"
|
|
|
|
"github.com/metacubex/mihomo/component/trie"
|
2023-01-16 15:20:39 +08:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2023-03-06 18:10:14 +08:00
|
|
|
"github.com/zhangyunhao116/fastrand"
|
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
|
2023-03-12 15:00:59 +08:00
|
|
|
DefaultHosts = NewHosts(trie.New[HostValue]())
|
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-11-12 13:18:36 +08:00
|
|
|
LookupIP(ctx context.Context, host string) (ips []netip.Addr, err error)
|
|
|
|
LookupIPv4(ctx context.Context, host string) (ips []netip.Addr, err error)
|
|
|
|
LookupIPv6(ctx context.Context, host string) (ips []netip.Addr, err error)
|
2023-01-16 15:20:39 +08:00
|
|
|
ExchangeContext(ctx context.Context, m *dns.Msg) (msg *dns.Msg, err error)
|
2023-04-11 14:10:57 +08:00
|
|
|
Invalid() bool
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
// LookupIPv4WithResolver same as LookupIPv4, but with a resolver
|
|
|
|
func LookupIPv4WithResolver(ctx context.Context, host string, r Resolver) ([]netip.Addr, error) {
|
2023-03-12 15:00:59 +08:00
|
|
|
if node, ok := DefaultHosts.Search(host, false); ok {
|
|
|
|
if addrs := utils.Filter(node.IPs, func(ip netip.Addr) bool {
|
|
|
|
return ip.Is4()
|
|
|
|
}); len(addrs) > 0 {
|
|
|
|
return addrs, nil
|
2022-11-12 21:31:07 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-23 02:38:50 +08:00
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
ip, err := netip.ParseAddr(host)
|
|
|
|
if err == nil {
|
|
|
|
if ip.Is4() || ip.Is4In6() {
|
|
|
|
return []netip.Addr{ip}, nil
|
|
|
|
}
|
|
|
|
return []netip.Addr{}, ErrIPVersion
|
2022-04-29 13:03:55 +08:00
|
|
|
}
|
|
|
|
|
2023-04-11 14:10:57 +08:00
|
|
|
if r != nil && r.Invalid() {
|
2022-11-12 21:31:07 +08:00
|
|
|
return r.LookupIPv4(ctx, host)
|
|
|
|
}
|
2022-04-29 13:03:55 +08:00
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
ipAddrs, err := net.DefaultResolver.LookupNetIP(ctx, "ip4", host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(ipAddrs) == 0 {
|
|
|
|
return nil, ErrIPNotFound
|
2022-04-29 13:03:55 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
return ipAddrs, nil
|
2022-04-29 13:03:55 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
// LookupIPv4 with a host, return ipv4 list
|
|
|
|
func LookupIPv4(ctx context.Context, host string) ([]netip.Addr, error) {
|
|
|
|
return LookupIPv4WithResolver(ctx, host, DefaultResolver)
|
2022-04-29 13:03:55 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
// ResolveIPv4WithResolver same as ResolveIPv4, but with a resolver
|
|
|
|
func ResolveIPv4WithResolver(ctx context.Context, host string, r Resolver) (netip.Addr, error) {
|
|
|
|
ips, err := LookupIPv4WithResolver(ctx, host, r)
|
|
|
|
if err != nil {
|
|
|
|
return netip.Addr{}, err
|
|
|
|
} else if len(ips) == 0 {
|
|
|
|
return netip.Addr{}, fmt.Errorf("%w: %s", ErrIPNotFound, host)
|
2022-04-29 13:03:55 +08:00
|
|
|
}
|
2023-03-06 18:10:14 +08:00
|
|
|
return ips[fastrand.Intn(len(ips))], nil
|
2022-04-29 13:03:55 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
// ResolveIPv4 with a host, return ipv4
|
|
|
|
func ResolveIPv4(ctx context.Context, host string) (netip.Addr, error) {
|
|
|
|
return ResolveIPv4WithResolver(ctx, host, DefaultResolver)
|
2022-04-29 13:03:55 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
// LookupIPv6WithResolver same as LookupIPv6, but with a resolver
|
2022-11-12 13:18:36 +08:00
|
|
|
func LookupIPv6WithResolver(ctx context.Context, host string, r Resolver) ([]netip.Addr, error) {
|
2022-04-29 13:03:55 +08:00
|
|
|
if DisableIPv6 {
|
2022-11-12 21:31:07 +08:00
|
|
|
return nil, ErrIPv6Disabled
|
2022-04-29 13:03:55 +08:00
|
|
|
}
|
|
|
|
|
2023-03-12 15:00:59 +08:00
|
|
|
if node, ok := DefaultHosts.Search(host, false); ok {
|
|
|
|
if addrs := utils.Filter(node.IPs, func(ip netip.Addr) bool {
|
|
|
|
return ip.Is6()
|
|
|
|
}); len(addrs) > 0 {
|
|
|
|
return addrs, nil
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
if ip, err := netip.ParseAddr(host); err == nil {
|
|
|
|
if strings.Contains(host, ":") {
|
2022-04-29 13:03:55 +08:00
|
|
|
return []netip.Addr{ip}, nil
|
2019-09-27 10:33:37 +08:00
|
|
|
}
|
2022-11-12 21:31:07 +08:00
|
|
|
return nil, ErrIPVersion
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2023-04-11 14:10:57 +08:00
|
|
|
if r != nil && r.Invalid() {
|
2022-11-12 13:18:36 +08:00
|
|
|
return r.LookupIPv6(ctx, host)
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
ipAddrs, err := net.DefaultResolver.LookupNetIP(ctx, "ip6", host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(ipAddrs) == 0 {
|
|
|
|
return nil, ErrIPNotFound
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
return ipAddrs, nil
|
|
|
|
}
|
2022-03-28 00:44:13 +08:00
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
// LookupIPv6 with a host, return ipv6 list
|
|
|
|
func LookupIPv6(ctx context.Context, host string) ([]netip.Addr, error) {
|
|
|
|
return LookupIPv6WithResolver(ctx, host, DefaultResolver)
|
|
|
|
}
|
2022-04-29 13:03:55 +08:00
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
// ResolveIPv6WithResolver same as ResolveIPv6, but with a resolver
|
|
|
|
func ResolveIPv6WithResolver(ctx context.Context, host string, r Resolver) (netip.Addr, error) {
|
|
|
|
ips, err := LookupIPv6WithResolver(ctx, host, r)
|
|
|
|
if err != nil {
|
|
|
|
return netip.Addr{}, err
|
|
|
|
} else if len(ips) == 0 {
|
|
|
|
return netip.Addr{}, fmt.Errorf("%w: %s", ErrIPNotFound, host)
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
2023-03-06 18:10:14 +08:00
|
|
|
return ips[fastrand.Intn(len(ips))], nil
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
func ResolveIPv6(ctx context.Context, host string) (netip.Addr, error) {
|
|
|
|
return ResolveIPv6WithResolver(ctx, host, DefaultResolver)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupIPWithResolver same as LookupIP, but with a resolver
|
2022-11-12 13:18:36 +08:00
|
|
|
func LookupIPWithResolver(ctx context.Context, host string, r Resolver) ([]netip.Addr, error) {
|
2023-03-12 15:00:59 +08:00
|
|
|
if node, ok := DefaultHosts.Search(host, false); ok {
|
|
|
|
return node.IPs, nil
|
2019-09-11 17:00:55 +08:00
|
|
|
}
|
|
|
|
|
2023-04-11 14:10:57 +08:00
|
|
|
if r != nil && r.Invalid() {
|
2020-06-18 18:11:02 +08:00
|
|
|
if DisableIPv6 {
|
2022-11-12 13:18:36 +08:00
|
|
|
return r.LookupIPv4(ctx, host)
|
2020-06-18 18:11:02 +08:00
|
|
|
}
|
2022-11-12 13:18:36 +08:00
|
|
|
return r.LookupIP(ctx, host)
|
2020-06-18 18:11:02 +08:00
|
|
|
} else if DisableIPv6 {
|
2023-03-06 19:15:12 +08:00
|
|
|
return LookupIPv4WithResolver(ctx, host, r)
|
2019-06-29 00:58:59 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
if ip, err := netip.ParseAddr(host); err == nil {
|
|
|
|
return []netip.Addr{ip}, nil
|
|
|
|
}
|
2022-03-28 00:44:13 +08:00
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
ips, err := net.DefaultResolver.LookupNetIP(ctx, "ip", host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(ips) == 0 {
|
|
|
|
return nil, ErrIPNotFound
|
2019-06-29 00:58:59 +08:00
|
|
|
}
|
2022-11-12 21:31:07 +08:00
|
|
|
|
|
|
|
return ips, nil
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
// LookupIP with a host, return ip
|
2022-11-12 13:18:36 +08:00
|
|
|
func LookupIP(ctx context.Context, host string) ([]netip.Addr, error) {
|
|
|
|
return LookupIPWithResolver(ctx, host, DefaultResolver)
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:31:07 +08:00
|
|
|
// ResolveIPWithResolver same as ResolveIP, but with a resolver
|
|
|
|
func ResolveIPWithResolver(ctx context.Context, host string, r Resolver) (netip.Addr, error) {
|
|
|
|
ips, err := LookupIPWithResolver(ctx, host, r)
|
|
|
|
if err != nil {
|
|
|
|
return netip.Addr{}, err
|
|
|
|
} else if len(ips) == 0 {
|
|
|
|
return netip.Addr{}, fmt.Errorf("%w: %s", ErrIPNotFound, host)
|
|
|
|
}
|
2023-04-12 22:06:21 +08:00
|
|
|
ipv4s, ipv6s := SortationAddr(ips)
|
|
|
|
if len(ipv4s) > 0 {
|
|
|
|
return ipv4s[fastrand.Intn(len(ipv4s))], nil
|
|
|
|
}
|
|
|
|
return ipv6s[fastrand.Intn(len(ipv6s))], nil
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
|
|
|
|
2023-04-12 22:06:21 +08:00
|
|
|
// ResolveIP with a host, return ip and priority return TypeA
|
2022-11-12 21:31:07 +08:00
|
|
|
func ResolveIP(ctx context.Context, host string) (netip.Addr, error) {
|
|
|
|
return ResolveIPWithResolver(ctx, host, DefaultResolver)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveIPv4ProxyServerHost proxies server host only
|
|
|
|
func ResolveIPv4ProxyServerHost(ctx context.Context, host string) (netip.Addr, error) {
|
|
|
|
if ProxyServerHostResolver != nil {
|
2024-03-28 23:33:56 +08:00
|
|
|
return ResolveIPv4WithResolver(ctx, host, ProxyServerHostResolver)
|
2022-11-12 21:31:07 +08:00
|
|
|
}
|
|
|
|
return ResolveIPv4(ctx, host)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveIPv6ProxyServerHost proxies server host only
|
|
|
|
func ResolveIPv6ProxyServerHost(ctx context.Context, host string) (netip.Addr, error) {
|
|
|
|
if ProxyServerHostResolver != nil {
|
2024-03-28 23:33:56 +08:00
|
|
|
return ResolveIPv6WithResolver(ctx, host, ProxyServerHostResolver)
|
2022-11-12 21:31:07 +08:00
|
|
|
}
|
|
|
|
return ResolveIPv6(ctx, host)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveProxyServerHost proxies server host only
|
|
|
|
func ResolveProxyServerHost(ctx context.Context, host string) (netip.Addr, error) {
|
|
|
|
if ProxyServerHostResolver != nil {
|
2024-03-28 23:33:56 +08:00
|
|
|
return ResolveIPWithResolver(ctx, host, ProxyServerHostResolver)
|
2022-11-12 21:31:07 +08:00
|
|
|
}
|
|
|
|
return ResolveIP(ctx, host)
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 13:18:36 +08:00
|
|
|
func LookupIPv6ProxyServerHost(ctx context.Context, host string) ([]netip.Addr, error) {
|
2022-04-23 00:27:22 +08:00
|
|
|
if ProxyServerHostResolver != nil {
|
2022-11-12 13:18:36 +08:00
|
|
|
return LookupIPv6WithResolver(ctx, host, ProxyServerHostResolver)
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
2022-11-12 13:18:36 +08:00
|
|
|
return LookupIPv6(ctx, host)
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 13:18:36 +08:00
|
|
|
func LookupIPv4ProxyServerHost(ctx context.Context, host string) ([]netip.Addr, error) {
|
2022-04-23 00:27:22 +08:00
|
|
|
if ProxyServerHostResolver != nil {
|
2022-11-12 13:18:36 +08:00
|
|
|
return LookupIPv4WithResolver(ctx, host, ProxyServerHostResolver)
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
2022-11-12 13:18:36 +08:00
|
|
|
return LookupIPv4(ctx, host)
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 13:18:36 +08:00
|
|
|
func LookupIPProxyServerHost(ctx context.Context, host string) ([]netip.Addr, error) {
|
2022-04-23 00:27:22 +08:00
|
|
|
if ProxyServerHostResolver != nil {
|
2022-11-12 13:18:36 +08:00
|
|
|
return LookupIPWithResolver(ctx, host, ProxyServerHostResolver)
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
2022-11-12 13:18:36 +08:00
|
|
|
return LookupIP(ctx, host)
|
2022-04-23 00:27:22 +08:00
|
|
|
}
|
2023-04-12 22:06:21 +08:00
|
|
|
|
|
|
|
func SortationAddr(ips []netip.Addr) (ipv4s, ipv6s []netip.Addr) {
|
|
|
|
for _, v := range ips {
|
|
|
|
if v.Unmap().Is4() {
|
|
|
|
ipv4s = append(ipv4s, v)
|
|
|
|
} else {
|
|
|
|
ipv6s = append(ipv6s, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|