chore: resolver read system hosts file
This commit is contained in:
parent
bf619d8586
commit
0207a7ac96
2 changed files with 46 additions and 15 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strings"
|
"strings"
|
||||||
|
_ "unsafe"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/utils"
|
"github.com/Dreamacro/clash/common/utils"
|
||||||
"github.com/Dreamacro/clash/component/trie"
|
"github.com/Dreamacro/clash/component/trie"
|
||||||
|
@ -20,28 +21,39 @@ func NewHosts(hosts *trie.DomainTrie[HostValue]) Hosts {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// lookupStaticHost looks up the addresses and the canonical name for the given host from /etc/hosts.
|
||||||
|
//
|
||||||
|
//go:linkname lookupStaticHost net.lookupStaticHost
|
||||||
|
func lookupStaticHost(host string) ([]string, string)
|
||||||
|
|
||||||
// Return the search result and whether to match the parameter `isDomain`
|
// Return the search result and whether to match the parameter `isDomain`
|
||||||
func (h *Hosts) Search(domain string, isDomain bool) (*HostValue, bool) {
|
func (h *Hosts) Search(domain string, isDomain bool) (*HostValue, bool) {
|
||||||
value := h.DomainTrie.Search(domain)
|
if value := h.DomainTrie.Search(domain); value != nil {
|
||||||
if value == nil {
|
hostValue := value.Data()
|
||||||
return nil, false
|
for {
|
||||||
}
|
if isDomain && hostValue.IsDomain {
|
||||||
hostValue := value.Data()
|
return &hostValue, true
|
||||||
for {
|
|
||||||
if isDomain && hostValue.IsDomain {
|
|
||||||
return &hostValue, true
|
|
||||||
} else {
|
|
||||||
if node := h.DomainTrie.Search(hostValue.Domain); node != nil {
|
|
||||||
hostValue = node.Data()
|
|
||||||
} else {
|
} else {
|
||||||
break
|
if node := h.DomainTrie.Search(hostValue.Domain); node != nil {
|
||||||
|
hostValue = node.Data()
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if isDomain == hostValue.IsDomain {
|
||||||
|
return &hostValue, true
|
||||||
|
}
|
||||||
|
|
||||||
|
return &hostValue, false
|
||||||
}
|
}
|
||||||
if isDomain == hostValue.IsDomain {
|
if !isDomain {
|
||||||
return &hostValue, true
|
addr, _ := lookupStaticHost(domain)
|
||||||
|
if hostValue, err := NewHostValue(addr); err == nil {
|
||||||
|
return &hostValue, true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return &hostValue, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
type HostValue struct {
|
type HostValue struct {
|
||||||
|
|
19
component/resolver/host_windows.go
Normal file
19
component/resolver/host_windows.go
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
//go:build !go1.22
|
||||||
|
|
||||||
|
// a simple standard lib fix from: https://github.com/golang/go/commit/33d4a5105cf2b2d549922e909e9239a48b8cefcc
|
||||||
|
|
||||||
|
package resolver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
|
_ "unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:linkname testHookHostsPath net.testHookHostsPath
|
||||||
|
var testHookHostsPath string
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if dir, err := windows.GetSystemDirectory(); err == nil {
|
||||||
|
testHookHostsPath = dir + "/Drivers/etc/hosts"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue