2023-03-12 15:00:59 +08:00
|
|
|
package resolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/netip"
|
2024-03-13 15:32:26 +08:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2023-03-12 15:00:59 +08:00
|
|
|
"strings"
|
2023-09-23 14:01:18 +08:00
|
|
|
_ "unsafe"
|
2023-03-12 15:00:59 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/common/utils"
|
2024-05-18 20:45:15 +08:00
|
|
|
"github.com/metacubex/mihomo/component/resolver/hosts"
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/trie"
|
2023-03-12 18:44:30 +08:00
|
|
|
"github.com/zhangyunhao116/fastrand"
|
2023-03-12 15:00:59 +08:00
|
|
|
)
|
|
|
|
|
2024-05-06 14:03:29 +08:00
|
|
|
var (
|
|
|
|
DisableSystemHosts, _ = strconv.ParseBool(os.Getenv("DISABLE_SYSTEM_HOSTS"))
|
|
|
|
UseSystemHosts bool
|
|
|
|
)
|
2024-03-13 15:32:26 +08:00
|
|
|
|
2023-03-12 15:00:59 +08:00
|
|
|
type Hosts struct {
|
|
|
|
*trie.DomainTrie[HostValue]
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHosts(hosts *trie.DomainTrie[HostValue]) Hosts {
|
|
|
|
return Hosts{
|
|
|
|
hosts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 15:05:28 +08:00
|
|
|
// Return the search result and whether to match the parameter `isDomain`
|
2023-03-12 15:00:59 +08:00
|
|
|
func (h *Hosts) Search(domain string, isDomain bool) (*HostValue, bool) {
|
2023-09-23 14:01:18 +08:00
|
|
|
if value := h.DomainTrie.Search(domain); value != nil {
|
|
|
|
hostValue := value.Data()
|
|
|
|
for {
|
|
|
|
if isDomain && hostValue.IsDomain {
|
|
|
|
return &hostValue, true
|
2023-03-12 15:00:59 +08:00
|
|
|
} else {
|
2023-09-23 14:01:18 +08:00
|
|
|
if node := h.DomainTrie.Search(hostValue.Domain); node != nil {
|
|
|
|
hostValue = node.Data()
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
2023-03-12 15:00:59 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-23 14:01:18 +08:00
|
|
|
if isDomain == hostValue.IsDomain {
|
|
|
|
return &hostValue, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return &hostValue, false
|
2023-03-12 15:00:59 +08:00
|
|
|
}
|
2024-05-06 14:03:29 +08:00
|
|
|
|
|
|
|
if !isDomain && !DisableSystemHosts && UseSystemHosts {
|
2024-05-18 20:45:15 +08:00
|
|
|
addr, _ := hosts.LookupStaticHost(domain)
|
2023-09-23 14:01:18 +08:00
|
|
|
if hostValue, err := NewHostValue(addr); err == nil {
|
|
|
|
return &hostValue, true
|
|
|
|
}
|
2023-03-12 15:00:59 +08:00
|
|
|
}
|
2023-09-23 14:01:18 +08:00
|
|
|
return nil, false
|
2023-03-12 15:00:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type HostValue struct {
|
|
|
|
IsDomain bool
|
|
|
|
IPs []netip.Addr
|
|
|
|
Domain string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHostValue(value any) (HostValue, error) {
|
|
|
|
isDomain := true
|
|
|
|
ips := make([]netip.Addr, 0)
|
|
|
|
domain := ""
|
|
|
|
if valueArr, err := utils.ToStringSlice(value); err != nil {
|
|
|
|
return HostValue{}, err
|
|
|
|
} else {
|
|
|
|
if len(valueArr) > 1 {
|
|
|
|
isDomain = false
|
|
|
|
for _, str := range valueArr {
|
|
|
|
if ip, err := netip.ParseAddr(str); err == nil {
|
|
|
|
ips = append(ips, ip)
|
|
|
|
} else {
|
|
|
|
return HostValue{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if len(valueArr) == 1 {
|
|
|
|
host := valueArr[0]
|
|
|
|
if ip, err := netip.ParseAddr(host); err == nil {
|
|
|
|
ips = append(ips, ip)
|
|
|
|
isDomain = false
|
|
|
|
} else {
|
|
|
|
domain = host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isDomain {
|
|
|
|
return NewHostValueByDomain(domain)
|
|
|
|
} else {
|
|
|
|
return NewHostValueByIPs(ips)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHostValueByIPs(ips []netip.Addr) (HostValue, error) {
|
|
|
|
if len(ips) == 0 {
|
|
|
|
return HostValue{}, errors.New("ip list is empty")
|
|
|
|
}
|
|
|
|
return HostValue{
|
|
|
|
IsDomain: false,
|
|
|
|
IPs: ips,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHostValueByDomain(domain string) (HostValue, error) {
|
|
|
|
domain = strings.Trim(domain, ".")
|
|
|
|
item := strings.Split(domain, ".")
|
|
|
|
if len(item) < 2 {
|
|
|
|
return HostValue{}, errors.New("invaild domain")
|
|
|
|
}
|
|
|
|
return HostValue{
|
|
|
|
IsDomain: true,
|
|
|
|
Domain: domain,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hv HostValue) RandIP() (netip.Addr, error) {
|
|
|
|
if hv.IsDomain {
|
|
|
|
return netip.Addr{}, errors.New("value type is error")
|
|
|
|
}
|
2023-03-12 18:44:30 +08:00
|
|
|
return hv.IPs[fastrand.Intn(len(hv.IPs))], nil
|
2023-03-12 15:00:59 +08:00
|
|
|
}
|