Skip to content

Commit

Permalink
generate a random mac address if none are available on the system
Browse files Browse the repository at this point in the history
  • Loading branch information
tinyzimmer committed Jul 8, 2023
1 parent 6763bea commit da292fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
tests:
name: Unit Tests
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
Expand Down
13 changes: 10 additions & 3 deletions pkg/util/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,18 @@ func RandomLocalMAC() (net.HardwareAddr, error) {
}
}

if len(addrs) < 1 {
return nil, fmt.Errorf("no valid MAC addresses found")
r := rand.New(rand.NewSource(time.Now().UnixNano()))

if len(addrs) == 0 {
// Generate a random MAC
mac := make([]byte, 6)
_, err := r.Read(mac)
if err != nil {
return nil, fmt.Errorf("failed to generate random MAC address: %w", err)
}
return mac, nil
}

r := rand.New(rand.NewSource(time.Now().UnixNano()))
ri := r.Intn(len(addrs))
return addrs[ri], nil
}
Expand Down

0 comments on commit da292fe

Please sign in to comment.