diff --git a/component/dhcp/dhcp.go b/component/dhcp/dhcp.go index b2ca6928..b7e9f506 100644 --- a/component/dhcp/dhcp.go +++ b/component/dhcp/dhcp.go @@ -3,9 +3,10 @@ package dhcp import ( "context" "errors" - "math/rand" "net" + "github.com/Dreamacro/clash/component/iface" + "github.com/insomniacslk/dhcp/dhcpv4" ) @@ -23,7 +24,12 @@ func ResolveDNSFromDHCP(context context.Context, ifaceName string) ([]net.IP, er result := make(chan []net.IP, 1) - discovery, err := dhcpv4.NewDiscovery(randomHardware(), dhcpv4.WithBroadcast(true), dhcpv4.WithRequestedOptions(dhcpv4.OptionDomainNameServer)) + ifaceObj, err := iface.ResolveInterface(ifaceName) + if err != nil { + return nil, err + } + + discovery, err := dhcpv4.NewDiscovery(ifaceObj.HardwareAddr, dhcpv4.WithBroadcast(true), dhcpv4.WithRequestedOptions(dhcpv4.OptionDomainNameServer)) if err != nil { return nil, err } @@ -80,15 +86,3 @@ func receiveOffer(conn net.PacketConn, id dhcpv4.TransactionID, result chan<- [] return } } - -func randomHardware() net.HardwareAddr { - addr := make(net.HardwareAddr, 6) - - addr[0] = 0xff - - for i := 1; i < len(addr); i++ { - addr[i] = byte(rand.Intn(254) + 1) - } - - return addr -}