mihomo/component/dialer/resolver.go

30 lines
598 B
Go
Raw Permalink Normal View History

2022-03-09 05:08:35 +08:00
package dialer
import (
"context"
"net"
2022-04-20 01:52:51 +08:00
"net/netip"
2022-03-09 05:08:35 +08:00
)
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 != "" {
2022-04-20 01:52:51 +08:00
dstIP, err := netip.ParseAddr(address)
if err == nil {
_ = bindIfaceToDialer(interfaceName, d, network, dstIP)
2022-03-09 05:08:35 +08:00
}
}
return d.DialContext(ctx, network, address)
}