chore: Add use-system-hosts option

This commit is contained in:
xishang0128 2024-05-06 14:03:29 +08:00
parent a2b43faa0b
commit 5dd883e790
No known key found for this signature in database
GPG Key ID: 44A1E10B5ADF68CB
3 changed files with 23 additions and 14 deletions

View File

@ -13,7 +13,10 @@ import (
"github.com/zhangyunhao116/fastrand" "github.com/zhangyunhao116/fastrand"
) )
var DisableSystemHosts, _ = strconv.ParseBool(os.Getenv("DISABLE_SYSTEM_HOSTS")) var (
DisableSystemHosts, _ = strconv.ParseBool(os.Getenv("DISABLE_SYSTEM_HOSTS"))
UseSystemHosts bool
)
type Hosts struct { type Hosts struct {
*trie.DomainTrie[HostValue] *trie.DomainTrie[HostValue]
@ -51,7 +54,8 @@ func (h *Hosts) Search(domain string, isDomain bool) (*HostValue, bool) {
return &hostValue, false return &hostValue, false
} }
if !isDomain && !DisableSystemHosts {
if !isDomain && !DisableSystemHosts && UseSystemHosts {
addr, _ := lookupStaticHost(domain) addr, _ := lookupStaticHost(domain)
if hostValue, err := NewHostValue(addr); err == nil { if hostValue, err := NewHostValue(addr); err == nil {
return &hostValue, true return &hostValue, true

View File

@ -114,6 +114,7 @@ type DNS struct {
PreferH3 bool `yaml:"prefer-h3"` PreferH3 bool `yaml:"prefer-h3"`
IPv6 bool `yaml:"ipv6"` IPv6 bool `yaml:"ipv6"`
IPv6Timeout uint `yaml:"ipv6-timeout"` IPv6Timeout uint `yaml:"ipv6-timeout"`
UseSystemHosts bool `yaml:"use-system-hosts"`
NameServer []dns.NameServer `yaml:"nameserver"` NameServer []dns.NameServer `yaml:"nameserver"`
Fallback []dns.NameServer `yaml:"fallback"` Fallback []dns.NameServer `yaml:"fallback"`
FallbackFilter FallbackFilter `yaml:"fallback-filter"` FallbackFilter FallbackFilter `yaml:"fallback-filter"`
@ -209,6 +210,7 @@ type RawDNS struct {
IPv6 bool `yaml:"ipv6" json:"ipv6"` IPv6 bool `yaml:"ipv6" json:"ipv6"`
IPv6Timeout uint `yaml:"ipv6-timeout" json:"ipv6-timeout"` IPv6Timeout uint `yaml:"ipv6-timeout" json:"ipv6-timeout"`
UseHosts bool `yaml:"use-hosts" json:"use-hosts"` UseHosts bool `yaml:"use-hosts" json:"use-hosts"`
UseSystemHosts bool `yaml:"use-system-hosts" json:"use-system-hosts"`
NameServer []string `yaml:"nameserver" json:"nameserver"` NameServer []string `yaml:"nameserver" json:"nameserver"`
Fallback []string `yaml:"fallback" json:"fallback"` Fallback []string `yaml:"fallback" json:"fallback"`
FallbackFilter RawFallbackFilter `yaml:"fallback-filter" json:"fallback-filter"` FallbackFilter RawFallbackFilter `yaml:"fallback-filter" json:"fallback-filter"`
@ -456,12 +458,13 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
Interval: 30, Interval: 30,
}, },
DNS: RawDNS{ DNS: RawDNS{
Enable: false, Enable: false,
IPv6: false, IPv6: false,
UseHosts: true, UseHosts: true,
IPv6Timeout: 100, UseSystemHosts: true,
EnhancedMode: C.DNSMapping, IPv6Timeout: 100,
FakeIPRange: "198.18.0.1/16", EnhancedMode: C.DNSMapping,
FakeIPRange: "198.18.0.1/16",
FallbackFilter: RawFallbackFilter{ FallbackFilter: RawFallbackFilter{
GeoIP: true, GeoIP: true,
GeoIPCode: "CN", GeoIPCode: "CN",
@ -1285,12 +1288,13 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul
} }
dnsCfg := &DNS{ dnsCfg := &DNS{
Enable: cfg.Enable, Enable: cfg.Enable,
Listen: cfg.Listen, Listen: cfg.Listen,
PreferH3: cfg.PreferH3, PreferH3: cfg.PreferH3,
IPv6Timeout: cfg.IPv6Timeout, IPv6Timeout: cfg.IPv6Timeout,
IPv6: cfg.IPv6, IPv6: cfg.IPv6,
EnhancedMode: cfg.EnhancedMode, UseSystemHosts: cfg.UseSystemHosts,
EnhancedMode: cfg.EnhancedMode,
FallbackFilter: FallbackFilter{ FallbackFilter: FallbackFilter{
IPCIDR: []netip.Prefix{}, IPCIDR: []netip.Prefix{},
GeoSite: []router.DomainMatcher{}, GeoSite: []router.DomainMatcher{},

View File

@ -253,6 +253,7 @@ func updateDNS(c *config.DNS, ruleProvider map[string]provider.RuleProvider, gen
resolver.DefaultResolver = r resolver.DefaultResolver = r
resolver.DefaultHostMapper = m resolver.DefaultHostMapper = m
resolver.DefaultLocalServer = dns.NewLocalServer(r, m) resolver.DefaultLocalServer = dns.NewLocalServer(r, m)
resolver.UseSystemHosts = c.UseSystemHosts
if pr.Invalid() { if pr.Invalid() {
resolver.ProxyServerHostResolver = pr resolver.ProxyServerHostResolver = pr