chore: no longer used net.DefaultResolver when dns section is disabled, now is equally only "system://"

This commit is contained in:
wwqgtxx 2024-10-02 14:44:52 +08:00
parent 990de84391
commit 4a16d22398
10 changed files with 40 additions and 93 deletions

View File

@ -69,7 +69,7 @@ func (h *Hysteria) ListenPacketContext(ctx context.Context, metadata *C.Metadata
func (h *Hysteria) genHdc(ctx context.Context, opts ...dialer.Option) utils.PacketDialer { func (h *Hysteria) genHdc(ctx context.Context, opts ...dialer.Option) utils.PacketDialer {
return &hyDialerWithContext{ return &hyDialerWithContext{
ctx: context.Background(), ctx: context.Background(),
hyDialer: func(network string) (net.PacketConn, error) { hyDialer: func(network string, rAddr net.Addr) (net.PacketConn, error) {
var err error var err error
var cDialer C.Dialer = dialer.NewDialer(h.Base.DialOptions(opts...)...) var cDialer C.Dialer = dialer.NewDialer(h.Base.DialOptions(opts...)...)
if len(h.option.DialerProxy) > 0 { if len(h.option.DialerProxy) > 0 {
@ -78,7 +78,7 @@ func (h *Hysteria) genHdc(ctx context.Context, opts ...dialer.Option) utils.Pack
return nil, err return nil, err
} }
} }
rAddrPort, _ := netip.ParseAddrPort(h.Addr()) rAddrPort, _ := netip.ParseAddrPort(rAddr.String())
return cDialer.ListenPacket(ctx, network, "", rAddrPort) return cDialer.ListenPacket(ctx, network, "", rAddrPort)
}, },
remoteAddr: func(addr string) (net.Addr, error) { remoteAddr: func(addr string) (net.Addr, error) {
@ -284,7 +284,7 @@ func (c *hyPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
} }
type hyDialerWithContext struct { type hyDialerWithContext struct {
hyDialer func(network string) (net.PacketConn, error) hyDialer func(network string, rAddr net.Addr) (net.PacketConn, error)
ctx context.Context ctx context.Context
remoteAddr func(host string) (net.Addr, error) remoteAddr func(host string) (net.Addr, error)
} }
@ -294,7 +294,7 @@ func (h *hyDialerWithContext) ListenPacket(rAddr net.Addr) (net.PacketConn, erro
if addrPort, err := netip.ParseAddrPort(rAddr.String()); err == nil { if addrPort, err := netip.ParseAddrPort(rAddr.String()); err == nil {
network = dialer.ParseNetwork(network, addrPort.Addr()) network = dialer.ParseNetwork(network, addrPort.Addr())
} }
return h.hyDialer(network) return h.hyDialer(network, rAddr)
} }
func (h *hyDialerWithContext) Context() context.Context { func (h *hyDialerWithContext) Context() context.Context {

View File

@ -1,29 +0,0 @@
package dialer
import (
"context"
"net"
"net/netip"
)
func init() {
// We must use this DialContext to query DNS
// when using net default resolver.
net.DefaultResolver.PreferGo = true
net.DefaultResolver.Dial = resolverDialContext
}
func resolverDialContext(ctx context.Context, network, address string) (net.Conn, error) {
d := &net.Dialer{}
interfaceName := DefaultInterface.Load()
if interfaceName != "" {
dstIP, err := netip.ParseAddr(address)
if err == nil {
_ = bindIfaceToDialer(interfaceName, d, network, dstIP)
}
}
return d.DialContext(ctx, network, address)
}

View File

@ -1,12 +0,0 @@
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
package resolver
import _ "unsafe"
//go:linkname defaultNS net.defaultNS
var defaultNS []string
func init() {
defaultNS = []string{"114.114.114.114:53", "8.8.8.8:53"}
}

View File

@ -1,19 +0,0 @@
//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"
}
}

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"net"
"net/netip" "net/netip"
"strings" "strings"
"time" "time"
@ -23,6 +22,9 @@ var (
// ProxyServerHostResolver resolve ip to proxies server host // ProxyServerHostResolver resolve ip to proxies server host
ProxyServerHostResolver Resolver ProxyServerHostResolver Resolver
// SystemResolver always using system dns, and was init in dns module
SystemResolver Resolver
// DisableIPv6 means don't resolve ipv6 host // DisableIPv6 means don't resolve ipv6 host
// default value is true // default value is true
DisableIPv6 = true DisableIPv6 = true
@ -72,14 +74,7 @@ func LookupIPv4WithResolver(ctx context.Context, host string, r Resolver) ([]net
return r.LookupIPv4(ctx, host) return r.LookupIPv4(ctx, host)
} }
ipAddrs, err := net.DefaultResolver.LookupNetIP(ctx, "ip4", host) return SystemResolver.LookupIPv4(ctx, host)
if err != nil {
return nil, err
} else if len(ipAddrs) == 0 {
return nil, ErrIPNotFound
}
return ipAddrs, nil
} }
// LookupIPv4 with a host, return ipv4 list // LookupIPv4 with a host, return ipv4 list
@ -128,14 +123,7 @@ func LookupIPv6WithResolver(ctx context.Context, host string, r Resolver) ([]net
return r.LookupIPv6(ctx, host) return r.LookupIPv6(ctx, host)
} }
ipAddrs, err := net.DefaultResolver.LookupNetIP(ctx, "ip6", host) return SystemResolver.LookupIPv6(ctx, host)
if err != nil {
return nil, err
} else if len(ipAddrs) == 0 {
return nil, ErrIPNotFound
}
return ipAddrs, nil
} }
// LookupIPv6 with a host, return ipv6 list // LookupIPv6 with a host, return ipv6 list
@ -177,14 +165,7 @@ func LookupIPWithResolver(ctx context.Context, host string, r Resolver) ([]netip
return []netip.Addr{ip}, nil return []netip.Addr{ip}, nil
} }
ips, err := net.DefaultResolver.LookupNetIP(ctx, "ip", host) return SystemResolver.LookupIP(ctx, host)
if err != nil {
return nil, err
} else if len(ips) == 0 {
return nil, ErrIPNotFound
}
return ips, nil
} }
// LookupIP with a host, return ip // LookupIP with a host, return ip

View File

@ -12,6 +12,12 @@ func FlushCacheWithDefaultResolver() {
if r := resolver.DefaultResolver; r != nil { if r := resolver.DefaultResolver; r != nil {
r.ClearCache() r.ClearCache()
} }
if r := resolver.ProxyServerHostResolver; r != nil {
r.ClearCache()
}
if r := resolver.SystemResolver; r != nil {
r.ClearCache()
}
resolver.ResetConnection() resolver.ResetConnection()
} }

View File

@ -13,7 +13,6 @@ import (
"github.com/metacubex/mihomo/component/resolver" "github.com/metacubex/mihomo/component/resolver"
"github.com/metacubex/mihomo/component/trie" "github.com/metacubex/mihomo/component/trie"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/constant/provider"
"github.com/metacubex/mihomo/log" "github.com/metacubex/mihomo/log"
D "github.com/miekg/dns" D "github.com/miekg/dns"
@ -436,7 +435,6 @@ type Config struct {
Pool *fakeip.Pool Pool *fakeip.Pool
Hosts *trie.DomainTrie[resolver.HostValue] Hosts *trie.DomainTrie[resolver.HostValue]
Policy []Policy Policy []Policy
Tunnel provider.Tunnel
CacheAlgorithm string CacheAlgorithm string
} }

View File

@ -7,6 +7,8 @@ import (
"sync" "sync"
"time" "time"
"github.com/metacubex/mihomo/component/resolver"
D "github.com/miekg/dns" D "github.com/miekg/dns"
) )
@ -24,12 +26,17 @@ type systemClient struct {
mu sync.Mutex mu sync.Mutex
dnsClients map[string]*systemDnsClient dnsClients map[string]*systemDnsClient
lastFlush time.Time lastFlush time.Time
defaultNS []dnsClient
} }
func (c *systemClient) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) { func (c *systemClient) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
dnsClients, err := c.getDnsClients() dnsClients, err := c.getDnsClients()
if err != nil { if err != nil {
return if len(c.defaultNS) > 0 {
dnsClients = c.defaultNS
} else {
return
}
} }
msg, _, err = batchExchange(ctx, dnsClients, m) msg, _, err = batchExchange(ctx, dnsClients, m)
return return
@ -52,3 +59,11 @@ func newSystemClient() *systemClient {
dnsClients: map[string]*systemDnsClient{}, dnsClients: map[string]*systemDnsClient{},
} }
} }
func init() {
r, _ := NewResolver(Config{})
c := newSystemClient()
c.defaultNS = transform([]NameServer{{Addr: "114.114.114.114:53"}, {Addr: "8.8.8.8:53"}}, nil)
r.main = []dnsClient{c}
resolver.SystemResolver = r
}

View File

@ -251,7 +251,6 @@ func updateDNS(c *config.DNS, generalIPv6 bool) {
Default: c.DefaultNameserver, Default: c.DefaultNameserver,
Policy: c.NameServerPolicy, Policy: c.NameServerPolicy,
ProxyServer: c.ProxyServerNameserver, ProxyServer: c.ProxyServerNameserver,
Tunnel: tunnel.Tunnel,
CacheAlgorithm: c.CacheAlgorithm, CacheAlgorithm: c.CacheAlgorithm,
} }

View File

@ -1,9 +1,11 @@
package main package main
import ( import (
"context"
"encoding/base64" "encoding/base64"
"flag" "flag"
"fmt" "fmt"
"net"
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
@ -55,6 +57,12 @@ func init() {
} }
func main() { func main() {
// Defensive programming: panic when code mistakenly calls net.DefaultResolver
net.DefaultResolver.PreferGo = true
net.DefaultResolver.Dial = func(ctx context.Context, network, address string) (net.Conn, error) {
panic("should never be called")
}
_, _ = maxprocs.Set(maxprocs.Logger(func(string, ...any) {})) _, _ = maxprocs.Set(maxprocs.Logger(func(string, ...any) {}))
if len(os.Args) > 1 && os.Args[1] == "convert-ruleset" { if len(os.Args) > 1 && os.Args[1] == "convert-ruleset" {