From b9d470cf79d145736fc87f73e3b7f58119e684a0 Mon Sep 17 00:00:00 2001 From: Dreamacro <8615343+Dreamacro@users.noreply.github.com> Date: Tue, 5 Oct 2021 13:31:19 +0800 Subject: [PATCH] Fix: dhcp client should request special interface --- dns/client.go | 13 +++++++++---- dns/dhcp.go | 7 +++++-- dns/resolver.go | 5 +++-- dns/util.go | 7 ++++--- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/dns/client.go b/dns/client.go index 6a54f9fa..5cb1fe02 100644 --- a/dns/client.go +++ b/dns/client.go @@ -15,9 +15,10 @@ import ( type client struct { *D.Client - r *Resolver - port string - host string + r *Resolver + port string + host string + iface string } 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" } - 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 { return nil, err } diff --git a/dns/dhcp.go b/dns/dhcp.go index 94f8a36c..f964cec8 100644 --- a/dns/dhcp.go +++ b/dns/dhcp.go @@ -68,8 +68,11 @@ func (d *dhcpClient) resolve(ctx context.Context) (*Resolver, error) { dns, err := dhcp.ResolveDNSFromDHCP(ctx, d.ifaceName) if err == nil { nameserver := make([]NameServer, 0, len(dns)) - for _, d := range dns { - nameserver = append(nameserver, NameServer{Addr: net.JoinHostPort(d.String(), "53")}) + for _, item := range dns { + nameserver = append(nameserver, NameServer{ + Addr: net.JoinHostPort(item.String(), "53"), + Interface: d.ifaceName, + }) } res = NewResolver(Config{ diff --git a/dns/resolver.go b/dns/resolver.go index 914f1418..8bbd0e8b 100644 --- a/dns/resolver.go +++ b/dns/resolver.go @@ -302,8 +302,9 @@ func (r *Resolver) asyncExchange(ctx context.Context, client []dnsClient, msg *D } type NameServer struct { - Net string - Addr string + Net string + Addr string + Interface string } type FallbackFilter struct { diff --git a/dns/util.go b/dns/util.go index 0c3f42e5..b167809a 100644 --- a/dns/util.go +++ b/dns/util.go @@ -138,9 +138,10 @@ func transform(servers []NameServer, resolver *Resolver) []dnsClient { UDPSize: 4096, Timeout: 5 * time.Second, }, - port: port, - host: host, - r: resolver, + port: port, + host: host, + iface: s.Interface, + r: resolver, }) } return ret