feat: host support domain and multiple ips

This commit is contained in:
Skyxim
2023-03-12 10:53:38 +08:00
parent ae4d114802
commit 2f69b64d82
10 changed files with 188 additions and 49 deletions

View File

@ -9,6 +9,7 @@ import (
"strings"
"time"
"github.com/Dreamacro/clash/common/utils"
"github.com/Dreamacro/clash/component/trie"
"github.com/miekg/dns"
@ -27,7 +28,7 @@ var (
DisableIPv6 = true
// DefaultHosts aim to resolve hosts
DefaultHosts = trie.New[netip.Addr]()
DefaultHosts = trie.New[HostValue]()
// DefaultDNSTimeout defined the default dns request timeout
DefaultDNSTimeout = time.Second * 5
@ -52,8 +53,14 @@ type Resolver interface {
// LookupIPv4WithResolver same as LookupIPv4, but with a resolver
func LookupIPv4WithResolver(ctx context.Context, host string, r Resolver) ([]netip.Addr, error) {
if node := DefaultHosts.Search(host); node != nil {
if ip := node.Data(); ip.Is4() {
return []netip.Addr{node.Data()}, nil
if value := node.Data(); !value.IsDomain {
if addrs := utils.Filter(value.IPs, func(ip netip.Addr) bool {
return ip.Is4()
}); len(addrs) > 0 {
return addrs, nil
}
}else{
return LookupIPv4WithResolver(ctx,value.Domain,r)
}
}
@ -107,8 +114,14 @@ func LookupIPv6WithResolver(ctx context.Context, host string, r Resolver) ([]net
}
if node := DefaultHosts.Search(host); node != nil {
if ip := node.Data(); ip.Is6() {
return []netip.Addr{ip}, nil
if value := node.Data(); !value.IsDomain {
if addrs := utils.Filter(value.IPs, func(ip netip.Addr) bool {
return ip.Is6()
}); len(addrs) > 0 {
return addrs, nil
}
}else{
return LookupIPv6WithResolver(ctx,value.Domain,r)
}
}
@ -156,7 +169,11 @@ func ResolveIPv6(ctx context.Context, host string) (netip.Addr, error) {
// LookupIPWithResolver same as LookupIP, but with a resolver
func LookupIPWithResolver(ctx context.Context, host string, r Resolver) ([]netip.Addr, error) {
if node := DefaultHosts.Search(host); node != nil {
return []netip.Addr{node.Data()}, nil
if !node.Data().IsDomain{
return node.Data().IPs, nil
}else{
return LookupIPWithResolver(ctx,node.Data().Domain,r)
}
}
if r != nil {