Fix: dhcp client should request special interface

This commit is contained in:
Dreamacro 2021-10-05 13:31:19 +08:00
parent 4f1fac02ab
commit b9d470cf79
4 changed files with 21 additions and 11 deletions

View file

@ -15,9 +15,10 @@ import (
type client struct { type client struct {
*D.Client *D.Client
r *Resolver r *Resolver
port string port string
host string host string
iface string
} }
func (c *client) Exchange(m *D.Msg) (*D.Msg, error) { func (c *client) Exchange(m *D.Msg) (*D.Msg, error) {
@ -45,7 +46,11 @@ func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error)
network = "tcp" network = "tcp"
} }
conn, err := dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), c.port)) options := []dialer.Option{}
if c.iface != "" {
options = append(options, dialer.WithInterface(c.iface))
}
conn, err := dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), c.port), options...)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -68,8 +68,11 @@ func (d *dhcpClient) resolve(ctx context.Context) (*Resolver, error) {
dns, err := dhcp.ResolveDNSFromDHCP(ctx, d.ifaceName) dns, err := dhcp.ResolveDNSFromDHCP(ctx, d.ifaceName)
if err == nil { if err == nil {
nameserver := make([]NameServer, 0, len(dns)) nameserver := make([]NameServer, 0, len(dns))
for _, d := range dns { for _, item := range dns {
nameserver = append(nameserver, NameServer{Addr: net.JoinHostPort(d.String(), "53")}) nameserver = append(nameserver, NameServer{
Addr: net.JoinHostPort(item.String(), "53"),
Interface: d.ifaceName,
})
} }
res = NewResolver(Config{ res = NewResolver(Config{

View file

@ -302,8 +302,9 @@ func (r *Resolver) asyncExchange(ctx context.Context, client []dnsClient, msg *D
} }
type NameServer struct { type NameServer struct {
Net string Net string
Addr string Addr string
Interface string
} }
type FallbackFilter struct { type FallbackFilter struct {

View file

@ -138,9 +138,10 @@ func transform(servers []NameServer, resolver *Resolver) []dnsClient {
UDPSize: 4096, UDPSize: 4096,
Timeout: 5 * time.Second, Timeout: 5 * time.Second,
}, },
port: port, port: port,
host: host, host: host,
r: resolver, iface: s.Interface,
r: resolver,
}) })
} }
return ret return ret