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

This commit is contained in:
gVisor bot 2022-06-01 10:50:54 +08:00
parent 649177855a
commit bbf0f1af4b
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":
@ -508,6 +512,7 @@ func parseNameServer(servers []string) ([]dns.NameServer, error) {
dns.NameServer{
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))