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"
|
||||
"net/netip"
|
||||
"strings"
|
||||
_ "unsafe"
|
||||
|
||||
"github.com/Dreamacro/clash/common/utils"
|
||||
"github.com/Dreamacro/clash/component/trie"
|
||||
|
@ -20,12 +21,14 @@ 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`
|
||||
func (h *Hosts) Search(domain string, isDomain bool) (*HostValue, bool) {
|
||||
value := h.DomainTrie.Search(domain)
|
||||
if value == nil {
|
||||
return nil, false
|
||||
}
|
||||
if value := h.DomainTrie.Search(domain); value != nil {
|
||||
hostValue := value.Data()
|
||||
for {
|
||||
if isDomain && hostValue.IsDomain {
|
||||
|
@ -41,7 +44,16 @@ func (h *Hosts) Search(domain string, isDomain bool) (*HostValue, bool) {
|
|||
if isDomain == hostValue.IsDomain {
|
||||
return &hostValue, true
|
||||
}
|
||||
|
||||
return &hostValue, false
|
||||
}
|
||||
if !isDomain {
|
||||
addr, _ := lookupStaticHost(domain)
|
||||
if hostValue, err := NewHostValue(addr); err == nil {
|
||||
return &hostValue, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
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