Feature: can set custom interface for dns nameserver (#2126)

This commit is contained in:
Hongqi Yu 2022-06-01 10:50:54 +08:00 committed by GitHub
parent 9d2fc976e2
commit c1285adbf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 5 deletions

View File

@ -477,6 +477,10 @@ func parseNameServer(servers []string) ([]dns.NameServer, error) {
return nil, fmt.Errorf("DNS NameServer[%d] format error: %s", idx, err.Error())
}
// parse with specific interface
// .e.g 10.0.0.1#en0
interfaceName := u.Fragment
var addr, dnsNetType string
switch u.Scheme {
case "udp":
@ -506,8 +510,9 @@ func parseNameServer(servers []string) ([]dns.NameServer, error) {
nameservers = append(
nameservers,
dns.NameServer{
Net: dnsNetType,
Addr: addr,
Net: dnsNetType,
Addr: addr,
Interface: interfaceName,
},
)
}

View File

@ -79,7 +79,7 @@ func (dc *dohClient) doRequest(req *http.Request) (msg *D.Msg, err error) {
return msg, err
}
func newDoHClient(url string, r *Resolver) *dohClient {
func newDoHClient(url, iface string, r *Resolver) *dohClient {
return &dohClient{
url: url,
transport: &http.Transport{
@ -95,7 +95,12 @@ func newDoHClient(url string, r *Resolver) *dohClient {
return nil, err
}
return dialer.DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port))
options := []dialer.Option{}
if iface != "" {
options = append(options, dialer.WithInterface(iface))
}
return dialer.DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port), options...)
},
},
}

View File

@ -51,7 +51,7 @@ func transform(servers []NameServer, resolver *Resolver) []dnsClient {
for _, s := range servers {
switch s.Net {
case "https":
ret = append(ret, newDoHClient(s.Addr, resolver))
ret = append(ret, newDoHClient(s.Addr, s.Interface, resolver))
continue
case "dhcp":
ret = append(ret, newDHCPClient(s.Addr))