mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-16 06:01:21 +08:00
29 lines
574 B
Go
29 lines
574 B
Go
|
package dialer
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"net"
|
||
|
)
|
||
|
|
||
|
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 := net.ParseIP(address)
|
||
|
if dstIP != nil {
|
||
|
bindIfaceToDialer(interfaceName, d, network, dstIP)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return d.DialContext(ctx, network, address)
|
||
|
}
|